Skip to content

Instantly share code, notes, and snippets.

@astojanov
Last active March 25, 2016 02:57
Show Gist options
  • Save astojanov/cb0805e2dacf6a0b3c85 to your computer and use it in GitHub Desktop.
Save astojanov/cb0805e2dacf6a0b3c85 to your computer and use it in GitHub Desktop.
IppsBigNumState* createBigNumState(int len, const Ipp32u* pData) {
int size;
ippsBigNumGetSize(len, &size);
IppsBigNumState* pBN = (IppsBigNumState*) ippMalloc(size);;
ippsBigNumInit(len, pBN);
if (pData != NULL) {
ippsSet_BN(IppsBigNumPOS, len, pData, pBN);
}
return pBN;
}
IppStatus RSA_IPPCrypto (int bitsRSA) {
IppStatus status;
// Security parameter specified for the
// Miller-Rabin test for probable primality.
int nTrials = 1;
// Number of bits of the exponent
int bitsExp = 24;
int sizeOfPrimeGen = -1;
int sizeOfRandomGen = -1;
int sizeOfPublicKey = -1;
int sizeOfPrivateKey = -1;
int sizeOfScratchBuffer = -1;
IppsPrimeState * primeGen = NULL;
IppsPRNGState * randomGen = NULL;
IppsRSAPublicKeyState * publicKey = NULL;
IppsRSAPrivateKeyState * privateKey = NULL;
Ipp8u * scratchBuffer = NULL;
Ipp32u E = 65537;
IppsBigNumState * pSrcPublicExp = createBigNumState (1, &E);
IppsBigNumState * pModulus = createBigNumState (bitsRSA / 32, NULL);
IppsBigNumState * pPublicExp = createBigNumState (bitsRSA / 32, NULL);
IppsBigNumState * pPrivateExp = createBigNumState (bitsRSA / 32, NULL);
// Prime Number Generator
ippsPrimeGetSize(bitsRSA, &sizeOfPrimeGen);
primeGen = (IppsPrimeState *) ippMalloc(sizeOfPrimeGen);
ippsPrimeInit(bitsRSA, primeGen);
// Pseudo Random Generator (default settings)
ippsPRNGGetSize(&sizeOfRandomGen);
randomGen = (IppsPRNGState*) ippMalloc (sizeOfRandomGen);
ippsPRNGInit(160, randomGen);
// Initialize the Public Key State
ippsRSA_GetSizePublicKey(bitsRSA, bitsExp, &sizeOfPublicKey);
publicKey = (IppsRSAPublicKeyState *)ippMalloc(sizeOfPublicKey);
ippsRSA_InitPublicKey(bitsRSA, bitsExp, publicKey, sizeOfPublicKey);
// Initialize the Private Key State
int bitsP = (bitsRSA + 1) / 2;
int bitsQ = bitsRSA - bitsP;
ippsRSA_GetSizePrivateKeyType2(bitsRSA, bitsRSA, &sizeOfPrivateKey);
privateKey = (IppsRSAPrivateKeyState *) ippMalloc(sizeOfPrivateKey);
ippsRSA_InitPrivateKeyType2(bitsP, bitsQ, privateKey, sizeOfPrivateKey);
// Initialize scratch buffer
ippsRSA_GetBufferSizePrivateKey(&sizeOfScratchBuffer, privateKey);
scratchBuffer = (Ipp8u *) ippMalloc (sizeOfScratchBuffer);
status = ippsRSA_GenerateKeys(
pSrcPublicExp,
pModulus, pPublicExp, pPrivateExp,
privateKey, scratchBuffer, nTrials,
primeGen, ippsPRNGen, randomGen
);
ippFree(pSrcPublicExp);
ippFree(pModulus);
ippFree(pPublicExp);
ippFree(pPrivateExp);
ippFree(primeGen);
ippFree(randomGen);
ippFree(publicKey);
ippFree(privateKey);
ippFree(scratchBuffer);
return status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment