cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Cryptographic Library RSA pkcs#1 v1.5 encryption unable to be decrypted with python RSA library

WSnid.1
Associate

I'm using STM's cryptographic library on a STMf412ret6 to encrypt a 16 byte message using a 2048 bit public key generated by the Python-RSA 4.8 library, first result on google for "python rsa".

I've verified the key being received by the MCU is identical to the one generated and I've verified the encrypted message is identical once it makes it back to the Raspberry Pi running the python script.

Upon attempting to decrypt the message it throws the error for not being able to find proper pkcs#1 v1.5 padding. I've tried manually adding padding, so I know that the issue is not of the ST library not automatically doing padding. Stepping through the python RSA library I have found that the resulting clear text message doesn't contain the original message anywhere which leads me to believe it is an issue with ST's library. Here is my code for calling the library:

static void dcu_encrypt_aes_key(uint8_t aes_key_to_crypt[16], uint8_t encrypted_aes_key[256], size_t* encrypted_key_size)
{
	if (cmox_initialize(NULL) != CMOX_INIT_SUCCESS)
	{
		Error_Handler();
	}
	cmox_rsa_handle_t Rsa_Ctx;
	/* RSA key */
	cmox_rsa_key_t Rsa_Key;
 
	cmox_rsa_retval_t retval;
	cmox_rsa_construct(&Rsa_Ctx, CMOX_RSA_MATH_FUNCS, CMOX_MODEXP_PUBLIC, Working_Buffer, 7000);
	retval = cmox_rsa_setKey(&Rsa_Key,                                    
						   rsa_key, 256,                     
						   rsa_exponent, 3);    
	/* Verify API returned value */
 
	if (retval != CMOX_RSA_SUCCESS)
	{
		Error_Handler();
	}
	do
	{
		uint32_t rand_num;
		uint8_t rand_array[237];
		for (int i = 0; i < 59; i++)
		{
			HAL_RNG_GenerateRandomNumber(&hrng, &rand_num);
			memcpy(&rand_array[i*4], &rand_num, 4);
		}
		HAL_RNG_GenerateRandomNumber(&hrng, &rand_num);
		memcpy(&rand_array[236], &rand_num, 1);
		  /* Compute directly the encrypted message passing all the needed parameters */
		retval = cmox_rsa_pkcs1v15_encrypt(&Rsa_Ctx,                                 /* RSA context */
										 &Rsa_Key,                                 /* RSA key to use */
										 aes_key_to_crypt, 16,                 /* Message to encrypt */
		//									 CMOX_RSA_PKCS1V22_HASH_SHA1,              /* Hash method to use */ v2.2 only
										 rand_array, 237,                       /* Random seed */
		//									 NULL, 0,                                  /* No label */ v2.2 only
										 encrypted_aes_key, encrypted_key_size);  /* Data buffer to receive encrypted text */
	}
	while(retval ==  CMOX_RSA_ERR_WRONG_RANDOM);
 
	/* Verify API returned value */
	if (retval != CMOX_RSA_SUCCESS)
	{
		Error_Handler();
	}
 
 
	/* Cleanup context */
	cmox_rsa_cleanup(&Rsa_Ctx);
 
	/* No more need of cryptographic services, finalize cryptographic library */
	if (cmox_finalize(NULL) != CMOX_INIT_SUCCESS)
	{
		Error_Handler();
	}
}

It is as close to identical to the example code found in the projects that come with the library as I could be.

Here is the code for the Pi:

(pubRSA, privRSA) = rsa.newkeys(2048)
conn, addr = sock.accept()
conn.sendall(pubRSA.n.to_bytes(256, 'little')
AESKeyEncrypted = conn.recv(256)
AESKey = rsa.decrypt(AESKeyEncrypted, privRSA).decode() #we error inside rsa.decrypt

Some other information that could be helpful:

  • Data transferred between MCU and Pi is done over Ethernet using a Wiznet w5500 on the MCU and it's related API
  • Data is received on the Pi using pythons low level socket library
  • The Python RSA library works exclusively with pkcs#1 v1.5 (why I am not using the cryptographic library's v2.2)
  • I'm using ST's TRNG library for the random bytes needed by encryption
1 REPLY 1
WSnid.1
Associate

Any help would really be appreciated, we're trying to implement this for a school project and have been banging our head against encryption for a while now.