Intel® Integrated Performance Primitives
Community support and discussions relating to developing high-performance vision, signal, security, and storage applications.

RSA Encryption using C


I am trying to create a function for a dll that takes in a char* to a string and encrypts the string. I am not sure why I get a ippStscontextMatchErr when I send in my context to the ippsRSASetkey function. I am following the steps stated in the user manual for cryptography:

"The application code for conducting a typical RSA encryption must perform the following
sequence of operations, starting with building of a crypto system:

1. Call the function RSAGetSize to get the size required to configure IppsRSAState

2. Ensure that the required memory space is properly allocated. With the allocated memory,
call the RSAInit function to initialize the context for the RSA encryption.

3. Keep calling the RSASetKey to set up RSA public key (n, e).

4. Invoke the RSAEncrypt function with the established RSA public key to encode the
plaintext into the respective ciphertext.

5. Free the memory allocated for the IppsRSAState context by calling the operating
system memory free service function."

This is what I did prior to the error:

void __declspec(dllexport) RSAEncyrption(char* myString, unsigned char* encrypted)

int pSize, bitsN, bitsP;
IppsRSAState *pCtx;
IppsBigNumState *pY, *pBN, *pE;

bitsN = 128;
bitsP = 64;

//allocate space for cyphertext (not sure if its
//the same way as the AES IPP
pY = (IppsBigNumState *)malloc(strlen(myString))

//get the size required to configure IppsRSAState context
ippsRSAGetSize(bitsN, bitsP, IppRSApublic, &pSize);

//allocate memory
pCtx = (IppsRSAState *)malloc(sizeof(Ipp8u)*pSize);

//init key
ippsRSAInit(bitsN, bitsP, IppRSApublic, pCtx);

pBN = (IppsBigNumState *)malloc(sizeof(Ipp8u)*pSize);
pE = (IppsBigNumState *)malloc(sizeof(Ipp8u)*pSize);

ippsRSASetKey(pBN, IppRSAkeyN, pCtx);
ippsRSASetKey(pE, IppRSAkeyE, pCtx);

//not sure if I can just pass in a char* like the AES IPP
ippsRSAEncrypt((IppsBigNumState*)myString, pY, pCtx);

memcpy(encrypted, pY, strlen(myString));

So all in all, the call to ippsRSASetkey throws a return value of -17 which says that the context parameter does not match the operation. I am confused as I thought the ippsRSAinit function takes care of the context (pCtx).

Any help would be greatly appreciated. Thanks.
0 Kudos
1 Reply

the main problem of code snippet you provide is that it does not use ippsBigNumber data type (just casting of memory buffer to ippsBigNumber type is not enoughand is notcorrect). Please refer to IPP documentation for more details.

ippsRSAEncrypt((IppsBigNumState*)myString, pY, pCtx); [gres] Both "myString" and "Y" are not ippsBigNumState. That is why "ippStscontextMatchErr" is detected