PolarSSL v1.3.9
rsa.c
Go to the documentation of this file.
1 /*
2  * The RSA public-key cryptosystem
3  *
4  * Copyright (C) 2006-2014, Brainspark B.V.
5  *
6  * This file is part of PolarSSL (http://www.polarssl.org)
7  * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8  *
9  * All rights reserved.
10  *
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.
15  *
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.
20  *
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.
24  */
25 /*
26  * RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
27  *
28  * http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
29  * http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
30  */
31 
32 #if !defined(POLARSSL_CONFIG_FILE)
33 #include "polarssl/config.h"
34 #else
35 #include POLARSSL_CONFIG_FILE
36 #endif
37 
38 #if defined(POLARSSL_RSA_C)
39 
40 #include "polarssl/rsa.h"
41 #include "polarssl/oid.h"
42 
43 #if defined(POLARSSL_PKCS1_V21)
44 #include "polarssl/md.h"
45 #endif
46 
47 #include <stdlib.h>
48 #include <stdio.h>
49 
50 #if defined(POLARSSL_PLATFORM_C)
51 #include "polarssl/platform.h"
52 #else
53 #define polarssl_printf printf
54 #endif
55 
56 /*
57  * Initialize an RSA context
58  */
59 void rsa_init( rsa_context *ctx,
60  int padding,
61  int hash_id )
62 {
63  memset( ctx, 0, sizeof( rsa_context ) );
64 
65  rsa_set_padding( ctx, padding, hash_id );
66 
67 #if defined(POLARSSL_THREADING_C)
68  polarssl_mutex_init( &ctx->mutex );
69 #endif
70 }
71 
72 /*
73  * Set padding for an existing RSA context
74  */
75 void rsa_set_padding( rsa_context *ctx, int padding, int hash_id )
76 {
77  ctx->padding = padding;
78  ctx->hash_id = hash_id;
79 }
80 
81 #if defined(POLARSSL_GENPRIME)
82 
83 /*
84  * Generate an RSA keypair
85  */
86 int rsa_gen_key( rsa_context *ctx,
87  int (*f_rng)(void *, unsigned char *, size_t),
88  void *p_rng,
89  unsigned int nbits, int exponent )
90 {
91  int ret;
92  mpi P1, Q1, H, G;
93 
94  if( f_rng == NULL || nbits < 128 || exponent < 3 )
96 
97  mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
98 
99  /*
100  * find primes P and Q with Q < P so that:
101  * GCD( E, (P-1)*(Q-1) ) == 1
102  */
103  MPI_CHK( mpi_lset( &ctx->E, exponent ) );
104 
105  do
106  {
107  MPI_CHK( mpi_gen_prime( &ctx->P, ( nbits + 1 ) >> 1, 0,
108  f_rng, p_rng ) );
109 
110  MPI_CHK( mpi_gen_prime( &ctx->Q, ( nbits + 1 ) >> 1, 0,
111  f_rng, p_rng ) );
112 
113  if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
114  mpi_swap( &ctx->P, &ctx->Q );
115 
116  if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
117  continue;
118 
119  MPI_CHK( mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
120  if( mpi_msb( &ctx->N ) != nbits )
121  continue;
122 
123  MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
124  MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
125  MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
126  MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
127  }
128  while( mpi_cmp_int( &G, 1 ) != 0 );
129 
130  /*
131  * D = E^-1 mod ((P-1)*(Q-1))
132  * DP = D mod (P - 1)
133  * DQ = D mod (Q - 1)
134  * QP = Q^-1 mod P
135  */
136  MPI_CHK( mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
137  MPI_CHK( mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
138  MPI_CHK( mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
139  MPI_CHK( mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
140 
141  ctx->len = ( mpi_msb( &ctx->N ) + 7 ) >> 3;
142 
143 cleanup:
144 
145  mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
146 
147  if( ret != 0 )
148  {
149  rsa_free( ctx );
150  return( POLARSSL_ERR_RSA_KEY_GEN_FAILED + ret );
151  }
152 
153  return( 0 );
154 }
155 
156 #endif /* POLARSSL_GENPRIME */
157 
158 /*
159  * Check a public RSA key
160  */
161 int rsa_check_pubkey( const rsa_context *ctx )
162 {
163  if( !ctx->N.p || !ctx->E.p )
165 
166  if( ( ctx->N.p[0] & 1 ) == 0 ||
167  ( ctx->E.p[0] & 1 ) == 0 )
169 
170  if( mpi_msb( &ctx->N ) < 128 ||
171  mpi_msb( &ctx->N ) > POLARSSL_MPI_MAX_BITS )
173 
174  if( mpi_msb( &ctx->E ) < 2 ||
175  mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
177 
178  return( 0 );
179 }
180 
181 /*
182  * Check a private RSA key
183  */
184 int rsa_check_privkey( const rsa_context *ctx )
185 {
186  int ret;
187  mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
188 
189  if( ( ret = rsa_check_pubkey( ctx ) ) != 0 )
190  return( ret );
191 
192  if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
194 
195  mpi_init( &PQ ); mpi_init( &DE ); mpi_init( &P1 ); mpi_init( &Q1 );
196  mpi_init( &H ); mpi_init( &I ); mpi_init( &G ); mpi_init( &G2 );
197  mpi_init( &L1 ); mpi_init( &L2 ); mpi_init( &DP ); mpi_init( &DQ );
198  mpi_init( &QP );
199 
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_gcd( &G, &ctx->E, &H ) );
206 
207  MPI_CHK( mpi_gcd( &G2, &P1, &Q1 ) );
208  MPI_CHK( mpi_div_mpi( &L1, &L2, &H, &G2 ) );
209  MPI_CHK( mpi_mod_mpi( &I, &DE, &L1 ) );
210 
211  MPI_CHK( mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
212  MPI_CHK( mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
213  MPI_CHK( mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
214  /*
215  * Check for a valid PKCS1v2 private key
216  */
217  if( mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
218  mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
219  mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
220  mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
221  mpi_cmp_int( &L2, 0 ) != 0 ||
222  mpi_cmp_int( &I, 1 ) != 0 ||
223  mpi_cmp_int( &G, 1 ) != 0 )
224  {
226  }
227 
228 cleanup:
229  mpi_free( &PQ ); mpi_free( &DE ); mpi_free( &P1 ); mpi_free( &Q1 );
230  mpi_free( &H ); mpi_free( &I ); mpi_free( &G ); mpi_free( &G2 );
231  mpi_free( &L1 ); mpi_free( &L2 ); mpi_free( &DP ); mpi_free( &DQ );
232  mpi_free( &QP );
233 
235  return( ret );
236 
237  if( ret != 0 )
238  return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED + ret );
239 
240  return( 0 );
241 }
242 
243 /*
244  * Do an RSA public key operation
245  */
246 int rsa_public( rsa_context *ctx,
247  const unsigned char *input,
248  unsigned char *output )
249 {
250  int ret;
251  size_t olen;
252  mpi T;
253 
254  mpi_init( &T );
255 
256  MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
257 
258  if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
259  {
260  mpi_free( &T );
262  }
263 
264  olen = ctx->len;
265  MPI_CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
266  MPI_CHK( mpi_write_binary( &T, output, olen ) );
267 
268 cleanup:
269 
270  mpi_free( &T );
271 
272  if( ret != 0 )
273  return( POLARSSL_ERR_RSA_PUBLIC_FAILED + ret );
274 
275  return( 0 );
276 }
277 
278 #if !defined(POLARSSL_RSA_NO_CRT)
279 /*
280  * Generate or update blinding values, see section 10 of:
281  * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
282  * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
283  * Berlin Heidelberg, 1996. p. 104-113.
284  */
285 static int rsa_prepare_blinding( rsa_context *ctx, mpi *Vi, mpi *Vf,
286  int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
287 {
288  int ret, count = 0;
289 
290 #if defined(POLARSSL_THREADING_C)
291  polarssl_mutex_lock( &ctx->mutex );
292 #endif
293 
294  if( ctx->Vf.p != NULL )
295  {
296  /* We already have blinding values, just update them by squaring */
297  MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
298  MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
299  MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
300  MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
301 
302  goto done;
303  }
304 
305  /* Unblinding value: Vf = random number, invertible mod N */
306  do {
307  if( count++ > 10 )
308  return( POLARSSL_ERR_RSA_RNG_FAILED );
309 
310  MPI_CHK( mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
311  MPI_CHK( mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
312  } while( mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
313 
314  /* Blinding value: Vi = Vf^(-e) mod N */
315  MPI_CHK( mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
316  MPI_CHK( mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
317 
318 done:
319  if( Vi != &ctx->Vi )
320  {
321  MPI_CHK( mpi_copy( Vi, &ctx->Vi ) );
322  MPI_CHK( mpi_copy( Vf, &ctx->Vf ) );
323  }
324 
325 cleanup:
326 #if defined(POLARSSL_THREADING_C)
327  polarssl_mutex_unlock( &ctx->mutex );
328 #endif
329 
330  return( ret );
331 }
332 #endif /* !POLARSSL_RSA_NO_CRT */
333 
334 /*
335  * Do an RSA private key operation
336  */
337 int rsa_private( rsa_context *ctx,
338  int (*f_rng)(void *, unsigned char *, size_t),
339  void *p_rng,
340  const unsigned char *input,
341  unsigned char *output )
342 {
343  int ret;
344  size_t olen;
345  mpi T, T1, T2;
346 #if !defined(POLARSSL_RSA_NO_CRT)
347  mpi *Vi, *Vf;
348 
349  /*
350  * When using the Chinese Remainder Theorem, we use blinding values.
351  * Without threading, we just read them directly from the context,
352  * otherwise we make a local copy in order to reduce locking contention.
353  */
354 #if defined(POLARSSL_THREADING_C)
355  mpi Vi_copy, Vf_copy;
356 
357  mpi_init( &Vi_copy ); mpi_init( &Vf_copy );
358  Vi = &Vi_copy;
359  Vf = &Vf_copy;
360 #else
361  Vi = &ctx->Vi;
362  Vf = &ctx->Vf;
363 #endif
364 #endif /* !POLARSSL_RSA_NO_CRT */
365 
366  mpi_init( &T ); mpi_init( &T1 ); mpi_init( &T2 );
367 
368  MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
369  if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
370  {
371  mpi_free( &T );
373  }
374 
375 #if defined(POLARSSL_RSA_NO_CRT)
376  ((void) f_rng);
377  ((void) p_rng);
378  MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
379 #else
380  if( f_rng != NULL )
381  {
382  /*
383  * Blinding
384  * T = T * Vi mod N
385  */
386  MPI_CHK( rsa_prepare_blinding( ctx, Vi, Vf, f_rng, p_rng ) );
387  MPI_CHK( mpi_mul_mpi( &T, &T, Vi ) );
388  MPI_CHK( mpi_mod_mpi( &T, &T, &ctx->N ) );
389  }
390 
391  /*
392  * faster decryption using the CRT
393  *
394  * T1 = input ^ dP mod P
395  * T2 = input ^ dQ mod Q
396  */
397  MPI_CHK( mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
398  MPI_CHK( mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
399 
400  /*
401  * T = (T1 - T2) * (Q^-1 mod P) mod P
402  */
403  MPI_CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
404  MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
405  MPI_CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
406 
407  /*
408  * T = T2 + T * Q
409  */
410  MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
411  MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) );
412 
413  if( f_rng != NULL )
414  {
415  /*
416  * Unblind
417  * T = T * Vf mod N
418  */
419  MPI_CHK( mpi_mul_mpi( &T, &T, Vf ) );
420  MPI_CHK( mpi_mod_mpi( &T, &T, &ctx->N ) );
421  }
422 #endif /* POLARSSL_RSA_NO_CRT */
423 
424  olen = ctx->len;
425  MPI_CHK( mpi_write_binary( &T, output, olen ) );
426 
427 cleanup:
428  mpi_free( &T ); mpi_free( &T1 ); mpi_free( &T2 );
429 #if !defined(POLARSSL_RSA_NO_CRT) && defined(POLARSSL_THREADING_C)
430  mpi_free( &Vi_copy ); mpi_free( &Vf_copy );
431 #endif
432 
433  if( ret != 0 )
434  return( POLARSSL_ERR_RSA_PRIVATE_FAILED + ret );
435 
436  return( 0 );
437 }
438 
439 #if defined(POLARSSL_PKCS1_V21)
440 
449 static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
450  size_t slen, md_context_t *md_ctx )
451 {
452  unsigned char mask[POLARSSL_MD_MAX_SIZE];
453  unsigned char counter[4];
454  unsigned char *p;
455  unsigned int hlen;
456  size_t i, use_len;
457 
458  memset( mask, 0, POLARSSL_MD_MAX_SIZE );
459  memset( counter, 0, 4 );
460 
461  hlen = md_ctx->md_info->size;
462 
463  // Generate and apply dbMask
464  //
465  p = dst;
466 
467  while( dlen > 0 )
468  {
469  use_len = hlen;
470  if( dlen < hlen )
471  use_len = dlen;
472 
473  md_starts( md_ctx );
474  md_update( md_ctx, src, slen );
475  md_update( md_ctx, counter, 4 );
476  md_finish( md_ctx, mask );
477 
478  for( i = 0; i < use_len; ++i )
479  *p++ ^= mask[i];
480 
481  counter[3]++;
482 
483  dlen -= use_len;
484  }
485 }
486 #endif /* POLARSSL_PKCS1_V21 */
487 
488 #if defined(POLARSSL_PKCS1_V21)
489 /*
490  * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
491  */
493  int (*f_rng)(void *, unsigned char *, size_t),
494  void *p_rng,
495  int mode,
496  const unsigned char *label, size_t label_len,
497  size_t ilen,
498  const unsigned char *input,
499  unsigned char *output )
500 {
501  size_t olen;
502  int ret;
503  unsigned char *p = output;
504  unsigned int hlen;
505  const md_info_t *md_info;
506  md_context_t md_ctx;
507 
508  if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V21 )
510 
511  if( f_rng == NULL )
513 
514  md_info = md_info_from_type( ctx->hash_id );
515  if( md_info == NULL )
517 
518  olen = ctx->len;
519  hlen = md_get_size( md_info );
520 
521  if( olen < ilen + 2 * hlen + 2 )
523 
524  memset( output, 0, olen );
525 
526  *p++ = 0;
527 
528  // Generate a random octet string seed
529  //
530  if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
531  return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
532 
533  p += hlen;
534 
535  // Construct DB
536  //
537  md( md_info, label, label_len, p );
538  p += hlen;
539  p += olen - 2 * hlen - 2 - ilen;
540  *p++ = 1;
541  memcpy( p, input, ilen );
542 
543  md_init( &md_ctx );
544  md_init_ctx( &md_ctx, md_info );
545 
546  // maskedDB: Apply dbMask to DB
547  //
548  mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
549  &md_ctx );
550 
551  // maskedSeed: Apply seedMask to seed
552  //
553  mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
554  &md_ctx );
555 
556  md_free( &md_ctx );
557 
558  return( ( mode == RSA_PUBLIC )
559  ? rsa_public( ctx, output, output )
560  : rsa_private( ctx, f_rng, p_rng, output, output ) );
561 }
562 #endif /* POLARSSL_PKCS1_V21 */
563 
564 #if defined(POLARSSL_PKCS1_V15)
565 /*
566  * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
567  */
569  int (*f_rng)(void *, unsigned char *, size_t),
570  void *p_rng,
571  int mode, size_t ilen,
572  const unsigned char *input,
573  unsigned char *output )
574 {
575  size_t nb_pad, olen;
576  int ret;
577  unsigned char *p = output;
578 
579  if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V15 )
581 
582  if( f_rng == NULL )
584 
585  olen = ctx->len;
586 
587  if( olen < ilen + 11 )
589 
590  nb_pad = olen - 3 - ilen;
591 
592  *p++ = 0;
593  if( mode == RSA_PUBLIC )
594  {
595  *p++ = RSA_CRYPT;
596 
597  while( nb_pad-- > 0 )
598  {
599  int rng_dl = 100;
600 
601  do {
602  ret = f_rng( p_rng, p, 1 );
603  } while( *p == 0 && --rng_dl && ret == 0 );
604 
605  // Check if RNG failed to generate data
606  //
607  if( rng_dl == 0 || ret != 0 )
608  return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
609 
610  p++;
611  }
612  }
613  else
614  {
615  *p++ = RSA_SIGN;
616 
617  while( nb_pad-- > 0 )
618  *p++ = 0xFF;
619  }
620 
621  *p++ = 0;
622  memcpy( p, input, ilen );
623 
624  return( ( mode == RSA_PUBLIC )
625  ? rsa_public( ctx, output, output )
626  : rsa_private( ctx, f_rng, p_rng, output, output ) );
627 }
628 #endif /* POLARSSL_PKCS1_V15 */
629 
630 /*
631  * Add the message padding, then do an RSA operation
632  */
634  int (*f_rng)(void *, unsigned char *, size_t),
635  void *p_rng,
636  int mode, size_t ilen,
637  const unsigned char *input,
638  unsigned char *output )
639 {
640  switch( ctx->padding )
641  {
642 #if defined(POLARSSL_PKCS1_V15)
643  case RSA_PKCS_V15:
644  return rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
645  input, output );
646 #endif
647 
648 #if defined(POLARSSL_PKCS1_V21)
649  case RSA_PKCS_V21:
650  return rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
651  ilen, input, output );
652 #endif
653 
654  default:
656  }
657 }
658 
659 #if defined(POLARSSL_PKCS1_V21)
660 /*
661  * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
662  */
664  int (*f_rng)(void *, unsigned char *, size_t),
665  void *p_rng,
666  int mode,
667  const unsigned char *label, size_t label_len,
668  size_t *olen,
669  const unsigned char *input,
670  unsigned char *output,
671  size_t output_max_len )
672 {
673  int ret;
674  size_t ilen, i, pad_len;
675  unsigned char *p, bad, pad_done;
676  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
677  unsigned char lhash[POLARSSL_MD_MAX_SIZE];
678  unsigned int hlen;
679  const md_info_t *md_info;
680  md_context_t md_ctx;
681 
682  /*
683  * Parameters sanity checks
684  */
685  if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V21 )
687 
688  ilen = ctx->len;
689 
690  if( ilen < 16 || ilen > sizeof( buf ) )
692 
693  md_info = md_info_from_type( ctx->hash_id );
694  if( md_info == NULL )
696 
697  /*
698  * RSA operation
699  */
700  ret = ( mode == RSA_PUBLIC )
701  ? rsa_public( ctx, input, buf )
702  : rsa_private( ctx, f_rng, p_rng, input, buf );
703 
704  if( ret != 0 )
705  return( ret );
706 
707  /*
708  * Unmask data and generate lHash
709  */
710  hlen = md_get_size( md_info );
711 
712  md_init( &md_ctx );
713  md_init_ctx( &md_ctx, md_info );
714 
715  /* Generate lHash */
716  md( md_info, label, label_len, lhash );
717 
718  /* seed: Apply seedMask to maskedSeed */
719  mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
720  &md_ctx );
721 
722  /* DB: Apply dbMask to maskedDB */
723  mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
724  &md_ctx );
725 
726  md_free( &md_ctx );
727 
728  /*
729  * Check contents, in "constant-time"
730  */
731  p = buf;
732  bad = 0;
733 
734  bad |= *p++; /* First byte must be 0 */
735 
736  p += hlen; /* Skip seed */
737 
738  /* Check lHash */
739  for( i = 0; i < hlen; i++ )
740  bad |= lhash[i] ^ *p++;
741 
742  /* Get zero-padding len, but always read till end of buffer
743  * (minus one, for the 01 byte) */
744  pad_len = 0;
745  pad_done = 0;
746  for( i = 0; i < ilen - 2 * hlen - 2; i++ )
747  {
748  pad_done |= p[i];
749  pad_len += ( pad_done == 0 );
750  }
751 
752  p += pad_len;
753  bad |= *p++ ^ 0x01;
754 
755  /*
756  * The only information "leaked" is whether the padding was correct or not
757  * (eg, no data is copied if it was not correct). This meets the
758  * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
759  * the different error conditions.
760  */
761  if( bad != 0 )
763 
764  if( ilen - ( p - buf ) > output_max_len )
766 
767  *olen = ilen - (p - buf);
768  memcpy( output, p, *olen );
769 
770  return( 0 );
771 }
772 #endif /* POLARSSL_PKCS1_V21 */
773 
774 #if defined(POLARSSL_PKCS1_V15)
775 /*
776  * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
777  */
779  int (*f_rng)(void *, unsigned char *, size_t),
780  void *p_rng,
781  int mode, size_t *olen,
782  const unsigned char *input,
783  unsigned char *output,
784  size_t output_max_len)
785 {
786  int ret;
787  size_t ilen, pad_count = 0, i;
788  unsigned char *p, bad, pad_done = 0;
789  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
790 
791  if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V15 )
793 
794  ilen = ctx->len;
795 
796  if( ilen < 16 || ilen > sizeof( buf ) )
798 
799  ret = ( mode == RSA_PUBLIC )
800  ? rsa_public( ctx, input, buf )
801  : rsa_private( ctx, f_rng, p_rng, input, buf );
802 
803  if( ret != 0 )
804  return( ret );
805 
806  p = buf;
807  bad = 0;
808 
809  /*
810  * Check and get padding len in "constant-time"
811  */
812  bad |= *p++; /* First byte must be 0 */
813 
814  /* This test does not depend on secret data */
815  if( mode == RSA_PRIVATE )
816  {
817  bad |= *p++ ^ RSA_CRYPT;
818 
819  /* Get padding len, but always read till end of buffer
820  * (minus one, for the 00 byte) */
821  for( i = 0; i < ilen - 3; i++ )
822  {
823  pad_done |= ( p[i] == 0 );
824  pad_count += ( pad_done == 0 );
825  }
826 
827  p += pad_count;
828  bad |= *p++; /* Must be zero */
829  }
830  else
831  {
832  bad |= *p++ ^ RSA_SIGN;
833 
834  /* Get padding len, but always read till end of buffer
835  * (minus one, for the 00 byte) */
836  for( i = 0; i < ilen - 3; i++ )
837  {
838  pad_done |= ( p[i] != 0xFF );
839  pad_count += ( pad_done == 0 );
840  }
841 
842  p += pad_count;
843  bad |= *p++; /* Must be zero */
844  }
845 
846  if( bad )
848 
849  if( ilen - ( p - buf ) > output_max_len )
851 
852  *olen = ilen - (p - buf);
853  memcpy( output, p, *olen );
854 
855  return( 0 );
856 }
857 #endif /* POLARSSL_PKCS1_V15 */
858 
859 /*
860  * Do an RSA operation, then remove the message padding
861  */
863  int (*f_rng)(void *, unsigned char *, size_t),
864  void *p_rng,
865  int mode, size_t *olen,
866  const unsigned char *input,
867  unsigned char *output,
868  size_t output_max_len)
869 {
870  switch( ctx->padding )
871  {
872 #if defined(POLARSSL_PKCS1_V15)
873  case RSA_PKCS_V15:
874  return rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
875  input, output, output_max_len );
876 #endif
877 
878 #if defined(POLARSSL_PKCS1_V21)
879  case RSA_PKCS_V21:
880  return rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
881  olen, input, output,
882  output_max_len );
883 #endif
884 
885  default:
887  }
888 }
889 
890 #if defined(POLARSSL_PKCS1_V21)
891 /*
892  * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
893  */
895  int (*f_rng)(void *, unsigned char *, size_t),
896  void *p_rng,
897  int mode,
898  md_type_t md_alg,
899  unsigned int hashlen,
900  const unsigned char *hash,
901  unsigned char *sig )
902 {
903  size_t olen;
904  unsigned char *p = sig;
905  unsigned char salt[POLARSSL_MD_MAX_SIZE];
906  unsigned int slen, hlen, offset = 0;
907  int ret;
908  size_t msb;
909  const md_info_t *md_info;
910  md_context_t md_ctx;
911 
912  if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V21 )
914 
915  if( f_rng == NULL )
917 
918  olen = ctx->len;
919 
920  if( md_alg != POLARSSL_MD_NONE )
921  {
922  // Gather length of hash to sign
923  //
924  md_info = md_info_from_type( md_alg );
925  if( md_info == NULL )
927 
928  hashlen = md_get_size( md_info );
929  }
930 
931  md_info = md_info_from_type( ctx->hash_id );
932  if( md_info == NULL )
934 
935  hlen = md_get_size( md_info );
936  slen = hlen;
937 
938  if( olen < hlen + slen + 2 )
940 
941  memset( sig, 0, olen );
942 
943  // Generate salt of length slen
944  //
945  if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
946  return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
947 
948  // Note: EMSA-PSS encoding is over the length of N - 1 bits
949  //
950  msb = mpi_msb( &ctx->N ) - 1;
951  p += olen - hlen * 2 - 2;
952  *p++ = 0x01;
953  memcpy( p, salt, slen );
954  p += slen;
955 
956  md_init( &md_ctx );
957  md_init_ctx( &md_ctx, md_info );
958 
959  // Generate H = Hash( M' )
960  //
961  md_starts( &md_ctx );
962  md_update( &md_ctx, p, 8 );
963  md_update( &md_ctx, hash, hashlen );
964  md_update( &md_ctx, salt, slen );
965  md_finish( &md_ctx, p );
966 
967  // Compensate for boundary condition when applying mask
968  //
969  if( msb % 8 == 0 )
970  offset = 1;
971 
972  // maskedDB: Apply dbMask to DB
973  //
974  mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
975 
976  md_free( &md_ctx );
977 
978  msb = mpi_msb( &ctx->N ) - 1;
979  sig[0] &= 0xFF >> ( olen * 8 - msb );
980 
981  p += hlen;
982  *p++ = 0xBC;
983 
984  return( ( mode == RSA_PUBLIC )
985  ? rsa_public( ctx, sig, sig )
986  : rsa_private( ctx, f_rng, p_rng, sig, sig ) );
987 }
988 #endif /* POLARSSL_PKCS1_V21 */
989 
990 #if defined(POLARSSL_PKCS1_V15)
991 /*
992  * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
993  */
994 /*
995  * Do an RSA operation to sign the message digest
996  */
998  int (*f_rng)(void *, unsigned char *, size_t),
999  void *p_rng,
1000  int mode,
1001  md_type_t md_alg,
1002  unsigned int hashlen,
1003  const unsigned char *hash,
1004  unsigned char *sig )
1005 {
1006  size_t nb_pad, olen, oid_size = 0;
1007  unsigned char *p = sig;
1008  const char *oid = NULL;
1009 
1010  if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V15 )
1012 
1013  olen = ctx->len;
1014  nb_pad = olen - 3;
1015 
1016  if( md_alg != POLARSSL_MD_NONE )
1017  {
1018  const md_info_t *md_info = md_info_from_type( md_alg );
1019  if( md_info == NULL )
1021 
1022  if( oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1024 
1025  nb_pad -= 10 + oid_size;
1026 
1027  hashlen = md_get_size( md_info );
1028  }
1029 
1030  nb_pad -= hashlen;
1031 
1032  if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
1034 
1035  *p++ = 0;
1036  *p++ = RSA_SIGN;
1037  memset( p, 0xFF, nb_pad );
1038  p += nb_pad;
1039  *p++ = 0;
1040 
1041  if( md_alg == POLARSSL_MD_NONE )
1042  {
1043  memcpy( p, hash, hashlen );
1044  }
1045  else
1046  {
1047  /*
1048  * DigestInfo ::= SEQUENCE {
1049  * digestAlgorithm DigestAlgorithmIdentifier,
1050  * digest Digest }
1051  *
1052  * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1053  *
1054  * Digest ::= OCTET STRING
1055  */
1057  *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
1059  *p++ = (unsigned char) ( 0x04 + oid_size );
1060  *p++ = ASN1_OID;
1061  *p++ = oid_size & 0xFF;
1062  memcpy( p, oid, oid_size );
1063  p += oid_size;
1064  *p++ = ASN1_NULL;
1065  *p++ = 0x00;
1066  *p++ = ASN1_OCTET_STRING;
1067  *p++ = hashlen;
1068  memcpy( p, hash, hashlen );
1069  }
1070 
1071  return( ( mode == RSA_PUBLIC )
1072  ? rsa_public( ctx, sig, sig )
1073  : rsa_private( ctx, f_rng, p_rng, sig, sig ) );
1074 }
1075 #endif /* POLARSSL_PKCS1_V15 */
1076 
1077 /*
1078  * Do an RSA operation to sign the message digest
1079  */
1080 int rsa_pkcs1_sign( rsa_context *ctx,
1081  int (*f_rng)(void *, unsigned char *, size_t),
1082  void *p_rng,
1083  int mode,
1084  md_type_t md_alg,
1085  unsigned int hashlen,
1086  const unsigned char *hash,
1087  unsigned char *sig )
1088 {
1089  switch( ctx->padding )
1090  {
1091 #if defined(POLARSSL_PKCS1_V15)
1092  case RSA_PKCS_V15:
1093  return rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
1094  hashlen, hash, sig );
1095 #endif
1096 
1097 #if defined(POLARSSL_PKCS1_V21)
1098  case RSA_PKCS_V21:
1099  return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
1100  hashlen, hash, sig );
1101 #endif
1102 
1103  default:
1105  }
1106 }
1107 
1108 #if defined(POLARSSL_PKCS1_V21)
1109 /*
1110  * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1111  */
1113  int (*f_rng)(void *, unsigned char *, size_t),
1114  void *p_rng,
1115  int mode,
1116  md_type_t md_alg,
1117  unsigned int hashlen,
1118  const unsigned char *hash,
1119  md_type_t mgf1_hash_id,
1120  int expected_salt_len,
1121  const unsigned char *sig )
1122 {
1123  int ret;
1124  size_t siglen;
1125  unsigned char *p;
1126  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
1127  unsigned char result[POLARSSL_MD_MAX_SIZE];
1128  unsigned char zeros[8];
1129  unsigned int hlen;
1130  size_t slen, msb;
1131  const md_info_t *md_info;
1132  md_context_t md_ctx;
1133 
1134  if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V21 )
1136 
1137  siglen = ctx->len;
1138 
1139  if( siglen < 16 || siglen > sizeof( buf ) )
1141 
1142  ret = ( mode == RSA_PUBLIC )
1143  ? rsa_public( ctx, sig, buf )
1144  : rsa_private( ctx, f_rng, p_rng, sig, buf );
1145 
1146  if( ret != 0 )
1147  return( ret );
1148 
1149  p = buf;
1150 
1151  if( buf[siglen - 1] != 0xBC )
1153 
1154  if( md_alg != POLARSSL_MD_NONE )
1155  {
1156  // Gather length of hash to sign
1157  //
1158  md_info = md_info_from_type( md_alg );
1159  if( md_info == NULL )
1161 
1162  hashlen = md_get_size( md_info );
1163  }
1164 
1165  md_info = md_info_from_type( mgf1_hash_id );
1166  if( md_info == NULL )
1168 
1169  hlen = md_get_size( md_info );
1170  slen = siglen - hlen - 1; /* Currently length of salt + padding */
1171 
1172  memset( zeros, 0, 8 );
1173 
1174  // Note: EMSA-PSS verification is over the length of N - 1 bits
1175  //
1176  msb = mpi_msb( &ctx->N ) - 1;
1177 
1178  // Compensate for boundary condition when applying mask
1179  //
1180  if( msb % 8 == 0 )
1181  {
1182  p++;
1183  siglen -= 1;
1184  }
1185  if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1187 
1188  md_init( &md_ctx );
1189  md_init_ctx( &md_ctx, md_info );
1190 
1191  mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
1192 
1193  buf[0] &= 0xFF >> ( siglen * 8 - msb );
1194 
1195  while( p < buf + siglen && *p == 0 )
1196  p++;
1197 
1198  if( p == buf + siglen ||
1199  *p++ != 0x01 )
1200  {
1201  md_free( &md_ctx );
1203  }
1204 
1205  /* Actual salt len */
1206  slen -= p - buf;
1207 
1208  if( expected_salt_len != RSA_SALT_LEN_ANY &&
1209  slen != (size_t) expected_salt_len )
1210  {
1211  md_free( &md_ctx );
1213  }
1214 
1215  // Generate H = Hash( M' )
1216  //
1217  md_starts( &md_ctx );
1218  md_update( &md_ctx, zeros, 8 );
1219  md_update( &md_ctx, hash, hashlen );
1220  md_update( &md_ctx, p, slen );
1221  md_finish( &md_ctx, result );
1222 
1223  md_free( &md_ctx );
1224 
1225  if( memcmp( p + slen, result, hlen ) == 0 )
1226  return( 0 );
1227  else
1229 }
1230 
1231 /*
1232  * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1233  */
1235  int (*f_rng)(void *, unsigned char *, size_t),
1236  void *p_rng,
1237  int mode,
1238  md_type_t md_alg,
1239  unsigned int hashlen,
1240  const unsigned char *hash,
1241  const unsigned char *sig )
1242 {
1243  md_type_t mgf1_hash_id = ( ctx->hash_id != POLARSSL_MD_NONE )
1244  ? (md_type_t) ctx->hash_id
1245  : md_alg;
1246 
1247  return( rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
1248  md_alg, hashlen, hash,
1249  mgf1_hash_id, RSA_SALT_LEN_ANY,
1250  sig ) );
1251 
1252 }
1253 #endif /* POLARSSL_PKCS1_V21 */
1254 
1255 #if defined(POLARSSL_PKCS1_V15)
1256 /*
1257  * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1258  */
1260  int (*f_rng)(void *, unsigned char *, size_t),
1261  void *p_rng,
1262  int mode,
1263  md_type_t md_alg,
1264  unsigned int hashlen,
1265  const unsigned char *hash,
1266  const unsigned char *sig )
1267 {
1268  int ret;
1269  size_t len, siglen, asn1_len;
1270  unsigned char *p, *end;
1271  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
1272  md_type_t msg_md_alg;
1273  const md_info_t *md_info;
1274  asn1_buf oid;
1275 
1276  if( mode == RSA_PRIVATE && ctx->padding != RSA_PKCS_V15 )
1278 
1279  siglen = ctx->len;
1280 
1281  if( siglen < 16 || siglen > sizeof( buf ) )
1283 
1284  ret = ( mode == RSA_PUBLIC )
1285  ? rsa_public( ctx, sig, buf )
1286  : rsa_private( ctx, f_rng, p_rng, sig, buf );
1287 
1288  if( ret != 0 )
1289  return( ret );
1290 
1291  p = buf;
1292 
1293  if( *p++ != 0 || *p++ != RSA_SIGN )
1295 
1296  while( *p != 0 )
1297  {
1298  if( p >= buf + siglen - 1 || *p != 0xFF )
1300  p++;
1301  }
1302  p++;
1303 
1304  len = siglen - ( p - buf );
1305 
1306  if( len == hashlen && md_alg == POLARSSL_MD_NONE )
1307  {
1308  if( memcmp( p, hash, hashlen ) == 0 )
1309  return( 0 );
1310  else
1312  }
1313 
1314  md_info = md_info_from_type( md_alg );
1315  if( md_info == NULL )
1317  hashlen = md_get_size( md_info );
1318 
1319  end = p + len;
1320 
1321  // Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1322  //
1323  if( ( ret = asn1_get_tag( &p, end, &asn1_len,
1324  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1326 
1327  if( asn1_len + 2 != len )
1329 
1330  if( ( ret = asn1_get_tag( &p, end, &asn1_len,
1331  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1333 
1334  if( asn1_len + 6 + hashlen != len )
1336 
1337  if( ( ret = asn1_get_tag( &p, end, &oid.len, ASN1_OID ) ) != 0 )
1339 
1340  oid.p = p;
1341  p += oid.len;
1342 
1343  if( oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1345 
1346  if( md_alg != msg_md_alg )
1348 
1349  /*
1350  * assume the algorithm parameters must be NULL
1351  */
1352  if( ( ret = asn1_get_tag( &p, end, &asn1_len, ASN1_NULL ) ) != 0 )
1354 
1355  if( ( ret = asn1_get_tag( &p, end, &asn1_len, ASN1_OCTET_STRING ) ) != 0 )
1357 
1358  if( asn1_len != hashlen )
1360 
1361  if( memcmp( p, hash, hashlen ) != 0 )
1363 
1364  p += hashlen;
1365 
1366  if( p != end )
1368 
1369  return( 0 );
1370 }
1371 #endif /* POLARSSL_PKCS1_V15 */
1372 
1373 /*
1374  * Do an RSA operation and check the message digest
1375  */
1376 int rsa_pkcs1_verify( rsa_context *ctx,
1377  int (*f_rng)(void *, unsigned char *, size_t),
1378  void *p_rng,
1379  int mode,
1380  md_type_t md_alg,
1381  unsigned int hashlen,
1382  const unsigned char *hash,
1383  const unsigned char *sig )
1384 {
1385  switch( ctx->padding )
1386  {
1387 #if defined(POLARSSL_PKCS1_V15)
1388  case RSA_PKCS_V15:
1389  return rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
1390  hashlen, hash, sig );
1391 #endif
1392 
1393 #if defined(POLARSSL_PKCS1_V21)
1394  case RSA_PKCS_V21:
1395  return rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
1396  hashlen, hash, sig );
1397 #endif
1398 
1399  default:
1401  }
1402 }
1403 
1404 /*
1405  * Copy the components of an RSA key
1406  */
1407 int rsa_copy( rsa_context *dst, const rsa_context *src )
1408 {
1409  int ret;
1410 
1411  dst->ver = src->ver;
1412  dst->len = src->len;
1413 
1414  MPI_CHK( mpi_copy( &dst->N, &src->N ) );
1415  MPI_CHK( mpi_copy( &dst->E, &src->E ) );
1416 
1417  MPI_CHK( mpi_copy( &dst->D, &src->D ) );
1418  MPI_CHK( mpi_copy( &dst->P, &src->P ) );
1419  MPI_CHK( mpi_copy( &dst->Q, &src->Q ) );
1420  MPI_CHK( mpi_copy( &dst->DP, &src->DP ) );
1421  MPI_CHK( mpi_copy( &dst->DQ, &src->DQ ) );
1422  MPI_CHK( mpi_copy( &dst->QP, &src->QP ) );
1423 
1424  MPI_CHK( mpi_copy( &dst->RN, &src->RN ) );
1425  MPI_CHK( mpi_copy( &dst->RP, &src->RP ) );
1426  MPI_CHK( mpi_copy( &dst->RQ, &src->RQ ) );
1427 
1428 #if !defined(POLARSSL_RSA_NO_CRT)
1429  MPI_CHK( mpi_copy( &dst->Vi, &src->Vi ) );
1430  MPI_CHK( mpi_copy( &dst->Vf, &src->Vf ) );
1431 #endif
1432 
1433  dst->padding = src->padding;
1434  dst->hash_id = src->hash_id;
1435 
1436 cleanup:
1437  if( ret != 0 )
1438  rsa_free( dst );
1439 
1440  return( ret );
1441 }
1442 
1443 /*
1444  * Free the components of an RSA key
1445  */
1446 void rsa_free( rsa_context *ctx )
1447 {
1448 #if !defined(POLARSSL_RSA_NO_CRT)
1449  mpi_free( &ctx->Vi ); mpi_free( &ctx->Vf );
1450 #endif
1451  mpi_free( &ctx->RQ ); mpi_free( &ctx->RP ); mpi_free( &ctx->RN );
1452  mpi_free( &ctx->QP ); mpi_free( &ctx->DQ ); mpi_free( &ctx->DP );
1453  mpi_free( &ctx->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D );
1454  mpi_free( &ctx->E ); mpi_free( &ctx->N );
1455 
1456 #if defined(POLARSSL_THREADING_C)
1457  polarssl_mutex_free( &ctx->mutex );
1458 #endif
1459 }
1460 
1461 #if defined(POLARSSL_SELF_TEST)
1462 
1463 #include "polarssl/sha1.h"
1464 
1465 /*
1466  * Example RSA-1024 keypair, for test purposes
1467  */
1468 #define KEY_LEN 128
1469 
1470 #define RSA_N "9292758453063D803DD603D5E777D788" \
1471  "8ED1D5BF35786190FA2F23EBC0848AEA" \
1472  "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1473  "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1474  "93A89813FBF3C4F8066D2D800F7C38A8" \
1475  "1AE31942917403FF4946B0A83D3D3E05" \
1476  "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1477  "5E94BB77B07507233A0BC7BAC8F90F79"
1478 
1479 #define RSA_E "10001"
1480 
1481 #define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1482  "66CA472BC44D253102F8B4A9D3BFA750" \
1483  "91386C0077937FE33FA3252D28855837" \
1484  "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1485  "DF79C5CE07EE72C7F123142198164234" \
1486  "CABB724CF78B8173B9F880FC86322407" \
1487  "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1488  "071513A1E85B5DFA031F21ECAE91A34D"
1489 
1490 #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1491  "2C01CAD19EA484A87EA4377637E75500" \
1492  "FCB2005C5C7DD6EC4AC023CDA285D796" \
1493  "C3D9E75E1EFC42488BB4F1D13AC30A57"
1494 
1495 #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1496  "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1497  "910E4168387E3C30AA1E00C339A79508" \
1498  "8452DD96A9A5EA5D9DCA68DA636032AF"
1499 
1500 #define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1501  "3C94D22288ACD763FD8E5600ED4A702D" \
1502  "F84198A5F06C2E72236AE490C93F07F8" \
1503  "3CC559CD27BC2D1CA488811730BB5725"
1504 
1505 #define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1506  "D8AAEA56749EA28623272E4F7D0592AF" \
1507  "7C1F1313CAC9471B5C523BFE592F517B" \
1508  "407A1BD76C164B93DA2D32A383E58357"
1509 
1510 #define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1511  "F38D18D2B2F0E2DD275AA977E2BF4411" \
1512  "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1513  "A74206CEC169D74BF5A8C50D6F48EA08"
1514 
1515 #define PT_LEN 24
1516 #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1517  "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1518 
1519 #if defined(POLARSSL_PKCS1_V15)
1520 static int myrand( void *rng_state, unsigned char *output, size_t len )
1521 {
1522 #if !defined(__OpenBSD__)
1523  size_t i;
1524 
1525  if( rng_state != NULL )
1526  rng_state = NULL;
1527 
1528  for( i = 0; i < len; ++i )
1529  output[i] = rand();
1530 #else
1531  if( rng_state != NULL )
1532  rng_state = NULL;
1533 
1534  arc4random_buf( output, len );
1535 #endif /* !OpenBSD */
1536 
1537  return( 0 );
1538 }
1539 #endif /* POLARSSL_PKCS1_V15 */
1540 
1541 /*
1542  * Checkup routine
1543  */
1544 int rsa_self_test( int verbose )
1545 {
1546  int ret = 0;
1547 #if defined(POLARSSL_PKCS1_V15)
1548  size_t len;
1549  rsa_context rsa;
1550  unsigned char rsa_plaintext[PT_LEN];
1551  unsigned char rsa_decrypted[PT_LEN];
1552  unsigned char rsa_ciphertext[KEY_LEN];
1553 #if defined(POLARSSL_SHA1_C)
1554  unsigned char sha1sum[20];
1555 #endif
1556 
1557  rsa_init( &rsa, RSA_PKCS_V15, 0 );
1558 
1559  rsa.len = KEY_LEN;
1560  MPI_CHK( mpi_read_string( &rsa.N , 16, RSA_N ) );
1561  MPI_CHK( mpi_read_string( &rsa.E , 16, RSA_E ) );
1562  MPI_CHK( mpi_read_string( &rsa.D , 16, RSA_D ) );
1563  MPI_CHK( mpi_read_string( &rsa.P , 16, RSA_P ) );
1564  MPI_CHK( mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1565  MPI_CHK( mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1566  MPI_CHK( mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1567  MPI_CHK( mpi_read_string( &rsa.QP, 16, RSA_QP ) );
1568 
1569  if( verbose != 0 )
1570  polarssl_printf( " RSA key validation: " );
1571 
1572  if( rsa_check_pubkey( &rsa ) != 0 ||
1573  rsa_check_privkey( &rsa ) != 0 )
1574  {
1575  if( verbose != 0 )
1576  polarssl_printf( "failed\n" );
1577 
1578  return( 1 );
1579  }
1580 
1581  if( verbose != 0 )
1582  polarssl_printf( "passed\n PKCS#1 encryption : " );
1583 
1584  memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1585 
1586  if( rsa_pkcs1_encrypt( &rsa, myrand, NULL, RSA_PUBLIC, PT_LEN,
1587  rsa_plaintext, rsa_ciphertext ) != 0 )
1588  {
1589  if( verbose != 0 )
1590  polarssl_printf( "failed\n" );
1591 
1592  return( 1 );
1593  }
1594 
1595  if( verbose != 0 )
1596  polarssl_printf( "passed\n PKCS#1 decryption : " );
1597 
1598  if( rsa_pkcs1_decrypt( &rsa, myrand, NULL, RSA_PRIVATE, &len,
1599  rsa_ciphertext, rsa_decrypted,
1600  sizeof(rsa_decrypted) ) != 0 )
1601  {
1602  if( verbose != 0 )
1603  polarssl_printf( "failed\n" );
1604 
1605  return( 1 );
1606  }
1607 
1608  if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1609  {
1610  if( verbose != 0 )
1611  polarssl_printf( "failed\n" );
1612 
1613  return( 1 );
1614  }
1615 
1616 #if defined(POLARSSL_SHA1_C)
1617  if( verbose != 0 )
1618  polarssl_printf( "passed\n PKCS#1 data sign : " );
1619 
1620  sha1( rsa_plaintext, PT_LEN, sha1sum );
1621 
1622  if( rsa_pkcs1_sign( &rsa, myrand, NULL, RSA_PRIVATE, POLARSSL_MD_SHA1, 0,
1623  sha1sum, rsa_ciphertext ) != 0 )
1624  {
1625  if( verbose != 0 )
1626  polarssl_printf( "failed\n" );
1627 
1628  return( 1 );
1629  }
1630 
1631  if( verbose != 0 )
1632  polarssl_printf( "passed\n PKCS#1 sig. verify: " );
1633 
1634  if( rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC, POLARSSL_MD_SHA1, 0,
1635  sha1sum, rsa_ciphertext ) != 0 )
1636  {
1637  if( verbose != 0 )
1638  polarssl_printf( "failed\n" );
1639 
1640  return( 1 );
1641  }
1642 
1643  if( verbose != 0 )
1644  polarssl_printf( "passed\n\n" );
1645 #endif /* POLARSSL_SHA1_C */
1646 
1647 cleanup:
1648  rsa_free( &rsa );
1649 #else /* POLARSSL_PKCS1_V15 */
1650  ((void) verbose);
1651 #endif /* POLARSSL_PKCS1_V15 */
1652  return( ret );
1653 }
1654 
1655 #endif /* POLARSSL_SELF_TEST */
1656 
1657 #endif /* POLARSSL_RSA_C */
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
int mpi_cmp_int(const mpi *X, t_sint z)
Compare signed values.
#define POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE
The output buffer for decryption is not large enough.
Definition: rsa.h:53
void mpi_swap(mpi *X, mpi *Y)
Swap the contents of X and Y.
#define ASN1_NULL
Definition: asn1.h:79
#define RSA_CRYPT
Definition: rsa.h:66
#define polarssl_printf
int rsa_self_test(int verbose)
Checkup routine.
#define ASN1_OID
Definition: asn1.h:80
int rsa_copy(rsa_context *dst, const rsa_context *src)
Copy the components of an RSA context.
int rsa_rsaes_oaep_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, const unsigned char *label, size_t label_len, size_t ilen, const unsigned char *input, unsigned char *output)
Perform a PKCS#1 v2.1 OAEP encryption (RSAES-OAEP-ENCRYPT)
int rsa_check_privkey(const rsa_context *ctx)
Check a private RSA key.
int mpi_gcd(mpi *G, const mpi *A, const mpi *B)
Greatest common divisor: G = gcd(A, B)
int padding
Definition: rsa.h:107
void sha1(const unsigned char *input, size_t ilen, unsigned char output[20])
Output = SHA-1( input buffer )
int md_starts(md_context_t *ctx)
Set-up the given context for a new message digest.
void md_init(md_context_t *ctx)
Initialize a md_context (as NONE)
int mpi_fill_random(mpi *X, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Fill an MPI X with size bytes of random.
int rsa_rsassa_pkcs1_v15_verify(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
Perform a PKCS#1 v1.5 verification (RSASSA-PKCS1-v1_5-VERIFY)
mpi Vf
Definition: rsa.h:104
int rsa_rsaes_pkcs1_v15_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
Perform a PKCS#1 v1.5 encryption (RSAES-PKCS1-v1_5-ENCRYPT)
int rsa_rsaes_oaep_decrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, const unsigned char *label, size_t label_len, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Perform a PKCS#1 v2.1 OAEP decryption (RSAES-OAEP-DECRYPT)
int md_init_ctx(md_context_t *ctx, const md_info_t *md_info)
Initialises and fills the message digest context structure with the appropriate values.
#define RSA_PUBLIC
Definition: rsa.h:59
int(* polarssl_mutex_lock)(threading_mutex_t *mutex)
#define RSA_PKCS_V21
Definition: rsa.h:63
#define ASN1_SEQUENCE
Definition: asn1.h:82
mpi DQ
Definition: rsa.h:95
Configuration options (set of defines)
int rsa_check_pubkey(const rsa_context *ctx)
Check a public RSA key.
mpi RP
Definition: rsa.h:99
int rsa_rsassa_pss_verify(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) (This is the "simple" version.)
int mpi_div_mpi(mpi *Q, mpi *R, const mpi *A, const mpi *B)
Division by mpi: A = Q * B + R.
int oid_get_md_alg(const asn1_buf *oid, md_type_t *md_alg)
Translate hash algorithm OID into md_type.
#define ASN1_CONSTRUCTED
Definition: asn1.h:92
static unsigned char md_get_size(const md_info_t *md_info)
Returns the size of the message digest output.
Definition: md.h:225
int rsa_pkcs1_decrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Generic wrapper to perform a PKCS#1 decryption using the mode from the context.
int mpi_lset(mpi *X, t_sint z)
Set value from integer.
#define POLARSSL_ERR_RSA_RNG_FAILED
The random generator failed to generate non-zeros.
Definition: rsa.h:54
MPI structure.
Definition: bignum.h:182
PolarSSL Platform abstraction layer.
void mpi_init(mpi *X)
Initialize one MPI.
int mpi_cmp_mpi(const mpi *X, const mpi *Y)
Compare signed values.
Object Identifier (OID) database.
const md_info_t * md_info
Information about the associated message digest.
Definition: md.h:134
size_t len
Definition: rsa.h:86
md_type_t
Definition: md.h:51
mpi P
Definition: rsa.h:92
mpi Vi
Definition: rsa.h:103
int rsa_rsaes_pkcs1_v15_decrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t *olen, const unsigned char *input, unsigned char *output, size_t output_max_len)
Perform a PKCS#1 v1.5 decryption (RSAES-PKCS1-v1_5-DECRYPT)
int mpi_add_mpi(mpi *X, const mpi *A, const mpi *B)
Signed addition: X = A + B.
#define POLARSSL_MPI_MAX_SIZE
mpi Q
Definition: rsa.h:93
int rsa_rsassa_pss_verify_ext(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, md_type_t mgf1_hash_id, int expected_salt_len, const unsigned char *sig)
Perform a PKCS#1 v2.1 PSS verification (RSASSA-PSS-VERIFY) (This is the version with "full" options...
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
int(* polarssl_mutex_unlock)(threading_mutex_t *mutex)
void rsa_free(rsa_context *ctx)
Free the components of an RSA key.
int rsa_private(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, const unsigned char *input, unsigned char *output)
Do an RSA private key operation.
RSA context structure.
Definition: rsa.h:83
mpi D
Definition: rsa.h:91
int rsa_pkcs1_encrypt(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, size_t ilen, const unsigned char *input, unsigned char *output)
Generic wrapper to perform a PKCS#1 encryption using the mode from the context.
#define POLARSSL_ERR_RSA_INVALID_PADDING
Input data contains invalid padding and is rejected.
Definition: rsa.h:47
mpi QP
Definition: rsa.h:96
void md_free(md_context_t *ctx)
Free and clear the message-specific context of ctx.
#define RSA_PKCS_V15
Definition: rsa.h:62
#define RSA_PRIVATE
Definition: rsa.h:60
mpi N
Definition: rsa.h:88
unsigned char * p
ASN1 data, e.g.
Definition: asn1.h:128
int mpi_inv_mod(mpi *X, const mpi *A, const mpi *N)
Modular inverse: X = A^-1 mod N.
void mpi_free(mpi *X)
Unallocate one MPI.
int(* polarssl_mutex_init)(threading_mutex_t *mutex)
#define RSA_SIGN
Definition: rsa.h:65
int mpi_exp_mod(mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR)
Sliding-window exponentiation: X = A^E mod N.
mpi RQ
Definition: rsa.h:100
int rsa_rsassa_pss_sign(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Perform a PKCS#1 v2.1 PSS signature (RSASSA-PSS-SIGN)
mpi E
Definition: rsa.h:89
int rsa_pkcs1_verify(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, const unsigned char *sig)
Generic wrapper to perform a PKCS#1 verification using the mode from the context. ...
mpi DP
Definition: rsa.h:94
#define POLARSSL_ERR_RSA_VERIFY_FAILED
The PKCS#1 verification failed.
Definition: rsa.h:52
int mpi_gen_prime(mpi *X, size_t nbits, int dh_flag, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Prime number generation.
int hash_id
Definition: rsa.h:109
size_t mpi_msb(const mpi *X)
Return the number of bits up to and including the most significant '1' bit'.
#define POLARSSL_MPI_MAX_BITS
Maximum number of bits for usable MPIs.
Definition: bignum.h:96
int rsa_pkcs1_sign(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Generic wrapper to perform a PKCS#1 signature using the mode from the context.
int rsa_rsassa_pkcs1_v15_sign(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, int mode, md_type_t md_alg, unsigned int hashlen, const unsigned char *hash, unsigned char *sig)
Perform a PKCS#1 v1.5 signature (RSASSA-PKCS1-v1_5-SIGN)
int mpi_read_string(mpi *X, int radix, const char *s)
Import from an ASCII string.
Generic message digest wrapper.
t_uint * p
Definition: bignum.h:186
#define RSA_SALT_LEN_ANY
Definition: rsa.h:68
int mpi_read_binary(mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, big endian.
Type-length-value structure that allows for ASN1 using DER.
Definition: asn1.h:124
The RSA public-key cryptosystem.
size_t len
ASN1 length, e.g.
Definition: asn1.h:127
#define POLARSSL_ERR_RSA_BAD_INPUT_DATA
Bad input parameters to function.
Definition: rsa.h:46
#define POLARSSL_ERR_RSA_PRIVATE_FAILED
The private key operation failed.
Definition: rsa.h:51
void rsa_set_padding(rsa_context *ctx, int padding, int hash_id)
Set padding for an already initialized RSA context See rsa_init() for details.
#define POLARSSL_MD_MAX_SIZE
Definition: md.h:67
SHA-1 cryptographic hash function.
#define POLARSSL_ERR_RSA_KEY_CHECK_FAILED
Key failed to pass the libraries validity check.
Definition: rsa.h:49
int mpi_copy(mpi *X, const mpi *Y)
Copy the contents of Y into X.
int rsa_gen_key(rsa_context *ctx, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng, unsigned int nbits, int exponent)
Generate an RSA keypair.
void rsa_init(rsa_context *ctx, int padding, int hash_id)
Initialize an RSA context.
int mpi_mod_mpi(mpi *R, const mpi *A, const mpi *B)
Modulo: R = A mod B.
int(* polarssl_mutex_free)(threading_mutex_t *mutex)
int oid_get_oid_by_md(md_type_t md_alg, const char **oid, size_t *olen)
Translate md_type into hash algorithm OID.
int asn1_get_tag(unsigned char **p, const unsigned char *end, size_t *len, int tag)
Get the tag and length of the tag.
int mpi_write_binary(const mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, big endian.
int size
Output length of the digest function.
Definition: md.h:82
#define ASN1_OCTET_STRING
Definition: asn1.h:78
#define POLARSSL_ERR_RSA_KEY_GEN_FAILED
Something failed during generation of a key.
Definition: rsa.h:48
int md_finish(md_context_t *ctx, unsigned char *output)
Generic message digest final digest.
int mpi_mul_mpi(mpi *X, const mpi *A, const mpi *B)
Baseline multiplication: X = A * B.
#define POLARSSL_ERR_RSA_PUBLIC_FAILED
The public key operation failed.
Definition: rsa.h:50
int mpi_sub_mpi(mpi *X, const mpi *A, const mpi *B)
Signed subtraction: X = A - B.
mpi RN
Definition: rsa.h:98
int ver
Definition: rsa.h:85
int mpi_sub_int(mpi *X, const mpi *A, t_sint b)
Signed subtraction: X = A - b.
Message digest information.
Definition: md.h:74
int md_update(md_context_t *ctx, const unsigned char *input, size_t ilen)
Generic message digest process buffer.
Generic message digest context.
Definition: md.h:132
#define MPI_CHK(f)
Definition: bignum.h:65
int rsa_public(rsa_context *ctx, const unsigned char *input, unsigned char *output)
Do an RSA public key operation.