2 * The RSA public-key cryptosystem
4 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
7 * Joined copyright on original XySSL code with: Christophe Devine
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
30 /* $Cambridge: exim/src/src/pdkim/rsa.c,v 1.3 2009/12/07 13:05:07 tom Exp $ */
40 /* *************** begin copy from x509parse.c ********************/
42 * ASN.1 DER decoding routines
44 static int asn1_get_len( unsigned char **p,
48 if( ( end - *p ) < 1 )
49 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
51 if( ( **p & 0x80 ) == 0 )
58 if( ( end - *p ) < 2 )
59 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
66 if( ( end - *p ) < 3 )
67 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
69 *len = ( (*p)[1] << 8 ) | (*p)[2];
74 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
79 if( *len > (int) ( end - *p ) )
80 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
85 static int asn1_get_tag( unsigned char **p,
89 if( ( end - *p ) < 1 )
90 return( POLARSSL_ERR_ASN1_OUT_OF_DATA );
93 return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
97 return( asn1_get_len( p, end, len ) );
100 static int asn1_get_int( unsigned char **p,
106 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
109 if( len > (int) sizeof( int ) || ( **p & 0x80 ) != 0 )
110 return( POLARSSL_ERR_ASN1_INVALID_LENGTH );
116 *val = ( *val << 8 ) | **p;
123 static int asn1_get_mpi( unsigned char **p,
129 if( ( ret = asn1_get_tag( p, end, &len, ASN1_INTEGER ) ) != 0 )
132 ret = mpi_read_binary( X, *p, len );
138 /* *************** end copy from x509parse.c ********************/
143 * Initialize an RSA context
145 void rsa_init( rsa_context *ctx,
148 int (*f_rng)(void *),
151 memset( ctx, 0, sizeof( rsa_context ) );
153 ctx->padding = padding;
154 ctx->hash_id = hash_id;
162 * Check a public RSA key
164 int rsa_check_pubkey( rsa_context *ctx )
166 if( !ctx->N.p || !ctx->E.p )
167 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
169 if( ( ctx->N.p[0] & 1 ) == 0 ||
170 ( ctx->E.p[0] & 1 ) == 0 )
171 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
173 if( mpi_msb( &ctx->N ) < 128 ||
174 mpi_msb( &ctx->N ) > 4096 )
175 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
177 if( mpi_msb( &ctx->E ) < 2 ||
178 mpi_msb( &ctx->E ) > 64 )
179 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
185 * Check a private RSA key
187 int rsa_check_privkey( rsa_context *ctx )
190 mpi PQ, DE, P1, Q1, H, I, G;
192 if( ( ret = rsa_check_pubkey( ctx ) ) != 0 )
195 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
196 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
198 mpi_init( &PQ, &DE, &P1, &Q1, &H, &I, &G, NULL );
200 MPI_CHK( mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
201 MPI_CHK( mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
202 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
203 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
204 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
205 MPI_CHK( mpi_mod_mpi( &I, &DE, &H ) );
206 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
208 if( mpi_cmp_mpi( &PQ, &ctx->N ) == 0 &&
209 mpi_cmp_int( &I, 1 ) == 0 &&
210 mpi_cmp_int( &G, 1 ) == 0 )
212 mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, NULL );
218 mpi_free( &G, &I, &H, &Q1, &P1, &DE, &PQ, NULL );
219 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED | ret );
223 * Do an RSA public key operation
225 int rsa_public( rsa_context *ctx,
226 unsigned char *input,
227 unsigned char *output )
232 mpi_init( &T, NULL );
234 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
236 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
238 mpi_free( &T, NULL );
239 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
243 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
244 MPI_CHK( mpi_write_binary( &T, output, olen ) );
248 mpi_free( &T, NULL );
251 return( POLARSSL_ERR_RSA_PUBLIC_FAILED | ret );
257 * Do an RSA private key operation
259 int rsa_private( rsa_context *ctx,
260 unsigned char *input,
261 unsigned char *output )
266 mpi_init( &T, &T1, &T2, NULL );
268 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
270 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
272 mpi_free( &T, NULL );
273 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
277 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
280 * faster decryption using the CRT
282 * T1 = input ^ dP mod P
283 * T2 = input ^ dQ mod Q
285 MPI_CHK( mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
286 MPI_CHK( mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
289 * T = (T1 - T2) * (Q^-1 mod P) mod P
291 MPI_CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
292 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
293 MPI_CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
296 * output = T2 + T * Q
298 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
299 MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) );
303 MPI_CHK( mpi_write_binary( &T, output, olen ) );
307 mpi_free( &T, &T1, &T2, NULL );
310 return( POLARSSL_ERR_RSA_PRIVATE_FAILED | ret );
316 * Add the message padding, then do an RSA operation
318 int rsa_pkcs1_encrypt( rsa_context *ctx,
320 unsigned char *input,
321 unsigned char *output )
324 unsigned char *p = output;
328 switch( ctx->padding )
332 if( ilen < 0 || olen < ilen + 11 )
333 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
335 nb_pad = olen - 3 - ilen;
340 while( nb_pad-- > 0 )
343 *p = (unsigned char) rand();
348 memcpy( p, input, ilen );
353 return( POLARSSL_ERR_RSA_INVALID_PADDING );
356 return( ( mode == RSA_PUBLIC )
357 ? rsa_public( ctx, output, output )
358 : rsa_private( ctx, output, output ) );
362 * Do an RSA operation, then remove the message padding
364 int rsa_pkcs1_decrypt( rsa_context *ctx,
366 unsigned char *input,
367 unsigned char *output,
372 unsigned char buf[1024];
376 if( ilen < 16 || ilen > (int) sizeof( buf ) )
377 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
379 ret = ( mode == RSA_PUBLIC )
380 ? rsa_public( ctx, input, buf )
381 : rsa_private( ctx, input, buf );
388 switch( ctx->padding )
392 if( *p++ != 0 || *p++ != RSA_CRYPT )
393 return( POLARSSL_ERR_RSA_INVALID_PADDING );
397 if( p >= buf + ilen - 1 )
398 return( POLARSSL_ERR_RSA_INVALID_PADDING );
406 return( POLARSSL_ERR_RSA_INVALID_PADDING );
409 if (ilen - (int)(p - buf) > output_max_len)
410 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
412 *olen = ilen - (int)(p - buf);
413 memcpy( output, p, *olen );
419 * Do an RSA operation to sign the message digest
421 int rsa_pkcs1_sign( rsa_context *ctx,
429 unsigned char *p = sig;
433 switch( ctx->padding )
440 nb_pad = olen - 3 - hashlen;
446 nb_pad = olen - 3 - 34;
450 nb_pad = olen - 3 - 35;
454 nb_pad = olen - 3 - 47;
458 nb_pad = olen - 3 - 51;
462 nb_pad = olen - 3 - 67;
466 nb_pad = olen - 3 - 83;
471 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
475 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
479 memset( p, 0xFF, nb_pad );
486 return( POLARSSL_ERR_RSA_INVALID_PADDING );
492 memcpy( p, hash, hashlen );
496 memcpy( p, ASN1_HASH_MDX, 18 );
497 memcpy( p + 18, hash, 16 );
501 memcpy( p, ASN1_HASH_MDX, 18 );
502 memcpy( p + 18, hash, 16 );
506 memcpy( p, ASN1_HASH_MDX, 18 );
507 memcpy( p + 18, hash, 16 );
511 memcpy( p, ASN1_HASH_SHA1, 15 );
512 memcpy( p + 15, hash, 20 );
516 memcpy( p, ASN1_HASH_SHA2X, 19 );
517 memcpy( p + 19, hash, 28 );
518 p[1] += 28; p[14] = 4; p[18] += 28; break;
521 memcpy( p, ASN1_HASH_SHA2X, 19 );
522 memcpy( p + 19, hash, 32 );
523 p[1] += 32; p[14] = 1; p[18] += 32; break;
526 memcpy( p, ASN1_HASH_SHA2X, 19 );
527 memcpy( p + 19, hash, 48 );
528 p[1] += 48; p[14] = 2; p[18] += 48; break;
531 memcpy( p, ASN1_HASH_SHA2X, 19 );
532 memcpy( p + 19, hash, 64 );
533 p[1] += 64; p[14] = 3; p[18] += 64; break;
536 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
539 return( ( mode == RSA_PUBLIC )
540 ? rsa_public( ctx, sig, sig )
541 : rsa_private( ctx, sig, sig ) );
545 * Do an RSA operation and check the message digest
547 int rsa_pkcs1_verify( rsa_context *ctx,
554 int ret, len, siglen;
556 unsigned char buf[1024];
560 if( siglen < 16 || siglen > (int) sizeof( buf ) )
561 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
563 ret = ( mode == RSA_PUBLIC )
564 ? rsa_public( ctx, sig, buf )
565 : rsa_private( ctx, sig, buf );
572 switch( ctx->padding )
576 if( *p++ != 0 || *p++ != RSA_SIGN )
577 return( POLARSSL_ERR_RSA_INVALID_PADDING );
581 if( p >= buf + siglen - 1 || *p != 0xFF )
582 return( POLARSSL_ERR_RSA_INVALID_PADDING );
590 return( POLARSSL_ERR_RSA_INVALID_PADDING );
593 len = siglen - (int)( p - buf );
600 if( memcmp( p, ASN1_HASH_MDX, 18 ) != 0 )
601 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
603 if( ( c == 2 && hash_id == SIG_RSA_MD2 ) ||
604 ( c == 4 && hash_id == SIG_RSA_MD4 ) ||
605 ( c == 5 && hash_id == SIG_RSA_MD5 ) )
607 if( memcmp( p + 18, hash, 16 ) == 0 )
610 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
614 if( len == 35 && hash_id == SIG_RSA_SHA1 )
616 if( memcmp( p, ASN1_HASH_SHA1, 15 ) == 0 &&
617 memcmp( p + 15, hash, 20 ) == 0 )
620 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
622 if( ( len == 19 + 28 && p[14] == 4 && hash_id == SIG_RSA_SHA224 ) ||
623 ( len == 19 + 32 && p[14] == 1 && hash_id == SIG_RSA_SHA256 ) ||
624 ( len == 19 + 48 && p[14] == 2 && hash_id == SIG_RSA_SHA384 ) ||
625 ( len == 19 + 64 && p[14] == 3 && hash_id == SIG_RSA_SHA512 ) )
632 memcmp( p, ASN1_HASH_SHA2X, 18 ) == 0 &&
633 memcmp( p + 19, hash, c ) == 0 )
636 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
639 if( len == hashlen && hash_id == SIG_RSA_RAW )
641 if( memcmp( p, hash, hashlen ) == 0 )
644 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
647 return( POLARSSL_ERR_RSA_INVALID_PADDING );
651 * Free the components of an RSA key
653 void rsa_free( rsa_context *ctx )
655 mpi_free( &ctx->RQ, &ctx->RP, &ctx->RN,
656 &ctx->QP, &ctx->DQ, &ctx->DP,
657 &ctx->Q, &ctx->P, &ctx->D,
658 &ctx->E, &ctx->N, NULL );
662 * Parse a public RSA key
664 OpenSSL RSA public key ASN1 container
665 0:d=0 hl=3 l= 159 cons: SEQUENCE
666 3:d=1 hl=2 l= 13 cons: SEQUENCE
667 5:d=2 hl=2 l= 9 prim: OBJECT:rsaEncryption
668 16:d=2 hl=2 l= 0 prim: NULL
669 18:d=1 hl=3 l= 141 prim: BIT STRING:RSAPublicKey (below)
671 RSAPublicKey ASN1 container
672 0:d=0 hl=3 l= 137 cons: SEQUENCE
673 3:d=1 hl=3 l= 129 prim: INTEGER:Public modulus
674 135:d=1 hl=2 l= 3 prim: INTEGER:Public exponent
677 int rsa_parse_public_key( rsa_context *rsa, unsigned char *buf, int buflen )
679 unsigned char *p, *end;
685 if( ( ret = asn1_get_tag( &p, end, &len,
686 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) {
687 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
690 if( ( ret = asn1_get_tag( &p, end, &len,
691 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) == 0 ) {
692 /* Skip over embedded rsaEncryption Object */
695 /* The RSAPublicKey ASN1 container is wrapped in a BIT STRING */
696 if( ( ret = asn1_get_tag( &p, end, &len,
697 ASN1_BIT_STRING ) ) != 0 ) {
698 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
701 /* Limit range to that BIT STRING */
705 if( ( ret = asn1_get_tag( &p, end, &len,
706 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 ) {
707 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
711 if ( ( ( ret = asn1_get_mpi( &p, end, &(rsa->N) ) ) == 0 ) &&
712 ( ( ret = asn1_get_mpi( &p, end, &(rsa->E) ) ) == 0 ) ) {
713 rsa->len = mpi_size( &rsa->N );
717 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
721 * Parse a private RSA key
723 int rsa_parse_key( rsa_context *rsa, unsigned char *buf, int buflen,
724 unsigned char *pwd, int pwdlen )
727 unsigned char *s1, *s2;
728 unsigned char *p, *end;
730 s1 = (unsigned char *) strstr( (char *) buf,
731 "-----BEGIN RSA PRIVATE KEY-----" );
735 s2 = (unsigned char *) strstr( (char *) buf,
736 "-----END RSA PRIVATE KEY-----" );
738 if( s2 == NULL || s2 <= s1 )
739 return( POLARSSL_ERR_X509_KEY_INVALID_PEM );
742 if( *s1 == '\r' ) s1++;
743 if( *s1 == '\n' ) s1++;
744 else return( POLARSSL_ERR_X509_KEY_INVALID_PEM );
748 if( memcmp( s1, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
750 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
754 ret = base64_decode( NULL, &len, s1, s2 - s1 );
756 if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
757 return( ret | POLARSSL_ERR_X509_KEY_INVALID_PEM );
759 if( ( buf = (unsigned char *) malloc( len ) ) == NULL )
762 if( ( ret = base64_decode( buf, &len, s1, s2 - s1 ) ) != 0 )
765 return( ret | POLARSSL_ERR_X509_KEY_INVALID_PEM );
772 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
776 memset( rsa, 0, sizeof( rsa_context ) );
782 * RSAPrivateKey ::= SEQUENCE {
784 * modulus INTEGER, -- n
785 * publicExponent INTEGER, -- e
786 * privateExponent INTEGER, -- d
787 * prime1 INTEGER, -- p
788 * prime2 INTEGER, -- q
789 * exponent1 INTEGER, -- d mod (p-1)
790 * exponent2 INTEGER, -- d mod (q-1)
791 * coefficient INTEGER, -- (inverse of q) mod p
792 * otherPrimeInfos OtherPrimeInfos OPTIONAL
795 if( ( ret = asn1_get_tag( &p, end, &len,
796 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
802 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
807 if( ( ret = asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
813 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT | ret );
822 return( ret | POLARSSL_ERR_X509_KEY_INVALID_VERSION );
825 if( ( ret = asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
826 ( ret = asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
827 ( ret = asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
828 ( ret = asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
829 ( ret = asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
830 ( ret = asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
831 ( ret = asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
832 ( ret = asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
838 return( ret | POLARSSL_ERR_X509_KEY_INVALID_FORMAT );
841 rsa->len = mpi_size( &rsa->N );
849 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT |
850 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
853 if( ( ret = rsa_check_privkey( rsa ) ) != 0 )