#include #include #include #include #include "ipp.h" #include "ippcp.h" #define KEYLENBYTES 128 #define BITS_BYTES(n) (((n)+7)>>3) #define BITS_DWORDS(n) (((n)+31)>>5) #define BYTE_DWORDS(n) (((n)+3)>>2) uint8_t private_key_1024_p[] = { /* Prime 1 (p). */ 0xba, 0xfb, 0x89, 0xd7, 0xf1, 0x72, 0x80, 0x3a, 0x5d, 0x60, 0xd0, 0x64, 0x34, 0x4e, 0x28, 0x89, 0x46, 0xac, 0xfc, 0x8b, 0xb2, 0x10, 0x41, 0xcd, 0xcc, 0xb7, 0xbf, 0xdd, 0x28, 0x78, 0x84, 0xe6, 0x07, 0xed, 0x8c, 0xe0, 0xd7, 0x13, 0x41, 0x26, 0xfe, 0x80, 0x49, 0x70, 0xd7, 0x5c, 0x85, 0x3c, 0xfe, 0xd2, 0x46, 0x6c, 0x1a, 0x32, 0x2f, 0x7a, 0x2f, 0xeb, 0x03, 0xed, 0xcb, 0xe0, 0xf2, 0x05 }; uint8_t private_key_1024_q[] = { /* Prime 2 (q). */ 0xd2, 0x96, 0x55, 0x8d, 0xd7, 0x81, 0xee, 0x06, 0x02, 0x59, 0xb3, 0x43, 0x93, 0x7c, 0xde, 0xf1, 0x89, 0xa2, 0xbc, 0x9f, 0xb4, 0x7d, 0x3e, 0x62, 0x6e, 0x3f, 0xb7, 0x60, 0xb9, 0x0f, 0x60, 0x3f, 0x84, 0xe5, 0x0a, 0x51, 0xcc, 0x0d, 0xf9, 0x6b, 0x12, 0x29, 0x81, 0x16, 0xec, 0x61, 0x34, 0x39, 0xcd, 0x5d, 0x24, 0x81, 0xed, 0xaa, 0x89, 0x2f, 0xde, 0x98, 0xa5, 0xe3, 0x9b, 0xe2, 0xbc, 0x15 }; uint8_t private_key_1024_dp[] = { /* Exponent dp = d (mod p-1). */ 0xad, 0x56, 0xd0, 0xd3, 0xca, 0xb6, 0x7e, 0x1f, 0x1d, 0x3f, 0xca, 0x10, 0x8a, 0xd8, 0xc6, 0x05, 0x74, 0xea, 0x9a, 0xf8, 0x9a, 0x2b, 0x2e, 0x05, 0xae, 0x39, 0xcd, 0xf9, 0x25, 0x32, 0x8c, 0xe9, 0x7f, 0x01, 0xfe, 0x6e, 0xd5, 0x93, 0xb8, 0xfb, 0x80, 0x45, 0xc7, 0x4f, 0x7f, 0xa6, 0xfa, 0xb1, 0xaf, 0xfd, 0x99, 0x61, 0x4f, 0xeb, 0xa4, 0x4d, 0xde, 0xac, 0x3d, 0x29, 0x2e, 0xaf, 0x2b, 0x4d }; uint8_t private_key_1024_dq[] = { /* Exponent dq = d (mod q-1). */ 0x48, 0x14, 0x6c, 0xf4, 0x3f, 0xef, 0x99, 0x29, 0x35, 0x64, 0xbe, 0x97, 0x42, 0xc4, 0x5b, 0x0b, 0x11, 0xb1, 0x42, 0x1e, 0xe8, 0x08, 0x76, 0x71, 0xfa, 0x8a, 0x94, 0x99, 0xe6, 0xbd, 0xfc, 0x93, 0xc1, 0x49, 0x24, 0x5f, 0x1b, 0x19, 0x2d, 0x66, 0x18, 0xb1, 0x5c, 0x1e, 0x1c, 0x8a, 0xa8, 0xf5, 0x9f, 0xf5, 0x61, 0xa9, 0x97, 0x4f, 0xe7, 0xe5, 0x1a, 0xab, 0xe5, 0x1c, 0x23, 0x1f, 0x58, 0x81 }; uint8_t private_key_1024_invp[] = { /* Co-efficient = Inverse of q (mod p). */ 0x48, 0xe5, 0xca, 0xbc, 0xf1, 0xdb, 0x8d, 0x6f, 0x35, 0x49, 0x8a, 0xca, 0x8c, 0x40, 0x6d, 0xb7, 0x1e, 0x99, 0xe9, 0x5b, 0xd3, 0xb0, 0x5c, 0x1d, 0x47, 0x95, 0x05, 0xe9, 0x33, 0xd0, 0x8c, 0x76, 0xea, 0x3c, 0x25, 0x9d, 0x8d, 0xfa, 0x3c, 0xa1, 0x34, 0x19, 0x8d, 0xbc, 0x02, 0xcc, 0x47, 0x59, 0xcc, 0xef, 0x7f, 0x76, 0x76, 0x08, 0x8f, 0xb2, 0x7b, 0x4c, 0xde, 0x3b, 0xde, 0x14, 0xee, 0x82 }; IppsBigNumState* NewIppBn(int bitSize, const uint8_t* pData) { IppsBigNumState* pCtx = NULL; int ctxSize = 0; int length = BITS_DWORDS(bitSize); ippsBigNumGetSize(length, &ctxSize); pCtx = (IppsBigNumState*)( malloc(ctxSize) ); if(NULL!=pCtx) { ippsBigNumInit(length, pCtx); if(NULL!=pData) ippsSetOctString_BN(pData, BITS_BYTES(bitSize), pCtx); } return pCtx; } void DeleteIppBn(IppsBigNumState* pCtx) { free(pCtx); } int main() { int ret; uint32_t keyCtxSizeBytes = 0, bitsP = 0, bitsQ = 0,modulusBitSize = 1024; IppsRSAPrivateKeyState* pPvtCtx = (IppsRSAPrivateKeyState *) NULL; IppStatus ippstatus = ippStsNoErr; const IppLibraryVersion *lib; const IppLibraryVersion *libcp; /* Init Intel IPP library */ ippInit(); /* Get Intel IPP library version info */ lib = ippGetLibVersion(); printf("%s %s built at %s for CPU %s (%d.%d.%d.%d)\n", lib->Name, lib->Version, lib->BuildDate, lib->targetCpu, lib->major, lib->minor, lib->majorBuild, lib->build); libcp = ippcpGetLibVersion(); printf("%s %s built at %s for CPU %s (%d.%d.%d.%d)\n", libcp->Name, libcp->Version, libcp->BuildDate, libcp->targetCpu, libcp->major, libcp->minor, libcp->majorBuild, libcp->build); IppsBigNumState* pP = NewIppBn(modulusBitSize, NULL); IppsBigNumState* pQ = NewIppBn(modulusBitSize, NULL); IppsBigNumState* pdP = NewIppBn(modulusBitSize, NULL); IppsBigNumState* pdQ = NewIppBn(modulusBitSize, NULL); IppsBigNumState* pQInvP = NewIppBn(modulusBitSize, NULL); if (pP == NULL || pQ == NULL || pdP == NULL || pdQ == NULL || pQInvP == NULL) { ret = -1; goto error; } /* store the prime P as a big number */ ippstatus = ippsSetOctString_BN(private_key_1024_p,64,pP); if (ippStsNoErr != ippstatus){ ret = -2; goto error; } /* store the prime Q as a big number */ ippstatus = ippsSetOctString_BN(private_key_1024_q,64,pQ); if (ippStsNoErr != ippstatus) { ret = -3; goto error; } /* store the ExpDp as a big number */ ippstatus = ippsSetOctString_BN(private_key_1024_dp,64,pdP); if (ippStsNoErr != ippstatus) { ret = -4; goto error; } /* store the ExpDq as a big number */ ippstatus = ippsSetOctString_BN(private_key_1024_dq,64,pdQ); if (ippStsNoErr != ippstatus) { ret = -5; goto error; } /* store Inv Q as a big number */ ippstatus = ippsSetOctString_BN(private_key_1024_invp,64,pQInvP); if (ippStsNoErr != ippstatus) { ret = -6; goto error; } /* Obtain the size of the first factor of the private key, in bits. */ ippstatus = ippsRef_BN((IppsBigNumSGN *) NULL, (int *) &bitsP, (Ipp32u** const) NULL, pP); if ( ippStsNoErr != ippstatus ) { ret = -7; goto error; } printf("bitsP = %d\n",(int)bitsP); /* Obtain the size of second factor of the private key, in bits. */ ippstatus = ippsRef_BN((IppsBigNumSGN *) NULL, (int *) &bitsQ, (Ipp32u** const) NULL, pQ); if ( ippStsNoErr != ippstatus ) { ret = -8; goto error; } printf("bitsQ = %d\n",(int)bitsQ); /* Obtain the size of the memory buffer (bytes) required for the private key of Type 2. */ ippstatus = ippsRSA_GetSizePrivateKeyType2(bitsP, bitsQ,(int *) &keyCtxSizeBytes); if ( ippStsNoErr != ippstatus ) { ret = -9; goto error; } printf("keyCtxSizeBytes=%d\n",(int)keyCtxSizeBytes); /* Set up storage space for the Private Key of type 2. */ pPvtCtx = (IppsRSAPrivateKeyState *) malloc(keyCtxSizeBytes *sizeof(Ipp8u)); if (pPvtCtx == (IppsRSAPrivateKeyState *) NULL) { ret = -10; goto error; } printf("keyCtxSizeBytes *sizeof(Ipp8u) = %d\n",(int)((int)keyCtxSizeBytes *(int)(sizeof(Ipp8u)))); /* Initialize the private key of Type 2. */ ippstatus = ippsRSA_InitPrivateKeyType2(bitsP,bitsQ,pPvtCtx,keyCtxSizeBytes); if (ippStsNoErr != ippstatus) { ret = -11; goto error; } ippstatus = ippsRSA_SetPrivateKeyType2(pP, pQ, pdP, pdQ, pQInvP, pPvtCtx); if (ippStsNoErr != ippstatus) { ret = -12; printf("set error----\n"); goto error; } ret = 1; error: if(pP != NULL) { DeleteIppBn(pP); } if(pQ != NULL) { DeleteIppBn(pQ); } if(pdP != NULL) { DeleteIppBn(pdP); } if(pdQ != NULL) { DeleteIppBn(pdQ); } if(pQInvP != NULL) { DeleteIppBn(pQInvP); } if(pPvtCtx != NULL) { free(pPvtCtx); } printf("ret = %d\n",ret); return ret; }