1 /* $Cambridge: exim/src/src/pdkim/rsa.c,v 1.1.2.3 2009/03/17 21:11:56 tom Exp $ */
3 * The RSA public-key cryptosystem
5 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
7 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
26 * http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
27 * http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
39 * ASN.1 DER decoding routines
41 static int asn1_get_len( unsigned char **p,
45 if( ( end - *p ) < 1 )
46 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
48 if( ( **p & 0x80 ) == 0 )
55 if( ( end - *p ) < 2 )
56 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
63 if( ( end - *p ) < 3 )
64 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
66 *len = ( (*p)[1] << 8 ) | (*p)[2];
71 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
76 if( *len > (int) ( end - *p ) )
77 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
82 static int asn1_get_tag( unsigned char **p,
86 if( ( end - *p ) < 1 )
87 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
90 return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
94 return( asn1_get_len( p, end, len ) );
97 static int asn1_get_int( unsigned char **p,
103 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
106 if( len > (int) sizeof( int ) || ( **p & 0x80 ) != 0 )
107 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
113 *val = ( *val << 8 ) | **p;
120 static int asn1_get_mpi( unsigned char **p,
126 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
129 ret = mpi_read_binary( X, *p, len );
138 * Initialize an RSA context
140 void rsa_init( rsa_context *ctx,
143 int (*f_rng)(void *),
146 memset( ctx, 0, sizeof( rsa_context ) );
148 ctx->padding = padding;
149 ctx->hash_id = hash_id;
157 * Check a public RSA key
159 int rsa_check_pubkey( rsa_context *ctx )
161 if( ( ctx->N.p[0] & 1 ) == 0 ||
162 ( ctx->E.p[0] & 1 ) == 0 )
163 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
165 if( mpi_msb( &ctx->N ) < 128 ||
166 mpi_msb( &ctx->N ) > 4096 )
167 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
169 if( mpi_msb( &ctx->E ) < 2 ||
170 mpi_msb( &ctx->E ) > 64 )
171 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
177 * Check a private RSA key
179 int rsa_check_privkey( rsa_context *ctx )
182 mpi PQ, DE, P1, Q1, H, I, G;
184 if( ( ret = rsa_check_pubkey( ctx ) ) != 0 )
187 mpi_init( &PQ, &DE, &P1, &Q1, &H, &I, &G, NULL );
189 MPI_CHK( mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
190 MPI_CHK( mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
191 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
192 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
193 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
194 MPI_CHK( mpi_mod_mpi( &I, &DE, &H ) );
195 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
197 if( mpi_cmp_mpi( &PQ, &ctx->N ) == 0 &&
198 mpi_cmp_int( &I, 1 ) == 0 &&
199 mpi_cmp_int( &G, 1 ) == 0 )
201 mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, NULL );
207 mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, NULL );
208 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED | ret );
212 * Do an RSA public key operation
214 int rsa_public( rsa_context *ctx,
215 unsigned char *input,
216 unsigned char *output )
221 mpi_init( &T, NULL );
223 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
225 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
227 mpi_free( &T, NULL );
228 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
232 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
233 MPI_CHK( mpi_write_binary( &T, output, olen ) );
237 mpi_free( &T, NULL );
240 return( POLARSSL_ERR_RSA_PUBLIC_FAILED | ret );
246 * Do an RSA private key operation
248 int rsa_private( rsa_context *ctx,
249 unsigned char *input,
250 unsigned char *output )
255 mpi_init( &T, &T1, &T2, NULL );
257 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
259 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
261 mpi_free( &T, NULL );
262 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
266 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
269 * faster decryption using the CRT
271 * T1 = input ^ dP mod P
272 * T2 = input ^ dQ mod Q
274 MPI_CHK( mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
275 MPI_CHK( mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
278 * T = (T1 - T2) * (Q^-1 mod P) mod P
280 MPI_CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
281 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
282 MPI_CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
285 * output = T2 + T * Q
287 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
288 MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) );
292 MPI_CHK( mpi_write_binary( &T, output, olen ) );
296 mpi_free( &T, &T1, &T2, NULL );
299 return( POLARSSL_ERR_RSA_PRIVATE_FAILED | ret );
305 * Add the message padding, then do an RSA operation
307 int rsa_pkcs1_encrypt( rsa_context *ctx,
309 unsigned char *input,
310 unsigned char *output )
313 unsigned char *p = output;
317 switch( ctx->padding )
321 if( ilen < 0 || olen < ilen + 11 )
322 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
324 nb_pad = olen - 3 - ilen;
329 while( nb_pad-- > 0 )
332 *p = (unsigned char) rand();
337 memcpy( p, input, ilen );
342 return( POLARSSL_ERR_RSA_INVALID_PADDING );
345 return( ( mode == RSA_PUBLIC )
346 ? rsa_public( ctx, output, output )
347 : rsa_private( ctx, output, output ) );
351 * Do an RSA operation, then remove the message padding
353 int rsa_pkcs1_decrypt( rsa_context *ctx,
355 unsigned char *input,
356 unsigned char *output,
361 unsigned char buf[512];
365 if( ilen < 16 || ilen > (int) sizeof( buf ) )
366 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
368 ret = ( mode == RSA_PUBLIC )
369 ? rsa_public( ctx, input, buf )
370 : rsa_private( ctx, input, buf );
377 switch( ctx->padding )
381 if( *p++ != 0 || *p++ != RSA_CRYPT )
382 return( POLARSSL_ERR_RSA_INVALID_PADDING );
386 if( p >= buf + ilen - 1 )
387 return( POLARSSL_ERR_RSA_INVALID_PADDING );
395 return( POLARSSL_ERR_RSA_INVALID_PADDING );
398 if (ilen - (int)(p - buf) > output_max_len)
399 return( POLARSSL_ERR_RSA_OUTPUT_TO_LARGE );
401 *olen = ilen - (int)(p - buf);
402 memcpy( output, p, *olen );
408 * Do an RSA operation to sign the message digest
410 int rsa_pkcs1_sign( rsa_context *ctx,
418 unsigned char *p = sig;
422 switch( ctx->padding )
429 nb_pad = olen - 3 - hashlen;
435 nb_pad = olen - 3 - 16 - 18;
439 nb_pad = olen - 3 - 20 - 15;
443 nb_pad = olen - 3 - 32 - 19;
447 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
451 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
455 memset( p, 0xFF, nb_pad );
462 return( POLARSSL_ERR_RSA_INVALID_PADDING );
468 memcpy( p, hash, hashlen );
472 memcpy( p, ASN1_HASH_MDX, 18 );
473 memcpy( p + 18, hash, 16 );
477 memcpy( p, ASN1_HASH_MDX, 18 );
478 memcpy( p + 18, hash, 16 );
482 memcpy( p, ASN1_HASH_MDX, 18 );
483 memcpy( p + 18, hash, 16 );
487 memcpy( p, ASN1_HASH_SHA1, 15 );
488 memcpy( p + 15, hash, 20 );
492 memcpy( p, ASN1_HASH_SHA256, 19 );
493 memcpy( p + 19, hash, 32 );
497 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
500 return( ( mode == RSA_PUBLIC )
501 ? rsa_public( ctx, sig, sig )
502 : rsa_private( ctx, sig, sig ) );
506 * Do an RSA operation and check the message digest
508 int rsa_pkcs1_verify( rsa_context *ctx,
515 int ret, len, siglen;
517 unsigned char buf[512];
521 if( siglen < 16 || siglen > (int) sizeof( buf ) )
522 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
524 ret = ( mode == RSA_PUBLIC )
525 ? rsa_public( ctx, sig, buf )
526 : rsa_private( ctx, sig, buf );
533 switch( ctx->padding )
537 if( *p++ != 0 || *p++ != RSA_SIGN )
538 return( POLARSSL_ERR_RSA_INVALID_PADDING );
542 if( p >= buf + siglen - 1 || *p != 0xFF )
543 return( POLARSSL_ERR_RSA_INVALID_PADDING );
551 return( POLARSSL_ERR_RSA_INVALID_PADDING );
554 len = siglen - (int)( p - buf );
561 if( memcmp( p, ASN1_HASH_MDX, 18 ) != 0 )
562 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
564 if( ( c == 2 && hash_id == RSA_MD2 ) ||
565 ( c == 4 && hash_id == RSA_MD4 ) ||
566 ( c == 5 && hash_id == RSA_MD5 ) )
568 if( memcmp( p + 18, hash, 16 ) == 0 )
571 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
575 if( len == 35 && hash_id == RSA_SHA1 )
577 if( memcmp( p, ASN1_HASH_SHA1, 15 ) == 0 &&
578 memcmp( p + 15, hash, 20 ) == 0 )
581 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
584 if( len == 51 && hash_id == RSA_SHA256 )
586 if( memcmp( p, ASN1_HASH_SHA256, 19 ) == 0 &&
587 memcmp( p + 19, hash, 32 ) == 0 )
590 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
593 if( len == hashlen && hash_id == RSA_RAW )
595 if( memcmp( p, hash, hashlen ) == 0 )
598 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
601 return( POLARSSL_ERR_RSA_INVALID_PADDING );
605 * Free the components of an RSA key
607 void rsa_free( rsa_context *ctx )
609 mpi_free( &ctx->RQ, &ctx->RP, &ctx->RN,
610 &ctx->QP, &ctx->DQ, &ctx->DP,
611 &ctx->Q, &ctx->P, &ctx->D,
612 &ctx->E, &ctx->N, NULL );
617 * Parse a public RSA key
619 OpenSSL RSA public key ASN1 container
620 0:d=0 hl=3 l= 159 cons: SEQUENCE
621 3:d=1 hl=2 l= 13 cons: SEQUENCE
622 5:d=2 hl=2 l= 9 prim: OBJECT:rsaEncryption
623 16:d=2 hl=2 l= 0 prim: NULL
624 18:d=1 hl=3 l= 141 prim: BIT STRING:RSAPublicKey (below)
626 RSAPublicKey ASN1 container
627 0:d=0 hl=3 l= 137 cons: SEQUENCE
628 3:d=1 hl=3 l= 129 prim: INTEGER:Public modulus
629 135:d=1 hl=2 l= 3 prim: INTEGER:Public exponent
632 int rsa_parse_public_key( rsa_context *rsa, unsigned char *buf, int buflen )
634 unsigned char *p, *end;
640 if( ( ret = asn1_get_tag( &p, end, &len,
641 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) {
642 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
645 if( ( ret = asn1_get_tag( &p, end, &len,
646 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) == 0 ) {
647 /* Skip over embedded rsaEncryption Object */
650 /* The RSAPublicKey ASN1 container is wrapped in a BIT STRING */
651 if( ( ret = asn1_get_tag( &p, end, &len,
652 ASN1_BIT_STRING ) ) != 0 ) {
653 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
656 /* Limit range to that BIT STRING */
660 if( ( ret = asn1_get_tag( &p, end, &len,
661 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) {
662 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
666 if ( ( ( ret = asn1_get_mpi( &p, end, &(rsa->N) ) ) == 0 ) &&
667 ( ( ret = asn1_get_mpi( &p, end, &(rsa->E) ) ) == 0 ) ) {
668 rsa->len = mpi_size( &rsa->N );
672 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
676 * Parse a private RSA key
678 int rsa_parse_key( rsa_context *rsa, unsigned char *buf, int buflen,
679 unsigned char *pwd, int pwdlen )
682 unsigned char *s1, *s2;
683 unsigned char *p, *end;
685 s1 = (unsigned char *) strstr( (char *) buf,
686 "-----BEGIN RSA PRIVATE KEY-----" );
690 s2 = (unsigned char *) strstr( (char *) buf,
691 "-----END RSA PRIVATE KEY-----" );
693 if( s2 == NULL || s2 <= s1 )
694 return( POLARSSL_ERR_X509_KEY_INVALID_PEM );
697 if( *s1 == '\r' ) s1++;
698 if( *s1 == '\n' ) s1++;
699 else return( POLARSSL_ERR_X509_KEY_INVALID_PEM );
703 if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
705 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
709 ret = base64_decode( NULL, &len, s1, s2 - s1 );
711 if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
712 return( ret | POLARSSL_ERR_X509_KEY_INVALID_PEM );
714 if( ( buf = (unsigned char *) malloc( len ) ) == NULL )
717 if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 )
720 return( ret | POLARSSL_ERR_X509_KEY_INVALID_PEM );
727 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
731 memset( rsa, 0, sizeof( rsa_context ) );
737 * RSAPrivateKey ::= SEQUENCE {
739 * modulus INTEGER, -- n
740 * publicExponent INTEGER, -- e
741 * privateExponent INTEGER, -- d
742 * prime1 INTEGER, -- p
743 * prime2 INTEGER, -- q
744 * exponent1 INTEGER, -- d mod (p-1)
745 * exponent2 INTEGER, -- d mod (q-1)
746 * coefficient INTEGER, -- (inverse of q) mod p
747 * otherPrimeInfos OtherPrimeInfos OPTIONAL
750 if( ( ret = asn1_get_tag( &p, end, &len,
751 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
757 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
762 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
768 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
777 return( ret | POLARSSL_ERR_X509_KEY_INVALID_VERSION );
780 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
781 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
782 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
783 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
784 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
785 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
786 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
787 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
793 return( ret | POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
796 rsa->len = mpi_size( &rsa->N );
804 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT |
805 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
808 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )