PolarSSL v1.3.7
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( ctx->padding != RSA_PKCS_V21 || f_rng == NULL )
510 
511  md_info = md_info_from_type( ctx->hash_id );
512  if( md_info == NULL )
514 
515  olen = ctx->len;
516  hlen = md_get_size( md_info );
517 
518  if( olen < ilen + 2 * hlen + 2 || f_rng == NULL )
520 
521  memset( output, 0, olen );
522 
523  *p++ = 0;
524 
525  // Generate a random octet string seed
526  //
527  if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
528  return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
529 
530  p += hlen;
531 
532  // Construct DB
533  //
534  md( md_info, label, label_len, p );
535  p += hlen;
536  p += olen - 2 * hlen - 2 - ilen;
537  *p++ = 1;
538  memcpy( p, input, ilen );
539 
540  md_init_ctx( &md_ctx, md_info );
541 
542  // maskedDB: Apply dbMask to DB
543  //
544  mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
545  &md_ctx );
546 
547  // maskedSeed: Apply seedMask to seed
548  //
549  mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
550  &md_ctx );
551 
552  md_free_ctx( &md_ctx );
553 
554  return( ( mode == RSA_PUBLIC )
555  ? rsa_public( ctx, output, output )
556  : rsa_private( ctx, f_rng, p_rng, output, output ) );
557 }
558 #endif /* POLARSSL_PKCS1_V21 */
559 
560 #if defined(POLARSSL_PKCS1_V15)
561 /*
562  * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
563  */
565  int (*f_rng)(void *, unsigned char *, size_t),
566  void *p_rng,
567  int mode, size_t ilen,
568  const unsigned char *input,
569  unsigned char *output )
570 {
571  size_t nb_pad, olen;
572  int ret;
573  unsigned char *p = output;
574 
575  if( ctx->padding != RSA_PKCS_V15 || f_rng == NULL )
577 
578  olen = ctx->len;
579 
580  if( olen < ilen + 11 )
582 
583  nb_pad = olen - 3 - ilen;
584 
585  *p++ = 0;
586  if( mode == RSA_PUBLIC )
587  {
588  *p++ = RSA_CRYPT;
589 
590  while( nb_pad-- > 0 )
591  {
592  int rng_dl = 100;
593 
594  do {
595  ret = f_rng( p_rng, p, 1 );
596  } while( *p == 0 && --rng_dl && ret == 0 );
597 
598  // Check if RNG failed to generate data
599  //
600  if( rng_dl == 0 || ret != 0)
601  return POLARSSL_ERR_RSA_RNG_FAILED + ret;
602 
603  p++;
604  }
605  }
606  else
607  {
608  *p++ = RSA_SIGN;
609 
610  while( nb_pad-- > 0 )
611  *p++ = 0xFF;
612  }
613 
614  *p++ = 0;
615  memcpy( p, input, ilen );
616 
617  return( ( mode == RSA_PUBLIC )
618  ? rsa_public( ctx, output, output )
619  : rsa_private( ctx, f_rng, p_rng, output, output ) );
620 }
621 #endif /* POLARSSL_PKCS1_V15 */
622 
623 /*
624  * Add the message padding, then do an RSA operation
625  */
627  int (*f_rng)(void *, unsigned char *, size_t),
628  void *p_rng,
629  int mode, size_t ilen,
630  const unsigned char *input,
631  unsigned char *output )
632 {
633  switch( ctx->padding )
634  {
635 #if defined(POLARSSL_PKCS1_V15)
636  case RSA_PKCS_V15:
637  return rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
638  input, output );
639 #endif
640 
641 #if defined(POLARSSL_PKCS1_V21)
642  case RSA_PKCS_V21:
643  return rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
644  ilen, input, output );
645 #endif
646 
647  default:
649  }
650 }
651 
652 #if defined(POLARSSL_PKCS1_V21)
653 /*
654  * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
655  */
657  int (*f_rng)(void *, unsigned char *, size_t),
658  void *p_rng,
659  int mode,
660  const unsigned char *label, size_t label_len,
661  size_t *olen,
662  const unsigned char *input,
663  unsigned char *output,
664  size_t output_max_len )
665 {
666  int ret;
667  size_t ilen, i, pad_len;
668  unsigned char *p, bad, pad_done;
669  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
670  unsigned char lhash[POLARSSL_MD_MAX_SIZE];
671  unsigned int hlen;
672  const md_info_t *md_info;
673  md_context_t md_ctx;
674 
675  /*
676  * Parameters sanity checks
677  */
678  if( ctx->padding != RSA_PKCS_V21 )
680 
681  ilen = ctx->len;
682 
683  if( ilen < 16 || ilen > sizeof( buf ) )
685 
686  md_info = md_info_from_type( ctx->hash_id );
687  if( md_info == NULL )
689 
690  /*
691  * RSA operation
692  */
693  ret = ( mode == RSA_PUBLIC )
694  ? rsa_public( ctx, input, buf )
695  : rsa_private( ctx, f_rng, p_rng, input, buf );
696 
697  if( ret != 0 )
698  return( ret );
699 
700  /*
701  * Unmask data and generate lHash
702  */
703  hlen = md_get_size( md_info );
704 
705  md_init_ctx( &md_ctx, md_info );
706 
707  /* Generate lHash */
708  md( md_info, label, label_len, lhash );
709 
710  /* seed: Apply seedMask to maskedSeed */
711  mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
712  &md_ctx );
713 
714  /* DB: Apply dbMask to maskedDB */
715  mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
716  &md_ctx );
717 
718  md_free_ctx( &md_ctx );
719 
720  /*
721  * Check contents, in "constant-time"
722  */
723  p = buf;
724  bad = 0;
725 
726  bad |= *p++; /* First byte must be 0 */
727 
728  p += hlen; /* Skip seed */
729 
730  /* Check lHash */
731  for( i = 0; i < hlen; i++ )
732  bad |= lhash[i] ^ *p++;
733 
734  /* Get zero-padding len, but always read till end of buffer
735  * (minus one, for the 01 byte) */
736  pad_len = 0;
737  pad_done = 0;
738  for( i = 0; i < ilen - 2 * hlen - 2; i++ )
739  {
740  pad_done |= p[i];
741  pad_len += ( pad_done == 0 );
742  }
743 
744  p += pad_len;
745  bad |= *p++ ^ 0x01;
746 
747  /*
748  * The only information "leaked" is whether the padding was correct or not
749  * (eg, no data is copied if it was not correct). This meets the
750  * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
751  * the different error conditions.
752  */
753  if( bad != 0 )
755 
756  if (ilen - (p - buf) > output_max_len)
758 
759  *olen = ilen - (p - buf);
760  memcpy( output, p, *olen );
761 
762  return( 0 );
763 }
764 #endif /* POLARSSL_PKCS1_V21 */
765 
766 #if defined(POLARSSL_PKCS1_V15)
767 /*
768  * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
769  */
771  int (*f_rng)(void *, unsigned char *, size_t),
772  void *p_rng,
773  int mode, size_t *olen,
774  const unsigned char *input,
775  unsigned char *output,
776  size_t output_max_len)
777 {
778  int ret;
779  size_t ilen, pad_count = 0, i;
780  unsigned char *p, bad, pad_done = 0;
781  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
782 
783  if( ctx->padding != RSA_PKCS_V15 )
785 
786  ilen = ctx->len;
787 
788  if( ilen < 16 || ilen > sizeof( buf ) )
790 
791  ret = ( mode == RSA_PUBLIC )
792  ? rsa_public( ctx, input, buf )
793  : rsa_private( ctx, f_rng, p_rng, input, buf );
794 
795  if( ret != 0 )
796  return( ret );
797 
798  p = buf;
799  bad = 0;
800 
801  /*
802  * Check and get padding len in "constant-time"
803  */
804  bad |= *p++; /* First byte must be 0 */
805 
806  /* This test does not depend on secret data */
807  if( mode == RSA_PRIVATE )
808  {
809  bad |= *p++ ^ RSA_CRYPT;
810 
811  /* Get padding len, but always read till end of buffer
812  * (minus one, for the 00 byte) */
813  for( i = 0; i < ilen - 3; i++ )
814  {
815  pad_done |= ( p[i] == 0 );
816  pad_count += ( pad_done == 0 );
817  }
818 
819  p += pad_count;
820  bad |= *p++; /* Must be zero */
821  }
822  else
823  {
824  bad |= *p++ ^ RSA_SIGN;
825 
826  /* Get padding len, but always read till end of buffer
827  * (minus one, for the 00 byte) */
828  for( i = 0; i < ilen - 3; i++ )
829  {
830  pad_done |= ( p[i] != 0xFF );
831  pad_count += ( pad_done == 0 );
832  }
833 
834  p += pad_count;
835  bad |= *p++; /* Must be zero */
836  }
837 
838  if( bad )
840 
841  if (ilen - (p - buf) > output_max_len)
843 
844  *olen = ilen - (p - buf);
845  memcpy( output, p, *olen );
846 
847  return( 0 );
848 }
849 #endif /* POLARSSL_PKCS1_V15 */
850 
851 /*
852  * Do an RSA operation, then remove the message padding
853  */
855  int (*f_rng)(void *, unsigned char *, size_t),
856  void *p_rng,
857  int mode, size_t *olen,
858  const unsigned char *input,
859  unsigned char *output,
860  size_t output_max_len)
861 {
862  switch( ctx->padding )
863  {
864 #if defined(POLARSSL_PKCS1_V15)
865  case RSA_PKCS_V15:
866  return rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
867  input, output, output_max_len );
868 #endif
869 
870 #if defined(POLARSSL_PKCS1_V21)
871  case RSA_PKCS_V21:
872  return rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
873  olen, input, output,
874  output_max_len );
875 #endif
876 
877  default:
879  }
880 }
881 
882 #if defined(POLARSSL_PKCS1_V21)
883 /*
884  * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
885  */
887  int (*f_rng)(void *, unsigned char *, size_t),
888  void *p_rng,
889  int mode,
890  md_type_t md_alg,
891  unsigned int hashlen,
892  const unsigned char *hash,
893  unsigned char *sig )
894 {
895  size_t olen;
896  unsigned char *p = sig;
897  unsigned char salt[POLARSSL_MD_MAX_SIZE];
898  unsigned int slen, hlen, offset = 0;
899  int ret;
900  size_t msb;
901  const md_info_t *md_info;
902  md_context_t md_ctx;
903 
904  if( ctx->padding != RSA_PKCS_V21 || f_rng == NULL )
906 
907  olen = ctx->len;
908 
909  if( md_alg != POLARSSL_MD_NONE )
910  {
911  // Gather length of hash to sign
912  //
913  md_info = md_info_from_type( md_alg );
914  if( md_info == NULL )
916 
917  hashlen = md_get_size( md_info );
918  }
919 
920  md_info = md_info_from_type( ctx->hash_id );
921  if( md_info == NULL )
923 
924  hlen = md_get_size( md_info );
925  slen = hlen;
926 
927  if( olen < hlen + slen + 2 )
929 
930  memset( sig, 0, olen );
931 
932  // Generate salt of length slen
933  //
934  if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
935  return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
936 
937  // Note: EMSA-PSS encoding is over the length of N - 1 bits
938  //
939  msb = mpi_msb( &ctx->N ) - 1;
940  p += olen - hlen * 2 - 2;
941  *p++ = 0x01;
942  memcpy( p, salt, slen );
943  p += slen;
944 
945  md_init_ctx( &md_ctx, md_info );
946 
947  // Generate H = Hash( M' )
948  //
949  md_starts( &md_ctx );
950  md_update( &md_ctx, p, 8 );
951  md_update( &md_ctx, hash, hashlen );
952  md_update( &md_ctx, salt, slen );
953  md_finish( &md_ctx, p );
954 
955  // Compensate for boundary condition when applying mask
956  //
957  if( msb % 8 == 0 )
958  offset = 1;
959 
960  // maskedDB: Apply dbMask to DB
961  //
962  mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
963 
964  md_free_ctx( &md_ctx );
965 
966  msb = mpi_msb( &ctx->N ) - 1;
967  sig[0] &= 0xFF >> ( olen * 8 - msb );
968 
969  p += hlen;
970  *p++ = 0xBC;
971 
972  return( ( mode == RSA_PUBLIC )
973  ? rsa_public( ctx, sig, sig )
974  : rsa_private( ctx, f_rng, p_rng, sig, sig ) );
975 }
976 #endif /* POLARSSL_PKCS1_V21 */
977 
978 #if defined(POLARSSL_PKCS1_V15)
979 /*
980  * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
981  */
982 /*
983  * Do an RSA operation to sign the message digest
984  */
986  int (*f_rng)(void *, unsigned char *, size_t),
987  void *p_rng,
988  int mode,
989  md_type_t md_alg,
990  unsigned int hashlen,
991  const unsigned char *hash,
992  unsigned char *sig )
993 {
994  size_t nb_pad, olen, oid_size = 0;
995  unsigned char *p = sig;
996  const char *oid;
997 
998  if( ctx->padding != RSA_PKCS_V15 )
1000 
1001  olen = ctx->len;
1002  nb_pad = olen - 3;
1003 
1004  if( md_alg != POLARSSL_MD_NONE )
1005  {
1006  const md_info_t *md_info = md_info_from_type( md_alg );
1007  if( md_info == NULL )
1009 
1010  if( oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1012 
1013  nb_pad -= 10 + oid_size;
1014 
1015  hashlen = md_get_size( md_info );
1016  }
1017 
1018  nb_pad -= hashlen;
1019 
1020  if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
1022 
1023  *p++ = 0;
1024  *p++ = RSA_SIGN;
1025  memset( p, 0xFF, nb_pad );
1026  p += nb_pad;
1027  *p++ = 0;
1028 
1029  if( md_alg == POLARSSL_MD_NONE )
1030  {
1031  memcpy( p, hash, hashlen );
1032  }
1033  else
1034  {
1035  /*
1036  * DigestInfo ::= SEQUENCE {
1037  * digestAlgorithm DigestAlgorithmIdentifier,
1038  * digest Digest }
1039  *
1040  * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1041  *
1042  * Digest ::= OCTET STRING
1043  */
1045  *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
1047  *p++ = (unsigned char) ( 0x04 + oid_size );
1048  *p++ = ASN1_OID;
1049  *p++ = oid_size & 0xFF;
1050  memcpy( p, oid, oid_size );
1051  p += oid_size;
1052  *p++ = ASN1_NULL;
1053  *p++ = 0x00;
1054  *p++ = ASN1_OCTET_STRING;
1055  *p++ = hashlen;
1056  memcpy( p, hash, hashlen );
1057  }
1058 
1059  return( ( mode == RSA_PUBLIC )
1060  ? rsa_public( ctx, sig, sig )
1061  : rsa_private( ctx, f_rng, p_rng, sig, sig ) );
1062 }
1063 #endif /* POLARSSL_PKCS1_V15 */
1064 
1065 /*
1066  * Do an RSA operation to sign the message digest
1067  */
1068 int rsa_pkcs1_sign( rsa_context *ctx,
1069  int (*f_rng)(void *, unsigned char *, size_t),
1070  void *p_rng,
1071  int mode,
1072  md_type_t md_alg,
1073  unsigned int hashlen,
1074  const unsigned char *hash,
1075  unsigned char *sig )
1076 {
1077  switch( ctx->padding )
1078  {
1079 #if defined(POLARSSL_PKCS1_V15)
1080  case RSA_PKCS_V15:
1081  return rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
1082  hashlen, hash, sig );
1083 #endif
1084 
1085 #if defined(POLARSSL_PKCS1_V21)
1086  case RSA_PKCS_V21:
1087  return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
1088  hashlen, hash, sig );
1089 #endif
1090 
1091  default:
1093  }
1094 }
1095 
1096 #if defined(POLARSSL_PKCS1_V21)
1097 /*
1098  * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1099  */
1101  int (*f_rng)(void *, unsigned char *, size_t),
1102  void *p_rng,
1103  int mode,
1104  md_type_t md_alg,
1105  unsigned int hashlen,
1106  const unsigned char *hash,
1107  const unsigned char *sig )
1108 {
1109  int ret;
1110  size_t siglen;
1111  unsigned char *p;
1112  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
1113  unsigned char result[POLARSSL_MD_MAX_SIZE];
1114  unsigned char zeros[8];
1115  unsigned int hlen;
1116  size_t slen, msb;
1117  const md_info_t *md_info;
1118  md_context_t md_ctx;
1119 
1120  if( ctx->padding != RSA_PKCS_V21 )
1122 
1123  siglen = ctx->len;
1124 
1125  if( siglen < 16 || siglen > sizeof( buf ) )
1127 
1128  ret = ( mode == RSA_PUBLIC )
1129  ? rsa_public( ctx, sig, buf )
1130  : rsa_private( ctx, f_rng, p_rng, sig, buf );
1131 
1132  if( ret != 0 )
1133  return( ret );
1134 
1135  p = buf;
1136 
1137  if( buf[siglen - 1] != 0xBC )
1139 
1140  if( md_alg != POLARSSL_MD_NONE )
1141  {
1142  // Gather length of hash to sign
1143  //
1144  md_info = md_info_from_type( md_alg );
1145  if( md_info == NULL )
1147 
1148  hashlen = md_get_size( md_info );
1149  }
1150 
1151  md_info = md_info_from_type( ctx->hash_id );
1152  if( md_info == NULL )
1154 
1155  hlen = md_get_size( md_info );
1156  slen = siglen - hlen - 1;
1157 
1158  memset( zeros, 0, 8 );
1159 
1160  // Note: EMSA-PSS verification is over the length of N - 1 bits
1161  //
1162  msb = mpi_msb( &ctx->N ) - 1;
1163 
1164  // Compensate for boundary condition when applying mask
1165  //
1166  if( msb % 8 == 0 )
1167  {
1168  p++;
1169  siglen -= 1;
1170  }
1171  if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1173 
1174  md_init_ctx( &md_ctx, md_info );
1175 
1176  mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
1177 
1178  buf[0] &= 0xFF >> ( siglen * 8 - msb );
1179 
1180  while( p < buf + siglen && *p == 0 )
1181  p++;
1182 
1183  if( p == buf + siglen ||
1184  *p++ != 0x01 )
1185  {
1186  md_free_ctx( &md_ctx );
1188  }
1189 
1190  slen -= p - buf;
1191 
1192  // Generate H = Hash( M' )
1193  //
1194  md_starts( &md_ctx );
1195  md_update( &md_ctx, zeros, 8 );
1196  md_update( &md_ctx, hash, hashlen );
1197  md_update( &md_ctx, p, slen );
1198  md_finish( &md_ctx, result );
1199 
1200  md_free_ctx( &md_ctx );
1201 
1202  if( memcmp( p + slen, result, hlen ) == 0 )
1203  return( 0 );
1204  else
1206 }
1207 #endif /* POLARSSL_PKCS1_V21 */
1208 
1209 #if defined(POLARSSL_PKCS1_V15)
1210 /*
1211  * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1212  */
1214  int (*f_rng)(void *, unsigned char *, size_t),
1215  void *p_rng,
1216  int mode,
1217  md_type_t md_alg,
1218  unsigned int hashlen,
1219  const unsigned char *hash,
1220  const unsigned char *sig )
1221 {
1222  int ret;
1223  size_t len, siglen, asn1_len;
1224  unsigned char *p, *end;
1225  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
1226  md_type_t msg_md_alg;
1227  const md_info_t *md_info;
1228  asn1_buf oid;
1229 
1230  if( ctx->padding != RSA_PKCS_V15 )
1232 
1233  siglen = ctx->len;
1234 
1235  if( siglen < 16 || siglen > sizeof( buf ) )
1237 
1238  ret = ( mode == RSA_PUBLIC )
1239  ? rsa_public( ctx, sig, buf )
1240  : rsa_private( ctx, f_rng, p_rng, sig, buf );
1241 
1242  if( ret != 0 )
1243  return( ret );
1244 
1245  p = buf;
1246 
1247  if( *p++ != 0 || *p++ != RSA_SIGN )
1249 
1250  while( *p != 0 )
1251  {
1252  if( p >= buf + siglen - 1 || *p != 0xFF )
1254  p++;
1255  }
1256  p++;
1257 
1258  len = siglen - ( p - buf );
1259 
1260  if( len == hashlen && md_alg == POLARSSL_MD_NONE )
1261  {
1262  if( memcmp( p, hash, hashlen ) == 0 )
1263  return( 0 );
1264  else
1266  }
1267 
1268  md_info = md_info_from_type( md_alg );
1269  if( md_info == NULL )
1271  hashlen = md_get_size( md_info );
1272 
1273  end = p + len;
1274 
1275  // Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1276  //
1277  if( ( ret = asn1_get_tag( &p, end, &asn1_len,
1278  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1280 
1281  if( asn1_len + 2 != len )
1283 
1284  if( ( ret = asn1_get_tag( &p, end, &asn1_len,
1285  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1287 
1288  if( asn1_len + 6 + hashlen != len )
1290 
1291  if( ( ret = asn1_get_tag( &p, end, &oid.len, ASN1_OID ) ) != 0 )
1293 
1294  oid.p = p;
1295  p += oid.len;
1296 
1297  if( oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1299 
1300  if( md_alg != msg_md_alg )
1302 
1303  /*
1304  * assume the algorithm parameters must be NULL
1305  */
1306  if( ( ret = asn1_get_tag( &p, end, &asn1_len, ASN1_NULL ) ) != 0 )
1308 
1309  if( ( ret = asn1_get_tag( &p, end, &asn1_len, ASN1_OCTET_STRING ) ) != 0 )
1311 
1312  if( asn1_len != hashlen )
1314 
1315  if( memcmp( p, hash, hashlen ) != 0 )
1317 
1318  p += hashlen;
1319 
1320  if( p != end )
1322 
1323  return( 0 );
1324 }
1325 #endif /* POLARSSL_PKCS1_V15 */
1326 
1327 /*
1328  * Do an RSA operation and check the message digest
1329  */
1330 int rsa_pkcs1_verify( rsa_context *ctx,
1331  int (*f_rng)(void *, unsigned char *, size_t),
1332  void *p_rng,
1333  int mode,
1334  md_type_t md_alg,
1335  unsigned int hashlen,
1336  const unsigned char *hash,
1337  const unsigned char *sig )
1338 {
1339  switch( ctx->padding )
1340  {
1341 #if defined(POLARSSL_PKCS1_V15)
1342  case RSA_PKCS_V15:
1343  return rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
1344  hashlen, hash, sig );
1345 #endif
1346 
1347 #if defined(POLARSSL_PKCS1_V21)
1348  case RSA_PKCS_V21:
1349  return rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
1350  hashlen, hash, sig );
1351 #endif
1352 
1353  default:
1355  }
1356 }
1357 
1358 /*
1359  * Copy the components of an RSA key
1360  */
1361 int rsa_copy( rsa_context *dst, const rsa_context *src )
1362 {
1363  int ret;
1364 
1365  dst->ver = src->ver;
1366  dst->len = src->len;
1367 
1368  MPI_CHK( mpi_copy( &dst->N, &src->N ) );
1369  MPI_CHK( mpi_copy( &dst->E, &src->E ) );
1370 
1371  MPI_CHK( mpi_copy( &dst->D, &src->D ) );
1372  MPI_CHK( mpi_copy( &dst->P, &src->P ) );
1373  MPI_CHK( mpi_copy( &dst->Q, &src->Q ) );
1374  MPI_CHK( mpi_copy( &dst->DP, &src->DP ) );
1375  MPI_CHK( mpi_copy( &dst->DQ, &src->DQ ) );
1376  MPI_CHK( mpi_copy( &dst->QP, &src->QP ) );
1377 
1378  MPI_CHK( mpi_copy( &dst->RN, &src->RN ) );
1379  MPI_CHK( mpi_copy( &dst->RP, &src->RP ) );
1380  MPI_CHK( mpi_copy( &dst->RQ, &src->RQ ) );
1381 
1382 #if !defined(POLARSSL_RSA_NO_CRT)
1383  MPI_CHK( mpi_copy( &dst->Vi, &src->Vi ) );
1384  MPI_CHK( mpi_copy( &dst->Vf, &src->Vf ) );
1385 #endif
1386 
1387  dst->padding = src->padding;
1388  dst->hash_id = src->hash_id;
1389 
1390 cleanup:
1391  if( ret != 0 )
1392  rsa_free( dst );
1393 
1394  return( ret );
1395 }
1396 
1397 /*
1398  * Free the components of an RSA key
1399  */
1400 void rsa_free( rsa_context *ctx )
1401 {
1402 #if !defined(POLARSSL_RSA_NO_CRT)
1403  mpi_free( &ctx->Vi ); mpi_free( &ctx->Vf );
1404 #endif
1405  mpi_free( &ctx->RQ ); mpi_free( &ctx->RP ); mpi_free( &ctx->RN );
1406  mpi_free( &ctx->QP ); mpi_free( &ctx->DQ ); mpi_free( &ctx->DP );
1407  mpi_free( &ctx->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D );
1408  mpi_free( &ctx->E ); mpi_free( &ctx->N );
1409 
1410 #if defined(POLARSSL_THREADING_C)
1411  polarssl_mutex_free( &ctx->mutex );
1412 #endif
1413 }
1414 
1415 #if defined(POLARSSL_SELF_TEST)
1416 
1417 #include "polarssl/sha1.h"
1418 
1419 /*
1420  * Example RSA-1024 keypair, for test purposes
1421  */
1422 #define KEY_LEN 128
1423 
1424 #define RSA_N "9292758453063D803DD603D5E777D788" \
1425  "8ED1D5BF35786190FA2F23EBC0848AEA" \
1426  "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1427  "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1428  "93A89813FBF3C4F8066D2D800F7C38A8" \
1429  "1AE31942917403FF4946B0A83D3D3E05" \
1430  "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1431  "5E94BB77B07507233A0BC7BAC8F90F79"
1432 
1433 #define RSA_E "10001"
1434 
1435 #define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1436  "66CA472BC44D253102F8B4A9D3BFA750" \
1437  "91386C0077937FE33FA3252D28855837" \
1438  "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1439  "DF79C5CE07EE72C7F123142198164234" \
1440  "CABB724CF78B8173B9F880FC86322407" \
1441  "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1442  "071513A1E85B5DFA031F21ECAE91A34D"
1443 
1444 #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1445  "2C01CAD19EA484A87EA4377637E75500" \
1446  "FCB2005C5C7DD6EC4AC023CDA285D796" \
1447  "C3D9E75E1EFC42488BB4F1D13AC30A57"
1448 
1449 #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1450  "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1451  "910E4168387E3C30AA1E00C339A79508" \
1452  "8452DD96A9A5EA5D9DCA68DA636032AF"
1453 
1454 #define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1455  "3C94D22288ACD763FD8E5600ED4A702D" \
1456  "F84198A5F06C2E72236AE490C93F07F8" \
1457  "3CC559CD27BC2D1CA488811730BB5725"
1458 
1459 #define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1460  "D8AAEA56749EA28623272E4F7D0592AF" \
1461  "7C1F1313CAC9471B5C523BFE592F517B" \
1462  "407A1BD76C164B93DA2D32A383E58357"
1463 
1464 #define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1465  "F38D18D2B2F0E2DD275AA977E2BF4411" \
1466  "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1467  "A74206CEC169D74BF5A8C50D6F48EA08"
1468 
1469 #define PT_LEN 24
1470 #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1471  "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1472 
1473 #if defined(POLARSSL_PKCS1_V15)
1474 static int myrand( void *rng_state, unsigned char *output, size_t len )
1475 {
1476 #if !defined(__OpenBSD__)
1477  size_t i;
1478 
1479  if( rng_state != NULL )
1480  rng_state = NULL;
1481 
1482  for( i = 0; i < len; ++i )
1483  output[i] = rand();
1484 #else
1485  if( rng_state != NULL )
1486  rng_state = NULL;
1487 
1488  arc4random_buf( output, len );
1489 #endif /* !OpenBSD */
1490 
1491  return( 0 );
1492 }
1493 #endif /* POLARSSL_PKCS1_V15 */
1494 
1495 /*
1496  * Checkup routine
1497  */
1498 int rsa_self_test( int verbose )
1499 {
1500  int ret = 0;
1501 #if defined(POLARSSL_PKCS1_V15)
1502  size_t len;
1503  rsa_context rsa;
1504  unsigned char rsa_plaintext[PT_LEN];
1505  unsigned char rsa_decrypted[PT_LEN];
1506  unsigned char rsa_ciphertext[KEY_LEN];
1507 #if defined(POLARSSL_SHA1_C)
1508  unsigned char sha1sum[20];
1509 #endif
1510 
1511  rsa_init( &rsa, RSA_PKCS_V15, 0 );
1512 
1513  rsa.len = KEY_LEN;
1514  MPI_CHK( mpi_read_string( &rsa.N , 16, RSA_N ) );
1515  MPI_CHK( mpi_read_string( &rsa.E , 16, RSA_E ) );
1516  MPI_CHK( mpi_read_string( &rsa.D , 16, RSA_D ) );
1517  MPI_CHK( mpi_read_string( &rsa.P , 16, RSA_P ) );
1518  MPI_CHK( mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1519  MPI_CHK( mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1520  MPI_CHK( mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1521  MPI_CHK( mpi_read_string( &rsa.QP, 16, RSA_QP ) );
1522 
1523  if( verbose != 0 )
1524  polarssl_printf( " RSA key validation: " );
1525 
1526  if( rsa_check_pubkey( &rsa ) != 0 ||
1527  rsa_check_privkey( &rsa ) != 0 )
1528  {
1529  if( verbose != 0 )
1530  polarssl_printf( "failed\n" );
1531 
1532  return( 1 );
1533  }
1534 
1535  if( verbose != 0 )
1536  polarssl_printf( "passed\n PKCS#1 encryption : " );
1537 
1538  memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1539 
1540  if( rsa_pkcs1_encrypt( &rsa, myrand, NULL, RSA_PUBLIC, PT_LEN,
1541  rsa_plaintext, rsa_ciphertext ) != 0 )
1542  {
1543  if( verbose != 0 )
1544  polarssl_printf( "failed\n" );
1545 
1546  return( 1 );
1547  }
1548 
1549  if( verbose != 0 )
1550  polarssl_printf( "passed\n PKCS#1 decryption : " );
1551 
1552  if( rsa_pkcs1_decrypt( &rsa, myrand, NULL, RSA_PRIVATE, &len,
1553  rsa_ciphertext, rsa_decrypted,
1554  sizeof(rsa_decrypted) ) != 0 )
1555  {
1556  if( verbose != 0 )
1557  polarssl_printf( "failed\n" );
1558 
1559  return( 1 );
1560  }
1561 
1562  if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1563  {
1564  if( verbose != 0 )
1565  polarssl_printf( "failed\n" );
1566 
1567  return( 1 );
1568  }
1569 
1570 #if defined(POLARSSL_SHA1_C)
1571  if( verbose != 0 )
1572  polarssl_printf( "passed\n PKCS#1 data sign : " );
1573 
1574  sha1( rsa_plaintext, PT_LEN, sha1sum );
1575 
1576  if( rsa_pkcs1_sign( &rsa, myrand, NULL, RSA_PRIVATE, POLARSSL_MD_SHA1, 0,
1577  sha1sum, rsa_ciphertext ) != 0 )
1578  {
1579  if( verbose != 0 )
1580  polarssl_printf( "failed\n" );
1581 
1582  return( 1 );
1583  }
1584 
1585  if( verbose != 0 )
1586  polarssl_printf( "passed\n PKCS#1 sig. verify: " );
1587 
1588  if( rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC, POLARSSL_MD_SHA1, 0,
1589  sha1sum, rsa_ciphertext ) != 0 )
1590  {
1591  if( verbose != 0 )
1592  polarssl_printf( "failed\n" );
1593 
1594  return( 1 );
1595  }
1596 
1597  if( verbose != 0 )
1598  polarssl_printf( "passed\n\n" );
1599 #endif /* POLARSSL_SHA1_C */
1600 
1601 cleanup:
1602  rsa_free( &rsa );
1603 #else /* POLARSSL_PKCS1_V15 */
1604  ((void) verbose);
1605 #endif /* POLARSSL_PKCS1_V15 */
1606  return( ret );
1607 }
1608 
1609 #endif /* POLARSSL_SELF_TEST */
1610 
1611 #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
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(* polarssl_mutex_lock)(threading_mutex_t *mutex)
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)
#define POLARSSL_MPI_MAX_SIZE
Maximum number of bytes for usable MPIs.
Definition: bignum.h:91
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:105
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.
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:102
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
#define RSA_PKCS_V21
Definition: rsa.h:63
#define ASN1_SEQUENCE
Definition: asn1.h:82
mpi DQ
Definition: rsa.h:93
Configuration options (set of defines)
int rsa_check_pubkey(const rsa_context *ctx)
Check a public RSA key.
mpi RP
Definition: rsa.h:97
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)
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:208
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:181
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:84
md_type_t
Definition: md.h:51
mpi P
Definition: rsa.h:90
mpi Vi
Definition: rsa.h:101
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.
mpi Q
Definition: rsa.h:91
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
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:81
mpi D
Definition: rsa.h:89
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:94
#define RSA_PKCS_V15
Definition: rsa.h:62
#define RSA_PRIVATE
Definition: rsa.h:60
mpi N
Definition: rsa.h:86
unsigned char * p
ASN1 data, e.g.
Definition: asn1.h:124
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.
#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:98
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:87
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:92
#define POLARSSL_ERR_RSA_VERIFY_FAILED
The PKCS#1 verification failed.
Definition: rsa.h:52
int(* polarssl_mutex_free)(threading_mutex_t *mutex)
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:107
size_t mpi_msb(const mpi *X)
Return the number of bits up to and including the most significant &#39;1&#39; bit&#39;.
#define POLARSSL_MPI_MAX_BITS
Maximum number of bits for usable MPIs.
Definition: bignum.h:95
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:185
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:120
The RSA public-key cryptosystem.
size_t len
ASN1 length, e.g.
Definition: asn1.h:123
#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.
#define POLARSSL_MD_MAX_SIZE
Definition: md.h:67
int(* polarssl_mutex_unlock)(threading_mutex_t *mutex)
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 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.
#define polarssl_printf
Definition: platform.h:109
int mpi_write_binary(const mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, big endian.
int(* polarssl_mutex_init)(threading_mutex_t *mutex)
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.
int md_free_ctx(md_context_t *ctx)
Free the message-specific context of ctx.
mpi RN
Definition: rsa.h:96
int ver
Definition: rsa.h:83
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.