38 #ifndef RSA_PUBLIC_EXPONENT
40 #define RSA_PUBLIC_EXPONENT 257
42 #define RSA_PUBLIC_EXPONENT 65537L
56 #if defined( USE_PGP ) || defined( USE_PGPKEYS )
57 #define MIN_PUBLIC_EXPONENT 17
58 #elif defined( USE_SSH )
59 #define MIN_PUBLIC_EXPONENT 33
61 #define MIN_PUBLIC_EXPONENT 257
63 #if ( MIN_PUBLIC_EXPONENT <= 0xFF && RSAPARAM_MIN_E > 1 ) || \
64 ( MIN_PUBLIC_EXPONENT <= 0xFFFF && RSAPARAM_MIN_E > 2 )
65 #error RSAPARAM_MIN_E is too large for MIN_PUBLIC_EXPONENT
76 #if defined( __WINCE__ ) && defined( ARMV4 ) && defined( NDEBUG )
77 #pragma optimize( "g", off )
81 static
int enableSidechannelProtection(
INOUT PKC_INFO *pkcInfo,
84 BIGNUM *
n = &pkcInfo->rsaParam_n, *e = &pkcInfo->rsaParam_e;
85 BIGNUM *k = &pkcInfo->rsaParam_blind_k;
86 BIGNUM *kInv = &pkcInfo->rsaParam_blind_kInv;
92 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
103 &msgData, CRYPT_IATTRIBUTE_RANDOM_NONCE );
106 buffer[ 0 ] &= 0xFF >> ( -pkcInfo->keySizeBits & 7 );
107 status = importBignum( k, buffer, noBytes,
MIN_PKCSIZE - 8,
116 CK(
BN_mod( k, k, n, pkcInfo->bnCTX ) );
120 &pkcInfo->rsaParam_mont_n ) );
141 #if defined( __WINCE__ ) && defined( ARMV4 ) && defined( NDEBUG )
142 #pragma optimize( "g", on )
148 static
int fixCRTvalues(
INOUT PKC_INFO *pkcInfo,
151 BIGNUM *
p = &pkcInfo->rsaParam_p, *q = &pkcInfo->rsaParam_q;
153 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
165 BN_swap( &pkcInfo->rsaParam_exponent1, &pkcInfo->rsaParam_exponent2 );
167 pkcInfo->bnCTX ) != NULL ? \
174 static
int getRSAMontgomery(
INOUT PKC_INFO *pkcInfo,
177 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
187 return(
BN_MONT_CTX_set( &pkcInfo->rsaParam_mont_p, &pkcInfo->rsaParam_p,
188 pkcInfo->bnCTX ) && \
205 static
int checkRSAPublicKeyComponents(
INOUT PKC_INFO *pkcInfo )
207 BIGNUM *
n = &pkcInfo->rsaParam_n, *e = &pkcInfo->rsaParam_e;
211 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
225 if( !primeSieve( n ) )
253 if( eWord != 17 && eWord != 257 && eWord != 65537L && !primeSieve( e ) )
266 if( eWord == 33 || eWord == 35 )
281 static
int checkRSAPrivateKeyComponents(
INOUT PKC_INFO *pkcInfo )
283 BIGNUM *n = &pkcInfo->rsaParam_n, *e = &pkcInfo->rsaParam_e;
284 BIGNUM *d = &pkcInfo->rsaParam_d, *p = &pkcInfo->rsaParam_p;
285 BIGNUM *q = &pkcInfo->rsaParam_q;
286 BIGNUM *p1 = &pkcInfo->tmp1, *q1 = &pkcInfo->tmp2, *tmp = &pkcInfo->tmp3;
290 assert(
isWritePtr( pkcInfo,
sizeof( PKC_INFO ) ) );
293 if( !primeSieve( p ) || !primeSieve( q ) )
330 CK(
BN_mul( tmp, p, q, pkcInfo->bnCTX ) );
370 CK(
BN_mod_mul( tmp, q, &pkcInfo->rsaParam_u, p, pkcInfo->bnCTX ) );
372 CK(
BN_mod_mul( tmp, p, &pkcInfo->rsaParam_u, q, pkcInfo->bnCTX ) );
377 if(
BN_cmp( &pkcInfo->rsaParam_exponent1, p ) >= 0 || \
378 BN_cmp( &pkcInfo->rsaParam_exponent2, q ) >= 0 )
382 if(
BN_cmp( &pkcInfo->rsaParam_u, p ) >= 0 )
391 CK(
BN_mod( tmp, p1, e, pkcInfo->bnCTX ) );
394 CK(
BN_mod( tmp, q1, e, pkcInfo->bnCTX ) );
439 PKC_INFO *pkcInfo = contextInfoPtr->ctxPKC;
440 BIGNUM *d = &pkcInfo->rsaParam_d, *p = &pkcInfo->rsaParam_p;
441 BIGNUM *q = &pkcInfo->rsaParam_q;
442 BIGNUM *tmp = &pkcInfo->tmp1;
451 pBits = ( keyBits + 1 ) / 2;
452 qBits = keyBits - pBits;
453 pkcInfo->keySizeBits = pBits + qBits;
466 status = fixCRTvalues( pkcInfo,
FALSE );
473 CK(
BN_mul( tmp, p, q, pkcInfo->bnCTX ) );
496 CK(
BN_mod( &pkcInfo->rsaParam_exponent1, d,
497 p, pkcInfo->bnCTX ) );
498 CK(
BN_mod( &pkcInfo->rsaParam_exponent2, d, q, pkcInfo->bnCTX ) );
505 CK(
BN_mul( &pkcInfo->rsaParam_n, p, q, pkcInfo->bnCTX ) );
515 pkcInfo->keySizeBits =
BN_num_bits( &pkcInfo->rsaParam_n );
518 status = getRSAMontgomery( pkcInfo,
TRUE );
523 status = checkRSAPublicKeyComponents( pkcInfo );
525 status = checkRSAPrivateKeyComponents( pkcInfo );
532 return( enableSidechannelProtection( pkcInfo,
TRUE ) );
540 PKC_INFO *pkcInfo = contextInfoPtr->ctxPKC;
541 BIGNUM *n = &pkcInfo->rsaParam_n, *e = &pkcInfo->rsaParam_e;
542 BIGNUM *d = &pkcInfo->rsaParam_d, *p = &pkcInfo->rsaParam_p;
543 BIGNUM *q = &pkcInfo->rsaParam_q;
558 (
BN_is_zero( &pkcInfo->rsaParam_exponent1 ) || \
559 BN_is_zero( &pkcInfo->rsaParam_exponent2 ) ) )
568 status = checkRSAPublicKeyComponents( pkcInfo );
576 status = getRSAMontgomery( pkcInfo,
FALSE );
579 pkcInfo->keySizeBits =
BN_num_bits( &pkcInfo->rsaParam_n );
584 return( enableSidechannelProtection( pkcInfo,
FALSE ) );
590 if(
BN_is_zero( &pkcInfo->rsaParam_exponent1 ) )
592 BIGNUM *exponent1 = &pkcInfo->rsaParam_exponent1;
593 BIGNUM *exponent2 = &pkcInfo->rsaParam_exponent2;
598 CK(
BN_mod( exponent1, d, exponent1, pkcInfo->bnCTX ) );
605 CK(
BN_mod( exponent2, d, exponent2, pkcInfo->bnCTX ) );
619 status = checkRSAPrivateKeyComponents( pkcInfo );
626 status = fixCRTvalues( pkcInfo,
TRUE );
631 status = getRSAMontgomery( pkcInfo,
TRUE );
634 pkcInfo->keySizeBits =
BN_num_bits( &pkcInfo->rsaParam_n );
639 return( enableSidechannelProtection( pkcInfo,
TRUE ) );