2 * The RSA public-key cryptosystem
4 * Copyright (C) 2006-2010, Brainspark B.V.
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
28 * http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
29 * http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
32 /* $Cambridge: exim/src/src/pdkim/rsa.c,v 1.3 2009/12/07 13:05:07 tom Exp $ */
43 /* *************** begin copy from x509parse.c ********************/
45 * ASN.1 DER decoding routines
47 static int asn1_get_len( unsigned char **p
,
48 const unsigned char *end
,
51 if( ( end
- *p
) < 1 )
52 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
54 if( ( **p
& 0x80 ) == 0 )
61 if( ( end
- *p
) < 2 )
62 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
69 if( ( end
- *p
) < 3 )
70 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
72 *len
= ( (*p
)[1] << 8 ) | (*p
)[2];
77 return( POLARSSL_ERR_ASN1_INVALID_LENGTH
);
82 if( *len
> (int) ( end
- *p
) )
83 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
88 static int asn1_get_tag( unsigned char **p
,
89 const unsigned char *end
,
92 if( ( end
- *p
) < 1 )
93 return( POLARSSL_ERR_ASN1_OUT_OF_DATA
);
96 return( POLARSSL_ERR_ASN1_UNEXPECTED_TAG
);
100 return( asn1_get_len( p
, end
, len
) );
103 static int asn1_get_int( unsigned char **p
,
104 const unsigned char *end
,
109 if( ( ret
= asn1_get_tag( p
, end
, &len
, ASN1_INTEGER
) ) != 0 )
112 if( len
> (int) sizeof( int ) || ( **p
& 0x80 ) != 0 )
113 return( POLARSSL_ERR_ASN1_INVALID_LENGTH
);
119 *val
= ( *val
<< 8 ) | **p
;
126 static int asn1_get_mpi( unsigned char **p
,
127 const unsigned char *end
,
132 if( ( ret
= asn1_get_tag( p
, end
, &len
, ASN1_INTEGER
) ) != 0 )
135 ret
= mpi_read_binary( X
, *p
, len
);
141 /* *************** end copy from x509parse.c ********************/
147 * Initialize an RSA context
149 void rsa_init( rsa_context
*ctx
,
153 memset( ctx
, 0, sizeof( rsa_context
) );
155 ctx
->padding
= padding
;
156 ctx
->hash_id
= hash_id
;
159 #if defined(POLARSSL_GENPRIME)
162 * Generate an RSA keypair
164 int rsa_gen_key( rsa_context
*ctx
,
165 int (*f_rng
)(void *),
167 int nbits
, int exponent
)
172 if( f_rng
== NULL
|| nbits
< 128 || exponent
< 3 )
173 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
175 mpi_init( &P1
, &Q1
, &H
, &G
, NULL
);
178 * find primes P and Q with Q < P so that:
179 * GCD( E, (P-1)*(Q-1) ) == 1
181 MPI_CHK( mpi_lset( &ctx
->E
, exponent
) );
185 MPI_CHK( mpi_gen_prime( &ctx
->P
, ( nbits
+ 1 ) >> 1, 0,
188 MPI_CHK( mpi_gen_prime( &ctx
->Q
, ( nbits
+ 1 ) >> 1, 0,
191 if( mpi_cmp_mpi( &ctx
->P
, &ctx
->Q
) < 0 )
192 mpi_swap( &ctx
->P
, &ctx
->Q
);
194 if( mpi_cmp_mpi( &ctx
->P
, &ctx
->Q
) == 0 )
197 MPI_CHK( mpi_mul_mpi( &ctx
->N
, &ctx
->P
, &ctx
->Q
) );
198 if( mpi_msb( &ctx
->N
) != nbits
)
201 MPI_CHK( mpi_sub_int( &P1
, &ctx
->P
, 1 ) );
202 MPI_CHK( mpi_sub_int( &Q1
, &ctx
->Q
, 1 ) );
203 MPI_CHK( mpi_mul_mpi( &H
, &P1
, &Q1
) );
204 MPI_CHK( mpi_gcd( &G
, &ctx
->E
, &H
) );
206 while( mpi_cmp_int( &G
, 1 ) != 0 );
209 * D = E^-1 mod ((P-1)*(Q-1))
214 MPI_CHK( mpi_inv_mod( &ctx
->D
, &ctx
->E
, &H
) );
215 MPI_CHK( mpi_mod_mpi( &ctx
->DP
, &ctx
->D
, &P1
) );
216 MPI_CHK( mpi_mod_mpi( &ctx
->DQ
, &ctx
->D
, &Q1
) );
217 MPI_CHK( mpi_inv_mod( &ctx
->QP
, &ctx
->Q
, &ctx
->P
) );
219 ctx
->len
= ( mpi_msb( &ctx
->N
) + 7 ) >> 3;
223 mpi_free( &G
, &H
, &Q1
, &P1
, NULL
);
228 return( POLARSSL_ERR_RSA_KEY_GEN_FAILED
| ret
);
237 * Check a public RSA key
239 int rsa_check_pubkey( const rsa_context
*ctx
)
241 if( !ctx
->N
.p
|| !ctx
->E
.p
)
242 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED
);
244 if( ( ctx
->N
.p
[0] & 1 ) == 0 ||
245 ( ctx
->E
.p
[0] & 1 ) == 0 )
246 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED
);
248 if( mpi_msb( &ctx
->N
) < 128 ||
249 mpi_msb( &ctx
->N
) > 4096 )
250 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED
);
252 if( mpi_msb( &ctx
->E
) < 2 ||
253 mpi_msb( &ctx
->E
) > 64 )
254 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED
);
260 * Check a private RSA key
262 int rsa_check_privkey( const rsa_context
*ctx
)
265 mpi PQ
, DE
, P1
, Q1
, H
, I
, G
, G2
, L1
, L2
;
267 if( ( ret
= rsa_check_pubkey( ctx
) ) != 0 )
270 if( !ctx
->P
.p
|| !ctx
->Q
.p
|| !ctx
->D
.p
)
271 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED
);
273 mpi_init( &PQ
, &DE
, &P1
, &Q1
, &H
, &I
, &G
, &G2
, &L1
, &L2
, NULL
);
275 MPI_CHK( mpi_mul_mpi( &PQ
, &ctx
->P
, &ctx
->Q
) );
276 MPI_CHK( mpi_mul_mpi( &DE
, &ctx
->D
, &ctx
->E
) );
277 MPI_CHK( mpi_sub_int( &P1
, &ctx
->P
, 1 ) );
278 MPI_CHK( mpi_sub_int( &Q1
, &ctx
->Q
, 1 ) );
279 MPI_CHK( mpi_mul_mpi( &H
, &P1
, &Q1
) );
280 MPI_CHK( mpi_gcd( &G
, &ctx
->E
, &H
) );
282 MPI_CHK( mpi_gcd( &G2
, &P1
, &Q1
) );
283 MPI_CHK( mpi_div_mpi( &L1
, &L2
, &H
, &G2
) );
284 MPI_CHK( mpi_mod_mpi( &I
, &DE
, &L1
) );
287 * Check for a valid PKCS1v2 private key
289 if( mpi_cmp_mpi( &PQ
, &ctx
->N
) == 0 &&
290 mpi_cmp_int( &L2
, 0 ) == 0 &&
291 mpi_cmp_int( &I
, 1 ) == 0 &&
292 mpi_cmp_int( &G
, 1 ) == 0 )
294 mpi_free( &G
, &I
, &H
, &Q1
, &P1
, &DE
, &PQ
, &G2
, &L1
, &L2
, NULL
);
301 mpi_free( &G
, &I
, &H
, &Q1
, &P1
, &DE
, &PQ
, &G2
, &L1
, &L2
, NULL
);
302 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED
| ret
);
306 * Do an RSA public key operation
308 int rsa_public( rsa_context
*ctx
,
309 const unsigned char *input
,
310 unsigned char *output
)
315 mpi_init( &T
, NULL
);
317 MPI_CHK( mpi_read_binary( &T
, input
, ctx
->len
) );
319 if( mpi_cmp_mpi( &T
, &ctx
->N
) >= 0 )
321 mpi_free( &T
, NULL
);
322 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
326 MPI_CHK( mpi_exp_mod( &T
, &T
, &ctx
->E
, &ctx
->N
, &ctx
->RN
) );
327 MPI_CHK( mpi_write_binary( &T
, output
, olen
) );
331 mpi_free( &T
, NULL
);
334 return( POLARSSL_ERR_RSA_PUBLIC_FAILED
| ret
);
340 * Do an RSA private key operation
342 int rsa_private( rsa_context
*ctx
,
343 const unsigned char *input
,
344 unsigned char *output
)
349 mpi_init( &T
, &T1
, &T2
, NULL
);
351 MPI_CHK( mpi_read_binary( &T
, input
, ctx
->len
) );
353 if( mpi_cmp_mpi( &T
, &ctx
->N
) >= 0 )
355 mpi_free( &T
, NULL
);
356 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
360 MPI_CHK( mpi_exp_mod( &T
, &T
, &ctx
->D
, &ctx
->N
, &ctx
->RN
) );
363 * faster decryption using the CRT
365 * T1 = input ^ dP mod P
366 * T2 = input ^ dQ mod Q
368 MPI_CHK( mpi_exp_mod( &T1
, &T
, &ctx
->DP
, &ctx
->P
, &ctx
->RP
) );
369 MPI_CHK( mpi_exp_mod( &T2
, &T
, &ctx
->DQ
, &ctx
->Q
, &ctx
->RQ
) );
372 * T = (T1 - T2) * (Q^-1 mod P) mod P
374 MPI_CHK( mpi_sub_mpi( &T
, &T1
, &T2
) );
375 MPI_CHK( mpi_mul_mpi( &T1
, &T
, &ctx
->QP
) );
376 MPI_CHK( mpi_mod_mpi( &T
, &T1
, &ctx
->P
) );
379 * output = T2 + T * Q
381 MPI_CHK( mpi_mul_mpi( &T1
, &T
, &ctx
->Q
) );
382 MPI_CHK( mpi_add_mpi( &T
, &T2
, &T1
) );
386 MPI_CHK( mpi_write_binary( &T
, output
, olen
) );
390 mpi_free( &T
, &T1
, &T2
, NULL
);
393 return( POLARSSL_ERR_RSA_PRIVATE_FAILED
| ret
);
399 * Add the message padding, then do an RSA operation
401 int rsa_pkcs1_encrypt( rsa_context
*ctx
,
402 int (*f_rng
)(void *),
405 const unsigned char *input
,
406 unsigned char *output
)
409 unsigned char *p
= output
;
413 switch( ctx
->padding
)
417 if( ilen
< 0 || olen
< ilen
+ 11 || f_rng
== NULL
)
418 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
420 nb_pad
= olen
- 3 - ilen
;
425 while( nb_pad
-- > 0 )
430 *p
= (unsigned char) f_rng( p_rng
);
431 } while( *p
== 0 && --rng_dl
);
433 // Check if RNG failed to generate data
436 return POLARSSL_ERR_RSA_RNG_FAILED
;
441 memcpy( p
, input
, ilen
);
446 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
449 return( ( mode
== RSA_PUBLIC
)
450 ? rsa_public( ctx
, output
, output
)
451 : rsa_private( ctx
, output
, output
) );
455 * Do an RSA operation, then remove the message padding
457 int rsa_pkcs1_decrypt( rsa_context
*ctx
,
459 const unsigned char *input
,
460 unsigned char *output
,
465 unsigned char buf
[1024];
469 if( ilen
< 16 || ilen
> (int) sizeof( buf
) )
470 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
472 ret
= ( mode
== RSA_PUBLIC
)
473 ? rsa_public( ctx
, input
, buf
)
474 : rsa_private( ctx
, input
, buf
);
481 switch( ctx
->padding
)
485 if( *p
++ != 0 || *p
++ != RSA_CRYPT
)
486 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
490 if( p
>= buf
+ ilen
- 1 )
491 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
499 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
502 if (ilen
- (int)(p
- buf
) > output_max_len
)
503 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE
);
505 *olen
= ilen
- (int)(p
- buf
);
506 memcpy( output
, p
, *olen
);
512 * Do an RSA operation to sign the message digest
514 int rsa_pkcs1_sign( rsa_context
*ctx
,
518 const unsigned char *hash
,
522 unsigned char *p
= sig
;
526 switch( ctx
->padding
)
533 nb_pad
= olen
- 3 - hashlen
;
539 nb_pad
= olen
- 3 - 34;
543 nb_pad
= olen
- 3 - 35;
547 nb_pad
= olen
- 3 - 47;
551 nb_pad
= olen
- 3 - 51;
555 nb_pad
= olen
- 3 - 67;
559 nb_pad
= olen
- 3 - 83;
564 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
568 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
572 memset( p
, 0xFF, nb_pad
);
579 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
585 memcpy( p
, hash
, hashlen
);
589 memcpy( p
, ASN1_HASH_MDX
, 18 );
590 memcpy( p
+ 18, hash
, 16 );
594 memcpy( p
, ASN1_HASH_MDX
, 18 );
595 memcpy( p
+ 18, hash
, 16 );
599 memcpy( p
, ASN1_HASH_MDX
, 18 );
600 memcpy( p
+ 18, hash
, 16 );
604 memcpy( p
, ASN1_HASH_SHA1
, 15 );
605 memcpy( p
+ 15, hash
, 20 );
609 memcpy( p
, ASN1_HASH_SHA2X
, 19 );
610 memcpy( p
+ 19, hash
, 28 );
611 p
[1] += 28; p
[14] = 4; p
[18] += 28; break;
614 memcpy( p
, ASN1_HASH_SHA2X
, 19 );
615 memcpy( p
+ 19, hash
, 32 );
616 p
[1] += 32; p
[14] = 1; p
[18] += 32; break;
619 memcpy( p
, ASN1_HASH_SHA2X
, 19 );
620 memcpy( p
+ 19, hash
, 48 );
621 p
[1] += 48; p
[14] = 2; p
[18] += 48; break;
624 memcpy( p
, ASN1_HASH_SHA2X
, 19 );
625 memcpy( p
+ 19, hash
, 64 );
626 p
[1] += 64; p
[14] = 3; p
[18] += 64; break;
629 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
632 return( ( mode
== RSA_PUBLIC
)
633 ? rsa_public( ctx
, sig
, sig
)
634 : rsa_private( ctx
, sig
, sig
) );
638 * Do an RSA operation and check the message digest
640 int rsa_pkcs1_verify( rsa_context
*ctx
,
644 const unsigned char *hash
,
647 int ret
, len
, siglen
;
649 unsigned char buf
[1024];
653 if( siglen
< 16 || siglen
> (int) sizeof( buf
) )
654 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
656 ret
= ( mode
== RSA_PUBLIC
)
657 ? rsa_public( ctx
, sig
, buf
)
658 : rsa_private( ctx
, sig
, buf
);
665 switch( ctx
->padding
)
669 if( *p
++ != 0 || *p
++ != RSA_SIGN
)
670 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
674 if( p
>= buf
+ siglen
- 1 || *p
!= 0xFF )
675 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
683 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
686 len
= siglen
- (int)( p
- buf
);
693 if( memcmp( p
, ASN1_HASH_MDX
, 18 ) != 0 )
694 return( POLARSSL_ERR_RSA_VERIFY_FAILED
);
696 if( ( c
== 2 && hash_id
== SIG_RSA_MD2
) ||
697 ( c
== 4 && hash_id
== SIG_RSA_MD4
) ||
698 ( c
== 5 && hash_id
== SIG_RSA_MD5
) )
700 if( memcmp( p
+ 18, hash
, 16 ) == 0 )
703 return( POLARSSL_ERR_RSA_VERIFY_FAILED
);
707 if( len
== 35 && hash_id
== SIG_RSA_SHA1
)
709 if( memcmp( p
, ASN1_HASH_SHA1
, 15 ) == 0 &&
710 memcmp( p
+ 15, hash
, 20 ) == 0 )
713 return( POLARSSL_ERR_RSA_VERIFY_FAILED
);
715 if( ( len
== 19 + 28 && p
[14] == 4 && hash_id
== SIG_RSA_SHA224
) ||
716 ( len
== 19 + 32 && p
[14] == 1 && hash_id
== SIG_RSA_SHA256
) ||
717 ( len
== 19 + 48 && p
[14] == 2 && hash_id
== SIG_RSA_SHA384
) ||
718 ( len
== 19 + 64 && p
[14] == 3 && hash_id
== SIG_RSA_SHA512
) )
725 memcmp( p
, ASN1_HASH_SHA2X
, 18 ) == 0 &&
726 memcmp( p
+ 19, hash
, c
) == 0 )
729 return( POLARSSL_ERR_RSA_VERIFY_FAILED
);
732 if( len
== hashlen
&& hash_id
== SIG_RSA_RAW
)
734 if( memcmp( p
, hash
, hashlen
) == 0 )
737 return( POLARSSL_ERR_RSA_VERIFY_FAILED
);
740 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
744 * Free the components of an RSA key
746 void rsa_free( rsa_context
*ctx
)
748 mpi_free( &ctx
->RQ
, &ctx
->RP
, &ctx
->RN
,
749 &ctx
->QP
, &ctx
->DQ
, &ctx
->DP
,
750 &ctx
->Q
, &ctx
->P
, &ctx
->D
,
751 &ctx
->E
, &ctx
->N
, NULL
);
755 /* PDKIM code (not copied from polarssl) */
757 * Parse a public RSA key
759 OpenSSL RSA public key ASN1 container
760 0:d=0 hl=3 l= 159 cons: SEQUENCE
761 3:d=1 hl=2 l= 13 cons: SEQUENCE
762 5:d=2 hl=2 l= 9 prim: OBJECT:rsaEncryption
763 16:d=2 hl=2 l= 0 prim: NULL
764 18:d=1 hl=3 l= 141 prim: BIT STRING:RSAPublicKey (below)
766 RSAPublicKey ASN1 container
767 0:d=0 hl=3 l= 137 cons: SEQUENCE
768 3:d=1 hl=3 l= 129 prim: INTEGER:Public modulus
769 135:d=1 hl=2 l= 3 prim: INTEGER:Public exponent
772 int rsa_parse_public_key( rsa_context
*rsa
, unsigned char *buf
, int buflen
)
774 unsigned char *p
, *end
;
780 if( ( ret
= asn1_get_tag( &p
, end
, &len
,
781 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) ) != 0 ) {
782 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT
| ret
);
785 if( ( ret
= asn1_get_tag( &p
, end
, &len
,
786 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) ) == 0 ) {
787 /* Skip over embedded rsaEncryption Object */
790 /* The RSAPublicKey ASN1 container is wrapped in a BIT STRING */
791 if( ( ret
= asn1_get_tag( &p
, end
, &len
,
792 ASN1_BIT_STRING
) ) != 0 ) {
793 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT
| ret
);
796 /* Limit range to that BIT STRING */
800 if( ( ret
= asn1_get_tag( &p
, end
, &len
,
801 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) ) != 0 ) {
802 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT
| ret
);
806 if ( ( ( ret
= asn1_get_mpi( &p
, end
, &(rsa
->N
) ) ) == 0 ) &&
807 ( ( ret
= asn1_get_mpi( &p
, end
, &(rsa
->E
) ) ) == 0 ) ) {
808 rsa
->len
= mpi_size( &rsa
->N
);
812 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT
| ret
);
816 * Parse a private RSA key
818 int rsa_parse_key( rsa_context
*rsa
, unsigned char *buf
, int buflen
,
819 unsigned char *pwd
, int pwdlen
)
822 unsigned char *s1
, *s2
;
823 unsigned char *p
, *end
;
825 s1
= (unsigned char *) strstr( (char *) buf
,
826 "-----BEGIN RSA PRIVATE KEY-----" );
830 s2
= (unsigned char *) strstr( (char *) buf
,
831 "-----END RSA PRIVATE KEY-----" );
833 if( s2
== NULL
|| s2
<= s1
)
834 return( POLARSSL_ERR_X509_KEY_INVALID_PEM
);
837 if( *s1
== '\r' ) s1
++;
838 if( *s1
== '\n' ) s1
++;
839 else return( POLARSSL_ERR_X509_KEY_INVALID_PEM
);
843 if( memcmp( s1
, "Proc-Type: 4,ENCRYPTED", 22 ) == 0 )
845 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE
);
849 ret
= base64_decode( NULL
, &len
, s1
, s2
- s1
);
851 if( ret
== POLARSSL_ERR_BASE64_INVALID_CHARACTER
)
852 return( ret
| POLARSSL_ERR_X509_KEY_INVALID_PEM
);
854 if( ( buf
= (unsigned char *) malloc( len
) ) == NULL
)
857 if( ( ret
= base64_decode( buf
, &len
, s1
, s2
- s1
) ) != 0 )
860 return( ret
| POLARSSL_ERR_X509_KEY_INVALID_PEM
);
867 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE
);
871 memset( rsa
, 0, sizeof( rsa_context
) );
877 * RSAPrivateKey ::= SEQUENCE {
879 * modulus INTEGER, -- n
880 * publicExponent INTEGER, -- e
881 * privateExponent INTEGER, -- d
882 * prime1 INTEGER, -- p
883 * prime2 INTEGER, -- q
884 * exponent1 INTEGER, -- d mod (p-1)
885 * exponent2 INTEGER, -- d mod (q-1)
886 * coefficient INTEGER, -- (inverse of q) mod p
887 * otherPrimeInfos OtherPrimeInfos OPTIONAL
890 if( ( ret
= asn1_get_tag( &p
, end
, &len
,
891 ASN1_CONSTRUCTED
| ASN1_SEQUENCE
) ) != 0 )
897 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT
| ret
);
902 if( ( ret
= asn1_get_int( &p
, end
, &rsa
->ver
) ) != 0 )
908 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT
| ret
);
917 return( ret
| POLARSSL_ERR_X509_KEY_INVALID_VERSION
);
920 if( ( ret
= asn1_get_mpi( &p
, end
, &rsa
->N
) ) != 0 ||
921 ( ret
= asn1_get_mpi( &p
, end
, &rsa
->E
) ) != 0 ||
922 ( ret
= asn1_get_mpi( &p
, end
, &rsa
->D
) ) != 0 ||
923 ( ret
= asn1_get_mpi( &p
, end
, &rsa
->P
) ) != 0 ||
924 ( ret
= asn1_get_mpi( &p
, end
, &rsa
->Q
) ) != 0 ||
925 ( ret
= asn1_get_mpi( &p
, end
, &rsa
->DP
) ) != 0 ||
926 ( ret
= asn1_get_mpi( &p
, end
, &rsa
->DQ
) ) != 0 ||
927 ( ret
= asn1_get_mpi( &p
, end
, &rsa
->QP
) ) != 0 )
933 return( ret
| POLARSSL_ERR_X509_KEY_INVALID_FORMAT
);
936 rsa
->len
= mpi_size( &rsa
->N
);
944 return( POLARSSL_ERR_X509_KEY_INVALID_FORMAT
|
945 POLARSSL_ERR_ASN1_LENGTH_MISMATCH
);
948 if( ( ret
= rsa_check_privkey( rsa
) ) != 0 )