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