blob: ec7e1da9bce5e3224b1b9047522da08123007dc1 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
23 *
24 * http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
25 * http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
Janos Follath578517d2017-03-22 13:38:28 +000026 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
27 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
28 * Stefan Mangard
29 * https://arxiv.org/abs/1702.08719v2
30 *
Paul Bakker5121ce52009-01-03 21:22:43 +000031 */
32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/rsa.h"
42#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000043
Rich Evans00ab4702015-02-06 13:43:58 +000044#include <string.h>
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000048#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000051#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000052#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010056#else
Rich Evans00ab4702015-02-06 13:43:58 +000057#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020059#define mbedtls_calloc calloc
60#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010061#endif
62
Gilles Peskine8877ec22017-03-23 14:37:37 +010063/* Implementation that should never be optimized out by the compiler */
64static void mbedtls_zeroize( void *v, size_t n ) {
65 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
66}
67
Paul Bakker5121ce52009-01-03 21:22:43 +000068/*
69 * Initialize an RSA context
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000072 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000073 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_THREADING_C)
80 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020081#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000082}
83
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010084/*
85 * Set padding for an existing RSA context
86 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010088{
89 ctx->padding = padding;
90 ctx->hash_id = hash_id;
91}
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000094
95/*
96 * Generate an RSA keypair
97 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +000099 int (*f_rng)(void *, unsigned char *, size_t),
100 void *p_rng,
101 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000102{
103 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Paul Bakker21eb2802010-08-16 11:10:02 +0000106 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
Janos Follath95b30362016-09-21 13:18:12 +0100109 if( nbits % 2 )
110 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
111
112 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath1a59a502016-02-23 14:42:48 +0000113 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115 /*
116 * find primes P and Q with Q < P so that:
117 * GCD( E, (P-1)*(Q-1) ) == 1
118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 do
122 {
Janos Follath1a59a502016-02-23 14:42:48 +0000123 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000124 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Janos Follath95b30362016-09-21 13:18:12 +0100126 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000127 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 continue;
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200133 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 continue;
135
Janos Follath95b30362016-09-21 13:18:12 +0100136 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
137 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
140 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
141 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
142 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146 /*
147 * D = E^-1 mod ((P-1)*(Q-1))
148 * DP = D mod (P - 1)
149 * DQ = D mod (Q - 1)
150 * QP = Q^-1 mod P
151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
153 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
154 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
155 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200157 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159cleanup:
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163 if( ret != 0 )
164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_rsa_free( ctx );
166 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 }
168
Paul Bakker48377d92013-08-30 12:06:24 +0200169 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170}
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
174/*
175 * Check a public RSA key
176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000178{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000179 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000181
Paul Bakker48377d92013-08-30 12:06:24 +0200182 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200186 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
187 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200190 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
192 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194 return( 0 );
195}
196
197/*
198 * Check a private RSA key
199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201{
202 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 return( ret );
207
Paul Bakker37940d9f2009-07-10 22:38:58 +0000208 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
212 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
213 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
214 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
217 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
218 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
219 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
220 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
221 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
225 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
228 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
229 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000230 /*
231 * Check for a valid PKCS1v2 private key
232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
234 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
235 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
236 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
237 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
238 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
239 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 }
Paul Bakker48377d92013-08-30 12:06:24 +0200243
Paul Bakker5121ce52009-01-03 21:22:43 +0000244cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
246 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
247 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
248 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000251 return( ret );
252
Paul Bakker6c591fa2011-05-05 11:49:20 +0000253 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000255
256 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257}
258
259/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100260 * Check if contexts holding a public and private key match
261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100263{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
265 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100268 }
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
271 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100274 }
275
276 return( 0 );
277}
278
279/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 * Do an RSA public key operation
281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000283 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 unsigned char *output )
285{
Paul Bakker23986e52011-04-24 08:57:21 +0000286 int ret;
287 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200292#if defined(MBEDTLS_THREADING_C)
293 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
294 return( ret );
295#endif
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200301 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
302 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 }
304
305 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
307 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200311 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
312 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100313#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000316
317 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000319
320 return( 0 );
321}
322
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200323/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200324 * Generate or update blinding values, see section 10 of:
325 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200326 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200327 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200328 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200329static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200330 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
331{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200332 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200333
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200334 if( ctx->Vf.p != NULL )
335 {
336 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
338 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
339 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
340 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200341
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200342 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200343 }
344
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200345 /* Unblinding value: Vf = random number, invertible mod N */
346 do {
347 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
351 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
352 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200353
354 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
356 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200357
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200358
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200359cleanup:
360 return( ret );
361}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200362
Paul Bakker5121ce52009-01-03 21:22:43 +0000363/*
Janos Follath578517d2017-03-22 13:38:28 +0000364 * Exponent blinding supposed to prevent side-channel attacks using multiple
365 * traces of measurements to recover the RSA key. The more collisions are there,
366 * the more bits of the key can be recovered. See [3].
367 *
368 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
369 * observations on avarage.
370 *
371 * For example with 28 byte blinding to achieve 2 collisions the adversary has
372 * to make 2^112 observations on avarage.
373 *
374 * (With the currently (as of 2017 April) known best algorithms breaking 2048
375 * bit RSA requires approximately as much time as trying out 2^112 random keys.
376 * Thus in this sense with 28 byte blinding the security is not reduced by
377 * side-channel attacks like the one in [3])
378 *
379 * This countermeasure does not help if the key recovery is possible with a
380 * single trace.
381 */
382#define RSA_EXPONENT_BLINDING 28
383
384/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 * Do an RSA private key operation
386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200388 int (*f_rng)(void *, unsigned char *, size_t),
389 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000390 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 unsigned char *output )
392{
Paul Bakker23986e52011-04-24 08:57:21 +0000393 int ret;
394 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_mpi T, T1, T2;
Janos Follath9ef9f102017-03-22 15:13:15 +0000396 mbedtls_mpi P1, Q1, R;
Janos Follath578517d2017-03-22 13:38:28 +0000397#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath9ef9f102017-03-22 15:13:15 +0000398 mbedtls_mpi D_blind;
Janos Follath578517d2017-03-22 13:38:28 +0000399 mbedtls_mpi *D = &ctx->D;
Janos Follath9ef9f102017-03-22 15:13:15 +0000400#else
401 mbedtls_mpi DP_blind, DQ_blind;
402 mbedtls_mpi *DP = &ctx->DP;
403 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follath578517d2017-03-22 13:38:28 +0000404#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
Hanno Beckera82f8912017-11-06 15:08:27 +0000406 /* Temporaries holding the initial input and the double
407 * checked result; should be the same in the end. */
408 mbedtls_mpi I, C;
409
Manuel Pégourié-Gonnard9f44a802015-10-30 10:56:25 +0100410 /* Make sure we have private key info, prevent possible misuse */
Hanno Beckerde0b70c2017-11-06 15:08:53 +0000411#if defined(MBEDTLS_RSA_NO_CRT)
412 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
413 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
414 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 ||
415 ( f_rng != NULL && mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ) ||
416 ( f_rng != NULL && mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ) )
417 {
Manuel Pégourié-Gonnard9f44a802015-10-30 10:56:25 +0100418 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Beckerde0b70c2017-11-06 15:08:53 +0000419 }
420#else /* ! MBEDTLS_RSA_NO_CRT */
421 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
422 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 ||
423 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
424 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
425 mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
426 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
427 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
428 {
429 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
430 }
431#endif /* ! MBEDTLS_RSA_NO_CRT */
432
433
434#if defined(MBEDTLS_THREADING_C)
435 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
436 return( ret );
437#endif
Manuel Pégourié-Gonnard9f44a802015-10-30 10:56:25 +0100438
Hanno Beckera82f8912017-11-06 15:08:27 +0000439 mbedtls_mpi_init( &I );
440 mbedtls_mpi_init( &C );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200441
442 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follath9ef9f102017-03-22 15:13:15 +0000443 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
444
445
446 if( f_rng != NULL )
447 {
Janos Follath578517d2017-03-22 13:38:28 +0000448#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath9ef9f102017-03-22 15:13:15 +0000449 mbedtls_mpi_init( &D_blind );
450#else
451 mbedtls_mpi_init( &DP_blind );
452 mbedtls_mpi_init( &DQ_blind );
Janos Follath578517d2017-03-22 13:38:28 +0000453#endif
Janos Follath9ef9f102017-03-22 15:13:15 +0000454 }
Janos Follath578517d2017-03-22 13:38:28 +0000455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
457 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200459 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
460 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 }
462
Hanno Beckera82f8912017-11-06 15:08:27 +0000463 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
464
Paul Bakkerf451bac2013-08-30 15:37:02 +0200465 if( f_rng != NULL )
466 {
467 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200468 * Blinding
469 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200470 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200471 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
472 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follath578517d2017-03-22 13:38:28 +0000474
Janos Follath578517d2017-03-22 13:38:28 +0000475 /*
476 * Exponent blinding
477 */
478 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
479 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
480
Janos Follath9ef9f102017-03-22 15:13:15 +0000481#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath578517d2017-03-22 13:38:28 +0000482 /*
483 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
484 */
485 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
486 f_rng, p_rng ) );
487 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
488 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
489 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
490
491 D = &D_blind;
Janos Follath9ef9f102017-03-22 15:13:15 +0000492#else
493 /*
494 * DP_blind = ( P - 1 ) * R + DP
495 */
496 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
497 f_rng, p_rng ) );
498 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
499 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
500 &ctx->DP ) );
501
502 DP = &DP_blind;
503
504 /*
505 * DQ_blind = ( Q - 1 ) * R + DQ
506 */
507 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
508 f_rng, p_rng ) );
509 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
510 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
511 &ctx->DQ ) );
512
513 DQ = &DQ_blind;
Janos Follath578517d2017-03-22 13:38:28 +0000514#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200515 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath578517d2017-03-22 13:38:28 +0000518 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100519#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200520 /*
Janos Follath578517d2017-03-22 13:38:28 +0000521 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 *
523 * T1 = input ^ dP mod P
524 * T2 = input ^ dQ mod Q
525 */
Janos Follath9ef9f102017-03-22 15:13:15 +0000526 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
527 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529 /*
530 * T = (T1 - T2) * (Q^-1 mod P) mod P
531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
533 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
534 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000535
536 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200537 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
540 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
541#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200542
Paul Bakkerf451bac2013-08-30 15:37:02 +0200543 if( f_rng != NULL )
544 {
545 /*
546 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200547 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200548 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200549 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200551 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
Hanno Beckera82f8912017-11-06 15:08:27 +0000553 /* Verify the result to prevent glitching attacks. */
554 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
555 &ctx->N, &ctx->RN ) );
556 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
557 {
558 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
559 goto cleanup;
560 }
561
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
565cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200567 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
568 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200569#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follath9ef9f102017-03-22 15:13:15 +0000572 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
573
574 if( f_rng != NULL )
575 {
Janos Follath578517d2017-03-22 13:38:28 +0000576#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follath9ef9f102017-03-22 15:13:15 +0000577 mbedtls_mpi_free( &D_blind );
578#else
579 mbedtls_mpi_free( &DP_blind );
580 mbedtls_mpi_free( &DQ_blind );
Janos Follath578517d2017-03-22 13:38:28 +0000581#endif
Janos Follath9ef9f102017-03-22 15:13:15 +0000582 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
Hanno Beckera82f8912017-11-06 15:08:27 +0000584 mbedtls_mpi_free( &C );
585 mbedtls_mpi_free( &I );
586
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590 return( 0 );
591}
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000594/**
595 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
596 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000597 * \param dst buffer to mask
598 * \param dlen length of destination buffer
599 * \param src source of the mask generation
600 * \param slen length of the source buffer
601 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000602 */
Paul Bakker48377d92013-08-30 12:06:24 +0200603static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000605{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000607 unsigned char counter[4];
608 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000609 unsigned int hlen;
610 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000613 memset( counter, 0, 4 );
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000616
617 // Generate and apply dbMask
618 //
619 p = dst;
620
621 while( dlen > 0 )
622 {
623 use_len = hlen;
624 if( dlen < hlen )
625 use_len = dlen;
626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_md_starts( md_ctx );
628 mbedtls_md_update( md_ctx, src, slen );
629 mbedtls_md_update( md_ctx, counter, 4 );
630 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000631
632 for( i = 0; i < use_len; ++i )
633 *p++ ^= mask[i];
634
635 counter[3]++;
636
637 dlen -= use_len;
638 }
Gilles Peskine74fd8682017-05-05 19:24:06 +0200639
640 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000641}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100645/*
646 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100649 int (*f_rng)(void *, unsigned char *, size_t),
650 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100651 int mode,
652 const unsigned char *label, size_t label_len,
653 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100654 const unsigned char *input,
655 unsigned char *output )
656{
657 size_t olen;
658 int ret;
659 unsigned char *p = output;
660 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 const mbedtls_md_info_t *md_info;
662 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
665 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200666
667 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100671 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100673
674 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100676
Janos Follathe33f5592016-02-08 14:52:29 +0000677 // first comparison checks for overflow
678 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100680
681 memset( output, 0, olen );
682
683 *p++ = 0;
684
685 // Generate a random octet string seed
686 //
687 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100689
690 p += hlen;
691
692 // Construct DB
693 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100695 p += hlen;
696 p += olen - 2 * hlen - 2 - ilen;
697 *p++ = 1;
698 memcpy( p, input, ilen );
699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -0700701 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
702 {
703 mbedtls_md_free( &md_ctx );
704 return( ret );
705 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100706
707 // maskedDB: Apply dbMask to DB
708 //
709 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
710 &md_ctx );
711
712 // maskedSeed: Apply seedMask to seed
713 //
714 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
715 &md_ctx );
716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 return( ( mode == MBEDTLS_RSA_PUBLIC )
720 ? mbedtls_rsa_public( ctx, output, output )
721 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100722}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100726/*
727 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100730 int (*f_rng)(void *, unsigned char *, size_t),
731 void *p_rng,
732 int mode, size_t ilen,
733 const unsigned char *input,
734 unsigned char *output )
735{
736 size_t nb_pad, olen;
737 int ret;
738 unsigned char *p = output;
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
741 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200742
Janos Follath689a6272016-03-18 11:45:44 +0000743 // We don't check p_rng because it won't be dereferenced here
744 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100746
747 olen = ctx->len;
Hanno Beckerde0b70c2017-11-06 15:08:53 +0000748
Janos Follathe33f5592016-02-08 14:52:29 +0000749 // first comparison checks for overflow
750 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100752
753 nb_pad = olen - 3 - ilen;
754
755 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100759
760 while( nb_pad-- > 0 )
761 {
762 int rng_dl = 100;
763
764 do {
765 ret = f_rng( p_rng, p, 1 );
766 } while( *p == 0 && --rng_dl && ret == 0 );
767
768 // Check if RNG failed to generate data
769 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200770 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100772
773 p++;
774 }
775 }
776 else
777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100779
780 while( nb_pad-- > 0 )
781 *p++ = 0xFF;
782 }
783
784 *p++ = 0;
785 memcpy( p, input, ilen );
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( ( mode == MBEDTLS_RSA_PUBLIC )
788 ? mbedtls_rsa_public( ctx, output, output )
789 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100790}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100792
Paul Bakker5121ce52009-01-03 21:22:43 +0000793/*
794 * Add the message padding, then do an RSA operation
795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000797 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000798 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000799 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000800 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000801 unsigned char *output )
802{
Paul Bakker5121ce52009-01-03 21:22:43 +0000803 switch( ctx->padding )
804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805#if defined(MBEDTLS_PKCS1_V15)
806 case MBEDTLS_RSA_PKCS_V15:
807 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100808 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200809#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811#if defined(MBEDTLS_PKCS1_V21)
812 case MBEDTLS_RSA_PKCS_V21:
813 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100814 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000815#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000816
817 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000819 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000820}
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000823/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100824 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200827 int (*f_rng)(void *, unsigned char *, size_t),
828 void *p_rng,
829 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100830 const unsigned char *label, size_t label_len,
831 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100832 const unsigned char *input,
833 unsigned char *output,
834 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000835{
Paul Bakker23986e52011-04-24 08:57:21 +0000836 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100837 size_t ilen, i, pad_len;
838 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
840 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000841 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 const mbedtls_md_info_t *md_info;
843 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100844
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100845 /*
846 * Parameters sanity checks
847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
849 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000850
851 ilen = ctx->len;
852
Paul Bakker27fdf462011-06-09 13:55:13 +0000853 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100857 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100859
Janos Follath25da9b32016-02-11 11:08:18 +0000860 hlen = mbedtls_md_get_size( md_info );
861
862 // checking for integer underflow
863 if( 2 * hlen + 2 > ilen )
864 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
865
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100866 /*
867 * RSA operation
868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 ret = ( mode == MBEDTLS_RSA_PUBLIC )
870 ? mbedtls_rsa_public( ctx, input, buf )
871 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000872
873 if( ret != 0 )
Gilles Peskine8877ec22017-03-23 14:37:37 +0100874 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000875
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100876 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100877 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -0700880 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
881 {
882 mbedtls_md_free( &md_ctx );
Gilles Peskine8877ec22017-03-23 14:37:37 +0100883 goto cleanup;
Brian J Murray88c2d222016-06-23 12:57:03 -0700884 }
885
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100886
887 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100889
890 /* seed: Apply seedMask to maskedSeed */
891 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
892 &md_ctx );
893
894 /* DB: Apply dbMask to maskedDB */
895 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
896 &md_ctx );
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100899
900 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100901 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100902 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000903 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100904 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100906 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100907
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100908 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100909
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100910 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100911 for( i = 0; i < hlen; i++ )
912 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100913
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100914 /* Get zero-padding len, but always read till end of buffer
915 * (minus one, for the 01 byte) */
916 pad_len = 0;
917 pad_done = 0;
918 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
919 {
920 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100921 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100922 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100923
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100924 p += pad_len;
925 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100926
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100927 /*
928 * The only information "leaked" is whether the padding was correct or not
929 * (eg, no data is copied if it was not correct). This meets the
930 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
931 * the different error conditions.
932 */
933 if( bad != 0 )
Gilles Peskine8877ec22017-03-23 14:37:37 +0100934 {
935 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
936 goto cleanup;
937 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100938
Paul Bakker66d5d072014-06-17 16:39:18 +0200939 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine8877ec22017-03-23 14:37:37 +0100940 {
941 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
942 goto cleanup;
943 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100944
945 *olen = ilen - (p - buf);
946 memcpy( output, p, *olen );
Gilles Peskine8877ec22017-03-23 14:37:37 +0100947 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100948
Gilles Peskine8877ec22017-03-23 14:37:37 +0100949cleanup:
950 mbedtls_zeroize( buf, sizeof( buf ) );
951 mbedtls_zeroize( lhash, sizeof( lhash ) );
952
953 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100954}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinef50ee602018-10-02 22:44:41 +0200958/** Turn zero-or-nonzero into zero-or-all-bits-one, without branches.
959 *
960 * \param value The value to analyze.
Gilles Peskine9f11f212018-10-04 18:32:29 +0200961 * \return Zero if \p value is zero, otherwise all-bits-one.
Gilles Peskinef50ee602018-10-02 22:44:41 +0200962 */
Gilles Peskine9f11f212018-10-04 18:32:29 +0200963static unsigned all_or_nothing_int( unsigned value )
Gilles Peskinef50ee602018-10-02 22:44:41 +0200964{
965 /* MSVC has a warning about unary minus on unsigned, but this is
966 * well-defined and precisely what we want to do here */
967#if defined(_MSC_VER)
968#pragma warning( push )
969#pragma warning( disable : 4146 )
970#endif
971 return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) );
972#if defined(_MSC_VER)
973#pragma warning( pop )
974#endif
975}
976
977/** Choose between two integer values, without branches.
978 *
Gilles Peskine9f11f212018-10-04 18:32:29 +0200979 * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled
980 * to code using bitwise operation rather than a branch.
981 *
982 * \param cond Condition to test.
983 * \param if1 Value to use if \p cond is nonzero.
984 * \param if0 Value to use if \p cond is zero.
985 * \return \c if1 if \p cond is nonzero, otherwise \c if0.
Gilles Peskinef50ee602018-10-02 22:44:41 +0200986 */
Gilles Peskine9f11f212018-10-04 18:32:29 +0200987static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 )
Gilles Peskinef50ee602018-10-02 22:44:41 +0200988{
Gilles Peskine9f11f212018-10-04 18:32:29 +0200989 unsigned mask = all_or_nothing_int( cond );
Gilles Peskinef50ee602018-10-02 22:44:41 +0200990 return( ( mask & if1 ) | (~mask & if0 ) );
991}
992
Paul Bakkerb3869132013-02-28 17:21:01 +0100993/*
994 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200997 int (*f_rng)(void *, unsigned char *, size_t),
998 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100999 int mode, size_t *olen,
1000 const unsigned char *input,
1001 unsigned char *output,
Gilles Peskinef7a88142018-10-02 22:43:06 +02001002 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +01001003{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001004 int ret;
Gilles Peskinef7a88142018-10-02 22:43:06 +02001005 size_t ilen = ctx->len;
1006 size_t pad_count = 0;
1007 size_t i;
1008 unsigned bad = 0;
1009 unsigned char pad_done = 0;
Gilles Peskinef50ee602018-10-02 22:44:41 +02001010 size_t plaintext_size = 0;
1011 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1012 ilen - 11 :
1013 output_max_len );
Gilles Peskine20365082018-10-04 19:13:43 +02001014 unsigned output_too_large;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskinef7a88142018-10-02 22:43:06 +02001016 unsigned char *p = buf;
Paul Bakkerb3869132013-02-28 17:21:01 +01001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1019 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001020
Paul Bakkerb3869132013-02-28 17:21:01 +01001021 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1025 ? mbedtls_rsa_public( ctx, input, buf )
1026 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001027
1028 if( ret != 0 )
Gilles Peskine8877ec22017-03-23 14:37:37 +01001029 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001030
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001031 /*
1032 * Check and get padding len in "constant-time"
1033 */
1034 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001035
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001036 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001040
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001041 /* Get padding len, but always read till end of buffer
1042 * (minus one, for the 00 byte) */
1043 for( i = 0; i < ilen - 3; i++ )
1044 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001045 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1046 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001047 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001048
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001049 p += pad_count;
1050 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001051 }
1052 else
1053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001055
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001056 /* Get padding len, but always read till end of buffer
1057 * (minus one, for the 00 byte) */
1058 for( i = 0; i < ilen - 3; i++ )
1059 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001060 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001061 pad_count += ( pad_done == 0 );
1062 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001063
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001064 p += pad_count;
1065 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 }
1067
Gilles Peskinef50ee602018-10-02 22:44:41 +02001068 /* There must be at least 8 bytes of padding. */
Janos Follathe007c9f2016-02-12 13:30:09 +00001069 bad |= ( pad_count < 8 );
Janos Follatha9583432016-02-08 13:59:25 +00001070
Gilles Peskinef50ee602018-10-02 22:44:41 +02001071 /* If the padding is valid, set plaintext_size to the number of
1072 * remaining bytes after stripping the padding. If the padding
1073 * is invalid, avoid leaking this fact through the size of the
1074 * output: use the maximum message size that fits in the output
1075 * buffer. Do it without branches to avoid leaking the padding
1076 * validity through timing. RSA keys are small enough that all the
1077 * size_t values involved fit in unsigned int. */
Gilles Peskine9f11f212018-10-04 18:32:29 +02001078 plaintext_size = if_int( bad,
1079 (unsigned) plaintext_max_size,
1080 (unsigned) ( ilen - ( p - buf ) ) );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001081
Gilles Peskine20365082018-10-04 19:13:43 +02001082 /* Set output_too_large to 0 if the plaintext fits in the output
1083 * buffer and to 1 otherwise. This is the sign bit (1 for negative)
1084 * of (output_max_len - plaintext_size). */
1085 output_too_large = ( ( output_max_len - plaintext_size ) >>
1086 ( ( sizeof( output_max_len ) * 8 - 1 ) ) );
Paul Bakker060c5682009-01-12 21:48:39 +00001087
Gilles Peskine20365082018-10-04 19:13:43 +02001088 /* Set ret without branches to avoid timing attacks. Return:
1089 * - INVALID_PADDING if the padding is bad (bad != 0).
1090 * - OUTPUT_TOO_LARGE if the padding is good but the decrypted
1091 * plaintext does not fit in the output buffer.
1092 * - 0 if the padding is correct. */
1093 ret = - if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING,
1094 if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE,
1095 0 ) );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001096
Gilles Peskine20365082018-10-04 19:13:43 +02001097 /* If the padding is bad or the plaintext is too large, zero the
1098 * data that we're about to copy to the output buffer.
1099 * We need to copy the same amount of data
Gilles Peskinef50ee602018-10-02 22:44:41 +02001100 * from the same buffer whether the padding is good or not to
1101 * avoid leaking the padding validity through overall timing or
1102 * through memory or cache access patterns. */
1103 for( i = 11; i < ilen; i++ )
Gilles Peskine20365082018-10-04 19:13:43 +02001104 buf[i] &= ~( bad | output_too_large );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001105
Gilles Peskine20365082018-10-04 19:13:43 +02001106 /* If the plaintext is too large, truncate it to the buffer size.
1107 * Copy anyway to avoid revealing the length through timing, because
1108 * revealing the length is as bad as revealing the padding validity
1109 * for a Bleichenbacher attack. */
1110 plaintext_size = if_int( output_too_large,
1111 (unsigned) plaintext_max_size,
1112 (unsigned) plaintext_size );
Gilles Peskinef50ee602018-10-02 22:44:41 +02001113
Gilles Peskine20365082018-10-04 19:13:43 +02001114 /* Move the plaintext to the beginning of the working buffer so that
1115 * its position no longer depends on the padding and we have enough
1116 * room from the beginning of the plaintext to copy a number of bytes
1117 * that does not depend on the padding. */
1118 /* FIXME: we need a constant-time, constant-trace memmove here. */
1119 memmove( buf, p, plaintext_size );
1120
1121 /* Finally copy the decrypted plaintext plus trailing data
1122 * into the output buffer. */
1123 memcpy( output, buf, plaintext_max_size );
1124
1125 /* Report the amount of data we copied to the output buffer. In case
1126 * of errors (bad padding or output too large), the value of *olen
1127 * when this function returns is not specified. Making it equivalent
1128 * to the good case limits the risks of leaking the padding validity. */
Gilles Peskinef50ee602018-10-02 22:44:41 +02001129 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001130
Gilles Peskine8877ec22017-03-23 14:37:37 +01001131cleanup:
1132 mbedtls_zeroize( buf, sizeof( buf ) );
1133
1134 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001137
1138/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001139 * Do an RSA operation, then remove the message padding
1140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001142 int (*f_rng)(void *, unsigned char *, size_t),
1143 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001144 int mode, size_t *olen,
1145 const unsigned char *input,
1146 unsigned char *output,
1147 size_t output_max_len)
1148{
1149 switch( ctx->padding )
1150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#if defined(MBEDTLS_PKCS1_V15)
1152 case MBEDTLS_RSA_PKCS_V15:
1153 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001154 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001155#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_PKCS1_V21)
1158 case MBEDTLS_RSA_PKCS_V21:
1159 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001160 olen, input, output,
1161 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001162#endif
1163
1164 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001166 }
1167}
1168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001170/*
1171 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001174 int (*f_rng)(void *, unsigned char *, size_t),
1175 void *p_rng,
1176 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001178 unsigned int hashlen,
1179 const unsigned char *hash,
1180 unsigned char *sig )
1181{
1182 size_t olen;
1183 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001185 unsigned int slen, hlen, offset = 0;
1186 int ret;
1187 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 const mbedtls_md_info_t *md_info;
1189 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1192 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001193
1194 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001196
1197 olen = ctx->len;
1198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001200 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001201 // Gather length of hash to sign
1202 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001204 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001208 }
1209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001211 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001215 slen = hlen;
1216
1217 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001219
1220 memset( sig, 0, olen );
1221
Paul Bakkerb3869132013-02-28 17:21:01 +01001222 // Generate salt of length slen
1223 //
1224 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001226
1227 // Note: EMSA-PSS encoding is over the length of N - 1 bits
1228 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001229 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001230 p += olen - hlen * 2 - 2;
1231 *p++ = 0x01;
1232 memcpy( p, salt, slen );
1233 p += slen;
1234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -07001236 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1237 {
1238 mbedtls_md_free( &md_ctx );
Gilles Peskine74fd8682017-05-05 19:24:06 +02001239 /* No need to zeroize salt: we didn't use it. */
Brian J Murray88c2d222016-06-23 12:57:03 -07001240 return( ret );
1241 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001242
1243 // Generate H = Hash( M' )
1244 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 mbedtls_md_starts( &md_ctx );
1246 mbedtls_md_update( &md_ctx, p, 8 );
1247 mbedtls_md_update( &md_ctx, hash, hashlen );
1248 mbedtls_md_update( &md_ctx, salt, slen );
1249 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine74fd8682017-05-05 19:24:06 +02001250 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001251
1252 // Compensate for boundary condition when applying mask
1253 //
1254 if( msb % 8 == 0 )
1255 offset = 1;
1256
1257 // maskedDB: Apply dbMask to DB
1258 //
1259 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001262
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001263 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001264 sig[0] &= 0xFF >> ( olen * 8 - msb );
1265
1266 p += hlen;
1267 *p++ = 0xBC;
1268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 return( ( mode == MBEDTLS_RSA_PUBLIC )
1270 ? mbedtls_rsa_public( ctx, sig, sig )
1271 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001276/*
1277 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1278 */
1279/*
1280 * Do an RSA operation to sign the message digest
1281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001283 int (*f_rng)(void *, unsigned char *, size_t),
1284 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001285 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001287 unsigned int hashlen,
1288 const unsigned char *hash,
1289 unsigned char *sig )
1290{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001291 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001292 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001293 const char *oid = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1296 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001297
1298 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001299 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001304 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1308 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001309
Paul Bakkerc70b9822013-04-07 22:00:46 +02001310 nb_pad -= 10 + oid_size;
1311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001313 }
1314
Paul Bakkerc70b9822013-04-07 22:00:46 +02001315 nb_pad -= hashlen;
1316
Paul Bakkerb3869132013-02-28 17:21:01 +01001317 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001319
1320 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001322 memset( p, 0xFF, nb_pad );
1323 p += nb_pad;
1324 *p++ = 0;
1325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001327 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001328 memcpy( p, hash, hashlen );
1329 }
1330 else
1331 {
1332 /*
1333 * DigestInfo ::= SEQUENCE {
1334 * digestAlgorithm DigestAlgorithmIdentifier,
1335 * digest Digest }
1336 *
1337 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1338 *
1339 * Digest ::= OCTET STRING
1340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001342 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001344 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001346 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001347 memcpy( p, oid, oid_size );
1348 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001350 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001352 *p++ = hashlen;
1353 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001354 }
1355
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001356 if( mode == MBEDTLS_RSA_PUBLIC )
1357 return( mbedtls_rsa_public( ctx, sig, sig ) );
1358
Hanno Becker21f83752017-11-06 15:09:33 +00001359 return( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001360}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001362
1363/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 * Do an RSA operation to sign the message digest
1365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001367 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001368 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001371 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001372 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 unsigned char *sig )
1374{
Paul Bakker5121ce52009-01-03 21:22:43 +00001375 switch( ctx->padding )
1376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#if defined(MBEDTLS_PKCS1_V15)
1378 case MBEDTLS_RSA_PKCS_V15:
1379 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001380 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001381#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_PKCS1_V21)
1384 case MBEDTLS_RSA_PKCS_V21:
1385 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001386 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001387#endif
1388
Paul Bakker5121ce52009-01-03 21:22:43 +00001389 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001392}
1393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001395/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001396 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001399 int (*f_rng)(void *, unsigned char *, size_t),
1400 void *p_rng,
1401 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001403 unsigned int hashlen,
1404 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001406 int expected_salt_len,
1407 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001408{
Paul Bakker23986e52011-04-24 08:57:21 +00001409 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001410 size_t siglen;
1411 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskined0cd8552017-10-17 19:02:13 +02001413 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001415 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001416 unsigned int hlen;
Gilles Peskined0cd8552017-10-17 19:02:13 +02001417 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 const mbedtls_md_info_t *md_info;
1419 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1422 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001423
Paul Bakker5121ce52009-01-03 21:22:43 +00001424 siglen = ctx->len;
1425
Paul Bakker27fdf462011-06-09 13:55:13 +00001426 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1430 ? mbedtls_rsa_public( ctx, sig, buf )
1431 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001432
1433 if( ret != 0 )
1434 return( ret );
1435
1436 p = buf;
1437
Paul Bakkerb3869132013-02-28 17:21:01 +01001438 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001442 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001443 // Gather length of hash to sign
1444 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001446 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001450 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001453 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001457
Paul Bakkerb3869132013-02-28 17:21:01 +01001458 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001459
Paul Bakkerb3869132013-02-28 17:21:01 +01001460 // Note: EMSA-PSS verification is over the length of N - 1 bits
1461 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001462 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001463
Gilles Peskine31a2d142017-10-19 15:23:49 +02001464 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1465 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1466
1467 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001468 if( msb % 8 == 0 )
1469 {
1470 p++;
1471 siglen -= 1;
1472 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001473
Gilles Peskine9e205822017-10-18 19:03:42 +02001474 if( siglen < hlen + 2 )
1475 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1476 hash_start = p + siglen - hlen - 1;
1477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -07001479 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1480 {
1481 mbedtls_md_free( &md_ctx );
1482 return( ret );
1483 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001484
Gilles Peskined0cd8552017-10-17 19:02:13 +02001485 mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001486
Paul Bakkerb3869132013-02-28 17:21:01 +01001487 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001488
Gilles Peskined0cd8552017-10-17 19:02:13 +02001489 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001490 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001491
Gilles Peskine9745cfd2017-10-19 17:46:14 +02001492 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 mbedtls_md_free( &md_ctx );
1495 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001496 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001497
Gilles Peskined0cd8552017-10-17 19:02:13 +02001498 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskined0cd8552017-10-17 19:02:13 +02001501 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 mbedtls_md_free( &md_ctx );
1504 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001505 }
1506
Paul Bakkerb3869132013-02-28 17:21:01 +01001507 // Generate H = Hash( M' )
1508 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 mbedtls_md_starts( &md_ctx );
1510 mbedtls_md_update( &md_ctx, zeros, 8 );
1511 mbedtls_md_update( &md_ctx, hash, hashlen );
Gilles Peskined0cd8552017-10-17 19:02:13 +02001512 mbedtls_md_update( &md_ctx, p, observed_salt_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001516
Gilles Peskined0cd8552017-10-17 19:02:13 +02001517 if( memcmp( hash_start, result, hlen ) == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001518 return( 0 );
1519 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001521}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001522
1523/*
1524 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001527 int (*f_rng)(void *, unsigned char *, size_t),
1528 void *p_rng,
1529 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001531 unsigned int hashlen,
1532 const unsigned char *hash,
1533 const unsigned char *sig )
1534{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1536 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001537 : md_alg;
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001540 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001542 sig ) );
1543
1544}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001548/*
1549 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001552 int (*f_rng)(void *, unsigned char *, size_t),
1553 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001554 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001556 unsigned int hashlen,
1557 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001558 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001559{
1560 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001561 size_t len, siglen, asn1_len;
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001562 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1564 mbedtls_md_type_t msg_md_alg;
1565 const mbedtls_md_info_t *md_info;
1566 mbedtls_asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1569 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001570
1571 siglen = ctx->len;
1572
1573 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1577 ? mbedtls_rsa_public( ctx, sig, buf )
1578 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001579
1580 if( ret != 0 )
1581 return( ret );
1582
1583 p = buf;
1584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1586 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001587
1588 while( *p != 0 )
1589 {
1590 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001592 p++;
1593 }
Manuel Pégourié-Gonnard230ee312017-05-11 12:49:51 +02001594 p++; /* skip 00 byte */
1595
1596 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
1597 if( p - buf < 11 )
1598 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001599
1600 len = siglen - ( p - buf );
1601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001603 {
1604 if( memcmp( p, hash, hashlen ) == 0 )
1605 return( 0 );
1606 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001608 }
1609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001611 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1613 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001614
1615 end = p + len;
1616
Gilles Peskinebd908512017-05-04 12:48:39 +02001617 /*
1618 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
1619 * Insist on 2-byte length tags, to protect against variants of
1620 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
1621 */
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001622 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1624 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1625 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001626 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001628
Gilles Peskinebd908512017-05-04 12:48:39 +02001629 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1631 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1632 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001633 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001635
Gilles Peskinebd908512017-05-04 12:48:39 +02001636 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1638 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001639 if( p != p0 + 2 )
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001640 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001641
1642 oid.p = p;
1643 p += oid.len;
1644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1646 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001647
1648 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001650
1651 /*
1652 * assume the algorithm parameters must be NULL
1653 */
Gilles Peskinebd908512017-05-04 12:48:39 +02001654 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1656 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001657 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001659
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001660 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001661 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1662 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001663 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001665
1666 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001668
1669 p += hashlen;
1670
1671 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001673
1674 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
1678/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001679 * Do an RSA operation and check the message digest
1680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001682 int (*f_rng)(void *, unsigned char *, size_t),
1683 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001684 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001686 unsigned int hashlen,
1687 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001688 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001689{
1690 switch( ctx->padding )
1691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_PKCS1_V15)
1693 case MBEDTLS_RSA_PKCS_V15:
1694 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001695 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001696#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#if defined(MBEDTLS_PKCS1_V21)
1699 case MBEDTLS_RSA_PKCS_V21:
1700 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001701 hashlen, hash, sig );
1702#endif
1703
1704 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001706 }
1707}
1708
1709/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001710 * Copy the components of an RSA key
1711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001713{
1714 int ret;
1715
1716 dst->ver = src->ver;
1717 dst->len = src->len;
1718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001719 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1720 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1723 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1724 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1725 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1726 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1727 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1730 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1731 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1734 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001735
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001736 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001737 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001738
1739cleanup:
1740 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001741 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001742
1743 return( ret );
1744}
1745
1746/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001747 * Free the components of an RSA key
1748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1752 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1753 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1754 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1755 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757#if defined(MBEDTLS_THREADING_C)
1758 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001759#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001760}
1761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001763
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001764#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
1766/*
1767 * Example RSA-1024 keypair, for test purposes
1768 */
1769#define KEY_LEN 128
1770
1771#define RSA_N "9292758453063D803DD603D5E777D788" \
1772 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1773 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1774 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1775 "93A89813FBF3C4F8066D2D800F7C38A8" \
1776 "1AE31942917403FF4946B0A83D3D3E05" \
1777 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1778 "5E94BB77B07507233A0BC7BAC8F90F79"
1779
1780#define RSA_E "10001"
1781
1782#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1783 "66CA472BC44D253102F8B4A9D3BFA750" \
1784 "91386C0077937FE33FA3252D28855837" \
1785 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1786 "DF79C5CE07EE72C7F123142198164234" \
1787 "CABB724CF78B8173B9F880FC86322407" \
1788 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1789 "071513A1E85B5DFA031F21ECAE91A34D"
1790
1791#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1792 "2C01CAD19EA484A87EA4377637E75500" \
1793 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1794 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1795
1796#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1797 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1798 "910E4168387E3C30AA1E00C339A79508" \
1799 "8452DD96A9A5EA5D9DCA68DA636032AF"
1800
1801#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1802 "3C94D22288ACD763FD8E5600ED4A702D" \
1803 "F84198A5F06C2E72236AE490C93F07F8" \
1804 "3CC559CD27BC2D1CA488811730BB5725"
1805
1806#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1807 "D8AAEA56749EA28623272E4F7D0592AF" \
1808 "7C1F1313CAC9471B5C523BFE592F517B" \
1809 "407A1BD76C164B93DA2D32A383E58357"
1810
1811#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1812 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1813 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1814 "A74206CEC169D74BF5A8C50D6F48EA08"
1815
1816#define PT_LEN 24
1817#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1818 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001821static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001822{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001823#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001824 size_t i;
1825
Paul Bakker545570e2010-07-18 09:00:25 +00001826 if( rng_state != NULL )
1827 rng_state = NULL;
1828
Paul Bakkera3d195c2011-11-27 21:07:34 +00001829 for( i = 0; i < len; ++i )
1830 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001831#else
1832 if( rng_state != NULL )
1833 rng_state = NULL;
1834
1835 arc4random_buf( output, len );
1836#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001837
Paul Bakkera3d195c2011-11-27 21:07:34 +00001838 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001839}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001841
Paul Bakker5121ce52009-01-03 21:22:43 +00001842/*
1843 * Checkup routine
1844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001846{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001847 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001848#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001849 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001851 unsigned char rsa_plaintext[PT_LEN];
1852 unsigned char rsa_decrypted[PT_LEN];
1853 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001855 unsigned char sha1sum[20];
1856#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001859
1860 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1862 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1863 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1864 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1865 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1866 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1867 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1868 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
1870 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1874 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001875 {
1876 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001878
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001879 ret = 1;
1880 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001881 }
1882
1883 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
1886 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 rsa_plaintext, rsa_ciphertext ) != 0 )
1890 {
1891 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001893
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001894 ret = 1;
1895 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001896 }
1897
1898 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001902 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001903 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 {
1905 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001907
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001908 ret = 1;
1909 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 }
1911
1912 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1913 {
1914 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001916
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001917 ret = 1;
1918 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001919 }
1920
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001921 if( verbose != 0 )
1922 mbedtls_printf( "passed\n" );
1923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001925 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001926 mbedtls_printf( "PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001930 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001931 sha1sum, rsa_ciphertext ) != 0 )
1932 {
1933 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001936 ret = 1;
1937 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001938 }
1939
1940 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001944 sha1sum, rsa_ciphertext ) != 0 )
1945 {
1946 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001948
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001949 ret = 1;
1950 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001951 }
1952
1953 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001954 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001956
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001957 if( verbose != 0 )
1958 mbedtls_printf( "\n" );
1959
Paul Bakker3d8fb632014-04-17 12:42:41 +02001960cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 mbedtls_rsa_free( &rsa );
1962#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001963 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001964#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001965 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001966}
1967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001968#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970#endif /* MBEDTLS_RSA_C */