PolarSSL v1.3.2
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;
650  unsigned char *p;
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  if( ctx->padding != RSA_PKCS_V21 )
659 
660  ilen = ctx->len;
661 
662  if( ilen < 16 || ilen > sizeof( buf ) )
664 
665  ret = ( mode == RSA_PUBLIC )
666  ? rsa_public( ctx, input, buf )
667  : rsa_private( ctx, f_rng, p_rng, input, buf );
668 
669  if( ret != 0 )
670  return( ret );
671 
672  p = buf;
673 
674  if( *p++ != 0 )
676 
677  md_info = md_info_from_type( ctx->hash_id );
678  if( md_info == NULL )
680 
681  hlen = md_get_size( md_info );
682 
683  md_init_ctx( &md_ctx, md_info );
684 
685  // Generate lHash
686  //
687  md( md_info, label, label_len, lhash );
688 
689  // seed: Apply seedMask to maskedSeed
690  //
691  mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
692  &md_ctx );
693 
694  // DB: Apply dbMask to maskedDB
695  //
696  mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
697  &md_ctx );
698 
699  p += hlen;
700  md_free_ctx( &md_ctx );
701 
702  // Check validity
703  //
704  if( memcmp( lhash, p, hlen ) != 0 )
706 
707  p += hlen;
708 
709  while( *p == 0 && p < buf + ilen )
710  p++;
711 
712  if( p == buf + ilen )
714 
715  if( *p++ != 0x01 )
717 
718  if (ilen - (p - buf) > output_max_len)
720 
721  *olen = ilen - (p - buf);
722  memcpy( output, p, *olen );
723 
724  return( 0 );
725 }
726 #endif /* POLARSSL_PKCS1_V21 */
727 
728 #if defined(POLARSSL_PKCS1_V15)
729 /*
730  * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
731  */
733  int (*f_rng)(void *, unsigned char *, size_t),
734  void *p_rng,
735  int mode, size_t *olen,
736  const unsigned char *input,
737  unsigned char *output,
738  size_t output_max_len)
739 {
740  int ret, correct = 1;
741  size_t ilen, pad_count = 0;
742  unsigned char *p, *q;
743  unsigned char bt;
744  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
745 
746  if( ctx->padding != RSA_PKCS_V15 )
748 
749  ilen = ctx->len;
750 
751  if( ilen < 16 || ilen > sizeof( buf ) )
753 
754  ret = ( mode == RSA_PUBLIC )
755  ? rsa_public( ctx, input, buf )
756  : rsa_private( ctx, f_rng, p_rng, input, buf );
757 
758  if( ret != 0 )
759  return( ret );
760 
761  p = buf;
762 
763  if( *p++ != 0 )
764  correct = 0;
765 
766  bt = *p++;
767  if( ( bt != RSA_CRYPT && mode == RSA_PRIVATE ) ||
768  ( bt != RSA_SIGN && mode == RSA_PUBLIC ) )
769  {
770  correct = 0;
771  }
772 
773  if( bt == RSA_CRYPT )
774  {
775  while( *p != 0 && p < buf + ilen - 1 )
776  pad_count += ( *p++ != 0 );
777 
778  correct &= ( *p == 0 && p < buf + ilen - 1 );
779 
780  q = p;
781 
782  // Also pass over all other bytes to reduce timing differences
783  //
784  while ( q < buf + ilen - 1 )
785  pad_count += ( *q++ != 0 );
786 
787  // Prevent compiler optimization of pad_count
788  //
789  correct |= pad_count & 0x100000; /* Always 0 unless 1M bit keys */
790  p++;
791  }
792  else
793  {
794  while( *p == 0xFF && p < buf + ilen - 1 )
795  pad_count += ( *p++ == 0xFF );
796 
797  correct &= ( *p == 0 && p < buf + ilen - 1 );
798 
799  q = p;
800 
801  // Also pass over all other bytes to reduce timing differences
802  //
803  while ( q < buf + ilen - 1 )
804  pad_count += ( *q++ != 0 );
805 
806  // Prevent compiler optimization of pad_count
807  //
808  correct |= pad_count & 0x100000; /* Always 0 unless 1M bit keys */
809  p++;
810  }
811 
812  if( correct == 0 )
814 
815  if (ilen - (p - buf) > output_max_len)
817 
818  *olen = ilen - (p - buf);
819  memcpy( output, p, *olen );
820 
821  return( 0 );
822 }
823 #endif /* POLARSSL_PKCS1_V15 */
824 
825 /*
826  * Do an RSA operation, then remove the message padding
827  */
829  int (*f_rng)(void *, unsigned char *, size_t),
830  void *p_rng,
831  int mode, size_t *olen,
832  const unsigned char *input,
833  unsigned char *output,
834  size_t output_max_len)
835 {
836  switch( ctx->padding )
837  {
838 #if defined(POLARSSL_PKCS1_V15)
839  case RSA_PKCS_V15:
840  return rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
841  input, output, output_max_len );
842 #endif
843 
844 #if defined(POLARSSL_PKCS1_V21)
845  case RSA_PKCS_V21:
846  return rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
847  olen, input, output,
848  output_max_len );
849 #endif
850 
851  default:
853  }
854 }
855 
856 #if defined(POLARSSL_PKCS1_V21)
857 /*
858  * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
859  */
861  int (*f_rng)(void *, unsigned char *, size_t),
862  void *p_rng,
863  int mode,
864  md_type_t md_alg,
865  unsigned int hashlen,
866  const unsigned char *hash,
867  unsigned char *sig )
868 {
869  size_t olen;
870  unsigned char *p = sig;
871  unsigned char salt[POLARSSL_MD_MAX_SIZE];
872  unsigned int slen, hlen, offset = 0;
873  int ret;
874  size_t msb;
875  const md_info_t *md_info;
876  md_context_t md_ctx;
877 
878  if( ctx->padding != RSA_PKCS_V21 || f_rng == NULL )
880 
881  olen = ctx->len;
882 
883  if( md_alg != POLARSSL_MD_NONE )
884  {
885  // Gather length of hash to sign
886  //
887  md_info = md_info_from_type( md_alg );
888  if( md_info == NULL )
890 
891  hashlen = md_get_size( md_info );
892  }
893 
894  md_info = md_info_from_type( ctx->hash_id );
895  if( md_info == NULL )
897 
898  hlen = md_get_size( md_info );
899  slen = hlen;
900 
901  if( olen < hlen + slen + 2 )
903 
904  memset( sig, 0, olen );
905 
906  msb = mpi_msb( &ctx->N ) - 1;
907 
908  // Generate salt of length slen
909  //
910  if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
911  return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
912 
913  // Note: EMSA-PSS encoding is over the length of N - 1 bits
914  //
915  msb = mpi_msb( &ctx->N ) - 1;
916  p += olen - hlen * 2 - 2;
917  *p++ = 0x01;
918  memcpy( p, salt, slen );
919  p += slen;
920 
921  md_init_ctx( &md_ctx, md_info );
922 
923  // Generate H = Hash( M' )
924  //
925  md_starts( &md_ctx );
926  md_update( &md_ctx, p, 8 );
927  md_update( &md_ctx, hash, hashlen );
928  md_update( &md_ctx, salt, slen );
929  md_finish( &md_ctx, p );
930 
931  // Compensate for boundary condition when applying mask
932  //
933  if( msb % 8 == 0 )
934  offset = 1;
935 
936  // maskedDB: Apply dbMask to DB
937  //
938  mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
939 
940  md_free_ctx( &md_ctx );
941 
942  msb = mpi_msb( &ctx->N ) - 1;
943  sig[0] &= 0xFF >> ( olen * 8 - msb );
944 
945  p += hlen;
946  *p++ = 0xBC;
947 
948  return( ( mode == RSA_PUBLIC )
949  ? rsa_public( ctx, sig, sig )
950  : rsa_private( ctx, f_rng, p_rng, sig, sig ) );
951 }
952 #endif /* POLARSSL_PKCS1_V21 */
953 
954 #if defined(POLARSSL_PKCS1_V15)
955 /*
956  * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
957  */
958 /*
959  * Do an RSA operation to sign the message digest
960  */
962  int (*f_rng)(void *, unsigned char *, size_t),
963  void *p_rng,
964  int mode,
965  md_type_t md_alg,
966  unsigned int hashlen,
967  const unsigned char *hash,
968  unsigned char *sig )
969 {
970  size_t nb_pad, olen, oid_size = 0;
971  unsigned char *p = sig;
972  const char *oid;
973 
974  if( ctx->padding != RSA_PKCS_V15 )
976 
977  olen = ctx->len;
978  nb_pad = olen - 3;
979 
980  if( md_alg != POLARSSL_MD_NONE )
981  {
982  const md_info_t *md_info = md_info_from_type( md_alg );
983  if( md_info == NULL )
985 
986  if( oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
988 
989  nb_pad -= 10 + oid_size;
990 
991  hashlen = md_get_size( md_info );
992  }
993 
994  nb_pad -= hashlen;
995 
996  if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
998 
999  *p++ = 0;
1000  *p++ = RSA_SIGN;
1001  memset( p, 0xFF, nb_pad );
1002  p += nb_pad;
1003  *p++ = 0;
1004 
1005  if( md_alg == POLARSSL_MD_NONE )
1006  {
1007  memcpy( p, hash, hashlen );
1008  }
1009  else
1010  {
1011  /*
1012  * DigestInfo ::= SEQUENCE {
1013  * digestAlgorithm DigestAlgorithmIdentifier,
1014  * digest Digest }
1015  *
1016  * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1017  *
1018  * Digest ::= OCTET STRING
1019  */
1021  *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
1023  *p++ = (unsigned char) ( 0x04 + oid_size );
1024  *p++ = ASN1_OID;
1025  *p++ = oid_size & 0xFF;
1026  memcpy( p, oid, oid_size );
1027  p += oid_size;
1028  *p++ = ASN1_NULL;
1029  *p++ = 0x00;
1030  *p++ = ASN1_OCTET_STRING;
1031  *p++ = hashlen;
1032  memcpy( p, hash, hashlen );
1033  }
1034 
1035  return( ( mode == RSA_PUBLIC )
1036  ? rsa_public( ctx, sig, sig )
1037  : rsa_private( ctx, f_rng, p_rng, sig, sig ) );
1038 }
1039 #endif /* POLARSSL_PKCS1_V15 */
1040 
1041 /*
1042  * Do an RSA operation to sign the message digest
1043  */
1044 int rsa_pkcs1_sign( rsa_context *ctx,
1045  int (*f_rng)(void *, unsigned char *, size_t),
1046  void *p_rng,
1047  int mode,
1048  md_type_t md_alg,
1049  unsigned int hashlen,
1050  const unsigned char *hash,
1051  unsigned char *sig )
1052 {
1053  switch( ctx->padding )
1054  {
1055 #if defined(POLARSSL_PKCS1_V15)
1056  case RSA_PKCS_V15:
1057  return rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
1058  hashlen, hash, sig );
1059 #endif
1060 
1061 #if defined(POLARSSL_PKCS1_V21)
1062  case RSA_PKCS_V21:
1063  return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
1064  hashlen, hash, sig );
1065 #endif
1066 
1067  default:
1069  }
1070 }
1071 
1072 #if defined(POLARSSL_PKCS1_V21)
1073 /*
1074  * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1075  */
1077  int (*f_rng)(void *, unsigned char *, size_t),
1078  void *p_rng,
1079  int mode,
1080  md_type_t md_alg,
1081  unsigned int hashlen,
1082  const unsigned char *hash,
1083  const unsigned char *sig )
1084 {
1085  int ret;
1086  size_t siglen;
1087  unsigned char *p;
1088  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
1089  unsigned char result[POLARSSL_MD_MAX_SIZE];
1090  unsigned char zeros[8];
1091  unsigned int hlen;
1092  size_t slen, msb;
1093  const md_info_t *md_info;
1094  md_context_t md_ctx;
1095 
1096  if( ctx->padding != RSA_PKCS_V21 )
1098 
1099  siglen = ctx->len;
1100 
1101  if( siglen < 16 || siglen > sizeof( buf ) )
1103 
1104  ret = ( mode == RSA_PUBLIC )
1105  ? rsa_public( ctx, sig, buf )
1106  : rsa_private( ctx, f_rng, p_rng, sig, buf );
1107 
1108  if( ret != 0 )
1109  return( ret );
1110 
1111  p = buf;
1112 
1113  if( buf[siglen - 1] != 0xBC )
1115 
1116  if( md_alg != POLARSSL_MD_NONE )
1117  {
1118  // Gather length of hash to sign
1119  //
1120  md_info = md_info_from_type( md_alg );
1121  if( md_info == NULL )
1123 
1124  hashlen = md_get_size( md_info );
1125  }
1126 
1127  md_info = md_info_from_type( ctx->hash_id );
1128  if( md_info == NULL )
1130 
1131  hlen = md_get_size( md_info );
1132  slen = siglen - hlen - 1;
1133 
1134  memset( zeros, 0, 8 );
1135 
1136  // Note: EMSA-PSS verification is over the length of N - 1 bits
1137  //
1138  msb = mpi_msb( &ctx->N ) - 1;
1139 
1140  // Compensate for boundary condition when applying mask
1141  //
1142  if( msb % 8 == 0 )
1143  {
1144  p++;
1145  siglen -= 1;
1146  }
1147  if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1149 
1150  md_init_ctx( &md_ctx, md_info );
1151 
1152  mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
1153 
1154  buf[0] &= 0xFF >> ( siglen * 8 - msb );
1155 
1156  while( *p == 0 && p < buf + siglen )
1157  p++;
1158 
1159  if( p == buf + siglen ||
1160  *p++ != 0x01 )
1161  {
1162  md_free_ctx( &md_ctx );
1164  }
1165 
1166  slen -= p - buf;
1167 
1168  // Generate H = Hash( M' )
1169  //
1170  md_starts( &md_ctx );
1171  md_update( &md_ctx, zeros, 8 );
1172  md_update( &md_ctx, hash, hashlen );
1173  md_update( &md_ctx, p, slen );
1174  md_finish( &md_ctx, result );
1175 
1176  md_free_ctx( &md_ctx );
1177 
1178  if( memcmp( p + slen, result, hlen ) == 0 )
1179  return( 0 );
1180  else
1182 }
1183 #endif /* POLARSSL_PKCS1_V21 */
1184 
1185 #if defined(POLARSSL_PKCS1_V15)
1186 /*
1187  * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1188  */
1190  int (*f_rng)(void *, unsigned char *, size_t),
1191  void *p_rng,
1192  int mode,
1193  md_type_t md_alg,
1194  unsigned int hashlen,
1195  const unsigned char *hash,
1196  const unsigned char *sig )
1197 {
1198  int ret;
1199  size_t len, siglen, asn1_len;
1200  unsigned char *p, *end;
1201  unsigned char buf[POLARSSL_MPI_MAX_SIZE];
1202  md_type_t msg_md_alg;
1203  const md_info_t *md_info;
1204  asn1_buf oid;
1205 
1206  if( ctx->padding != RSA_PKCS_V15 )
1208 
1209  siglen = ctx->len;
1210 
1211  if( siglen < 16 || siglen > sizeof( buf ) )
1213 
1214  ret = ( mode == RSA_PUBLIC )
1215  ? rsa_public( ctx, sig, buf )
1216  : rsa_private( ctx, f_rng, p_rng, sig, buf );
1217 
1218  if( ret != 0 )
1219  return( ret );
1220 
1221  p = buf;
1222 
1223  if( *p++ != 0 || *p++ != RSA_SIGN )
1225 
1226  while( *p != 0 )
1227  {
1228  if( p >= buf + siglen - 1 || *p != 0xFF )
1230  p++;
1231  }
1232  p++;
1233 
1234  len = siglen - ( p - buf );
1235 
1236  if( len == hashlen && md_alg == POLARSSL_MD_NONE )
1237  {
1238  if( memcmp( p, hash, hashlen ) == 0 )
1239  return( 0 );
1240  else
1242  }
1243 
1244  md_info = md_info_from_type( md_alg );
1245  if( md_info == NULL )
1247  hashlen = md_get_size( md_info );
1248 
1249  end = p + len;
1250 
1251  // Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1252  //
1253  if( ( ret = asn1_get_tag( &p, end, &asn1_len,
1254  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1256 
1257  if( asn1_len + 2 != len )
1259 
1260  if( ( ret = asn1_get_tag( &p, end, &asn1_len,
1261  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1263 
1264  if( asn1_len + 6 + hashlen != len )
1266 
1267  if( ( ret = asn1_get_tag( &p, end, &oid.len, ASN1_OID ) ) != 0 )
1269 
1270  oid.p = p;
1271  p += oid.len;
1272 
1273  if( oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1275 
1276  if( md_alg != msg_md_alg )
1278 
1279  /*
1280  * assume the algorithm parameters must be NULL
1281  */
1282  if( ( ret = asn1_get_tag( &p, end, &asn1_len, ASN1_NULL ) ) != 0 )
1284 
1285  if( ( ret = asn1_get_tag( &p, end, &asn1_len, ASN1_OCTET_STRING ) ) != 0 )
1287 
1288  if( asn1_len != hashlen )
1290 
1291  if( memcmp( p, hash, hashlen ) != 0 )
1293 
1294  p += hashlen;
1295 
1296  if( p != end )
1298 
1299  return( 0 );
1300 }
1301 #endif /* POLARSSL_PKCS1_V15 */
1302 
1303 /*
1304  * Do an RSA operation and check the message digest
1305  */
1306 int rsa_pkcs1_verify( rsa_context *ctx,
1307  int (*f_rng)(void *, unsigned char *, size_t),
1308  void *p_rng,
1309  int mode,
1310  md_type_t md_alg,
1311  unsigned int hashlen,
1312  const unsigned char *hash,
1313  const unsigned char *sig )
1314 {
1315  switch( ctx->padding )
1316  {
1317 #if defined(POLARSSL_PKCS1_V15)
1318  case RSA_PKCS_V15:
1319  return rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
1320  hashlen, hash, sig );
1321 #endif
1322 
1323 #if defined(POLARSSL_PKCS1_V21)
1324  case RSA_PKCS_V21:
1325  return rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
1326  hashlen, hash, sig );
1327 #endif
1328 
1329  default:
1331  }
1332 }
1333 
1334 /*
1335  * Copy the components of an RSA key
1336  */
1337 int rsa_copy( rsa_context *dst, const rsa_context *src )
1338 {
1339  int ret;
1340 
1341  dst->ver = src->ver;
1342  dst->len = src->len;
1343 
1344  MPI_CHK( mpi_copy( &dst->N, &src->N ) );
1345  MPI_CHK( mpi_copy( &dst->E, &src->E ) );
1346 
1347  MPI_CHK( mpi_copy( &dst->D, &src->D ) );
1348  MPI_CHK( mpi_copy( &dst->P, &src->P ) );
1349  MPI_CHK( mpi_copy( &dst->Q, &src->Q ) );
1350  MPI_CHK( mpi_copy( &dst->DP, &src->DP ) );
1351  MPI_CHK( mpi_copy( &dst->DQ, &src->DQ ) );
1352  MPI_CHK( mpi_copy( &dst->QP, &src->QP ) );
1353 
1354  MPI_CHK( mpi_copy( &dst->RN, &src->RN ) );
1355  MPI_CHK( mpi_copy( &dst->RP, &src->RP ) );
1356  MPI_CHK( mpi_copy( &dst->RQ, &src->RQ ) );
1357 
1358 #if !defined(POLARSSL_RSA_NO_CRT)
1359  MPI_CHK( mpi_copy( &dst->Vi, &src->Vi ) );
1360  MPI_CHK( mpi_copy( &dst->Vf, &src->Vf ) );
1361 #endif
1362 
1363  dst->padding = src->padding;
1364  dst->hash_id = src->padding;
1365 
1366 cleanup:
1367  if( ret != 0 )
1368  rsa_free( dst );
1369 
1370  return( ret );
1371 }
1372 
1373 /*
1374  * Free the components of an RSA key
1375  */
1376 void rsa_free( rsa_context *ctx )
1377 {
1378 #if !defined(POLARSSL_RSA_NO_CRT)
1379  mpi_free( &ctx->Vi ); mpi_free( &ctx->Vf );
1380 #endif
1381  mpi_free( &ctx->RQ ); mpi_free( &ctx->RP ); mpi_free( &ctx->RN );
1382  mpi_free( &ctx->QP ); mpi_free( &ctx->DQ ); mpi_free( &ctx->DP );
1383  mpi_free( &ctx->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D );
1384  mpi_free( &ctx->E ); mpi_free( &ctx->N );
1385 
1386 #if defined(POLARSSL_THREADING_C)
1387  polarssl_mutex_free( &ctx->mutex );
1388 #endif
1389 }
1390 
1391 #if defined(POLARSSL_SELF_TEST)
1392 
1393 #include "polarssl/sha1.h"
1394 
1395 /*
1396  * Example RSA-1024 keypair, for test purposes
1397  */
1398 #define KEY_LEN 128
1399 
1400 #define RSA_N "9292758453063D803DD603D5E777D788" \
1401  "8ED1D5BF35786190FA2F23EBC0848AEA" \
1402  "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1403  "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1404  "93A89813FBF3C4F8066D2D800F7C38A8" \
1405  "1AE31942917403FF4946B0A83D3D3E05" \
1406  "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1407  "5E94BB77B07507233A0BC7BAC8F90F79"
1408 
1409 #define RSA_E "10001"
1410 
1411 #define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1412  "66CA472BC44D253102F8B4A9D3BFA750" \
1413  "91386C0077937FE33FA3252D28855837" \
1414  "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1415  "DF79C5CE07EE72C7F123142198164234" \
1416  "CABB724CF78B8173B9F880FC86322407" \
1417  "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1418  "071513A1E85B5DFA031F21ECAE91A34D"
1419 
1420 #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1421  "2C01CAD19EA484A87EA4377637E75500" \
1422  "FCB2005C5C7DD6EC4AC023CDA285D796" \
1423  "C3D9E75E1EFC42488BB4F1D13AC30A57"
1424 
1425 #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1426  "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1427  "910E4168387E3C30AA1E00C339A79508" \
1428  "8452DD96A9A5EA5D9DCA68DA636032AF"
1429 
1430 #define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1431  "3C94D22288ACD763FD8E5600ED4A702D" \
1432  "F84198A5F06C2E72236AE490C93F07F8" \
1433  "3CC559CD27BC2D1CA488811730BB5725"
1434 
1435 #define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1436  "D8AAEA56749EA28623272E4F7D0592AF" \
1437  "7C1F1313CAC9471B5C523BFE592F517B" \
1438  "407A1BD76C164B93DA2D32A383E58357"
1439 
1440 #define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1441  "F38D18D2B2F0E2DD275AA977E2BF4411" \
1442  "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1443  "A74206CEC169D74BF5A8C50D6F48EA08"
1444 
1445 #define PT_LEN 24
1446 #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1447  "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1448 
1449 #if defined(POLARSSL_PCKS1_V15)
1450 static int myrand( void *rng_state, unsigned char *output, size_t len )
1451 {
1452  size_t i;
1453 
1454  if( rng_state != NULL )
1455  rng_state = NULL;
1456 
1457  for( i = 0; i < len; ++i )
1458  output[i] = rand();
1459 
1460  return( 0 );
1461 }
1462 #endif
1463 
1464 /*
1465  * Checkup routine
1466  */
1467 int rsa_self_test( int verbose )
1468 {
1469 #if defined(POLARSSL_PCKS1_V15)
1470  size_t len;
1471  rsa_context rsa;
1472  unsigned char rsa_plaintext[PT_LEN];
1473  unsigned char rsa_decrypted[PT_LEN];
1474  unsigned char rsa_ciphertext[KEY_LEN];
1475 #if defined(POLARSSL_SHA1_C)
1476  unsigned char sha1sum[20];
1477 #endif
1478 
1479  rsa_init( &rsa, RSA_PKCS_V15, 0 );
1480 
1481  rsa.len = KEY_LEN;
1482  mpi_read_string( &rsa.N , 16, RSA_N );
1483  mpi_read_string( &rsa.E , 16, RSA_E );
1484  mpi_read_string( &rsa.D , 16, RSA_D );
1485  mpi_read_string( &rsa.P , 16, RSA_P );
1486  mpi_read_string( &rsa.Q , 16, RSA_Q );
1487  mpi_read_string( &rsa.DP, 16, RSA_DP );
1488  mpi_read_string( &rsa.DQ, 16, RSA_DQ );
1489  mpi_read_string( &rsa.QP, 16, RSA_QP );
1490 
1491  if( verbose != 0 )
1492  printf( " RSA key validation: " );
1493 
1494  if( rsa_check_pubkey( &rsa ) != 0 ||
1495  rsa_check_privkey( &rsa ) != 0 )
1496  {
1497  if( verbose != 0 )
1498  printf( "failed\n" );
1499 
1500  return( 1 );
1501  }
1502 
1503  if( verbose != 0 )
1504  printf( "passed\n PKCS#1 encryption : " );
1505 
1506  memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1507 
1508  if( rsa_pkcs1_encrypt( &rsa, myrand, NULL, RSA_PUBLIC, PT_LEN,
1509  rsa_plaintext, rsa_ciphertext ) != 0 )
1510  {
1511  if( verbose != 0 )
1512  printf( "failed\n" );
1513 
1514  return( 1 );
1515  }
1516 
1517  if( verbose != 0 )
1518  printf( "passed\n PKCS#1 decryption : " );
1519 
1520  if( rsa_pkcs1_decrypt( &rsa, myrand, NULL, RSA_PRIVATE, &len,
1521  rsa_ciphertext, rsa_decrypted,
1522  sizeof(rsa_decrypted) ) != 0 )
1523  {
1524  if( verbose != 0 )
1525  printf( "failed\n" );
1526 
1527  return( 1 );
1528  }
1529 
1530  if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1531  {
1532  if( verbose != 0 )
1533  printf( "failed\n" );
1534 
1535  return( 1 );
1536  }
1537 
1538 #if defined(POLARSSL_SHA1_C)
1539  if( verbose != 0 )
1540  printf( "passed\n PKCS#1 data sign : " );
1541 
1542  sha1( rsa_plaintext, PT_LEN, sha1sum );
1543 
1544  if( rsa_pkcs1_sign( &rsa, myrand, NULL, RSA_PRIVATE, POLARSSL_MD_SHA1, 0,
1545  sha1sum, rsa_ciphertext ) != 0 )
1546  {
1547  if( verbose != 0 )
1548  printf( "failed\n" );
1549 
1550  return( 1 );
1551  }
1552 
1553  if( verbose != 0 )
1554  printf( "passed\n PKCS#1 sig. verify: " );
1555 
1556  if( rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC, POLARSSL_MD_SHA1, 0,
1557  sha1sum, rsa_ciphertext ) != 0 )
1558  {
1559  if( verbose != 0 )
1560  printf( "failed\n" );
1561 
1562  return( 1 );
1563  }
1564 
1565  if( verbose != 0 )
1566  printf( "passed\n\n" );
1567 #endif /* POLARSSL_SHA1_C */
1568 
1569  rsa_free( &rsa );
1570 #else /* POLARSSL_PKCS1_V15 */
1571  ((void) verbose);
1572 #endif /* POLARSSL_PKCS1_V15 */
1573  return( 0 );
1574 }
1575 
1576 #endif
1577 
1578 #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:171
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:175
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.