RSA Security 5.2.2 manual Final, #define Blocksize

Models: 5.2.2

1 376
Download 376 pages 13.91 Kb
Page 250
Image 250

MultiPrime

You are encrypting 8 bytes, so you do not need to worry about that constraint. However, the output of RSA encryption is the same size as the modulus, as described in “The RSA Algorithm” on page 51. That means you must set the output buffer, which will hold the encrypted data, to be the same size as the modulus. Your modulus is 512 bits, or 64 bytes.

Note: The input to the RSA algorithm must also be the same size as the modulus, but AI_PKCS_RSAPublic will automatically pad.

The description of AI_PKCS_RSAPublic notes that “B_EncryptUpdate and

B_EncryptFinal require a random algorithm.” The random number generator is for the padding. You do not need random bytes, only an algorithm that can generate them. Although RSA encryption is not as slow as key pair generation, you will not see an immediate response. Use a surrender context so that you know the program is running and has not frozen:

#define BLOCK_SIZE 64

unsigned char encryptedData[BLOCK_SIZE]; unsigned int outputLenUpdate;

/* generalFlag is for the surrender function.*/ generalFlag = 0;

if ((status = B_EncryptUpdate

(rsaEncryptor, encryptedData, &outputLenUpdate,

BLOCK_SIZE, (unsigned char *)dataToEncryptWithRSA, 8, randomAlgorithm, (A_SURRENDER_CTX*)NULL_PTR)) != 0)

break;

Step 5: Final

unsigned int outputLenFinal;

/* generalFlag is for the surrender function.*/ generalFlag = 0;

if ((status = B_EncryptFinal

(rsaEncryptor, encryptedData + outputLenUpdate, &outputLenFinal, BLOCK_SIZE - outputLenUpdate, randomAlgorithm, (A_SURRENDER_CTX*)NULL_PTR)) != 0)

break;

2 2 8

R S A B S A F E C r y p t o - C D e v e l o p e r ’s G u i d e

Page 250
Image 250
RSA Security 5.2.2 manual Final, #define Blocksize