cancel
Showing results for 
Search instead for 
Did you mean: 

How to chose random seed for RSA encrypt in X-CUBE-CRYPTOLIB?

LWChris
Associate III

The call to "cmox_rsa_pkcs1v15_encrypt" requires a "Random buffer" and a "Random Length (in Bytes)" as input parameters. It will return error code CMOX_RSA_ERR_WRONG_RANDOM if "Random material too short or not valid for the functionality".

I keep getting this error code, but I cannot find any details on how I am SUPPOSED to choose or set the buffer size or contents etc.

The example uses a 36 byte message and a 20 byte random seed. I am trying to encrypt a 16 byte message with a 20 byte seed, but get the result "material too short or not valid". So, what shall I do? Any documentation on that parameter except for "it is a random seed that can be too short or not valid under certain untold circumstances"?

13 REPLIES 13
Jocelyn RICARD
ST Employee

Hello Chris,

I checked on my side I couldn't reproduce your issue.

I make 10 loops of encryption followed by decryption, changing the seed each time, and always get same decrypted value as the one encrypted.

Best regards

Jocelyn

LWChris
Associate III

Could my problem be explained with insufficient stack size within an RTOS task? Meaning the rsaContext variable may be not properly initialized or corrupted?

Jocelyn RICARD
ST Employee

Hello @LWChris,

I'm sorry to come back so late on this topic.

I have a new information to share that fully explains the issue you had.

This information is available in the wiki here

here is an extract:

"RSA PKCS#1 v1.5 Encryption
The array must contain at least (ModulusLength - InputLength - 3) not NULL bytes (that is, 0x00).
Only that number of bytes is used if no NULL bytes (that is, 0x00) are in the sequence, otherwise more bytes are necessary to replace the NULL bytes.

"

 

So, for a 36 bytes message, with a 2K RSA you need 256 - 36 - 3 = 217 non null bytes in the buffer

 

Best regards

Jocelyn

fandelxin
Associate