PolarSSL v1.3.3
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  msb = mpi_msb( &ctx->N ) - 1;
915 
916  // Generate salt of length slen
917  //
918  if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
919  return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
920 
921  // Note: EMSA-PSS encoding is over the length of N - 1 bits
922  //
923  msb = mpi_msb( &ctx->N ) - 1;
924  p += olen - hlen * 2 - 2;
925  *p++ = 0x01;
926  memcpy( p, salt, slen );
927  p += slen;
928 
929  md_init_ctx( &md_ctx, md_info );
930 
931  // Generate H = Hash( M' )
932  //
933  md_starts( &md_ctx );
934  md_update( &md_ctx, p, 8 );
935  md_update( &md_ctx, hash, hashlen );
936  md_update( &md_ctx, salt, slen );
937  md_finish( &md_ctx, p );
938 
939  // Compensate for boundary condition when applying mask
940  //
941  if( msb % 8 == 0 )
942  offset = 1;
943 
944  // maskedDB: Apply dbMask to DB
945  //
946  mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
947 
948  md_free_ctx( &md_ctx );
949 
950  msb = mpi_msb( &ctx->N ) - 1;
951  sig[0] &= 0xFF >> ( olen * 8 - msb );
952 
953  p += hlen;
954  *p++ = 0xBC;
955 
956  return( ( mode == RSA_PUBLIC )
957  ? rsa_public( ctx, sig, sig )
958  : rsa_private( ctx, f_rng, p_rng, sig, sig ) );
959 }
960 #endif /* POLARSSL_PKCS1_V21 */
961 
962 #if defined(POLARSSL_PKCS1_V15)
963 /*
964  * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
965  */
966 /*
967  * Do an RSA operation to sign the message digest
968  */
970  int (*f_rng)(void *, unsigned char *, size_t),
971  void *p_rng,
972  int mode,
973  md_type_t md_alg,
974  unsigned int hashlen,
975  const unsigned char *hash,
976  unsigned char *sig )
977 {
978  size_t nb_pad, olen, oid_size = 0;
979  unsigned char *p = sig;
980  const char *oid;
981 
982  if( ctx->padding != RSA_PKCS_V15 )
984 
985  olen = ctx->len;
986  nb_pad = olen - 3;
987 
988  if( md_alg != POLARSSL_MD_NONE )
989  {
990  const md_info_t *md_info = md_info_from_type( md_alg );
991  if( md_info == NULL )
993 
994  if( oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
996 
997  nb_pad -= 10 + oid_size;
998 
999  hashlen = md_get_size( md_info );
1000  }
1001 
1002  nb_pad -= hashlen;
1003 
1004  if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
1006 
1007  *p++ = 0;
1008  *p++ = RSA_SIGN;
1009  memset( p, 0xFF, nb_pad );
1010  p += nb_pad;
1011  *p++ = 0;
1012 
1013  if( md_alg == POLARSSL_MD_NONE )
1014  {
1015  memcpy( p, hash, hashlen );
1016  }
1017  else
1018  {
1019  /*
1020  * DigestInfo ::= SEQUENCE {
1021  * digestAlgorithm DigestAlgorithmIdentifier,
1022  * digest Digest }
1023  *
1024  * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1025  *
1026  * Digest ::= OCTET STRING
1027  */
1029  *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
1031  *p++ = (unsigned char) ( 0x04 + oid_size );
1032  *p++ = ASN1_OID;
1033  *p++ = oid_size & 0xFF;
1034  memcpy( p, oid, oid_size );
1035  p += oid_size;
1036  *p++ = ASN1_NULL;
1037  *p++ = 0x00;
1038  *p++ = ASN1_OCTET_STRING;
1039  *p++ = hashlen;
1040  memcpy( p, hash, hashlen );
1041  }
1042 
1043  return( ( mode == RSA_PUBLIC )
1044  ? rsa_public( ctx, sig, sig )
1045  : rsa_private( ctx, f_rng, p_rng, sig, sig ) );
1046 }
1047 #endif /* POLARSSL_PKCS1_V15 */
1048 
1049 /*
1050  * Do an RSA operation to sign the message digest
1051  */
1052 int rsa_pkcs1_sign( rsa_context *ctx,
1053  int (*f_rng)(void *, unsigned char *, size_t),
1054  void *p_rng,
1055  int mode,
1056  md_type_t md_alg,
1057  unsigned int hashlen,
1058  const unsigned char *hash,
1059  unsigned char *sig )
1060 {
1061  switch( ctx->padding )
1062  {
1063 #if defined(POLARSSL_PKCS1_V15)
1064  case RSA_PKCS_V15:
1065  return rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
1066  hashlen, hash, sig );
1067 #endif
1068 
1069 #if defined(POLARSSL_PKCS1_V21)
1070  case RSA_PKCS_V21:
1071  return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
1072  hashlen, hash, sig );
1073 #endif
1074 
1075  default:
1077  }
1078 }
1079 
1080 #if defined(POLARSSL_PKCS1_V21)
1081 /*
1082  * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1083  */
1085  int (*f_rng)(void *, unsigned char *, size_t),
1086  void *p_rng,
1087  int mode,
1088  md_type_t md_alg,
1089  unsigned int hashlen,
1090  const unsigned char *hash,
1091  const unsigned char *sig )
1092 {
1093  int ret;
1094  size_t siglen;
1095  unsigned char *p;
1096  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
1097  unsigned char result[POLARSSL_MD_MAX_SIZE];
1098  unsigned char zeros[8];
1099  unsigned int hlen;
1100  size_t slen, msb;
1101  const md_info_t *md_info;
1102  md_context_t md_ctx;
1103 
1104  if( ctx->padding != RSA_PKCS_V21 )
1106 
1107  siglen = ctx->len;
1108 
1109  if( siglen < 16 || siglen > sizeof( buf ) )
1111 
1112  ret = ( mode == RSA_PUBLIC )
1113  ? rsa_public( ctx, sig, buf )
1114  : rsa_private( ctx, f_rng, p_rng, sig, buf );
1115 
1116  if( ret != 0 )
1117  return( ret );
1118 
1119  p = buf;
1120 
1121  if( buf[siglen - 1] != 0xBC )
1123 
1124  if( md_alg != POLARSSL_MD_NONE )
1125  {
1126  // Gather length of hash to sign
1127  //
1128  md_info = md_info_from_type( md_alg );
1129  if( md_info == NULL )
1131 
1132  hashlen = md_get_size( md_info );
1133  }
1134 
1135  md_info = md_info_from_type( ctx->hash_id );
1136  if( md_info == NULL )
1138 
1139  hlen = md_get_size( md_info );
1140  slen = siglen - hlen - 1;
1141 
1142  memset( zeros, 0, 8 );
1143 
1144  // Note: EMSA-PSS verification is over the length of N - 1 bits
1145  //
1146  msb = mpi_msb( &ctx->N ) - 1;
1147 
1148  // Compensate for boundary condition when applying mask
1149  //
1150  if( msb % 8 == 0 )
1151  {
1152  p++;
1153  siglen -= 1;
1154  }
1155  if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1157 
1158  md_init_ctx( &md_ctx, md_info );
1159 
1160  mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
1161 
1162  buf[0] &= 0xFF >> ( siglen * 8 - msb );
1163 
1164  while( p < buf + siglen && *p == 0 )
1165  p++;
1166 
1167  if( p == buf + siglen ||
1168  *p++ != 0x01 )
1169  {
1170  md_free_ctx( &md_ctx );
1172  }
1173 
1174  slen -= p - buf;
1175 
1176  // Generate H = Hash( M' )
1177  //
1178  md_starts( &md_ctx );
1179  md_update( &md_ctx, zeros, 8 );
1180  md_update( &md_ctx, hash, hashlen );
1181  md_update( &md_ctx, p, slen );
1182  md_finish( &md_ctx, result );
1183 
1184  md_free_ctx( &md_ctx );
1185 
1186  if( memcmp( p + slen, result, hlen ) == 0 )
1187  return( 0 );
1188  else
1190 }
1191 #endif /* POLARSSL_PKCS1_V21 */
1192 
1193 #if defined(POLARSSL_PKCS1_V15)
1194 /*
1195  * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1196  */
1198  int (*f_rng)(void *, unsigned char *, size_t),
1199  void *p_rng,
1200  int mode,
1201  md_type_t md_alg,
1202  unsigned int hashlen,
1203  const unsigned char *hash,
1204  const unsigned char *sig )
1205 {
1206  int ret;
1207  size_t len, siglen, asn1_len;
1208  unsigned char *p, *end;
1209  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
1210  md_type_t msg_md_alg;
1211  const md_info_t *md_info;
1212  asn1_buf oid;
1213 
1214  if( ctx->padding != RSA_PKCS_V15 )
1216 
1217  siglen = ctx->len;
1218 
1219  if( siglen < 16 || siglen > sizeof( buf ) )
1221 
1222  ret = ( mode == RSA_PUBLIC )
1223  ? rsa_public( ctx, sig, buf )
1224  : rsa_private( ctx, f_rng, p_rng, sig, buf );
1225 
1226  if( ret != 0 )
1227  return( ret );
1228 
1229  p = buf;
1230 
1231  if( *p++ != 0 || *p++ != RSA_SIGN )
1233 
1234  while( *p != 0 )
1235  {
1236  if( p >= buf + siglen - 1 || *p != 0xFF )
1238  p++;
1239  }
1240  p++;
1241 
1242  len = siglen - ( p - buf );
1243 
1244  if( len == hashlen && md_alg == POLARSSL_MD_NONE )
1245  {
1246  if( memcmp( p, hash, hashlen ) == 0 )
1247  return( 0 );
1248  else
1250  }
1251 
1252  md_info = md_info_from_type( md_alg );
1253  if( md_info == NULL )
1255  hashlen = md_get_size( md_info );
1256 
1257  end = p + len;
1258 
1259  // Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1260  //
1261  if( ( ret = asn1_get_tag( &p, end, &asn1_len,
1262  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1264 
1265  if( asn1_len + 2 != len )
1267 
1268  if( ( ret = asn1_get_tag( &p, end, &asn1_len,
1269  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1271 
1272  if( asn1_len + 6 + hashlen != len )
1274 
1275  if( ( ret = asn1_get_tag( &p, end, &oid.len, ASN1_OID ) ) != 0 )
1277 
1278  oid.p = p;
1279  p += oid.len;
1280 
1281  if( oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1283 
1284  if( md_alg != msg_md_alg )
1286 
1287  /*
1288  * assume the algorithm parameters must be NULL
1289  */
1290  if( ( ret = asn1_get_tag( &p, end, &asn1_len, ASN1_NULL ) ) != 0 )
1292 
1293  if( ( ret = asn1_get_tag( &p, end, &asn1_len, ASN1_OCTET_STRING ) ) != 0 )
1295 
1296  if( asn1_len != hashlen )
1298 
1299  if( memcmp( p, hash, hashlen ) != 0 )
1301 
1302  p += hashlen;
1303 
1304  if( p != end )
1306 
1307  return( 0 );
1308 }
1309 #endif /* POLARSSL_PKCS1_V15 */
1310 
1311 /*
1312  * Do an RSA operation and check the message digest
1313  */
1314 int rsa_pkcs1_verify( rsa_context *ctx,
1315  int (*f_rng)(void *, unsigned char *, size_t),
1316  void *p_rng,
1317  int mode,
1318  md_type_t md_alg,
1319  unsigned int hashlen,
1320  const unsigned char *hash,
1321  const unsigned char *sig )
1322 {
1323  switch( ctx->padding )
1324  {
1325 #if defined(POLARSSL_PKCS1_V15)
1326  case RSA_PKCS_V15:
1327  return rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
1328  hashlen, hash, sig );
1329 #endif
1330 
1331 #if defined(POLARSSL_PKCS1_V21)
1332  case RSA_PKCS_V21:
1333  return rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
1334  hashlen, hash, sig );
1335 #endif
1336 
1337  default:
1339  }
1340 }
1341 
1342 /*
1343  * Copy the components of an RSA key
1344  */
1345 int rsa_copy( rsa_context *dst, const rsa_context *src )
1346 {
1347  int ret;
1348 
1349  dst->ver = src->ver;
1350  dst->len = src->len;
1351 
1352  MPI_CHK( mpi_copy( &dst->N, &src->N ) );
1353  MPI_CHK( mpi_copy( &dst->E, &src->E ) );
1354 
1355  MPI_CHK( mpi_copy( &dst->D, &src->D ) );
1356  MPI_CHK( mpi_copy( &dst->P, &src->P ) );
1357  MPI_CHK( mpi_copy( &dst->Q, &src->Q ) );
1358  MPI_CHK( mpi_copy( &dst->DP, &src->DP ) );
1359  MPI_CHK( mpi_copy( &dst->DQ, &src->DQ ) );
1360  MPI_CHK( mpi_copy( &dst->QP, &src->QP ) );
1361 
1362  MPI_CHK( mpi_copy( &dst->RN, &src->RN ) );
1363  MPI_CHK( mpi_copy( &dst->RP, &src->RP ) );
1364  MPI_CHK( mpi_copy( &dst->RQ, &src->RQ ) );
1365 
1366 #if !defined(POLARSSL_RSA_NO_CRT)
1367  MPI_CHK( mpi_copy( &dst->Vi, &src->Vi ) );
1368  MPI_CHK( mpi_copy( &dst->Vf, &src->Vf ) );
1369 #endif
1370 
1371  dst->padding = src->padding;
1372  dst->hash_id = src->padding;
1373 
1374 cleanup:
1375  if( ret != 0 )
1376  rsa_free( dst );
1377 
1378  return( ret );
1379 }
1380 
1381 /*
1382  * Free the components of an RSA key
1383  */
1384 void rsa_free( rsa_context *ctx )
1385 {
1386 #if !defined(POLARSSL_RSA_NO_CRT)
1387  mpi_free( &ctx->Vi ); mpi_free( &ctx->Vf );
1388 #endif
1389  mpi_free( &ctx->RQ ); mpi_free( &ctx->RP ); mpi_free( &ctx->RN );
1390  mpi_free( &ctx->QP ); mpi_free( &ctx->DQ ); mpi_free( &ctx->DP );
1391  mpi_free( &ctx->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D );
1392  mpi_free( &ctx->E ); mpi_free( &ctx->N );
1393 
1394 #if defined(POLARSSL_THREADING_C)
1395  polarssl_mutex_free( &ctx->mutex );
1396 #endif
1397 }
1398 
1399 #if defined(POLARSSL_SELF_TEST)
1400 
1401 #include "polarssl/sha1.h"
1402 
1403 /*
1404  * Example RSA-1024 keypair, for test purposes
1405  */
1406 #define KEY_LEN 128
1407 
1408 #define RSA_N "9292758453063D803DD603D5E777D788" \
1409  "8ED1D5BF35786190FA2F23EBC0848AEA" \
1410  "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1411  "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1412  "93A89813FBF3C4F8066D2D800F7C38A8" \
1413  "1AE31942917403FF4946B0A83D3D3E05" \
1414  "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1415  "5E94BB77B07507233A0BC7BAC8F90F79"
1416 
1417 #define RSA_E "10001"
1418 
1419 #define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1420  "66CA472BC44D253102F8B4A9D3BFA750" \
1421  "91386C0077937FE33FA3252D28855837" \
1422  "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1423  "DF79C5CE07EE72C7F123142198164234" \
1424  "CABB724CF78B8173B9F880FC86322407" \
1425  "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1426  "071513A1E85B5DFA031F21ECAE91A34D"
1427 
1428 #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1429  "2C01CAD19EA484A87EA4377637E75500" \
1430  "FCB2005C5C7DD6EC4AC023CDA285D796" \
1431  "C3D9E75E1EFC42488BB4F1D13AC30A57"
1432 
1433 #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1434  "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1435  "910E4168387E3C30AA1E00C339A79508" \
1436  "8452DD96A9A5EA5D9DCA68DA636032AF"
1437 
1438 #define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1439  "3C94D22288ACD763FD8E5600ED4A702D" \
1440  "F84198A5F06C2E72236AE490C93F07F8" \
1441  "3CC559CD27BC2D1CA488811730BB5725"
1442 
1443 #define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1444  "D8AAEA56749EA28623272E4F7D0592AF" \
1445  "7C1F1313CAC9471B5C523BFE592F517B" \
1446  "407A1BD76C164B93DA2D32A383E58357"
1447 
1448 #define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1449  "F38D18D2B2F0E2DD275AA977E2BF4411" \
1450  "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1451  "A74206CEC169D74BF5A8C50D6F48EA08"
1452 
1453 #define PT_LEN 24
1454 #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1455  "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1456 
1457 #if defined(POLARSSL_PKCS1_V15)
1458 static int myrand( void *rng_state, unsigned char *output, size_t len )
1459 {
1460  size_t i;
1461 
1462  if( rng_state != NULL )
1463  rng_state = NULL;
1464 
1465  for( i = 0; i < len; ++i )
1466  output[i] = rand();
1467 
1468  return( 0 );
1469 }
1470 #endif
1471 
1472 /*
1473  * Checkup routine
1474  */
1475 int rsa_self_test( int verbose )
1476 {
1477 #if defined(POLARSSL_PKCS1_V15)
1478  size_t len;
1479  rsa_context rsa;
1480  unsigned char rsa_plaintext[PT_LEN];
1481  unsigned char rsa_decrypted[PT_LEN];
1482  unsigned char rsa_ciphertext[KEY_LEN];
1483 #if defined(POLARSSL_SHA1_C)
1484  unsigned char sha1sum[20];
1485 #endif
1486 
1487  rsa_init( &rsa, RSA_PKCS_V15, 0 );
1488 
1489  rsa.len = KEY_LEN;
1490  mpi_read_string( &rsa.N , 16, RSA_N );
1491  mpi_read_string( &rsa.E , 16, RSA_E );
1492  mpi_read_string( &rsa.D , 16, RSA_D );
1493  mpi_read_string( &rsa.P , 16, RSA_P );
1494  mpi_read_string( &rsa.Q , 16, RSA_Q );
1495  mpi_read_string( &rsa.DP, 16, RSA_DP );
1496  mpi_read_string( &rsa.DQ, 16, RSA_DQ );
1497  mpi_read_string( &rsa.QP, 16, RSA_QP );
1498 
1499  if( verbose != 0 )
1500  printf( " RSA key validation: " );
1501 
1502  if( rsa_check_pubkey( &rsa ) != 0 ||
1503  rsa_check_privkey( &rsa ) != 0 )
1504  {
1505  if( verbose != 0 )
1506  printf( "failed\n" );
1507 
1508  return( 1 );
1509  }
1510 
1511  if( verbose != 0 )
1512  printf( "passed\n PKCS#1 encryption : " );
1513 
1514  memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1515 
1516  if( rsa_pkcs1_encrypt( &rsa, myrand, NULL, RSA_PUBLIC, PT_LEN,
1517  rsa_plaintext, rsa_ciphertext ) != 0 )
1518  {
1519  if( verbose != 0 )
1520  printf( "failed\n" );
1521 
1522  return( 1 );
1523  }
1524 
1525  if( verbose != 0 )
1526  printf( "passed\n PKCS#1 decryption : " );
1527 
1528  if( rsa_pkcs1_decrypt( &rsa, myrand, NULL, RSA_PRIVATE, &len,
1529  rsa_ciphertext, rsa_decrypted,
1530  sizeof(rsa_decrypted) ) != 0 )
1531  {
1532  if( verbose != 0 )
1533  printf( "failed\n" );
1534 
1535  return( 1 );
1536  }
1537 
1538  if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1539  {
1540  if( verbose != 0 )
1541  printf( "failed\n" );
1542 
1543  return( 1 );
1544  }
1545 
1546 #if defined(POLARSSL_SHA1_C)
1547  if( verbose != 0 )
1548  printf( "passed\n PKCS#1 data sign : " );
1549 
1550  sha1( rsa_plaintext, PT_LEN, sha1sum );
1551 
1552  if( rsa_pkcs1_sign( &rsa, myrand, NULL, RSA_PRIVATE, POLARSSL_MD_SHA1, 0,
1553  sha1sum, rsa_ciphertext ) != 0 )
1554  {
1555  if( verbose != 0 )
1556  printf( "failed\n" );
1557 
1558  return( 1 );
1559  }
1560 
1561  if( verbose != 0 )
1562  printf( "passed\n PKCS#1 sig. verify: " );
1563 
1564  if( rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC, POLARSSL_MD_SHA1, 0,
1565  sha1sum, rsa_ciphertext ) != 0 )
1566  {
1567  if( verbose != 0 )
1568  printf( "failed\n" );
1569 
1570  return( 1 );
1571  }
1572 
1573  if( verbose != 0 )
1574  printf( "passed\n\n" );
1575 #endif /* POLARSSL_SHA1_C */
1576 
1577  rsa_free( &rsa );
1578 #else /* POLARSSL_PKCS1_V15 */
1579  ((void) verbose);
1580 #endif /* POLARSSL_PKCS1_V15 */
1581  return( 0 );
1582 }
1583 
1584 #endif
1585 
1586 #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:205
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:131
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:66
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:81
#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:73
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:129
#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.