blob: ee6ca0195c9a409a9db00894203e1ab5d749f544 [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 Bakkerbb51f0c2012-08-23 07:46:58 +000037
38#if defined(POLARSSL_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +000039#include "polarssl/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000040#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000041
42#include <stdlib.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000043#include <stdio.h>
44
45/*
46 * Initialize an RSA context
47 */
48void rsa_init( rsa_context *ctx,
49 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000050 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000051{
52 memset( ctx, 0, sizeof( rsa_context ) );
53
54 ctx->padding = padding;
55 ctx->hash_id = hash_id;
Paul Bakker5121ce52009-01-03 21:22:43 +000056}
57
Paul Bakker40e46942009-01-03 21:51:57 +000058#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000059
60/*
61 * Generate an RSA keypair
62 */
Paul Bakker21eb2802010-08-16 11:10:02 +000063int rsa_gen_key( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +000064 int (*f_rng)(void *, unsigned char *, size_t),
65 void *p_rng,
66 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +000067{
68 int ret;
69 mpi P1, Q1, H, G;
70
Paul Bakker21eb2802010-08-16 11:10:02 +000071 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Paul Bakker40e46942009-01-03 21:51:57 +000072 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +000073
Paul Bakker6c591fa2011-05-05 11:49:20 +000074 mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +000075
76 /*
77 * find primes P and Q with Q < P so that:
78 * GCD( E, (P-1)*(Q-1) ) == 1
79 */
80 MPI_CHK( mpi_lset( &ctx->E, exponent ) );
81
82 do
83 {
84 MPI_CHK( mpi_gen_prime( &ctx->P, ( nbits + 1 ) >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +000085 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87 MPI_CHK( mpi_gen_prime( &ctx->Q, ( nbits + 1 ) >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +000088 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
91 mpi_swap( &ctx->P, &ctx->Q );
92
93 if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
94 continue;
95
96 MPI_CHK( mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
97 if( mpi_msb( &ctx->N ) != nbits )
98 continue;
99
100 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
101 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
102 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
103 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
104 }
105 while( mpi_cmp_int( &G, 1 ) != 0 );
106
107 /*
108 * D = E^-1 mod ((P-1)*(Q-1))
109 * DP = D mod (P - 1)
110 * DQ = D mod (Q - 1)
111 * QP = Q^-1 mod P
112 */
113 MPI_CHK( mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
114 MPI_CHK( mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
115 MPI_CHK( mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
116 MPI_CHK( mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
117
118 ctx->len = ( mpi_msb( &ctx->N ) + 7 ) >> 3;
119
120cleanup:
121
Paul Bakker6c591fa2011-05-05 11:49:20 +0000122 mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
124 if( ret != 0 )
125 {
126 rsa_free( ctx );
Paul Bakker9d781402011-05-09 16:17:09 +0000127 return( POLARSSL_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 }
129
130 return( 0 );
131}
132
133#endif
134
135/*
136 * Check a public RSA key
137 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138int rsa_check_pubkey( const rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000139{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000140 if( !ctx->N.p || !ctx->E.p )
141 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
142
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 if( ( ctx->N.p[0] & 1 ) == 0 ||
144 ( ctx->E.p[0] & 1 ) == 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000145 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 if( mpi_msb( &ctx->N ) < 128 ||
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000148 mpi_msb( &ctx->N ) > POLARSSL_MPI_MAX_BITS )
Paul Bakker40e46942009-01-03 21:51:57 +0000149 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151 if( mpi_msb( &ctx->E ) < 2 ||
152 mpi_msb( &ctx->E ) > 64 )
Paul Bakker40e46942009-01-03 21:51:57 +0000153 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
155 return( 0 );
156}
157
158/*
159 * Check a private RSA key
160 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000161int rsa_check_privkey( const rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000162{
163 int ret;
Paul Bakker321df6f2012-09-27 13:21:34 +0000164 mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166 if( ( ret = rsa_check_pubkey( ctx ) ) != 0 )
167 return( ret );
168
Paul Bakker37940d9f2009-07-10 22:38:58 +0000169 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
170 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
171
Paul Bakker6c591fa2011-05-05 11:49:20 +0000172 mpi_init( &PQ ); mpi_init( &DE ); mpi_init( &P1 ); mpi_init( &Q1 );
173 mpi_init( &H ); mpi_init( &I ); mpi_init( &G ); mpi_init( &G2 );
Paul Bakker321df6f2012-09-27 13:21:34 +0000174 mpi_init( &L1 ); mpi_init( &L2 ); mpi_init( &DP ); mpi_init( &DQ );
175 mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
177 MPI_CHK( mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
178 MPI_CHK( mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
179 MPI_CHK( mpi_sub_int( &P1, &ctx->P, 1 ) );
180 MPI_CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) );
181 MPI_CHK( mpi_mul_mpi( &H, &P1, &Q1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 MPI_CHK( mpi_gcd( &G, &ctx->E, &H ) );
183
Paul Bakkerb572adf2010-07-18 08:29:32 +0000184 MPI_CHK( mpi_gcd( &G2, &P1, &Q1 ) );
185 MPI_CHK( mpi_div_mpi( &L1, &L2, &H, &G2 ) );
186 MPI_CHK( mpi_mod_mpi( &I, &DE, &L1 ) );
187
Paul Bakker321df6f2012-09-27 13:21:34 +0000188 MPI_CHK( mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
189 MPI_CHK( mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
190 MPI_CHK( mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000191 /*
192 * Check for a valid PKCS1v2 private key
193 */
Paul Bakker6c591fa2011-05-05 11:49:20 +0000194 if( mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
Paul Bakker321df6f2012-09-27 13:21:34 +0000195 mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
196 mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
197 mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
Paul Bakker6c591fa2011-05-05 11:49:20 +0000198 mpi_cmp_int( &L2, 0 ) != 0 ||
199 mpi_cmp_int( &I, 1 ) != 0 ||
200 mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000202 ret = POLARSSL_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000203 }
Paul Bakkerb572adf2010-07-18 08:29:32 +0000204
Paul Bakker5121ce52009-01-03 21:22:43 +0000205cleanup:
Paul Bakker6c591fa2011-05-05 11:49:20 +0000206 mpi_free( &PQ ); mpi_free( &DE ); mpi_free( &P1 ); mpi_free( &Q1 );
207 mpi_free( &H ); mpi_free( &I ); mpi_free( &G ); mpi_free( &G2 );
Paul Bakker321df6f2012-09-27 13:21:34 +0000208 mpi_free( &L1 ); mpi_free( &L2 ); mpi_free( &DP ); mpi_free( &DQ );
209 mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000210
Paul Bakker9d781402011-05-09 16:17:09 +0000211 if( ret == POLARSSL_ERR_RSA_KEY_CHECK_FAILED )
212 return( ret );
213
Paul Bakker6c591fa2011-05-05 11:49:20 +0000214 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000215 return( POLARSSL_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000216
217 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218}
219
220/*
221 * Do an RSA public key operation
222 */
223int rsa_public( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000224 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 unsigned char *output )
226{
Paul Bakker23986e52011-04-24 08:57:21 +0000227 int ret;
228 size_t olen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 mpi T;
230
Paul Bakker6c591fa2011-05-05 11:49:20 +0000231 mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
233 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
234
235 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
236 {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000237 mpi_free( &T );
Paul Bakker40e46942009-01-03 21:51:57 +0000238 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 }
240
241 olen = ctx->len;
242 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
243 MPI_CHK( mpi_write_binary( &T, output, olen ) );
244
245cleanup:
246
Paul Bakker6c591fa2011-05-05 11:49:20 +0000247 mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
249 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000250 return( POLARSSL_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
252 return( 0 );
253}
254
255/*
256 * Do an RSA private key operation
257 */
258int rsa_private( rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000259 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 unsigned char *output )
261{
Paul Bakker23986e52011-04-24 08:57:21 +0000262 int ret;
263 size_t olen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 mpi T, T1, T2;
265
Paul Bakker6c591fa2011-05-05 11:49:20 +0000266 mpi_init( &T ); mpi_init( &T1 ); mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
268 MPI_CHK( mpi_read_binary( &T, input, ctx->len ) );
269
270 if( mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
271 {
Paul Bakker6c591fa2011-05-05 11:49:20 +0000272 mpi_free( &T );
Paul Bakker40e46942009-01-03 21:51:57 +0000273 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274 }
275
Paul Bakker0216cc12011-03-26 13:40:23 +0000276#if defined(POLARSSL_RSA_NO_CRT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 MPI_CHK( mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
278#else
279 /*
280 * faster decryption using the CRT
281 *
282 * T1 = input ^ dP mod P
283 * T2 = input ^ dQ mod Q
284 */
285 MPI_CHK( mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
286 MPI_CHK( mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
287
288 /*
289 * T = (T1 - T2) * (Q^-1 mod P) mod P
290 */
291 MPI_CHK( mpi_sub_mpi( &T, &T1, &T2 ) );
292 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->QP ) );
293 MPI_CHK( mpi_mod_mpi( &T, &T1, &ctx->P ) );
294
295 /*
296 * output = T2 + T * Q
297 */
298 MPI_CHK( mpi_mul_mpi( &T1, &T, &ctx->Q ) );
299 MPI_CHK( mpi_add_mpi( &T, &T2, &T1 ) );
300#endif
301
302 olen = ctx->len;
303 MPI_CHK( mpi_write_binary( &T, output, olen ) );
304
305cleanup:
306
Paul Bakker6c591fa2011-05-05 11:49:20 +0000307 mpi_free( &T ); mpi_free( &T1 ); mpi_free( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309 if( ret != 0 )
Paul Bakker9d781402011-05-09 16:17:09 +0000310 return( POLARSSL_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312 return( 0 );
313}
314
Paul Bakker9dcc3222011-03-08 14:16:06 +0000315#if defined(POLARSSL_PKCS1_V21)
316/**
317 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
318 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000319 * \param dst buffer to mask
320 * \param dlen length of destination buffer
321 * \param src source of the mask generation
322 * \param slen length of the source buffer
323 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000324 */
Paul Bakker23986e52011-04-24 08:57:21 +0000325static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src, size_t slen,
Paul Bakker9dcc3222011-03-08 14:16:06 +0000326 md_context_t *md_ctx )
327{
328 unsigned char mask[POLARSSL_MD_MAX_SIZE];
329 unsigned char counter[4];
330 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000331 unsigned int hlen;
332 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000333
334 memset( mask, 0, POLARSSL_MD_MAX_SIZE );
335 memset( counter, 0, 4 );
336
337 hlen = md_ctx->md_info->size;
338
339 // Generate and apply dbMask
340 //
341 p = dst;
342
343 while( dlen > 0 )
344 {
345 use_len = hlen;
346 if( dlen < hlen )
347 use_len = dlen;
348
349 md_starts( md_ctx );
350 md_update( md_ctx, src, slen );
351 md_update( md_ctx, counter, 4 );
352 md_finish( md_ctx, mask );
353
354 for( i = 0; i < use_len; ++i )
355 *p++ ^= mask[i];
356
357 counter[3]++;
358
359 dlen -= use_len;
360 }
361}
362#endif
363
Paul Bakker5121ce52009-01-03 21:22:43 +0000364/*
365 * Add the message padding, then do an RSA operation
366 */
367int rsa_pkcs1_encrypt( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000368 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000369 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000370 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000371 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 unsigned char *output )
373{
Paul Bakkered375ca2012-01-14 18:10:38 +0000374 size_t nb_pad, olen;
375 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 unsigned char *p = output;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000377#if defined(POLARSSL_PKCS1_V21)
Paul Bakkera3d195c2011-11-27 21:07:34 +0000378 unsigned int hlen;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000379 const md_info_t *md_info;
380 md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000381#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000382
383 olen = ctx->len;
384
Paul Bakker9dcc3222011-03-08 14:16:06 +0000385 if( f_rng == NULL )
386 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
387
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 switch( ctx->padding )
389 {
390 case RSA_PKCS_V15:
391
Paul Bakker23986e52011-04-24 08:57:21 +0000392 if( olen < ilen + 11 )
Paul Bakker40e46942009-01-03 21:51:57 +0000393 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
395 nb_pad = olen - 3 - ilen;
396
397 *p++ = 0;
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000398 if( mode == RSA_PUBLIC )
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 {
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000400 *p++ = RSA_CRYPT;
Paul Bakkerb572adf2010-07-18 08:29:32 +0000401
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000402 while( nb_pad-- > 0 )
403 {
404 int rng_dl = 100;
Paul Bakkerb572adf2010-07-18 08:29:32 +0000405
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000406 do {
407 ret = f_rng( p_rng, p, 1 );
408 } while( *p == 0 && --rng_dl && ret == 0 );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000409
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000410 // Check if RNG failed to generate data
411 //
412 if( rng_dl == 0 || ret != 0)
413 return POLARSSL_ERR_RSA_RNG_FAILED + ret;
414
415 p++;
416 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000417 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000418 else
419 {
420 *p++ = RSA_SIGN;
421
422 while( nb_pad-- > 0 )
423 *p++ = 0xFF;
424 }
425
Paul Bakker5121ce52009-01-03 21:22:43 +0000426 *p++ = 0;
427 memcpy( p, input, ilen );
428 break;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000429
430#if defined(POLARSSL_PKCS1_V21)
431 case RSA_PKCS_V21:
432
433 md_info = md_info_from_type( ctx->hash_id );
434 if( md_info == NULL )
435 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
436
437 hlen = md_get_size( md_info );
438
Paul Bakker23986e52011-04-24 08:57:21 +0000439 if( olen < ilen + 2 * hlen + 2 || f_rng == NULL )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000440 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
441
442 memset( output, 0, olen );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000443
Paul Bakker9dcc3222011-03-08 14:16:06 +0000444 *p++ = 0;
445
446 // Generate a random octet string seed
447 //
Paul Bakkera3d195c2011-11-27 21:07:34 +0000448 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
449 return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
450
451 p += hlen;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000452
453 // Construct DB
454 //
455 md( md_info, p, 0, p );
456 p += hlen;
457 p += olen - 2 * hlen - 2 - ilen;
458 *p++ = 1;
459 memcpy( p, input, ilen );
460
Paul Bakker02303e82013-01-03 11:08:31 +0100461 md_init_ctx( &md_ctx, md_info );
462
Paul Bakker9dcc3222011-03-08 14:16:06 +0000463 // maskedDB: Apply dbMask to DB
464 //
465 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
466 &md_ctx );
467
468 // maskedSeed: Apply seedMask to seed
469 //
470 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
471 &md_ctx );
Paul Bakker40628ba2013-01-03 10:50:31 +0100472
473 md_free_ctx( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000474 break;
475#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477 default:
478
Paul Bakker40e46942009-01-03 21:51:57 +0000479 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 }
481
482 return( ( mode == RSA_PUBLIC )
483 ? rsa_public( ctx, output, output )
484 : rsa_private( ctx, output, output ) );
485}
486
487/*
488 * Do an RSA operation, then remove the message padding
489 */
490int rsa_pkcs1_decrypt( rsa_context *ctx,
Paul Bakker23986e52011-04-24 08:57:21 +0000491 int mode, size_t *olen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000492 const unsigned char *input,
Paul Bakker060c5682009-01-12 21:48:39 +0000493 unsigned char *output,
Paul Bakker23986e52011-04-24 08:57:21 +0000494 size_t output_max_len)
Paul Bakker5121ce52009-01-03 21:22:43 +0000495{
Paul Bakker23986e52011-04-24 08:57:21 +0000496 int ret;
497 size_t ilen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 unsigned char *p;
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000499 unsigned char bt;
Paul Bakker0be82f22012-10-03 20:36:33 +0000500 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000501#if defined(POLARSSL_PKCS1_V21)
502 unsigned char lhash[POLARSSL_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000503 unsigned int hlen;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000504 const md_info_t *md_info;
505 md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000506#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000507
508 ilen = ctx->len;
509
Paul Bakker27fdf462011-06-09 13:55:13 +0000510 if( ilen < 16 || ilen > sizeof( buf ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000511 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513 ret = ( mode == RSA_PUBLIC )
514 ? rsa_public( ctx, input, buf )
515 : rsa_private( ctx, input, buf );
516
517 if( ret != 0 )
518 return( ret );
519
520 p = buf;
521
522 switch( ctx->padding )
523 {
524 case RSA_PKCS_V15:
525
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000526 if( *p++ != 0 )
Paul Bakker40e46942009-01-03 21:51:57 +0000527 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000528
529 bt = *p++;
530 if( ( bt != RSA_CRYPT && mode == RSA_PRIVATE ) ||
531 ( bt != RSA_SIGN && mode == RSA_PUBLIC ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 {
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000533 return( POLARSSL_ERR_RSA_INVALID_PADDING );
534 }
535
536 if( bt == RSA_CRYPT )
537 {
538 while( *p != 0 && p < buf + ilen - 1 )
539 p++;
540
541 if( *p != 0 || p >= buf + ilen - 1 )
Paul Bakker40e46942009-01-03 21:51:57 +0000542 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000543
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 p++;
545 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000546 else
547 {
548 while( *p == 0xFF && p < buf + ilen - 1 )
549 p++;
550
551 if( *p != 0 || p >= buf + ilen - 1 )
552 return( POLARSSL_ERR_RSA_INVALID_PADDING );
553
554 p++;
555 }
556
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 break;
558
Paul Bakker9dcc3222011-03-08 14:16:06 +0000559#if defined(POLARSSL_PKCS1_V21)
560 case RSA_PKCS_V21:
561
562 if( *p++ != 0 )
563 return( POLARSSL_ERR_RSA_INVALID_PADDING );
564
565 md_info = md_info_from_type( ctx->hash_id );
566 if( md_info == NULL )
567 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
568
569 hlen = md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000570
571 md_init_ctx( &md_ctx, md_info );
572
573 // Generate lHash
574 //
575 md( md_info, lhash, 0, lhash );
576
577 // seed: Apply seedMask to maskedSeed
578 //
579 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
580 &md_ctx );
581
582 // DB: Apply dbMask to maskedDB
583 //
584 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
585 &md_ctx );
586
587 p += hlen;
Paul Bakker40628ba2013-01-03 10:50:31 +0100588 md_free_ctx( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000589
590 // Check validity
591 //
592 if( memcmp( lhash, p, hlen ) != 0 )
593 return( POLARSSL_ERR_RSA_INVALID_PADDING );
594
595 p += hlen;
596
597 while( *p == 0 && p < buf + ilen )
598 p++;
599
600 if( p == buf + ilen )
601 return( POLARSSL_ERR_RSA_INVALID_PADDING );
602
603 if( *p++ != 0x01 )
604 return( POLARSSL_ERR_RSA_INVALID_PADDING );
605
606 break;
607#endif
608
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 default:
610
Paul Bakker40e46942009-01-03 21:51:57 +0000611 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 }
613
Paul Bakker27fdf462011-06-09 13:55:13 +0000614 if (ilen - (p - buf) > output_max_len)
Paul Bakker23986e52011-04-24 08:57:21 +0000615 return( POLARSSL_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000616
Paul Bakker27fdf462011-06-09 13:55:13 +0000617 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 memcpy( output, p, *olen );
619
620 return( 0 );
621}
622
623/*
624 * Do an RSA operation to sign the message digest
625 */
626int rsa_pkcs1_sign( rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000627 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +0000628 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 int mode,
630 int hash_id,
Paul Bakker23986e52011-04-24 08:57:21 +0000631 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000632 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 unsigned char *sig )
634{
Paul Bakker23986e52011-04-24 08:57:21 +0000635 size_t nb_pad, olen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 unsigned char *p = sig;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000637#if defined(POLARSSL_PKCS1_V21)
638 unsigned char salt[POLARSSL_MD_MAX_SIZE];
Paul Bakkered375ca2012-01-14 18:10:38 +0000639 unsigned int slen, hlen, offset = 0;
640 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +0000641 size_t msb;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000642 const md_info_t *md_info;
643 md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000644#else
645 (void) f_rng;
646 (void) p_rng;
647#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000648
649 olen = ctx->len;
650
651 switch( ctx->padding )
652 {
653 case RSA_PKCS_V15:
654
655 switch( hash_id )
656 {
Paul Bakkerfc22c442009-07-19 20:36:27 +0000657 case SIG_RSA_RAW:
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 nb_pad = olen - 3 - hashlen;
659 break;
660
Paul Bakker4593aea2009-02-09 22:32:35 +0000661 case SIG_RSA_MD2:
662 case SIG_RSA_MD4:
663 case SIG_RSA_MD5:
Paul Bakker5121ce52009-01-03 21:22:43 +0000664 nb_pad = olen - 3 - 34;
665 break;
666
Paul Bakker4593aea2009-02-09 22:32:35 +0000667 case SIG_RSA_SHA1:
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 nb_pad = olen - 3 - 35;
669 break;
670
Paul Bakkercde51572009-05-17 10:11:56 +0000671 case SIG_RSA_SHA224:
672 nb_pad = olen - 3 - 47;
673 break;
674
675 case SIG_RSA_SHA256:
676 nb_pad = olen - 3 - 51;
677 break;
678
679 case SIG_RSA_SHA384:
680 nb_pad = olen - 3 - 67;
681 break;
682
683 case SIG_RSA_SHA512:
684 nb_pad = olen - 3 - 83;
685 break;
686
687
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 default:
Paul Bakker40e46942009-01-03 21:51:57 +0000689 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000690 }
691
Paul Bakker3c16db92012-07-05 13:58:08 +0000692 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000693 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
695 *p++ = 0;
696 *p++ = RSA_SIGN;
697 memset( p, 0xFF, nb_pad );
698 p += nb_pad;
699 *p++ = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000700
701 switch( hash_id )
702 {
703 case SIG_RSA_RAW:
704 memcpy( p, hash, hashlen );
705 break;
706
707 case SIG_RSA_MD2:
708 memcpy( p, ASN1_HASH_MDX, 18 );
709 memcpy( p + 18, hash, 16 );
710 p[13] = 2; break;
711
712 case SIG_RSA_MD4:
713 memcpy( p, ASN1_HASH_MDX, 18 );
714 memcpy( p + 18, hash, 16 );
715 p[13] = 4; break;
716
717 case SIG_RSA_MD5:
718 memcpy( p, ASN1_HASH_MDX, 18 );
719 memcpy( p + 18, hash, 16 );
720 p[13] = 5; break;
721
722 case SIG_RSA_SHA1:
723 memcpy( p, ASN1_HASH_SHA1, 15 );
724 memcpy( p + 15, hash, 20 );
725 break;
726
727 case SIG_RSA_SHA224:
728 memcpy( p, ASN1_HASH_SHA2X, 19 );
729 memcpy( p + 19, hash, 28 );
730 p[1] += 28; p[14] = 4; p[18] += 28; break;
731
732 case SIG_RSA_SHA256:
733 memcpy( p, ASN1_HASH_SHA2X, 19 );
734 memcpy( p + 19, hash, 32 );
735 p[1] += 32; p[14] = 1; p[18] += 32; break;
736
737 case SIG_RSA_SHA384:
738 memcpy( p, ASN1_HASH_SHA2X, 19 );
739 memcpy( p + 19, hash, 48 );
740 p[1] += 48; p[14] = 2; p[18] += 48; break;
741
742 case SIG_RSA_SHA512:
743 memcpy( p, ASN1_HASH_SHA2X, 19 );
744 memcpy( p + 19, hash, 64 );
745 p[1] += 64; p[14] = 3; p[18] += 64; break;
746
747 default:
748 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
749 }
750
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 break;
752
Paul Bakker9dcc3222011-03-08 14:16:06 +0000753#if defined(POLARSSL_PKCS1_V21)
754 case RSA_PKCS_V21:
755
756 if( f_rng == NULL )
757 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
758
759 switch( hash_id )
760 {
761 case SIG_RSA_MD2:
762 case SIG_RSA_MD4:
763 case SIG_RSA_MD5:
764 hashlen = 16;
765 break;
766
767 case SIG_RSA_SHA1:
768 hashlen = 20;
769 break;
770
771 case SIG_RSA_SHA224:
772 hashlen = 28;
773 break;
774
775 case SIG_RSA_SHA256:
776 hashlen = 32;
777 break;
778
779 case SIG_RSA_SHA384:
780 hashlen = 48;
781 break;
782
783 case SIG_RSA_SHA512:
784 hashlen = 64;
785 break;
786
787 default:
788 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
789 }
790
791 md_info = md_info_from_type( ctx->hash_id );
792 if( md_info == NULL )
793 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
794
795 hlen = md_get_size( md_info );
Paul Bakker53019ae2011-03-25 13:58:48 +0000796 slen = hlen;
797
Paul Bakker9a736322012-11-14 12:39:52 +0000798 if( olen < hlen + slen + 2 )
799 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
800
Paul Bakker9dcc3222011-03-08 14:16:06 +0000801 memset( sig, 0, olen );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000802
Paul Bakker9dcc3222011-03-08 14:16:06 +0000803 msb = mpi_msb( &ctx->N ) - 1;
804
Paul Bakker53019ae2011-03-25 13:58:48 +0000805 // Generate salt of length slen
Paul Bakker9dcc3222011-03-08 14:16:06 +0000806 //
Paul Bakkera3d195c2011-11-27 21:07:34 +0000807 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
808 return( POLARSSL_ERR_RSA_RNG_FAILED + ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000809
810 // Note: EMSA-PSS encoding is over the length of N - 1 bits
811 //
812 msb = mpi_msb( &ctx->N ) - 1;
813 p += olen - hlen * 2 - 2;
814 *p++ = 0x01;
Paul Bakker53019ae2011-03-25 13:58:48 +0000815 memcpy( p, salt, slen );
816 p += slen;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000817
Paul Bakker02303e82013-01-03 11:08:31 +0100818 md_init_ctx( &md_ctx, md_info );
819
Paul Bakker9dcc3222011-03-08 14:16:06 +0000820 // Generate H = Hash( M' )
821 //
822 md_starts( &md_ctx );
823 md_update( &md_ctx, p, 8 );
824 md_update( &md_ctx, hash, hashlen );
Paul Bakker53019ae2011-03-25 13:58:48 +0000825 md_update( &md_ctx, salt, slen );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000826 md_finish( &md_ctx, p );
827
828 // Compensate for boundary condition when applying mask
829 //
830 if( msb % 8 == 0 )
831 offset = 1;
832
833 // maskedDB: Apply dbMask to DB
834 //
835 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
836
Paul Bakker40628ba2013-01-03 10:50:31 +0100837 md_free_ctx( &md_ctx );
838
Paul Bakker9dcc3222011-03-08 14:16:06 +0000839 msb = mpi_msb( &ctx->N ) - 1;
840 sig[0] &= 0xFF >> ( olen * 8 - msb );
841
842 p += hlen;
843 *p++ = 0xBC;
844 break;
845#endif
846
Paul Bakker5121ce52009-01-03 21:22:43 +0000847 default:
848
Paul Bakker40e46942009-01-03 21:51:57 +0000849 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000850 }
851
Paul Bakker5121ce52009-01-03 21:22:43 +0000852 return( ( mode == RSA_PUBLIC )
853 ? rsa_public( ctx, sig, sig )
854 : rsa_private( ctx, sig, sig ) );
855}
856
857/*
858 * Do an RSA operation and check the message digest
859 */
860int rsa_pkcs1_verify( rsa_context *ctx,
861 int mode,
862 int hash_id,
Paul Bakker23986e52011-04-24 08:57:21 +0000863 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000864 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +0000865 unsigned char *sig )
866{
Paul Bakker23986e52011-04-24 08:57:21 +0000867 int ret;
868 size_t len, siglen;
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 unsigned char *p, c;
Paul Bakker0be82f22012-10-03 20:36:33 +0000870 unsigned char buf[POLARSSL_MPI_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000871#if defined(POLARSSL_PKCS1_V21)
Paul Bakker1fe7d9b2011-11-15 15:26:03 +0000872 unsigned char result[POLARSSL_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000873 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +0000874 unsigned int hlen;
875 size_t slen, msb;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000876 const md_info_t *md_info;
877 md_context_t md_ctx;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000878#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000879 siglen = ctx->len;
880
Paul Bakker27fdf462011-06-09 13:55:13 +0000881 if( siglen < 16 || siglen > sizeof( buf ) )
Paul Bakker40e46942009-01-03 21:51:57 +0000882 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000883
884 ret = ( mode == RSA_PUBLIC )
885 ? rsa_public( ctx, sig, buf )
886 : rsa_private( ctx, sig, buf );
887
888 if( ret != 0 )
889 return( ret );
890
891 p = buf;
892
893 switch( ctx->padding )
894 {
895 case RSA_PKCS_V15:
896
897 if( *p++ != 0 || *p++ != RSA_SIGN )
Paul Bakker40e46942009-01-03 21:51:57 +0000898 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
900 while( *p != 0 )
901 {
902 if( p >= buf + siglen - 1 || *p != 0xFF )
Paul Bakker40e46942009-01-03 21:51:57 +0000903 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000904 p++;
905 }
906 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000907
Paul Bakker27fdf462011-06-09 13:55:13 +0000908 len = siglen - ( p - buf );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000909
Paul Bakker56a76842012-03-22 15:31:27 +0000910 if( len == 33 && hash_id == SIG_RSA_SHA1 )
911 {
912 if( memcmp( p, ASN1_HASH_SHA1_ALT, 13 ) == 0 &&
913 memcmp( p + 13, hash, 20 ) == 0 )
914 return( 0 );
915 else
916 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
917 }
Paul Bakker9dcc3222011-03-08 14:16:06 +0000918 if( len == 34 )
919 {
920 c = p[13];
921 p[13] = 0;
922
923 if( memcmp( p, ASN1_HASH_MDX, 18 ) != 0 )
924 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
925
926 if( ( c == 2 && hash_id == SIG_RSA_MD2 ) ||
927 ( c == 4 && hash_id == SIG_RSA_MD4 ) ||
928 ( c == 5 && hash_id == SIG_RSA_MD5 ) )
929 {
930 if( memcmp( p + 18, hash, 16 ) == 0 )
931 return( 0 );
932 else
933 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
934 }
935 }
936
937 if( len == 35 && hash_id == SIG_RSA_SHA1 )
938 {
939 if( memcmp( p, ASN1_HASH_SHA1, 15 ) == 0 &&
940 memcmp( p + 15, hash, 20 ) == 0 )
941 return( 0 );
942 else
943 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
944 }
945 if( ( len == 19 + 28 && p[14] == 4 && hash_id == SIG_RSA_SHA224 ) ||
946 ( len == 19 + 32 && p[14] == 1 && hash_id == SIG_RSA_SHA256 ) ||
947 ( len == 19 + 48 && p[14] == 2 && hash_id == SIG_RSA_SHA384 ) ||
948 ( len == 19 + 64 && p[14] == 3 && hash_id == SIG_RSA_SHA512 ) )
949 {
950 c = p[1] - 17;
951 p[1] = 17;
952 p[14] = 0;
953
954 if( p[18] == c &&
955 memcmp( p, ASN1_HASH_SHA2X, 18 ) == 0 &&
956 memcmp( p + 19, hash, c ) == 0 )
957 return( 0 );
958 else
959 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
960 }
961
962 if( len == hashlen && hash_id == SIG_RSA_RAW )
963 {
964 if( memcmp( p, hash, hashlen ) == 0 )
965 return( 0 );
966 else
967 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
968 }
969
Paul Bakker5121ce52009-01-03 21:22:43 +0000970 break;
971
Paul Bakker9dcc3222011-03-08 14:16:06 +0000972#if defined(POLARSSL_PKCS1_V21)
973 case RSA_PKCS_V21:
974
975 if( buf[siglen - 1] != 0xBC )
976 return( POLARSSL_ERR_RSA_INVALID_PADDING );
977
978 switch( hash_id )
979 {
980 case SIG_RSA_MD2:
981 case SIG_RSA_MD4:
982 case SIG_RSA_MD5:
983 hashlen = 16;
984 break;
985
986 case SIG_RSA_SHA1:
987 hashlen = 20;
988 break;
989
990 case SIG_RSA_SHA224:
991 hashlen = 28;
992 break;
993
994 case SIG_RSA_SHA256:
995 hashlen = 32;
996 break;
997
998 case SIG_RSA_SHA384:
999 hashlen = 48;
1000 break;
1001
1002 case SIG_RSA_SHA512:
1003 hashlen = 64;
1004 break;
1005
1006 default:
1007 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1008 }
1009
1010 md_info = md_info_from_type( ctx->hash_id );
1011 if( md_info == NULL )
1012 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1013
1014 hlen = md_get_size( md_info );
Paul Bakker53019ae2011-03-25 13:58:48 +00001015 slen = siglen - hlen - 1;
1016
Paul Bakker9dcc3222011-03-08 14:16:06 +00001017 memset( zeros, 0, 8 );
1018
Paul Bakker9dcc3222011-03-08 14:16:06 +00001019 // Note: EMSA-PSS verification is over the length of N - 1 bits
1020 //
1021 msb = mpi_msb( &ctx->N ) - 1;
1022
1023 // Compensate for boundary condition when applying mask
1024 //
1025 if( msb % 8 == 0 )
1026 {
1027 p++;
1028 siglen -= 1;
1029 }
1030 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1031 return( POLARSSL_ERR_RSA_BAD_INPUT_DATA );
1032
Paul Bakker02303e82013-01-03 11:08:31 +01001033 md_init_ctx( &md_ctx, md_info );
1034
Paul Bakker9dcc3222011-03-08 14:16:06 +00001035 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
1036
1037 buf[0] &= 0xFF >> ( siglen * 8 - msb );
1038
1039 while( *p == 0 && p < buf + siglen )
1040 p++;
1041
Paul Bakker02303e82013-01-03 11:08:31 +01001042 if( p == buf + siglen ||
1043 *p++ != 0x01 )
1044 {
1045 md_free_ctx( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001046 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker02303e82013-01-03 11:08:31 +01001047 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001048
Paul Bakker53019ae2011-03-25 13:58:48 +00001049 slen -= p - buf;
1050
Paul Bakker9dcc3222011-03-08 14:16:06 +00001051 // Generate H = Hash( M' )
1052 //
1053 md_starts( &md_ctx );
1054 md_update( &md_ctx, zeros, 8 );
1055 md_update( &md_ctx, hash, hashlen );
Paul Bakker53019ae2011-03-25 13:58:48 +00001056 md_update( &md_ctx, p, slen );
Paul Bakker1fe7d9b2011-11-15 15:26:03 +00001057 md_finish( &md_ctx, result );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001058
Paul Bakker40628ba2013-01-03 10:50:31 +01001059 md_free_ctx( &md_ctx );
1060
Paul Bakker1fe7d9b2011-11-15 15:26:03 +00001061 if( memcmp( p + slen, result, hlen ) == 0 )
Paul Bakker9dcc3222011-03-08 14:16:06 +00001062 return( 0 );
1063 else
1064 return( POLARSSL_ERR_RSA_VERIFY_FAILED );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001065#endif
1066
Paul Bakker5121ce52009-01-03 21:22:43 +00001067 default:
1068
Paul Bakker40e46942009-01-03 21:51:57 +00001069 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 }
1071
Paul Bakker40e46942009-01-03 21:51:57 +00001072 return( POLARSSL_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001073}
1074
1075/*
1076 * Free the components of an RSA key
1077 */
1078void rsa_free( rsa_context *ctx )
1079{
Paul Bakker6c591fa2011-05-05 11:49:20 +00001080 mpi_free( &ctx->RQ ); mpi_free( &ctx->RP ); mpi_free( &ctx->RN );
1081 mpi_free( &ctx->QP ); mpi_free( &ctx->DQ ); mpi_free( &ctx->DP );
1082 mpi_free( &ctx->Q ); mpi_free( &ctx->P ); mpi_free( &ctx->D );
1083 mpi_free( &ctx->E ); mpi_free( &ctx->N );
Paul Bakker5121ce52009-01-03 21:22:43 +00001084}
1085
Paul Bakker40e46942009-01-03 21:51:57 +00001086#if defined(POLARSSL_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001087
Paul Bakker40e46942009-01-03 21:51:57 +00001088#include "polarssl/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001089
1090/*
1091 * Example RSA-1024 keypair, for test purposes
1092 */
1093#define KEY_LEN 128
1094
1095#define RSA_N "9292758453063D803DD603D5E777D788" \
1096 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1097 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1098 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1099 "93A89813FBF3C4F8066D2D800F7C38A8" \
1100 "1AE31942917403FF4946B0A83D3D3E05" \
1101 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1102 "5E94BB77B07507233A0BC7BAC8F90F79"
1103
1104#define RSA_E "10001"
1105
1106#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1107 "66CA472BC44D253102F8B4A9D3BFA750" \
1108 "91386C0077937FE33FA3252D28855837" \
1109 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1110 "DF79C5CE07EE72C7F123142198164234" \
1111 "CABB724CF78B8173B9F880FC86322407" \
1112 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1113 "071513A1E85B5DFA031F21ECAE91A34D"
1114
1115#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1116 "2C01CAD19EA484A87EA4377637E75500" \
1117 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1118 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1119
1120#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1121 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1122 "910E4168387E3C30AA1E00C339A79508" \
1123 "8452DD96A9A5EA5D9DCA68DA636032AF"
1124
1125#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1126 "3C94D22288ACD763FD8E5600ED4A702D" \
1127 "F84198A5F06C2E72236AE490C93F07F8" \
1128 "3CC559CD27BC2D1CA488811730BB5725"
1129
1130#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1131 "D8AAEA56749EA28623272E4F7D0592AF" \
1132 "7C1F1313CAC9471B5C523BFE592F517B" \
1133 "407A1BD76C164B93DA2D32A383E58357"
1134
1135#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1136 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1137 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1138 "A74206CEC169D74BF5A8C50D6F48EA08"
1139
1140#define PT_LEN 24
1141#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1142 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1143
Paul Bakkera3d195c2011-11-27 21:07:34 +00001144static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001145{
Paul Bakkera3d195c2011-11-27 21:07:34 +00001146 size_t i;
1147
Paul Bakker545570e2010-07-18 09:00:25 +00001148 if( rng_state != NULL )
1149 rng_state = NULL;
1150
Paul Bakkera3d195c2011-11-27 21:07:34 +00001151 for( i = 0; i < len; ++i )
1152 output[i] = rand();
1153
1154 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001155}
1156
Paul Bakker5121ce52009-01-03 21:22:43 +00001157/*
1158 * Checkup routine
1159 */
1160int rsa_self_test( int verbose )
1161{
Paul Bakker23986e52011-04-24 08:57:21 +00001162 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001164 unsigned char rsa_plaintext[PT_LEN];
1165 unsigned char rsa_decrypted[PT_LEN];
1166 unsigned char rsa_ciphertext[KEY_LEN];
Paul Bakker5690efc2011-05-26 13:16:06 +00001167#if defined(POLARSSL_SHA1_C)
1168 unsigned char sha1sum[20];
1169#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001170
Paul Bakker21eb2802010-08-16 11:10:02 +00001171 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
1173 rsa.len = KEY_LEN;
1174 mpi_read_string( &rsa.N , 16, RSA_N );
1175 mpi_read_string( &rsa.E , 16, RSA_E );
1176 mpi_read_string( &rsa.D , 16, RSA_D );
1177 mpi_read_string( &rsa.P , 16, RSA_P );
1178 mpi_read_string( &rsa.Q , 16, RSA_Q );
1179 mpi_read_string( &rsa.DP, 16, RSA_DP );
1180 mpi_read_string( &rsa.DQ, 16, RSA_DQ );
1181 mpi_read_string( &rsa.QP, 16, RSA_QP );
1182
1183 if( verbose != 0 )
1184 printf( " RSA key validation: " );
1185
1186 if( rsa_check_pubkey( &rsa ) != 0 ||
1187 rsa_check_privkey( &rsa ) != 0 )
1188 {
1189 if( verbose != 0 )
1190 printf( "failed\n" );
1191
1192 return( 1 );
1193 }
1194
1195 if( verbose != 0 )
1196 printf( "passed\n PKCS#1 encryption : " );
1197
1198 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1199
Paul Bakker21eb2802010-08-16 11:10:02 +00001200 if( rsa_pkcs1_encrypt( &rsa, &myrand, NULL, RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 rsa_plaintext, rsa_ciphertext ) != 0 )
1202 {
1203 if( verbose != 0 )
1204 printf( "failed\n" );
1205
1206 return( 1 );
1207 }
1208
1209 if( verbose != 0 )
1210 printf( "passed\n PKCS#1 decryption : " );
1211
1212 if( rsa_pkcs1_decrypt( &rsa, RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001213 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001214 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001215 {
1216 if( verbose != 0 )
1217 printf( "failed\n" );
1218
1219 return( 1 );
1220 }
1221
1222 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1223 {
1224 if( verbose != 0 )
1225 printf( "failed\n" );
1226
1227 return( 1 );
1228 }
1229
Paul Bakker5690efc2011-05-26 13:16:06 +00001230#if defined(POLARSSL_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001231 if( verbose != 0 )
1232 printf( "passed\n PKCS#1 data sign : " );
1233
1234 sha1( rsa_plaintext, PT_LEN, sha1sum );
1235
Paul Bakker9dcc3222011-03-08 14:16:06 +00001236 if( rsa_pkcs1_sign( &rsa, NULL, NULL, RSA_PRIVATE, SIG_RSA_SHA1, 20,
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 sha1sum, rsa_ciphertext ) != 0 )
1238 {
1239 if( verbose != 0 )
1240 printf( "failed\n" );
1241
1242 return( 1 );
1243 }
1244
1245 if( verbose != 0 )
1246 printf( "passed\n PKCS#1 sig. verify: " );
1247
Paul Bakker4593aea2009-02-09 22:32:35 +00001248 if( rsa_pkcs1_verify( &rsa, RSA_PUBLIC, SIG_RSA_SHA1, 20,
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 sha1sum, rsa_ciphertext ) != 0 )
1250 {
1251 if( verbose != 0 )
1252 printf( "failed\n" );
1253
1254 return( 1 );
1255 }
1256
1257 if( verbose != 0 )
1258 printf( "passed\n\n" );
Paul Bakker5690efc2011-05-26 13:16:06 +00001259#endif /* POLARSSL_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
1261 rsa_free( &rsa );
1262
1263 return( 0 );
1264}
1265
1266#endif
1267
1268#endif