40 memcpy(
buffer + 1,
"abcde", 5 );
50 status = capabilityInfoPtr->encryptFunction( contextInfoPtr,
56 encrSize = dlpParams.
outLen;
59 status = capabilityInfoPtr->decryptFunction( contextInfoPtr,
63 return( !memcmp(
buffer + 1,
"abcde", 5 ) );
66 #ifndef CONFIG_NO_SELFTEST
74 const int pLen;
const BYTE p[ 128 ];
75 const int qLen;
const BYTE q[ 20 ];
76 const int gLen;
const BYTE g[ 128 ];
77 const int xLen;
const BYTE x[ 20 ];
78 const int yLen;
const BYTE y[ 128 ];
84 { 0x04, 0x4C, 0xDD, 0x5D, 0xB6, 0xED, 0x23, 0xAE,
85 0xB2, 0xA7, 0x59, 0xE6, 0xF8, 0x3D, 0xA6, 0x27,
86 0x85, 0xF2, 0xFE, 0xE2, 0xE8, 0xF3, 0xDA, 0xA3,
87 0x7B, 0xD6, 0x48, 0xD4, 0x44, 0xCA, 0x6E, 0x10,
88 0x97, 0x6C, 0x1D, 0x6C, 0x39, 0xA7, 0x0C, 0x88,
89 0x8E, 0x1F, 0xDD, 0xF7, 0x59, 0x69, 0xDA, 0x36,
90 0xDD, 0xB8, 0x3E, 0x1A, 0xD2, 0x91, 0x3E, 0x30,
91 0xB1, 0xB5, 0xC2, 0xBC, 0xA9, 0xA3, 0xA5, 0xDE,
92 0xC7, 0xCF, 0x51, 0x2C, 0x1B, 0x89, 0xD0, 0x71,
93 0xE3, 0x71, 0xBB, 0x50, 0x86, 0x26, 0x32, 0x9F,
94 0xF5, 0x4A, 0x9C, 0xB1, 0x78, 0x7B, 0x47, 0x1F,
95 0x19, 0xC7, 0x26, 0x22, 0x15, 0x62, 0x71, 0xAB,
96 0xD7, 0x25, 0xA5, 0xE4, 0x68, 0x71, 0x93, 0x5D,
97 0x1F, 0x29, 0x01, 0x05, 0x9C, 0x57, 0x3A, 0x09,
98 0xB0, 0xB8, 0xE4, 0xD2, 0x37, 0x90, 0x36, 0x2F,
99 0xBF, 0x1E, 0x74, 0xB4, 0x6B, 0xE4, 0x66, 0x07 },
103 { 0xFD, 0xD9, 0xC8, 0x5F, 0x73, 0x62, 0xC9, 0x79,
104 0xEF, 0xD5, 0x09, 0x07, 0x02, 0xE7, 0xF2, 0x90,
105 0x97, 0x13, 0x26, 0x1D },
109 { 0x02, 0x4E, 0xDD, 0x0D, 0x7F, 0x4D, 0xB1, 0x42,
110 0x01, 0x50, 0xE7, 0x9A, 0x65, 0x73, 0x8B, 0x31,
111 0x24, 0x6B, 0xC6, 0x74, 0xA7, 0x68, 0x26, 0x11,
112 0x06, 0x3C, 0x96, 0xA9, 0xA6, 0x23, 0x12, 0x79,
113 0xC4, 0xEE, 0x21, 0x88, 0xDD, 0xE3, 0xF0, 0x37,
114 0xCE, 0x3E, 0x54, 0x53, 0x57, 0x03, 0x30, 0xE4,
115 0xD3, 0xAB, 0x39, 0x4E, 0x39, 0xDC, 0xA2, 0x88,
116 0x82, 0xF6, 0xE8, 0xBA, 0xAC, 0xF5, 0x7D, 0x2F,
117 0x23, 0x9A, 0x09, 0x94, 0xB2, 0x89, 0xA2, 0xC9,
118 0x7C, 0xBE, 0x4D, 0x48, 0x0E, 0x59, 0x51, 0xB8,
119 0x7D, 0x99, 0x88, 0x79, 0xA8, 0x13, 0x0E, 0x12,
120 0x56, 0x9D, 0x4B, 0x2E, 0xE0, 0xE1, 0x37, 0x78,
121 0x6F, 0xCC, 0x4D, 0x97, 0xA9, 0x02, 0x0E, 0xD2,
122 0x43, 0x83, 0xEC, 0x4F, 0xC2, 0x70, 0xEF, 0x16,
123 0xDE, 0xBF, 0xBA, 0xD1, 0x6C, 0x8A, 0x36, 0xEE,
124 0x42, 0x41, 0xE9, 0xE7, 0x66, 0xAE, 0x46, 0x3B },
128 { 0xD9, 0x41, 0x29, 0xF7, 0x40, 0x32, 0x09, 0x71,
129 0xB8, 0xE2, 0xB8, 0xCB, 0x74, 0x46, 0x0B, 0xD4,
130 0xF2, 0xAB, 0x54, 0xA1 },
134 { 0x01, 0x7E, 0x16, 0x5B, 0x65,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 0x34, 0xE5, 0x79,
171 0xE9, 0x45, 0xB0, 0xDD, 0x5E, 0x56, 0xD7, 0x82,
172 0xEF, 0x93, 0xEF, 0x5F, 0xD0, 0x71, 0x8B, 0xA1,
173 0x3E, 0xA0, 0x55, 0x6A, 0xB9, 0x6E, 0x72, 0xFE,
174 0x17, 0x03, 0x95, 0x50, 0xB7, 0xA1, 0x11, 0xBA,
178 static
int selfTest(
void )
181 PKC_INFO contextData, *pkcInfo = &contextData;
185 status = staticInitContext( &contextInfo,
CONTEXT_PKC,
187 sizeof( PKC_INFO ), NULL );
190 status = importBignum( &pkcInfo->dlpParam_p, dlpTestKey.p,
194 status = importBignum( &pkcInfo->dlpParam_g, dlpTestKey.g,
199 status = importBignum( &pkcInfo->dlpParam_q, dlpTestKey.q,
204 status = importBignum( &pkcInfo->dlpParam_y, dlpTestKey.y,
209 status = importBignum( &pkcInfo->dlpParam_x, dlpTestKey.x,
215 staticDestroyContext( &contextInfo );
221 memset(
buffer,
'*', 20 );
226 memset(
buffer,
'*', 20 );
233 status = contextInfo.
capabilityInfo->initKeyFunction( &contextInfo, NULL, 0 );
235 !pairwiseConsistencyTest( &contextInfo,
FALSE ) )
239 staticDestroyContext( &contextInfo );
244 #define selfTest NULL
277 #define ELGAMAL_SIGPART_SIZE 20
283 PKC_INFO *pkcInfo = &contextInfoPtr->ctxPKC;
284 BIGNUM *
p = &pkcInfo->dlpParam_p, *g = &pkcInfo->dlpParam_g;
285 BIGNUM *x = &pkcInfo->dlpParam_x;
286 BIGNUM *tmp = &pkcInfo->tmp1, *k = &pkcInfo->tmp2, *kInv = &pkcInfo->tmp3;
287 BIGNUM *r = &pkcInfo->dlpTmp1, *
s = &pkcInfo->dlpTmp2;
288 BIGNUM *phi_p = &pkcInfo->dlpTmp3;
292 assert( noBytes == ELGAMAL_SIGPART_SIZE || noBytes == -1 );
307 status = importBignum( k, (
BYTE * ) kVal, ELGAMAL_SIGPART_SIZE,
308 ELGAMAL_SIGPART_SIZE, ELGAMAL_SIGPART_SIZE,
313 status = generateBignum( k,
bytesToBits( ELGAMAL_SIGPART_SIZE + \
331 BN_gcd( r, k, phi_p, pkcInfo->bnCTX );
335 BN_gcd( r, k, phi_p, pkcInfo->bnCTX );
339 status = importBignum(
s, bufPtr, ELGAMAL_SIGPART_SIZE, q,
345 BN_mod_exp_mont( r, g, k, p, pkcInfo->bnCTX, &pkcInfo->dlpParam_mont_p );
360 length = encodeDLValues( buffer, r,
s );
367 static int sigCheck(
CONTEXT_INFO *contextInfoPtr,
BYTE *buffer,
int noBytes )
369 PKC_INFO *pkcInfo = &contextInfoPtr->ctxPKC;
370 BIGNUM *p = &pkcInfo->dlpParam_p, *g = &pkcInfo->dlpParam_g;
371 BIGNUM *y = &pkcInfo->dlpParam_y;
372 BIGNUM *r = &pkcInfo->tmp1, *
s = &pkcInfo->tmp1;
377 status = decodeDLValues( buffer + ELGAMAL_SIGPART_SIZE, noBytes, &r, &
s );
393 status = importBignum( hash, buffer, ELGAMAL_SIGPART_SIZE,
400 pkcInfo->bnCTX, &pkcInfo->dlpParam_mont_p );
402 pkcInfo->bnCTX, &pkcInfo->dlpParam_mont_p );
403 BN_mod_mul_mont( u1, u1, r, p,
404 pkcInfo->bnCTX, &pkcInfo->dlpParam_mont_p );
408 &pkcInfo->dlpParam_mont_p );
438 PKC_INFO *pkcInfo = contextInfoPtr->ctxPKC;
440 BIGNUM *p = &pkcInfo->dlpParam_p, *g = &pkcInfo->dlpParam_g;
441 BIGNUM *y = &pkcInfo->dlpParam_y;
442 BIGNUM *tmp = &pkcInfo->tmp1, *k = &pkcInfo->tmp2;
443 BIGNUM *r = &pkcInfo->tmp3, *
s = &pkcInfo->dlpTmp1;
444 BIGNUM *phi_p = &pkcInfo->dlpTmp2;
445 const int length =
bitsToBytes( pkcInfo->keySizeBits );
455 REQUIRES( dlpParams->inParam2 == NULL && \
456 ( dlpParams->
inLen2 == 0 || dlpParams->
inLen2 == -999 ) );
464 for( i = 0; i <
length; i++ )
466 if( buffer[ i ] != 0 )
482 if( dlpParams->
inLen2 == -999 )
484 status = importBignum( k, (
BYTE * ) kRandomVal, length,
493 status = generateBignum( k,
bytesToBits( length + \
510 CK(
BN_gcd(
s, k, phi_p, pkcInfo->bnCTX ) );
514 CK(
BN_gcd(
s, k, phi_p, pkcInfo->bnCTX ) );
520 status = importBignum( tmp, (
BYTE * ) dlpParams->inParam1, length,
528 pkcInfo->bnCTX, &pkcInfo->dlpParam_mont_p ) );
536 &pkcInfo->dlpParam_mont_p ) );
541 status = pkcInfo->encodeDLValuesFunction( dlpParams->outParam,
564 PKC_INFO *pkcInfo = contextInfoPtr->ctxPKC;
566 BIGNUM *p = &pkcInfo->dlpParam_p, *x = &pkcInfo->dlpParam_x;
567 BIGNUM *r = &pkcInfo->tmp1, *
s = &pkcInfo->tmp2, *tmp = &pkcInfo->tmp3;
568 const int length =
bitsToBytes( pkcInfo->keySizeBits );
579 REQUIRES( dlpParams->inParam2 == NULL && dlpParams->
inLen2 == 0 );
585 status = pkcInfo->decodeDLValuesFunction( dlpParams->inParam1,
593 pkcInfo->bnCTX, &pkcInfo->dlpParam_mont_p ) );
609 memset( dlpParams->outParam, 0, offset );
611 status = exportBignum( dlpParams->outParam + offset,
612 dlpParams->
outLen - offset, &dummy,
s );
639 PKC_INFO *pkcInfo = contextInfoPtr->ctxPKC;
642 assert( ( key == NULL && keyLength == 0 ) || \
646 REQUIRES( ( key == NULL && keyLength == 0 ) || \
658 contextInfoPtr->flags |= ( egKey->
isPublicKey ) ? \
660 status = importBignum( &pkcInfo->dlpParam_p, egKey->
p,
665 status = importBignum( &pkcInfo->dlpParam_g, egKey->
g,
668 &pkcInfo->dlpParam_p,
671 status = importBignum( &pkcInfo->dlpParam_q, egKey->
q,
674 &pkcInfo->dlpParam_p,
677 status = importBignum( &pkcInfo->dlpParam_y, egKey->
y,
680 &pkcInfo->dlpParam_p,
683 status = importBignum( &pkcInfo->dlpParam_x, egKey->
x,
686 &pkcInfo->dlpParam_p,
688 contextInfoPtr->flags |= CONTEXT_FLAG_PBO;
705 return( initCheckDLPkey( contextInfoPtr,
FALSE,
TRUE ) );
707 return( initCheckDLPkey( contextInfoPtr,
FALSE,
FALSE ) );
715 const int keySizeBits )
724 status = generateDLPkey( contextInfoPtr, keySizeBits );
729 !pairwiseConsistencyTest( contextInfoPtr,
TRUE ) )
731 DEBUG_DIAG((
"Consistency check of freshly-generated Elgamal key "
748 selfTest, getDefaultInfo, NULL, NULL, initKey, generateKey, encryptFn, decryptFn
753 return( &capabilityInfo );