2 * The RSA public-key cryptosystem
4 * Copyright (C) 2006-2011, 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 #include "polarssl_config.h"
34 #if defined(POLARSSL_RSA_C)
38 #if defined(POLARSSL_PKCS1_V21)
46 * Initialize an RSA context
48 void rsa_init( rsa_context
*ctx
,
52 memset( ctx
, 0, sizeof( rsa_context
) );
54 ctx
->padding
= padding
;
55 ctx
->hash_id
= hash_id
;
58 #if defined(POLARSSL_GENPRIME)
61 * Generate an RSA keypair
63 int rsa_gen_key( rsa_context
*ctx
,
64 int (*f_rng
)(void *, unsigned char *, size_t),
66 unsigned int nbits
, int exponent
)
71 if( f_rng
== NULL
|| nbits
< 128 || exponent
< 3 )
72 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
74 mpi_init( &P1
); mpi_init( &Q1
); mpi_init( &H
); mpi_init( &G
);
77 * find primes P and Q with Q < P so that:
78 * GCD( E, (P-1)*(Q-1) ) == 1
80 MPI_CHK( mpi_lset( &ctx
->E
, exponent
) );
84 MPI_CHK( mpi_gen_prime( &ctx
->P
, ( nbits
+ 1 ) >> 1, 0,
87 MPI_CHK( mpi_gen_prime( &ctx
->Q
, ( nbits
+ 1 ) >> 1, 0,
90 if( mpi_cmp_mpi( &ctx
->P
, &ctx
->Q
) < 0 )
91 mpi_swap( &ctx
->P
, &ctx
->Q
);
93 if( mpi_cmp_mpi( &ctx
->P
, &ctx
->Q
) == 0 )
96 MPI_CHK( mpi_mul_mpi( &ctx
->N
, &ctx
->P
, &ctx
->Q
) );
97 if( mpi_msb( &ctx
->N
) != nbits
)
100 MPI_CHK( mpi_sub_int( &P1
, &ctx
->P
, 1 ) );
101 MPI_CHK( mpi_sub_int( &Q1
, &ctx
->Q
, 1 ) );
102 MPI_CHK( mpi_mul_mpi( &H
, &P1
, &Q1
) );
103 MPI_CHK( mpi_gcd( &G
, &ctx
->E
, &H
) );
105 while( mpi_cmp_int( &G
, 1 ) != 0 );
108 * D = E^-1 mod ((P-1)*(Q-1))
113 MPI_CHK( mpi_inv_mod( &ctx
->D
, &ctx
->E
, &H
) );
114 MPI_CHK( mpi_mod_mpi( &ctx
->DP
, &ctx
->D
, &P1
) );
115 MPI_CHK( mpi_mod_mpi( &ctx
->DQ
, &ctx
->D
, &Q1
) );
116 MPI_CHK( mpi_inv_mod( &ctx
->QP
, &ctx
->Q
, &ctx
->P
) );
118 ctx
->len
= ( mpi_msb( &ctx
->N
) + 7 ) >> 3;
122 mpi_free( &P1
); mpi_free( &Q1
); mpi_free( &H
); mpi_free( &G
);
127 return( POLARSSL_ERR_RSA_KEY_GEN_FAILED
+ ret
);
136 * Check a public RSA key
138 int rsa_check_pubkey( const rsa_context
*ctx
)
140 if( !ctx
->N
.p
|| !ctx
->E
.p
)
141 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED
);
143 if( ( ctx
->N
.p
[0] & 1 ) == 0 ||
144 ( ctx
->E
.p
[0] & 1 ) == 0 )
145 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED
);
147 if( mpi_msb( &ctx
->N
) < 128 ||
148 mpi_msb( &ctx
->N
) > POLARSSL_MPI_MAX_BITS
)
149 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED
);
151 if( mpi_msb( &ctx
->E
) < 2 ||
152 mpi_msb( &ctx
->E
) > 64 )
153 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED
);
159 * Check a private RSA key
161 int rsa_check_privkey( const rsa_context
*ctx
)
164 mpi PQ
, DE
, P1
, Q1
, H
, I
, G
, G2
, L1
, L2
, DP
, DQ
, QP
;
166 if( ( ret
= rsa_check_pubkey( ctx
) ) != 0 )
169 if( !ctx
->P
.p
|| !ctx
->Q
.p
|| !ctx
->D
.p
)
170 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED
);
172 mpi_init( &PQ
); mpi_init( &DE
); mpi_init( &P1
); mpi_init( &Q1
);
173 mpi_init( &H
); mpi_init( &I
); mpi_init( &G
); mpi_init( &G2
);
174 mpi_init( &L1
); mpi_init( &L2
); mpi_init( &DP
); mpi_init( &DQ
);
177 MPI_CHK( mpi_mul_mpi( &PQ
, &ctx
->P
, &ctx
->Q
) );
178 MPI_CHK( mpi_mul_mpi( &DE
, &ctx
->D
, &ctx
->E
) );
179 MPI_CHK( mpi_sub_int( &P1
, &ctx
->P
, 1 ) );
180 MPI_CHK( mpi_sub_int( &Q1
, &ctx
->Q
, 1 ) );
181 MPI_CHK( mpi_mul_mpi( &H
, &P1
, &Q1
) );
182 MPI_CHK( mpi_gcd( &G
, &ctx
->E
, &H
) );
184 MPI_CHK( mpi_gcd( &G2
, &P1
, &Q1
) );
185 MPI_CHK( mpi_div_mpi( &L1
, &L2
, &H
, &G2
) );
186 MPI_CHK( mpi_mod_mpi( &I
, &DE
, &L1
) );
188 MPI_CHK( mpi_mod_mpi( &DP
, &ctx
->D
, &P1
) );
189 MPI_CHK( mpi_mod_mpi( &DQ
, &ctx
->D
, &Q1
) );
190 MPI_CHK( mpi_inv_mod( &QP
, &ctx
->Q
, &ctx
->P
) );
192 * Check for a valid PKCS1v2 private key
194 if( mpi_cmp_mpi( &PQ
, &ctx
->N
) != 0 ||
195 mpi_cmp_mpi( &DP
, &ctx
->DP
) != 0 ||
196 mpi_cmp_mpi( &DQ
, &ctx
->DQ
) != 0 ||
197 mpi_cmp_mpi( &QP
, &ctx
->QP
) != 0 ||
198 mpi_cmp_int( &L2
, 0 ) != 0 ||
199 mpi_cmp_int( &I
, 1 ) != 0 ||
200 mpi_cmp_int( &G
, 1 ) != 0 )
202 ret
= POLARSSL_ERR_RSA_KEY_CHECK_FAILED
;
206 mpi_free( &PQ
); mpi_free( &DE
); mpi_free( &P1
); mpi_free( &Q1
);
207 mpi_free( &H
); mpi_free( &I
); mpi_free( &G
); mpi_free( &G2
);
208 mpi_free( &L1
); mpi_free( &L2
); mpi_free( &DP
); mpi_free( &DQ
);
211 if( ret
== POLARSSL_ERR_RSA_KEY_CHECK_FAILED
)
215 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED
+ ret
);
221 * Do an RSA public key operation
223 int rsa_public( rsa_context
*ctx
,
224 const unsigned char *input
,
225 unsigned char *output
)
233 MPI_CHK( mpi_read_binary( &T
, input
, ctx
->len
) );
235 if( mpi_cmp_mpi( &T
, &ctx
->N
) >= 0 )
238 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
242 MPI_CHK( mpi_exp_mod( &T
, &T
, &ctx
->E
, &ctx
->N
, &ctx
->RN
) );
243 MPI_CHK( mpi_write_binary( &T
, output
, olen
) );
250 return( POLARSSL_ERR_RSA_PUBLIC_FAILED
+ ret
);
256 * Do an RSA private key operation
258 int rsa_private( rsa_context
*ctx
,
259 const unsigned char *input
,
260 unsigned char *output
)
266 mpi_init( &T
); mpi_init( &T1
); mpi_init( &T2
);
268 MPI_CHK( mpi_read_binary( &T
, input
, ctx
->len
) );
270 if( mpi_cmp_mpi( &T
, &ctx
->N
) >= 0 )
273 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
276 #if defined(POLARSSL_RSA_NO_CRT)
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
); mpi_free( &T1
); mpi_free( &T2
);
310 return( POLARSSL_ERR_RSA_PRIVATE_FAILED
+ ret
);
315 #if defined(POLARSSL_PKCS1_V21)
317 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
319 * \param dst buffer to mask
320 * \param dlen length of destination buffer
321 * \param src source of the mask generation
322 * \param slen length of the source buffer
323 * \param md_ctx message digest context to use
325 static void mgf_mask( unsigned char *dst
, size_t dlen
, unsigned char *src
, size_t slen
,
326 md_context_t
*md_ctx
)
328 unsigned char mask
[POLARSSL_MD_MAX_SIZE
];
329 unsigned char counter
[4];
334 memset( mask
, 0, POLARSSL_MD_MAX_SIZE
);
335 memset( counter
, 0, 4 );
337 hlen
= md_ctx
->md_info
->size
;
339 // Generate and apply dbMask
350 md_update( md_ctx
, src
, slen
);
351 md_update( md_ctx
, counter
, 4 );
352 md_finish( md_ctx
, mask
);
354 for( i
= 0; i
< use_len
; ++i
)
364 #if defined(POLARSSL_PKCS1_V21)
366 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
368 int rsa_rsaes_oaep_encrypt( rsa_context
*ctx
,
369 int (*f_rng
)(void *, unsigned char *, size_t),
372 const unsigned char *label
, size_t label_len
,
374 const unsigned char *input
,
375 unsigned char *output
)
379 unsigned char *p
= output
;
381 const md_info_t
*md_info
;
384 if( ctx
->padding
!= RSA_PKCS_V21
|| f_rng
== NULL
)
385 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
387 md_info
= md_info_from_type( ctx
->hash_id
);
389 if( md_info
== NULL
)
390 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
393 hlen
= md_get_size( md_info
);
395 if( olen
< ilen
+ 2 * hlen
+ 2 || f_rng
== NULL
)
396 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
398 memset( output
, 0, olen
);
402 // Generate a random octet string seed
404 if( ( ret
= f_rng( p_rng
, p
, hlen
) ) != 0 )
405 return( POLARSSL_ERR_RSA_RNG_FAILED
+ ret
);
411 md( md_info
, label
, label_len
, p
);
413 p
+= olen
- 2 * hlen
- 2 - ilen
;
415 memcpy( p
, input
, ilen
);
417 md_init_ctx( &md_ctx
, md_info
);
419 // maskedDB: Apply dbMask to DB
421 mgf_mask( output
+ hlen
+ 1, olen
- hlen
- 1, output
+ 1, hlen
,
424 // maskedSeed: Apply seedMask to seed
426 mgf_mask( output
+ 1, hlen
, output
+ hlen
+ 1, olen
- hlen
- 1,
429 md_free_ctx( &md_ctx
);
431 return( ( mode
== RSA_PUBLIC
)
432 ? rsa_public( ctx
, output
, output
)
433 : rsa_private( ctx
, output
, output
) );
435 #endif /* POLARSSL_PKCS1_V21 */
438 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
440 int rsa_rsaes_pkcs1_v15_encrypt( rsa_context
*ctx
,
441 int (*f_rng
)(void *, unsigned char *, size_t),
443 int mode
, size_t ilen
,
444 const unsigned char *input
,
445 unsigned char *output
)
449 unsigned char *p
= output
;
451 if( ctx
->padding
!= RSA_PKCS_V15
|| f_rng
== NULL
)
452 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
456 if( olen
< ilen
+ 11 )
457 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
459 nb_pad
= olen
- 3 - ilen
;
462 if( mode
== RSA_PUBLIC
)
466 while( nb_pad
-- > 0 )
471 ret
= f_rng( p_rng
, p
, 1 );
472 } while( *p
== 0 && --rng_dl
&& ret
== 0 );
474 // Check if RNG failed to generate data
476 if( rng_dl
== 0 || ret
!= 0)
477 return POLARSSL_ERR_RSA_RNG_FAILED
+ ret
;
486 while( nb_pad
-- > 0 )
491 memcpy( p
, input
, ilen
);
493 return( ( mode
== RSA_PUBLIC
)
494 ? rsa_public( ctx
, output
, output
)
495 : rsa_private( ctx
, output
, output
) );
499 * Add the message padding, then do an RSA operation
501 int rsa_pkcs1_encrypt( rsa_context
*ctx
,
502 int (*f_rng
)(void *, unsigned char *, size_t),
504 int mode
, size_t ilen
,
505 const unsigned char *input
,
506 unsigned char *output
)
508 switch( ctx
->padding
)
511 return rsa_rsaes_pkcs1_v15_encrypt( ctx
, f_rng
, p_rng
, mode
, ilen
,
514 #if defined(POLARSSL_PKCS1_V21)
516 return rsa_rsaes_oaep_encrypt( ctx
, f_rng
, p_rng
, mode
, NULL
, 0,
517 ilen
, input
, output
);
521 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
525 #if defined(POLARSSL_PKCS1_V21)
527 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
529 int rsa_rsaes_oaep_decrypt( rsa_context
*ctx
,
531 const unsigned char *label
, size_t label_len
,
533 const unsigned char *input
,
534 unsigned char *output
,
535 size_t output_max_len
)
540 unsigned char buf
[POLARSSL_MPI_MAX_SIZE
];
541 unsigned char lhash
[POLARSSL_MD_MAX_SIZE
];
543 const md_info_t
*md_info
;
546 if( ctx
->padding
!= RSA_PKCS_V21
)
547 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
551 if( ilen
< 16 || ilen
> sizeof( buf
) )
552 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
554 ret
= ( mode
== RSA_PUBLIC
)
555 ? rsa_public( ctx
, input
, buf
)
556 : rsa_private( ctx
, input
, buf
);
564 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
566 md_info
= md_info_from_type( ctx
->hash_id
);
567 if( md_info
== NULL
)
568 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
570 hlen
= md_get_size( md_info
);
572 md_init_ctx( &md_ctx
, md_info
);
576 md( md_info
, label
, label_len
, lhash
);
578 // seed: Apply seedMask to maskedSeed
580 mgf_mask( buf
+ 1, hlen
, buf
+ hlen
+ 1, ilen
- hlen
- 1,
583 // DB: Apply dbMask to maskedDB
585 mgf_mask( buf
+ hlen
+ 1, ilen
- hlen
- 1, buf
+ 1, hlen
,
589 md_free_ctx( &md_ctx
);
593 if( memcmp( lhash
, p
, hlen
) != 0 )
594 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
598 while( *p
== 0 && p
< buf
+ ilen
)
601 if( p
== buf
+ ilen
)
602 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
605 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
607 if (ilen
- (p
- buf
) > output_max_len
)
608 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE
);
610 *olen
= ilen
- (p
- buf
);
611 memcpy( output
, p
, *olen
);
615 #endif /* POLARSSL_PKCS1_V21 */
618 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
620 int rsa_rsaes_pkcs1_v15_decrypt( rsa_context
*ctx
,
621 int mode
, size_t *olen
,
622 const unsigned char *input
,
623 unsigned char *output
,
624 size_t output_max_len
)
626 int ret
, correct
= 1;
627 size_t ilen
, pad_count
= 0;
628 unsigned char *p
, *q
;
630 unsigned char buf
[POLARSSL_MPI_MAX_SIZE
];
632 if( ctx
->padding
!= RSA_PKCS_V15
)
633 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
637 if( ilen
< 16 || ilen
> sizeof( buf
) )
638 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
640 ret
= ( mode
== RSA_PUBLIC
)
641 ? rsa_public( ctx
, input
, buf
)
642 : rsa_private( ctx
, input
, buf
);
653 if( ( bt
!= RSA_CRYPT
&& mode
== RSA_PRIVATE
) ||
654 ( bt
!= RSA_SIGN
&& mode
== RSA_PUBLIC
) )
659 if( bt
== RSA_CRYPT
)
661 while( *p
!= 0 && p
< buf
+ ilen
- 1 )
662 pad_count
+= ( *p
++ != 0 );
664 correct
&= ( *p
== 0 && p
< buf
+ ilen
- 1 );
668 // Also pass over all other bytes to reduce timing differences
670 while ( q
< buf
+ ilen
- 1 )
671 pad_count
+= ( *q
++ != 0 );
673 // Prevent compiler optimization of pad_count
675 correct
|= pad_count
& 0x100000; /* Always 0 unless 1M bit keys */
680 while( *p
== 0xFF && p
< buf
+ ilen
- 1 )
681 pad_count
+= ( *p
++ == 0xFF );
683 correct
&= ( *p
== 0 && p
< buf
+ ilen
- 1 );
687 // Also pass over all other bytes to reduce timing differences
689 while ( q
< buf
+ ilen
- 1 )
690 pad_count
+= ( *q
++ != 0 );
692 // Prevent compiler optimization of pad_count
694 correct
|= pad_count
& 0x100000; /* Always 0 unless 1M bit keys */
699 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
701 if (ilen
- (p
- buf
) > output_max_len
)
702 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE
);
704 *olen
= ilen
- (p
- buf
);
705 memcpy( output
, p
, *olen
);
711 * Do an RSA operation, then remove the message padding
713 int rsa_pkcs1_decrypt( rsa_context
*ctx
,
714 int mode
, size_t *olen
,
715 const unsigned char *input
,
716 unsigned char *output
,
717 size_t output_max_len
)
719 switch( ctx
->padding
)
722 return rsa_rsaes_pkcs1_v15_decrypt( ctx
, mode
, olen
, input
, output
,
725 #if defined(POLARSSL_PKCS1_V21)
727 return rsa_rsaes_oaep_decrypt( ctx
, mode
, NULL
, 0, olen
, input
,
728 output
, output_max_len
);
732 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
736 #if defined(POLARSSL_PKCS1_V21)
738 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
740 int rsa_rsassa_pss_sign( rsa_context
*ctx
,
741 int (*f_rng
)(void *, unsigned char *, size_t),
745 unsigned int hashlen
,
746 const unsigned char *hash
,
750 unsigned char *p
= sig
;
751 unsigned char salt
[POLARSSL_MD_MAX_SIZE
];
752 unsigned int slen
, hlen
, offset
= 0;
755 const md_info_t
*md_info
;
758 if( ctx
->padding
!= RSA_PKCS_V21
|| f_rng
== NULL
)
759 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
792 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
795 md_info
= md_info_from_type( ctx
->hash_id
);
796 if( md_info
== NULL
)
797 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
799 hlen
= md_get_size( md_info
);
802 if( olen
< hlen
+ slen
+ 2 )
803 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
805 memset( sig
, 0, olen
);
807 msb
= mpi_msb( &ctx
->N
) - 1;
809 // Generate salt of length slen
811 if( ( ret
= f_rng( p_rng
, salt
, slen
) ) != 0 )
812 return( POLARSSL_ERR_RSA_RNG_FAILED
+ ret
);
814 // Note: EMSA-PSS encoding is over the length of N - 1 bits
816 msb
= mpi_msb( &ctx
->N
) - 1;
817 p
+= olen
- hlen
* 2 - 2;
819 memcpy( p
, salt
, slen
);
822 md_init_ctx( &md_ctx
, md_info
);
824 // Generate H = Hash( M' )
826 md_starts( &md_ctx
);
827 md_update( &md_ctx
, p
, 8 );
828 md_update( &md_ctx
, hash
, hashlen
);
829 md_update( &md_ctx
, salt
, slen
);
830 md_finish( &md_ctx
, p
);
832 // Compensate for boundary condition when applying mask
837 // maskedDB: Apply dbMask to DB
839 mgf_mask( sig
+ offset
, olen
- hlen
- 1 - offset
, p
, hlen
, &md_ctx
);
841 md_free_ctx( &md_ctx
);
843 msb
= mpi_msb( &ctx
->N
) - 1;
844 sig
[0] &= 0xFF >> ( olen
* 8 - msb
);
849 return( ( mode
== RSA_PUBLIC
)
850 ? rsa_public( ctx
, sig
, sig
)
851 : rsa_private( ctx
, sig
, sig
) );
853 #endif /* POLARSSL_PKCS1_V21 */
856 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
859 * Do an RSA operation to sign the message digest
861 int rsa_rsassa_pkcs1_v15_sign( rsa_context
*ctx
,
864 unsigned int hashlen
,
865 const unsigned char *hash
,
869 unsigned char *p
= sig
;
871 if( ctx
->padding
!= RSA_PKCS_V15
)
872 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
879 nb_pad
= olen
- 3 - hashlen
;
885 nb_pad
= olen
- 3 - 34;
889 nb_pad
= olen
- 3 - 35;
893 nb_pad
= olen
- 3 - 47;
897 nb_pad
= olen
- 3 - 51;
901 nb_pad
= olen
- 3 - 67;
905 nb_pad
= olen
- 3 - 83;
910 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
913 if( ( nb_pad
< 8 ) || ( nb_pad
> olen
) )
914 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
918 memset( p
, 0xFF, nb_pad
);
925 memcpy( p
, hash
, hashlen
);
929 memcpy( p
, ASN1_HASH_MDX
, 18 );
930 memcpy( p
+ 18, hash
, 16 );
934 memcpy( p
, ASN1_HASH_MDX
, 18 );
935 memcpy( p
+ 18, hash
, 16 );
939 memcpy( p
, ASN1_HASH_MDX
, 18 );
940 memcpy( p
+ 18, hash
, 16 );
944 memcpy( p
, ASN1_HASH_SHA1
, 15 );
945 memcpy( p
+ 15, hash
, 20 );
949 memcpy( p
, ASN1_HASH_SHA2X
, 19 );
950 memcpy( p
+ 19, hash
, 28 );
951 p
[1] += 28; p
[14] = 4; p
[18] += 28; break;
954 memcpy( p
, ASN1_HASH_SHA2X
, 19 );
955 memcpy( p
+ 19, hash
, 32 );
956 p
[1] += 32; p
[14] = 1; p
[18] += 32; break;
959 memcpy( p
, ASN1_HASH_SHA2X
, 19 );
960 memcpy( p
+ 19, hash
, 48 );
961 p
[1] += 48; p
[14] = 2; p
[18] += 48; break;
964 memcpy( p
, ASN1_HASH_SHA2X
, 19 );
965 memcpy( p
+ 19, hash
, 64 );
966 p
[1] += 64; p
[14] = 3; p
[18] += 64; break;
969 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
972 return( ( mode
== RSA_PUBLIC
)
973 ? rsa_public( ctx
, sig
, sig
)
974 : rsa_private( ctx
, sig
, sig
) );
978 * Do an RSA operation to sign the message digest
980 int rsa_pkcs1_sign( rsa_context
*ctx
,
981 int (*f_rng
)(void *, unsigned char *, size_t),
985 unsigned int hashlen
,
986 const unsigned char *hash
,
989 switch( ctx
->padding
)
992 return rsa_rsassa_pkcs1_v15_sign( ctx
, mode
, hash_id
,
993 hashlen
, hash
, sig
);
995 #if defined(POLARSSL_PKCS1_V21)
997 return rsa_rsassa_pss_sign( ctx
, f_rng
, p_rng
, mode
, hash_id
,
998 hashlen
, hash
, sig
);
1002 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
1006 #if defined(POLARSSL_PKCS1_V21)
1008 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1010 int rsa_rsassa_pss_verify( rsa_context
*ctx
,
1013 unsigned int hashlen
,
1014 const unsigned char *hash
,
1015 unsigned char *sig
)
1020 unsigned char buf
[POLARSSL_MPI_MAX_SIZE
];
1021 unsigned char result
[POLARSSL_MD_MAX_SIZE
];
1022 unsigned char zeros
[8];
1025 const md_info_t
*md_info
;
1026 md_context_t md_ctx
;
1028 if( ctx
->padding
!= RSA_PKCS_V21
)
1029 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
1033 if( siglen
< 16 || siglen
> sizeof( buf
) )
1034 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
1036 ret
= ( mode
== RSA_PUBLIC
)
1037 ? rsa_public( ctx
, sig
, buf
)
1038 : rsa_private( ctx
, sig
, buf
);
1045 if( buf
[siglen
- 1] != 0xBC )
1046 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
1060 case SIG_RSA_SHA224
:
1064 case SIG_RSA_SHA256
:
1068 case SIG_RSA_SHA384
:
1072 case SIG_RSA_SHA512
:
1077 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
1080 md_info
= md_info_from_type( ctx
->hash_id
);
1081 if( md_info
== NULL
)
1082 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
1084 hlen
= md_get_size( md_info
);
1085 slen
= siglen
- hlen
- 1;
1087 memset( zeros
, 0, 8 );
1089 // Note: EMSA-PSS verification is over the length of N - 1 bits
1091 msb
= mpi_msb( &ctx
->N
) - 1;
1093 // Compensate for boundary condition when applying mask
1100 if( buf
[0] >> ( 8 - siglen
* 8 + msb
) )
1101 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
1103 md_init_ctx( &md_ctx
, md_info
);
1105 mgf_mask( p
, siglen
- hlen
- 1, p
+ siglen
- hlen
- 1, hlen
, &md_ctx
);
1107 buf
[0] &= 0xFF >> ( siglen
* 8 - msb
);
1109 while( *p
== 0 && p
< buf
+ siglen
)
1112 if( p
== buf
+ siglen
||
1115 md_free_ctx( &md_ctx
);
1116 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
1121 // Generate H = Hash( M' )
1123 md_starts( &md_ctx
);
1124 md_update( &md_ctx
, zeros
, 8 );
1125 md_update( &md_ctx
, hash
, hashlen
);
1126 md_update( &md_ctx
, p
, slen
);
1127 md_finish( &md_ctx
, result
);
1129 md_free_ctx( &md_ctx
);
1131 if( memcmp( p
+ slen
, result
, hlen
) == 0 )
1134 return( POLARSSL_ERR_RSA_VERIFY_FAILED
);
1136 #endif /* POLARSSL_PKCS1_V21 */
1139 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1141 int rsa_rsassa_pkcs1_v15_verify( rsa_context
*ctx
,
1144 unsigned int hashlen
,
1145 const unsigned char *hash
,
1146 unsigned char *sig
)
1150 unsigned char *p
, c
;
1151 unsigned char buf
[POLARSSL_MPI_MAX_SIZE
];
1153 if( ctx
->padding
!= RSA_PKCS_V15
)
1154 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
1158 if( siglen
< 16 || siglen
> sizeof( buf
) )
1159 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA
);
1161 ret
= ( mode
== RSA_PUBLIC
)
1162 ? rsa_public( ctx
, sig
, buf
)
1163 : rsa_private( ctx
, sig
, buf
);
1170 if( *p
++ != 0 || *p
++ != RSA_SIGN
)
1171 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
1175 if( p
>= buf
+ siglen
- 1 || *p
!= 0xFF )
1176 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
1181 len
= siglen
- ( p
- buf
);
1183 if( len
== 33 && hash_id
== SIG_RSA_SHA1
)
1185 if( memcmp( p
, ASN1_HASH_SHA1_ALT
, 13 ) == 0 &&
1186 memcmp( p
+ 13, hash
, 20 ) == 0 )
1189 return( POLARSSL_ERR_RSA_VERIFY_FAILED
);
1196 if( memcmp( p
, ASN1_HASH_MDX
, 18 ) != 0 )
1197 return( POLARSSL_ERR_RSA_VERIFY_FAILED
);
1199 if( ( c
== 2 && hash_id
== SIG_RSA_MD2
) ||
1200 ( c
== 4 && hash_id
== SIG_RSA_MD4
) ||
1201 ( c
== 5 && hash_id
== SIG_RSA_MD5
) )
1203 if( memcmp( p
+ 18, hash
, 16 ) == 0 )
1206 return( POLARSSL_ERR_RSA_VERIFY_FAILED
);
1210 if( len
== 35 && hash_id
== SIG_RSA_SHA1
)
1212 if( memcmp( p
, ASN1_HASH_SHA1
, 15 ) == 0 &&
1213 memcmp( p
+ 15, hash
, 20 ) == 0 )
1216 return( POLARSSL_ERR_RSA_VERIFY_FAILED
);
1218 if( ( len
== 19 + 28 && p
[14] == 4 && hash_id
== SIG_RSA_SHA224
) ||
1219 ( len
== 19 + 32 && p
[14] == 1 && hash_id
== SIG_RSA_SHA256
) ||
1220 ( len
== 19 + 48 && p
[14] == 2 && hash_id
== SIG_RSA_SHA384
) ||
1221 ( len
== 19 + 64 && p
[14] == 3 && hash_id
== SIG_RSA_SHA512
) )
1228 memcmp( p
, ASN1_HASH_SHA2X
, 18 ) == 0 &&
1229 memcmp( p
+ 19, hash
, c
) == 0 )
1232 return( POLARSSL_ERR_RSA_VERIFY_FAILED
);
1235 if( len
== hashlen
&& hash_id
== SIG_RSA_RAW
)
1237 if( memcmp( p
, hash
, hashlen
) == 0 )
1240 return( POLARSSL_ERR_RSA_VERIFY_FAILED
);
1243 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
1247 * Do an RSA operation and check the message digest
1249 int rsa_pkcs1_verify( rsa_context
*ctx
,
1252 unsigned int hashlen
,
1253 const unsigned char *hash
,
1254 unsigned char *sig
)
1256 switch( ctx
->padding
)
1259 return rsa_rsassa_pkcs1_v15_verify( ctx
, mode
, hash_id
,
1260 hashlen
, hash
, sig
);
1262 #if defined(POLARSSL_PKCS1_V21)
1264 return rsa_rsassa_pss_verify( ctx
, mode
, hash_id
,
1265 hashlen
, hash
, sig
);
1269 return( POLARSSL_ERR_RSA_INVALID_PADDING
);
1274 * Free the components of an RSA key
1276 void rsa_free( rsa_context
*ctx
)
1278 mpi_free( &ctx
->RQ
); mpi_free( &ctx
->RP
); mpi_free( &ctx
->RN
);
1279 mpi_free( &ctx
->QP
); mpi_free( &ctx
->DQ
); mpi_free( &ctx
->DP
);
1280 mpi_free( &ctx
->Q
); mpi_free( &ctx
->P
); mpi_free( &ctx
->D
);
1281 mpi_free( &ctx
->E
); mpi_free( &ctx
->N
);
1284 #if defined(POLARSSL_SELF_TEST)
1286 #include "polarssl/sha1.h"
1289 * Example RSA-1024 keypair, for test purposes
1293 #define RSA_N "9292758453063D803DD603D5E777D788" \
1294 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1295 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1296 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1297 "93A89813FBF3C4F8066D2D800F7C38A8" \
1298 "1AE31942917403FF4946B0A83D3D3E05" \
1299 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1300 "5E94BB77B07507233A0BC7BAC8F90F79"
1302 #define RSA_E "10001"
1304 #define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1305 "66CA472BC44D253102F8B4A9D3BFA750" \
1306 "91386C0077937FE33FA3252D28855837" \
1307 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1308 "DF79C5CE07EE72C7F123142198164234" \
1309 "CABB724CF78B8173B9F880FC86322407" \
1310 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1311 "071513A1E85B5DFA031F21ECAE91A34D"
1313 #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1314 "2C01CAD19EA484A87EA4377637E75500" \
1315 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1316 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1318 #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1319 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1320 "910E4168387E3C30AA1E00C339A79508" \
1321 "8452DD96A9A5EA5D9DCA68DA636032AF"
1323 #define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1324 "3C94D22288ACD763FD8E5600ED4A702D" \
1325 "F84198A5F06C2E72236AE490C93F07F8" \
1326 "3CC559CD27BC2D1CA488811730BB5725"
1328 #define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1329 "D8AAEA56749EA28623272E4F7D0592AF" \
1330 "7C1F1313CAC9471B5C523BFE592F517B" \
1331 "407A1BD76C164B93DA2D32A383E58357"
1333 #define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1334 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1335 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1336 "A74206CEC169D74BF5A8C50D6F48EA08"
1339 #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1340 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1342 static int myrand( void *rng_state
, unsigned char *output
, size_t len
)
1346 if( rng_state
!= NULL
)
1349 for( i
= 0; i
< len
; ++i
)
1358 int rsa_self_test( int verbose
)
1362 unsigned char rsa_plaintext
[PT_LEN
];
1363 unsigned char rsa_decrypted
[PT_LEN
];
1364 unsigned char rsa_ciphertext
[KEY_LEN
];
1365 #if defined(POLARSSL_SHA1_C)
1366 unsigned char sha1sum
[20];
1369 rsa_init( &rsa
, RSA_PKCS_V15
, 0 );
1372 mpi_read_string( &rsa
.N
, 16, RSA_N
);
1373 mpi_read_string( &rsa
.E
, 16, RSA_E
);
1374 mpi_read_string( &rsa
.D
, 16, RSA_D
);
1375 mpi_read_string( &rsa
.P
, 16, RSA_P
);
1376 mpi_read_string( &rsa
.Q
, 16, RSA_Q
);
1377 mpi_read_string( &rsa
.DP
, 16, RSA_DP
);
1378 mpi_read_string( &rsa
.DQ
, 16, RSA_DQ
);
1379 mpi_read_string( &rsa
.QP
, 16, RSA_QP
);
1382 printf( " RSA key validation: " );
1384 if( rsa_check_pubkey( &rsa
) != 0 ||
1385 rsa_check_privkey( &rsa
) != 0 )
1388 printf( "failed\n" );
1394 printf( "passed\n PKCS#1 encryption : " );
1396 memcpy( rsa_plaintext
, RSA_PT
, PT_LEN
);
1398 if( rsa_pkcs1_encrypt( &rsa
, &myrand
, NULL
, RSA_PUBLIC
, PT_LEN
,
1399 rsa_plaintext
, rsa_ciphertext
) != 0 )
1402 printf( "failed\n" );
1408 printf( "passed\n PKCS#1 decryption : " );
1410 if( rsa_pkcs1_decrypt( &rsa
, RSA_PRIVATE
, &len
,
1411 rsa_ciphertext
, rsa_decrypted
,
1412 sizeof(rsa_decrypted
) ) != 0 )
1415 printf( "failed\n" );
1420 if( memcmp( rsa_decrypted
, rsa_plaintext
, len
) != 0 )
1423 printf( "failed\n" );
1428 #if defined(POLARSSL_SHA1_C)
1430 printf( "passed\n PKCS#1 data sign : " );
1432 sha1( rsa_plaintext
, PT_LEN
, sha1sum
);
1434 if( rsa_pkcs1_sign( &rsa
, NULL
, NULL
, RSA_PRIVATE
, SIG_RSA_SHA1
, 20,
1435 sha1sum
, rsa_ciphertext
) != 0 )
1438 printf( "failed\n" );
1444 printf( "passed\n PKCS#1 sig. verify: " );
1446 if( rsa_pkcs1_verify( &rsa
, RSA_PUBLIC
, SIG_RSA_SHA1
, 20,
1447 sha1sum
, rsa_ciphertext
) != 0 )
1450 printf( "failed\n" );
1456 printf( "passed\n\n" );
1457 #endif /* POLARSSL_SHA1_C */