RSA PKCS1 V1.5 Signature Validation Problem

80 views
Skip to first unread message

Mark Satterfield

unread,
Apr 28, 2017, 2:05:59 AM4/28/17
to LibTom Projects
I am using LibTomCrypt and LibTomMath on my ARM embedded platform. I am creating a signature with a data hash  on a windows platform using
python pyOpenSSL package and placing that signature on the embedded platform. I am using libTomCrypt to validate the signature. The hash value
pulled from the signature is extracted correctly but when I generate a hash 256 value over the data (same data as signature hash was created from)
I get a mismatch. The calculated hash does not match. Any ideas? I tried defining MP_16BIT but that did not work. I am running ENDIAN_LITTLE.

Mark Satterfield

unread,
Apr 28, 2017, 9:09:03 AM4/28/17
to LibTom Projects
This is duplicate post of my previous post, Sorry for the extra data.

Steffen Jaeckel

unread,
Apr 28, 2017, 9:18:33 AM4/28/17
to lib...@googlegroups.com
Hi Mark,

how do you create the signature?
What's your code for the verification?

MP_16BIT has not much to do with that, only if you built libtommath with
MP_16BIT (which is definitely not recommended).

Cheers,
Steffen
--
Steffen Jaeckel - s_ja...@gmx.de
GnuPG fingerprint: C438 6A23 7ED4 3A47 5541 B942 7B2C D0DD 4BCF F59B
My OTR key has changed on 30. Sept. 2015!
jabber: jae...@jabber.ccc.de F052DE29 4FA9A02D 44A794E5 AE5AC0FB C5865C64

Mark Satterfield

unread,
Apr 28, 2017, 12:45:09 PM4/28/17
to LibTom Projects
On Windows:
    from OpenSSL import crypto

    private_key = crypto.PKey()
    
    # read private file for from file
    with open(expand_filename(_('output.private_key_file')), 'rb') as f:
        keystring = f.read()
      
    private_key = crypto.load_privatekey(crypto.FILETYPE_ASN1, keystring)

    # generate hash over data and sign it, sw_bin is data from a binary ELF file
    sign = crypto.sign(private_key, sw_bin, "sha256")

On ARM processor platform to calculate hash256:
     static int hash_index;
     static const char* hash_method = "sha256";

/**
 * Register the hash method and set hash index for method
 * @return void
 */
void
set_hash_index()
{
if( register_hash(&sha256_desc) == -1 )
{
print_string("Error: registering sha256 hash\n");
}
hash_index = find_hash(hash_method);
}

/*
 * Get the hash index
 * @return int - hash index
 */
int
get_hash_index()
{
return(hash_index);
}

/**
 * calculate a hash256 over data
 * 
 * @param data - array of data
 * @param datasize - length of array
 * @param unsigned char* - address where calculated hash is stored
 * @param unsigned long* - address where length of hash is stored
 * @return bool - true if hash calculated otherwise false 
 */
bool
get_data_hash(unsigned char* data,
 int datasize,
 unsigned char* hash,
 unsigned long* hash_len)
{
/* required to engage math library */
ltc_mp = ltm_desc;
set_hash_index();
if( (hash_memory(get_hash_index(), data, datasize, hash, hash_len)) != CRYPT_OK )
{
print_string("Failed to calculate hash on data\n");
return(false);
}
return(true);
}

The get_data_hash function calculate the hash, data points to bin file data in flash, and datasize is length of data.




On Friday, April 28, 2017 at 2:05:59 AM UTC-4, Mark Satterfield wrote:

Mark Satterfield

unread,
May 25, 2017, 8:45:13 AM5/25/17
to LibTom Projects
Replaced the sha256 function with one I found on the internet and now the hash calculation matches the hash calculation from the rsa signature code.


On Friday, April 28, 2017 at 2:05:59 AM UTC-4, Mark Satterfield wrote:
Reply all
Reply to author
Forward
0 new messages