blob: 577a14fe5e4e0602605c38252260c2cff05c765c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Paul Bakkerb125ed82011-11-10 13:33:51 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Paul Bakker40e46942009-01-03 21:51:57 +000034#if defined(POLARSSL_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Paul Bakker40e46942009-01-03 21:51:57 +000036#include "polarssl/rsa.h"
Paul Bakkerc70b9822013-04-07 22:00:46 +020037#include "polarssl/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000038
39#if defined(POLARSSL_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +000040#include "polarssl/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000041#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000042
43#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000044#include <stdio.h>
45
46/*
47 * Initialize an RSA context
48 */
49void rsa_init( rsa_context *ctx,
50 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000051 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000052{
53 memset( ctx, 0, sizeof( rsa_context ) );
54
55 ctx->padding = padding;
56 ctx->hash_id = hash_id;
Paul Bakkerc9965dc2013-09-29 14:58:17 +020057
58#if defined(POLARSSL_THREADING_C)
59 polarssl_mutex_init( &ctx->mutex );
60#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000061}
62
Paul Bakker40e46942009-01-03 21:51:57 +000063#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000064
65/*
66 * Generate an RSA keypair
67 */
Paul Bakker21eb2802010-08-16 11:10:02 +000068int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +000069 int (*f_rng)(void *, unsigned char *, size_t),
70 void *p_rng,
71 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +000072{
73 int ret;
74 mpi P1, Q1, H, G;
75
Paul Bakker21eb2802010-08-16 11:10:02 +000076 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Paul Bakker40e46942009-01-03 21:51:57 +000077 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Paul Bakker6c591fa2011-05-05 11:49:20 +000079 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +000080
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,
Paul Bakker21eb2802010-08-16 11:10:02 +000090 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92 MPI_CHK( mpi_gen_prime( &ctx->Q, ( nbits + 1 ) >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +000093 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000094
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
125cleanup:
126
Paul Bakker6c591fa2011-05-05 11:49:20 +0000127 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129 if( ret != 0 )
130 {
131 rsa_free( ctx );
Paul Bakker9d781402011-05-09 16:17:09 +0000132 return( POLARSSL_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 }
134
Paul Bakker48377d92013-08-30 12:06:24 +0200135 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136}
137
138#endif
139
140/*
141 * Check a public RSA key
142 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000143int rsa_check_pubkey( const rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000144{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000145 if( !ctx->N.p || !ctx->E.p )
146 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
147
Paul Bakker48377d92013-08-30 12:06:24 +0200148 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 ( ctx->E.p[0] & 1 ) == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000150 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 if( mpi_msb( &ctx->N ) < 128 ||
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000153 mpi_msb( &ctx->N ) > POLARSSL_MPI_MAX_BITS )
Paul Bakker40e46942009-01-03 21:51:57 +0000154 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
156 if( mpi_msb( &ctx->E ) < 2 ||
157 mpi_msb( &ctx->E ) > 64 )
Paul Bakker40e46942009-01-03 21:51:57 +0000158 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
160 return( 0 );
161}
162
163/*
164 * Check a private RSA key
165 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000166int rsa_check_privkey( const rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000167{
168 int ret;
Paul Bakker321df6f2012-09-27 13:21:34 +0000169 mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171 if( ( ret = rsa_check_pubkey( ctx ) ) != 0 )
172 return( ret );
173
Paul Bakker37940d9f2009-07-10 22:38:58 +0000174 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
175 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
176
Paul Bakker6c591fa2011-05-05 11:49:20 +0000177 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 );
Paul Bakker321df6f2012-09-27 13:21:34 +0000179 mpi_init( &L1 ); mpi_init( &L2 ); mpi_init( &DP ); mpi_init( &DQ );
180 mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
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 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
188
Paul Bakkerb572adf2010-07-18 08:29:32 +0000189 MPI_CHK( mpi_gcd( &G2, &P1, &Q1 ) );
Paul Bakker48377d92013-08-30 12:06:24 +0200190 MPI_CHK( mpi_div_mpi( &L1, &L2, &H, &G2 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000191 MPI_CHK( mpi_mod_mpi( &I, &DE, &L1 ) );
192
Paul Bakker321df6f2012-09-27 13:21:34 +0000193 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 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000196 /*
197 * Check for a valid PKCS1v2 private key
198 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000199 if( mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
Paul Bakker321df6f2012-09-27 13:21:34 +0000200 mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
201 mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
202 mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
Paul Bakker6c591fa2011-05-05 11:49:20 +0000203 mpi_cmp_int( &L2, 0 ) != 0 ||
204 mpi_cmp_int( &I, 1 ) != 0 ||
205 mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000207 ret = POLARSSL_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 }
Paul Bakker48377d92013-08-30 12:06:24 +0200209
Paul Bakker5121ce52009-01-03 21:22:43 +0000210cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +0000211 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 );
Paul Bakker321df6f2012-09-27 13:21:34 +0000213 mpi_free( &L1 ); mpi_free( &L2 ); mpi_free( &DP ); mpi_free( &DQ );
214 mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000215
Paul Bakker9d781402011-05-09 16:17:09 +0000216 if( ret == POLARSSL_ERR_RSA_KEY_CHECK_FAILED )
217 return( ret );
218
Paul Bakker6c591fa2011-05-05 11:49:20 +0000219 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000220 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000221
222 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223}
224
225/*
226 * Do an RSA public key operation
227 */
228int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000229 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 unsigned char *output )
231{
Paul Bakker23986e52011-04-24 08:57:21 +0000232 int ret;
233 size_t olen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 mpi T;
235
Paul Bakker6c591fa2011-05-05 11:49:20 +0000236 mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000237
238 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
239
240 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
241 {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000242 mpi_free( &T );
Paul Bakker40e46942009-01-03 21:51:57 +0000243 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 }
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
250cleanup:
251
Paul Bakker6c591fa2011-05-05 11:49:20 +0000252 mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
254 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000255 return( POLARSSL_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
257 return( 0 );
258}
259
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200260#if !defined(POLARSSL_RSA_NO_CRT)
261/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200262 * 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.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200266 */
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200267static int rsa_prepare_blinding( rsa_context *ctx, mpi *Vi, mpi *Vf,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200268 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
269{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200270 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200271
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200272#if defined(POLARSSL_THREADING_C)
273 polarssl_mutex_lock( &ctx->mutex );
274#endif
275
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200276 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 ) );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200280 MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200281 MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
Manuel Pégourié-Gonnard735b8fc2013-09-13 12:57:23 +0200282 MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200283
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200284 goto done;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200285 }
286
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200287 /* 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 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200295
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
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200300done:
301 if( Vi != &ctx->Vi )
302 {
303 MPI_CHK( mpi_copy( Vi, &ctx->Vi ) );
304 MPI_CHK( mpi_copy( Vf, &ctx->Vf ) );
305 }
306
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200307cleanup:
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200308#if defined(POLARSSL_THREADING_C)
309 polarssl_mutex_unlock( &ctx->mutex );
310#endif
311
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200312 return( ret );
313}
314#endif
315
Paul Bakker5121ce52009-01-03 21:22:43 +0000316/*
317 * Do an RSA private key operation
318 */
319int rsa_private( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200320 int (*f_rng)(void *, unsigned char *, size_t),
321 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000322 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 unsigned char *output )
324{
Paul Bakker23986e52011-04-24 08:57:21 +0000325 int ret;
326 size_t olen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 mpi T, T1, T2;
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200328#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
Paul Bakker5121ce52009-01-03 21:22:43 +0000347
Paul Bakker6c591fa2011-05-05 11:49:20 +0000348 mpi_init( &T ); mpi_init( &T1 ); mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
350 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
352 {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000353 mpi_free( &T );
Paul Bakker40e46942009-01-03 21:51:57 +0000354 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 }
356
Paul Bakker0216cc12011-03-26 13:40:23 +0000357#if defined(POLARSSL_RSA_NO_CRT)
Manuel Pégourié-Gonnard971f8b82013-10-04 14:10:43 +0200358 ((void) f_rng);
359 ((void) p_rng);
Paul Bakker5121ce52009-01-03 21:22:43 +0000360 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
361#else
Paul Bakkerf451bac2013-08-30 15:37:02 +0200362 if( f_rng != NULL )
363 {
364 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200365 * Blinding
366 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200367 */
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200368 MPI_CHK( rsa_prepare_blinding( ctx, Vi, Vf, f_rng, p_rng ) );
369 MPI_CHK( mpi_mul_mpi( &T, &T, Vi ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200370 MPI_CHK( mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200371 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200372
373 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 * 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 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200390 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 */
392 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200393 MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) );
Paul Bakkeraab30c12013-08-30 11:00:25 +0200394
Paul Bakkerf451bac2013-08-30 15:37:02 +0200395 if( f_rng != NULL )
396 {
397 /*
398 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200399 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200400 */
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200401 MPI_CHK( mpi_mul_mpi( &T, &T, Vf ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200402 MPI_CHK( mpi_mod_mpi( &T, &T, &ctx->N ) );
403 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000404#endif
405
406 olen = ctx->len;
407 MPI_CHK( mpi_write_binary( &T, output, olen ) );
408
409cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +0000410 mpi_free( &T ); mpi_free( &T1 ); mpi_free( &T2 );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200411#if !defined(POLARSSL_RSA_NO_CRT) && defined(POLARSSL_THREADING_C)
412 mpi_free( &Vi_copy ); mpi_free( &Vf_copy );
413#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000416 return( POLARSSL_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000417
418 return( 0 );
419}
420
Paul Bakker9dcc3222011-03-08 14:16:06 +0000421#if defined(POLARSSL_PKCS1_V21)
422/**
423 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
424 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000425 * \param dst buffer to mask
426 * \param dlen length of destination buffer
427 * \param src source of the mask generation
428 * \param slen length of the source buffer
429 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000430 */
Paul Bakker48377d92013-08-30 12:06:24 +0200431static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
432 size_t slen, md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000433{
434 unsigned char mask[POLARSSL_MD_MAX_SIZE];
435 unsigned char counter[4];
436 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000437 unsigned int hlen;
438 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000439
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
Paul Bakkerb3869132013-02-28 17:21:01 +0100470#if defined(POLARSSL_PKCS1_V21)
471/*
472 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
473 */
474int rsa_rsaes_oaep_encrypt( rsa_context *ctx,
475 int (*f_rng)(void *, unsigned char *, size_t),
476 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100477 int mode,
478 const unsigned char *label, size_t label_len,
479 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100480 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 )
491 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
492
493 md_info = md_info_from_type( ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100494 if( md_info == NULL )
495 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
496
497 olen = ctx->len;
498 hlen = md_get_size( md_info );
499
500 if( olen < ilen + 2 * hlen + 2 || f_rng == NULL )
501 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
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 //
Paul Bakkera43231c2013-02-28 17:33:49 +0100516 md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100517 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 )
Paul Bakker548957d2013-08-30 10:30:02 +0200538 : rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100539}
540#endif /* POLARSSL_PKCS1_V21 */
541
Paul Bakker48377d92013-08-30 12:06:24 +0200542#if defined(POLARSSL_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100543/*
544 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
545 */
546int rsa_rsaes_pkcs1_v15_encrypt( rsa_context *ctx,
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 )
558 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
559
560 olen = ctx->len;
561
562 if( olen < ilen + 11 )
563 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
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 )
Paul Bakker548957d2013-08-30 10:30:02 +0200601 : rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100602}
Paul Bakker48377d92013-08-30 12:06:24 +0200603#endif /* POLARSSL_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100604
Paul Bakker5121ce52009-01-03 21:22:43 +0000605/*
606 * Add the message padding, then do an RSA operation
607 */
608int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000609 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000610 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000611 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000612 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 unsigned char *output )
614{
Paul Bakker5121ce52009-01-03 21:22:43 +0000615 switch( ctx->padding )
616 {
Paul Bakker48377d92013-08-30 12:06:24 +0200617#if defined(POLARSSL_PKCS1_V15)
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 case RSA_PKCS_V15:
Paul Bakkerb3869132013-02-28 17:21:01 +0100619 return rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
620 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200621#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
Paul Bakker9dcc3222011-03-08 14:16:06 +0000623#if defined(POLARSSL_PKCS1_V21)
624 case RSA_PKCS_V21:
Paul Bakkerb3869132013-02-28 17:21:01 +0100625 return rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
626 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000627#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000628
629 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000630 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000631 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000632}
633
Paul Bakkerb3869132013-02-28 17:21:01 +0100634#if defined(POLARSSL_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000635/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100636 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100638int rsa_rsaes_oaep_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200639 int (*f_rng)(void *, unsigned char *, size_t),
640 void *p_rng,
641 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100642 const unsigned char *label, size_t label_len,
643 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100644 const unsigned char *input,
645 unsigned char *output,
646 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000647{
Paul Bakker23986e52011-04-24 08:57:21 +0000648 int ret;
649 size_t ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 unsigned char *p;
Paul Bakker0be82f22012-10-03 20:36:33 +0000651 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000652 unsigned char lhash[POLARSSL_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000653 unsigned int hlen;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000654 const md_info_t *md_info;
655 md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100656
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100657 /*
658 * Parameters sanity checks
659 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100660 if( ctx->padding != RSA_PKCS_V21 )
661 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000662
663 ilen = ctx->len;
664
Paul Bakker27fdf462011-06-09 13:55:13 +0000665 if( ilen < 16 || ilen > sizeof( buf ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000666 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100668 md_info = md_info_from_type( ctx->hash_id );
669 if( md_info == NULL )
670 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
671
672 /*
673 * RSA operation
674 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 ret = ( mode == RSA_PUBLIC )
676 ? rsa_public( ctx, input, buf )
Paul Bakker548957d2013-08-30 10:30:02 +0200677 : rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000678
679 if( ret != 0 )
680 return( ret );
681
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100682 /*
683 * Unmask data
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
704 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000705 p = buf;
706
Paul Bakkerb3869132013-02-28 17:21:01 +0100707 if( *p++ != 0 )
708 return( POLARSSL_ERR_RSA_INVALID_PADDING );
709
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100710 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100711
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100712 /* Check lHash */
Paul Bakkerb3869132013-02-28 17:21:01 +0100713 if( memcmp( lhash, p, hlen ) != 0 )
714 return( POLARSSL_ERR_RSA_INVALID_PADDING );
715
716 p += hlen;
717
718 while( *p == 0 && p < buf + ilen )
719 p++;
720
721 if( p == buf + ilen )
722 return( POLARSSL_ERR_RSA_INVALID_PADDING );
723
724 if( *p++ != 0x01 )
725 return( POLARSSL_ERR_RSA_INVALID_PADDING );
726
727 if (ilen - (p - buf) > output_max_len)
728 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
729
730 *olen = ilen - (p - buf);
731 memcpy( output, p, *olen );
732
733 return( 0 );
734}
735#endif /* POLARSSL_PKCS1_V21 */
736
Paul Bakker48377d92013-08-30 12:06:24 +0200737#if defined(POLARSSL_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100738/*
739 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
740 */
741int rsa_rsaes_pkcs1_v15_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200742 int (*f_rng)(void *, unsigned char *, size_t),
743 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100744 int mode, size_t *olen,
745 const unsigned char *input,
746 unsigned char *output,
747 size_t output_max_len)
748{
Paul Bakker8804f692013-02-28 18:06:26 +0100749 int ret, correct = 1;
750 size_t ilen, pad_count = 0;
751 unsigned char *p, *q;
Paul Bakkerb3869132013-02-28 17:21:01 +0100752 unsigned char bt;
753 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
754
755 if( ctx->padding != RSA_PKCS_V15 )
756 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
757
758 ilen = ctx->len;
759
760 if( ilen < 16 || ilen > sizeof( buf ) )
761 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
762
763 ret = ( mode == RSA_PUBLIC )
764 ? rsa_public( ctx, input, buf )
Paul Bakker548957d2013-08-30 10:30:02 +0200765 : rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100766
767 if( ret != 0 )
768 return( ret );
769
770 p = buf;
771
772 if( *p++ != 0 )
Paul Bakker8804f692013-02-28 18:06:26 +0100773 correct = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100774
775 bt = *p++;
776 if( ( bt != RSA_CRYPT && mode == RSA_PRIVATE ) ||
777 ( bt != RSA_SIGN && mode == RSA_PUBLIC ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000778 {
Paul Bakker8804f692013-02-28 18:06:26 +0100779 correct = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100780 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
Paul Bakkerb3869132013-02-28 17:21:01 +0100782 if( bt == RSA_CRYPT )
783 {
784 while( *p != 0 && p < buf + ilen - 1 )
Paul Bakker8804f692013-02-28 18:06:26 +0100785 pad_count += ( *p++ != 0 );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000786
Paul Bakker8804f692013-02-28 18:06:26 +0100787 correct &= ( *p == 0 && p < buf + ilen - 1 );
Paul Bakkerb3869132013-02-28 17:21:01 +0100788
Paul Bakker8804f692013-02-28 18:06:26 +0100789 q = p;
790
791 // Also pass over all other bytes to reduce timing differences
792 //
793 while ( q < buf + ilen - 1 )
794 pad_count += ( *q++ != 0 );
795
796 // Prevent compiler optimization of pad_count
797 //
798 correct |= pad_count & 0x100000; /* Always 0 unless 1M bit keys */
Paul Bakkerb3869132013-02-28 17:21:01 +0100799 p++;
800 }
801 else
802 {
803 while( *p == 0xFF && p < buf + ilen - 1 )
Paul Bakker8804f692013-02-28 18:06:26 +0100804 pad_count += ( *p++ == 0xFF );
Paul Bakkerb3869132013-02-28 17:21:01 +0100805
Paul Bakker8804f692013-02-28 18:06:26 +0100806 correct &= ( *p == 0 && p < buf + ilen - 1 );
Paul Bakkerb3869132013-02-28 17:21:01 +0100807
Paul Bakker8804f692013-02-28 18:06:26 +0100808 q = p;
809
810 // Also pass over all other bytes to reduce timing differences
811 //
812 while ( q < buf + ilen - 1 )
813 pad_count += ( *q++ != 0 );
814
815 // Prevent compiler optimization of pad_count
816 //
817 correct |= pad_count & 0x100000; /* Always 0 unless 1M bit keys */
Paul Bakkerb3869132013-02-28 17:21:01 +0100818 p++;
Paul Bakker5121ce52009-01-03 21:22:43 +0000819 }
820
Paul Bakker8804f692013-02-28 18:06:26 +0100821 if( correct == 0 )
822 return( POLARSSL_ERR_RSA_INVALID_PADDING );
823
Paul Bakker27fdf462011-06-09 13:55:13 +0000824 if (ilen - (p - buf) > output_max_len)
Paul Bakker23986e52011-04-24 08:57:21 +0000825 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000826
Paul Bakker27fdf462011-06-09 13:55:13 +0000827 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000828 memcpy( output, p, *olen );
829
830 return( 0 );
831}
Paul Bakker48377d92013-08-30 12:06:24 +0200832#endif /* POLARSSL_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000833
834/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100835 * Do an RSA operation, then remove the message padding
836 */
837int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200838 int (*f_rng)(void *, unsigned char *, size_t),
839 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100840 int mode, size_t *olen,
841 const unsigned char *input,
842 unsigned char *output,
843 size_t output_max_len)
844{
845 switch( ctx->padding )
846 {
Paul Bakker48377d92013-08-30 12:06:24 +0200847#if defined(POLARSSL_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100848 case RSA_PKCS_V15:
Paul Bakker548957d2013-08-30 10:30:02 +0200849 return rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
850 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +0200851#endif
Paul Bakkerb3869132013-02-28 17:21:01 +0100852
853#if defined(POLARSSL_PKCS1_V21)
854 case RSA_PKCS_V21:
Paul Bakker548957d2013-08-30 10:30:02 +0200855 return rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
856 olen, input, output,
857 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +0100858#endif
859
860 default:
861 return( POLARSSL_ERR_RSA_INVALID_PADDING );
862 }
863}
864
865#if defined(POLARSSL_PKCS1_V21)
866/*
867 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
868 */
869int rsa_rsassa_pss_sign( rsa_context *ctx,
870 int (*f_rng)(void *, unsigned char *, size_t),
871 void *p_rng,
872 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200873 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100874 unsigned int hashlen,
875 const unsigned char *hash,
876 unsigned char *sig )
877{
878 size_t olen;
879 unsigned char *p = sig;
880 unsigned char salt[POLARSSL_MD_MAX_SIZE];
881 unsigned int slen, hlen, offset = 0;
882 int ret;
883 size_t msb;
884 const md_info_t *md_info;
885 md_context_t md_ctx;
886
887 if( ctx->padding != RSA_PKCS_V21 || f_rng == NULL )
888 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
889
890 olen = ctx->len;
891
Paul Bakkerc70b9822013-04-07 22:00:46 +0200892 if( md_alg != POLARSSL_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +0100893 {
Paul Bakkerc70b9822013-04-07 22:00:46 +0200894 // Gather length of hash to sign
895 //
896 md_info = md_info_from_type( md_alg );
897 if( md_info == NULL )
Paul Bakkerb3869132013-02-28 17:21:01 +0100898 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200899
900 hashlen = md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100901 }
902
903 md_info = md_info_from_type( ctx->hash_id );
904 if( md_info == NULL )
905 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
906
907 hlen = md_get_size( md_info );
908 slen = hlen;
909
910 if( olen < hlen + slen + 2 )
911 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
912
913 memset( sig, 0, olen );
914
915 msb = mpi_msb( &ctx->N ) - 1;
916
917 // Generate salt of length slen
918 //
919 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
920 return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
921
922 // Note: EMSA-PSS encoding is over the length of N - 1 bits
923 //
924 msb = mpi_msb( &ctx->N ) - 1;
925 p += olen - hlen * 2 - 2;
926 *p++ = 0x01;
927 memcpy( p, salt, slen );
928 p += slen;
929
930 md_init_ctx( &md_ctx, md_info );
931
932 // Generate H = Hash( M' )
933 //
934 md_starts( &md_ctx );
935 md_update( &md_ctx, p, 8 );
936 md_update( &md_ctx, hash, hashlen );
937 md_update( &md_ctx, salt, slen );
938 md_finish( &md_ctx, p );
939
940 // Compensate for boundary condition when applying mask
941 //
942 if( msb % 8 == 0 )
943 offset = 1;
944
945 // maskedDB: Apply dbMask to DB
946 //
947 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
948
949 md_free_ctx( &md_ctx );
950
951 msb = mpi_msb( &ctx->N ) - 1;
952 sig[0] &= 0xFF >> ( olen * 8 - msb );
953
954 p += hlen;
955 *p++ = 0xBC;
956
957 return( ( mode == RSA_PUBLIC )
958 ? rsa_public( ctx, sig, sig )
Paul Bakker548957d2013-08-30 10:30:02 +0200959 : rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100960}
961#endif /* POLARSSL_PKCS1_V21 */
962
Paul Bakker48377d92013-08-30 12:06:24 +0200963#if defined(POLARSSL_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100964/*
965 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
966 */
967/*
968 * Do an RSA operation to sign the message digest
969 */
970int rsa_rsassa_pkcs1_v15_sign( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200971 int (*f_rng)(void *, unsigned char *, size_t),
972 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100973 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +0200974 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100975 unsigned int hashlen,
976 const unsigned char *hash,
977 unsigned char *sig )
978{
Paul Bakkerc70b9822013-04-07 22:00:46 +0200979 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100980 unsigned char *p = sig;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200981 const char *oid;
Paul Bakkerb3869132013-02-28 17:21:01 +0100982
983 if( ctx->padding != RSA_PKCS_V15 )
984 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
985
986 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +0200987 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +0100988
Paul Bakkerc70b9822013-04-07 22:00:46 +0200989 if( md_alg != POLARSSL_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +0100990 {
Paul Bakkerc70b9822013-04-07 22:00:46 +0200991 const md_info_t *md_info = md_info_from_type( md_alg );
992 if( md_info == NULL )
Paul Bakkerb3869132013-02-28 17:21:01 +0100993 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200994
Paul Bakker1c3853b2013-09-10 11:43:44 +0200995 if( oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
Paul Bakkerc70b9822013-04-07 22:00:46 +0200996 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
997
Paul Bakkerc70b9822013-04-07 22:00:46 +0200998 nb_pad -= 10 + oid_size;
999
1000 hashlen = md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001001 }
1002
Paul Bakkerc70b9822013-04-07 22:00:46 +02001003 nb_pad -= hashlen;
1004
Paul Bakkerb3869132013-02-28 17:21:01 +01001005 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
1006 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1007
1008 *p++ = 0;
1009 *p++ = RSA_SIGN;
1010 memset( p, 0xFF, nb_pad );
1011 p += nb_pad;
1012 *p++ = 0;
1013
Paul Bakkerc70b9822013-04-07 22:00:46 +02001014 if( md_alg == POLARSSL_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001015 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001016 memcpy( p, hash, hashlen );
1017 }
1018 else
1019 {
1020 /*
1021 * DigestInfo ::= SEQUENCE {
1022 * digestAlgorithm DigestAlgorithmIdentifier,
1023 * digest Digest }
1024 *
1025 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1026 *
1027 * Digest ::= OCTET STRING
1028 */
1029 *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001030 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001031 *p++ = ASN1_SEQUENCE | ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001032 *p++ = (unsigned char) ( 0x04 + oid_size );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001033 *p++ = ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001034 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001035 memcpy( p, oid, oid_size );
1036 p += oid_size;
1037 *p++ = ASN1_NULL;
1038 *p++ = 0x00;
1039 *p++ = ASN1_OCTET_STRING;
1040 *p++ = hashlen;
1041 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001042 }
1043
1044 return( ( mode == RSA_PUBLIC )
1045 ? rsa_public( ctx, sig, sig )
Paul Bakker548957d2013-08-30 10:30:02 +02001046 : rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001047}
Paul Bakker48377d92013-08-30 12:06:24 +02001048#endif /* POLARSSL_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001049
1050/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 * Do an RSA operation to sign the message digest
1052 */
1053int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001054 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001055 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +02001057 md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001058 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001059 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 unsigned char *sig )
1061{
Paul Bakker5121ce52009-01-03 21:22:43 +00001062 switch( ctx->padding )
1063 {
Paul Bakker48377d92013-08-30 12:06:24 +02001064#if defined(POLARSSL_PKCS1_V15)
Paul Bakker5121ce52009-01-03 21:22:43 +00001065 case RSA_PKCS_V15:
Paul Bakker548957d2013-08-30 10:30:02 +02001066 return rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001067 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001068#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001069
Paul Bakker9dcc3222011-03-08 14:16:06 +00001070#if defined(POLARSSL_PKCS1_V21)
1071 case RSA_PKCS_V21:
Paul Bakkerc70b9822013-04-07 22:00:46 +02001072 return rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001073 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001074#endif
1075
Paul Bakker5121ce52009-01-03 21:22:43 +00001076 default:
Paul Bakker40e46942009-01-03 21:51:57 +00001077 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001078 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001079}
1080
Paul Bakkerb3869132013-02-28 17:21:01 +01001081#if defined(POLARSSL_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001082/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001083 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001085int rsa_rsassa_pss_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001086 int (*f_rng)(void *, unsigned char *, size_t),
1087 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001088 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +02001089 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001090 unsigned int hashlen,
1091 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001092 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001093{
Paul Bakker23986e52011-04-24 08:57:21 +00001094 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001095 size_t siglen;
1096 unsigned char *p;
Paul Bakker0be82f22012-10-03 20:36:33 +00001097 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker1fe7d9b2011-11-15 15:26:03 +00001098 unsigned char result[POLARSSL_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001099 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001100 unsigned int hlen;
1101 size_t slen, msb;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001102 const md_info_t *md_info;
1103 md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001104
1105 if( ctx->padding != RSA_PKCS_V21 )
1106 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1107
Paul Bakker5121ce52009-01-03 21:22:43 +00001108 siglen = ctx->len;
1109
Paul Bakker27fdf462011-06-09 13:55:13 +00001110 if( siglen < 16 || siglen > sizeof( buf ) )
Paul Bakker40e46942009-01-03 21:51:57 +00001111 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001112
1113 ret = ( mode == RSA_PUBLIC )
1114 ? rsa_public( ctx, sig, buf )
Paul Bakker548957d2013-08-30 10:30:02 +02001115 : rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001116
1117 if( ret != 0 )
1118 return( ret );
1119
1120 p = buf;
1121
Paul Bakkerb3869132013-02-28 17:21:01 +01001122 if( buf[siglen - 1] != 0xBC )
1123 return( POLARSSL_ERR_RSA_INVALID_PADDING );
1124
Paul Bakkerc70b9822013-04-07 22:00:46 +02001125 if( md_alg != POLARSSL_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001127 // Gather length of hash to sign
1128 //
1129 md_info = md_info_from_type( md_alg );
1130 if( md_info == NULL )
Paul Bakkerb3869132013-02-28 17:21:01 +01001131 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001132
1133 hashlen = md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001134 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001135
Paul Bakkerb3869132013-02-28 17:21:01 +01001136 md_info = md_info_from_type( ctx->hash_id );
1137 if( md_info == NULL )
1138 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001139
Paul Bakkerb3869132013-02-28 17:21:01 +01001140 hlen = md_get_size( md_info );
1141 slen = siglen - hlen - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001142
Paul Bakkerb3869132013-02-28 17:21:01 +01001143 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001144
Paul Bakkerb3869132013-02-28 17:21:01 +01001145 // Note: EMSA-PSS verification is over the length of N - 1 bits
1146 //
1147 msb = mpi_msb( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001148
Paul Bakkerb3869132013-02-28 17:21:01 +01001149 // Compensate for boundary condition when applying mask
1150 //
1151 if( msb % 8 == 0 )
1152 {
1153 p++;
1154 siglen -= 1;
1155 }
1156 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1157 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001158
Paul Bakkerb3869132013-02-28 17:21:01 +01001159 md_init_ctx( &md_ctx, md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001160
Paul Bakkerb3869132013-02-28 17:21:01 +01001161 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001162
Paul Bakkerb3869132013-02-28 17:21:01 +01001163 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001164
Paul Bakkerb3869132013-02-28 17:21:01 +01001165 while( *p == 0 && p < buf + siglen )
1166 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001167
Paul Bakkerb3869132013-02-28 17:21:01 +01001168 if( p == buf + siglen ||
1169 *p++ != 0x01 )
1170 {
1171 md_free_ctx( &md_ctx );
1172 return( POLARSSL_ERR_RSA_INVALID_PADDING );
1173 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001174
Paul Bakkerb3869132013-02-28 17:21:01 +01001175 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001176
Paul Bakkerb3869132013-02-28 17:21:01 +01001177 // Generate H = Hash( M' )
1178 //
1179 md_starts( &md_ctx );
1180 md_update( &md_ctx, zeros, 8 );
1181 md_update( &md_ctx, hash, hashlen );
1182 md_update( &md_ctx, p, slen );
1183 md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001184
Paul Bakkerb3869132013-02-28 17:21:01 +01001185 md_free_ctx( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001186
Paul Bakkerb3869132013-02-28 17:21:01 +01001187 if( memcmp( p + slen, result, hlen ) == 0 )
1188 return( 0 );
1189 else
1190 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1191}
1192#endif /* POLARSSL_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001193
Paul Bakker48377d92013-08-30 12:06:24 +02001194#if defined(POLARSSL_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001195/*
1196 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1197 */
1198int rsa_rsassa_pkcs1_v15_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001199 int (*f_rng)(void *, unsigned char *, size_t),
1200 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001201 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +02001202 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001203 unsigned int hashlen,
1204 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001205 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001206{
1207 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001208 size_t len, siglen, asn1_len;
1209 unsigned char *p, *end;
Paul Bakkerb3869132013-02-28 17:21:01 +01001210 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakkerc70b9822013-04-07 22:00:46 +02001211 md_type_t msg_md_alg;
1212 const md_info_t *md_info;
1213 asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001214
1215 if( ctx->padding != RSA_PKCS_V15 )
1216 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1217
1218 siglen = ctx->len;
1219
1220 if( siglen < 16 || siglen > sizeof( buf ) )
1221 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1222
1223 ret = ( mode == RSA_PUBLIC )
1224 ? rsa_public( ctx, sig, buf )
Paul Bakker548957d2013-08-30 10:30:02 +02001225 : rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001226
1227 if( ret != 0 )
1228 return( ret );
1229
1230 p = buf;
1231
1232 if( *p++ != 0 || *p++ != RSA_SIGN )
1233 return( POLARSSL_ERR_RSA_INVALID_PADDING );
1234
1235 while( *p != 0 )
1236 {
1237 if( p >= buf + siglen - 1 || *p != 0xFF )
1238 return( POLARSSL_ERR_RSA_INVALID_PADDING );
1239 p++;
1240 }
1241 p++;
1242
1243 len = siglen - ( p - buf );
1244
Paul Bakkerc70b9822013-04-07 22:00:46 +02001245 if( len == hashlen && md_alg == POLARSSL_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001246 {
1247 if( memcmp( p, hash, hashlen ) == 0 )
1248 return( 0 );
1249 else
1250 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001251 }
1252
Paul Bakkerc70b9822013-04-07 22:00:46 +02001253 md_info = md_info_from_type( md_alg );
1254 if( md_info == NULL )
1255 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1256 hashlen = md_get_size( md_info );
1257
1258 end = p + len;
1259
1260 // Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1261 //
1262 if( ( ret = asn1_get_tag( &p, end, &asn1_len,
1263 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1264 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1265
1266 if( asn1_len + 2 != len )
1267 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1268
1269 if( ( ret = asn1_get_tag( &p, end, &asn1_len,
1270 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
1271 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1272
1273 if( asn1_len + 6 + hashlen != len )
1274 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1275
1276 if( ( ret = asn1_get_tag( &p, end, &oid.len, ASN1_OID ) ) != 0 )
1277 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1278
1279 oid.p = p;
1280 p += oid.len;
1281
1282 if( oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1283 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1284
1285 if( md_alg != msg_md_alg )
1286 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1287
1288 /*
1289 * assume the algorithm parameters must be NULL
1290 */
1291 if( ( ret = asn1_get_tag( &p, end, &asn1_len, ASN1_NULL ) ) != 0 )
1292 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1293
1294 if( ( ret = asn1_get_tag( &p, end, &asn1_len, ASN1_OCTET_STRING ) ) != 0 )
1295 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1296
1297 if( asn1_len != hashlen )
1298 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1299
1300 if( memcmp( p, hash, hashlen ) != 0 )
1301 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1302
1303 p += hashlen;
1304
1305 if( p != end )
1306 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
1307
1308 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001309}
Paul Bakker48377d92013-08-30 12:06:24 +02001310#endif /* POLARSSL_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001311
1312/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001313 * Do an RSA operation and check the message digest
1314 */
1315int rsa_pkcs1_verify( rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001316 int (*f_rng)(void *, unsigned char *, size_t),
1317 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001318 int mode,
Paul Bakkerc70b9822013-04-07 22:00:46 +02001319 md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001320 unsigned int hashlen,
1321 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001322 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001323{
1324 switch( ctx->padding )
1325 {
Paul Bakker48377d92013-08-30 12:06:24 +02001326#if defined(POLARSSL_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001327 case RSA_PKCS_V15:
Paul Bakker548957d2013-08-30 10:30:02 +02001328 return rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001329 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001330#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001331
1332#if defined(POLARSSL_PKCS1_V21)
1333 case RSA_PKCS_V21:
Paul Bakker548957d2013-08-30 10:30:02 +02001334 return rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001335 hashlen, hash, sig );
1336#endif
1337
1338 default:
1339 return( POLARSSL_ERR_RSA_INVALID_PADDING );
1340 }
1341}
1342
1343/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001344 * Copy the components of an RSA key
1345 */
1346int rsa_copy( rsa_context *dst, const rsa_context *src )
1347{
1348 int ret;
1349
1350 dst->ver = src->ver;
1351 dst->len = src->len;
1352
1353 MPI_CHK( mpi_copy( &dst->N, &src->N ) );
1354 MPI_CHK( mpi_copy( &dst->E, &src->E ) );
1355
1356 MPI_CHK( mpi_copy( &dst->D, &src->D ) );
1357 MPI_CHK( mpi_copy( &dst->P, &src->P ) );
1358 MPI_CHK( mpi_copy( &dst->Q, &src->Q ) );
1359 MPI_CHK( mpi_copy( &dst->DP, &src->DP ) );
1360 MPI_CHK( mpi_copy( &dst->DQ, &src->DQ ) );
1361 MPI_CHK( mpi_copy( &dst->QP, &src->QP ) );
1362
1363 MPI_CHK( mpi_copy( &dst->RN, &src->RN ) );
1364 MPI_CHK( mpi_copy( &dst->RP, &src->RP ) );
1365 MPI_CHK( mpi_copy( &dst->RQ, &src->RQ ) );
1366
Manuel Pégourié-Gonnard971f8b82013-10-04 14:10:43 +02001367#if !defined(POLARSSL_RSA_NO_CRT)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001368 MPI_CHK( mpi_copy( &dst->Vi, &src->Vi ) );
1369 MPI_CHK( mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnard971f8b82013-10-04 14:10:43 +02001370#endif
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001371
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001372 dst->padding = src->padding;
1373 dst->hash_id = src->padding;
1374
1375cleanup:
1376 if( ret != 0 )
1377 rsa_free( dst );
1378
1379 return( ret );
1380}
1381
1382/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 * Free the components of an RSA key
1384 */
1385void rsa_free( rsa_context *ctx )
1386{
Manuel Pégourié-Gonnard971f8b82013-10-04 14:10:43 +02001387#if !defined(POLARSSL_RSA_NO_CRT)
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001388 mpi_free( &ctx->Vi ); mpi_free( &ctx->Vf );
Manuel Pégourié-Gonnard971f8b82013-10-04 14:10:43 +02001389#endif
Paul Bakker6c591fa2011-05-05 11:49:20 +00001390 mpi_free( &ctx->RQ ); mpi_free( &ctx->RP ); mpi_free( &ctx->RN );
1391 mpi_free( &ctx->QP ); mpi_free( &ctx->DQ ); mpi_free( &ctx->DP );
1392 mpi_free( &ctx->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D );
1393 mpi_free( &ctx->E ); mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001394
1395#if defined(POLARSSL_THREADING_C)
1396 polarssl_mutex_free( &ctx->mutex );
1397#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001398}
1399
Paul Bakker40e46942009-01-03 21:51:57 +00001400#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Paul Bakker40e46942009-01-03 21:51:57 +00001402#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001403
1404/*
1405 * Example RSA-1024 keypair, for test purposes
1406 */
1407#define KEY_LEN 128
1408
1409#define RSA_N "9292758453063D803DD603D5E777D788" \
1410 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1411 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1412 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1413 "93A89813FBF3C4F8066D2D800F7C38A8" \
1414 "1AE31942917403FF4946B0A83D3D3E05" \
1415 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1416 "5E94BB77B07507233A0BC7BAC8F90F79"
1417
1418#define RSA_E "10001"
1419
1420#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1421 "66CA472BC44D253102F8B4A9D3BFA750" \
1422 "91386C0077937FE33FA3252D28855837" \
1423 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1424 "DF79C5CE07EE72C7F123142198164234" \
1425 "CABB724CF78B8173B9F880FC86322407" \
1426 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1427 "071513A1E85B5DFA031F21ECAE91A34D"
1428
1429#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1430 "2C01CAD19EA484A87EA4377637E75500" \
1431 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1432 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1433
1434#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1435 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1436 "910E4168387E3C30AA1E00C339A79508" \
1437 "8452DD96A9A5EA5D9DCA68DA636032AF"
1438
1439#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1440 "3C94D22288ACD763FD8E5600ED4A702D" \
1441 "F84198A5F06C2E72236AE490C93F07F8" \
1442 "3CC559CD27BC2D1CA488811730BB5725"
1443
1444#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1445 "D8AAEA56749EA28623272E4F7D0592AF" \
1446 "7C1F1313CAC9471B5C523BFE592F517B" \
1447 "407A1BD76C164B93DA2D32A383E58357"
1448
1449#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1450 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1451 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1452 "A74206CEC169D74BF5A8C50D6F48EA08"
1453
1454#define PT_LEN 24
1455#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1456 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1457
Paul Bakker48377d92013-08-30 12:06:24 +02001458#if defined(POLARSSL_PCKS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001459static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001460{
Paul Bakkera3d195c2011-11-27 21:07:34 +00001461 size_t i;
1462
Paul Bakker545570e2010-07-18 09:00:25 +00001463 if( rng_state != NULL )
1464 rng_state = NULL;
1465
Paul Bakkera3d195c2011-11-27 21:07:34 +00001466 for( i = 0; i < len; ++i )
1467 output[i] = rand();
Paul Bakker48377d92013-08-30 12:06:24 +02001468
Paul Bakkera3d195c2011-11-27 21:07:34 +00001469 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001470}
Paul Bakker48377d92013-08-30 12:06:24 +02001471#endif
Paul Bakker545570e2010-07-18 09:00:25 +00001472
Paul Bakker5121ce52009-01-03 21:22:43 +00001473/*
1474 * Checkup routine
1475 */
1476int rsa_self_test( int verbose )
1477{
Paul Bakker48377d92013-08-30 12:06:24 +02001478#if defined(POLARSSL_PCKS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001479 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001480 rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001481 unsigned char rsa_plaintext[PT_LEN];
1482 unsigned char rsa_decrypted[PT_LEN];
1483 unsigned char rsa_ciphertext[KEY_LEN];
Paul Bakker5690efc2011-05-26 13:16:06 +00001484#if defined(POLARSSL_SHA1_C)
1485 unsigned char sha1sum[20];
1486#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001487
Paul Bakker21eb2802010-08-16 11:10:02 +00001488 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001489
1490 rsa.len = KEY_LEN;
1491 mpi_read_string( &rsa.N , 16, RSA_N );
1492 mpi_read_string( &rsa.E , 16, RSA_E );
1493 mpi_read_string( &rsa.D , 16, RSA_D );
1494 mpi_read_string( &rsa.P , 16, RSA_P );
1495 mpi_read_string( &rsa.Q , 16, RSA_Q );
1496 mpi_read_string( &rsa.DP, 16, RSA_DP );
1497 mpi_read_string( &rsa.DQ, 16, RSA_DQ );
1498 mpi_read_string( &rsa.QP, 16, RSA_QP );
1499
1500 if( verbose != 0 )
1501 printf( " RSA key validation: " );
1502
1503 if( rsa_check_pubkey( &rsa ) != 0 ||
1504 rsa_check_privkey( &rsa ) != 0 )
1505 {
1506 if( verbose != 0 )
1507 printf( "failed\n" );
1508
1509 return( 1 );
1510 }
1511
1512 if( verbose != 0 )
1513 printf( "passed\n PKCS#1 encryption : " );
1514
1515 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1516
Paul Bakker548957d2013-08-30 10:30:02 +02001517 if( rsa_pkcs1_encrypt( &rsa, myrand, NULL, RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001518 rsa_plaintext, rsa_ciphertext ) != 0 )
1519 {
1520 if( verbose != 0 )
1521 printf( "failed\n" );
1522
1523 return( 1 );
1524 }
1525
1526 if( verbose != 0 )
1527 printf( "passed\n PKCS#1 decryption : " );
1528
Paul Bakker548957d2013-08-30 10:30:02 +02001529 if( rsa_pkcs1_decrypt( &rsa, myrand, NULL, RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001530 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001531 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001532 {
1533 if( verbose != 0 )
1534 printf( "failed\n" );
1535
1536 return( 1 );
1537 }
1538
1539 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1540 {
1541 if( verbose != 0 )
1542 printf( "failed\n" );
1543
1544 return( 1 );
1545 }
1546
Paul Bakker5690efc2011-05-26 13:16:06 +00001547#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 if( verbose != 0 )
1549 printf( "passed\n PKCS#1 data sign : " );
1550
1551 sha1( rsa_plaintext, PT_LEN, sha1sum );
1552
Paul Bakkeraab30c12013-08-30 11:00:25 +02001553 if( rsa_pkcs1_sign( &rsa, myrand, NULL, RSA_PRIVATE, POLARSSL_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001554 sha1sum, rsa_ciphertext ) != 0 )
1555 {
1556 if( verbose != 0 )
1557 printf( "failed\n" );
1558
1559 return( 1 );
1560 }
1561
1562 if( verbose != 0 )
1563 printf( "passed\n PKCS#1 sig. verify: " );
1564
Paul Bakker548957d2013-08-30 10:30:02 +02001565 if( rsa_pkcs1_verify( &rsa, NULL, NULL, RSA_PUBLIC, POLARSSL_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 sha1sum, rsa_ciphertext ) != 0 )
1567 {
1568 if( verbose != 0 )
1569 printf( "failed\n" );
1570
1571 return( 1 );
1572 }
1573
1574 if( verbose != 0 )
1575 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00001576#endif /* POLARSSL_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001577
1578 rsa_free( &rsa );
Paul Bakker48377d92013-08-30 12:06:24 +02001579#else /* POLARSSL_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001580 ((void) verbose);
Paul Bakker48377d92013-08-30 12:06:24 +02001581#endif /* POLARSSL_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001582 return( 0 );
1583}
1584
1585#endif
1586
1587#endif