blob: 11e2b7385222fb918bcb25e9fefabdf8ee7b71ae [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.
961 * \return \c 0 if \p value is zero, otherwise \c 0xff.
962 */
963static unsigned unsigned_all_or_nothing( unsigned value )
964{
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 *
979 * \param mask Either \c 0 or \c ~0.
980 * \param if0 Value to use if \p mask = \c 0.
981 * \param if1 Value to use if \p mask = \c ~0.
982 * \return \c if1 if \p value is zero, otherwise \c if0.
983 */
984static unsigned choose_int_from_mask( unsigned mask, unsigned if1, unsigned if0 )
985{
986 return( ( mask & if1 ) | (~mask & if0 ) );
987}
988
Paul Bakkerb3869132013-02-28 17:21:01 +0100989/*
990 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200993 int (*f_rng)(void *, unsigned char *, size_t),
994 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100995 int mode, size_t *olen,
996 const unsigned char *input,
997 unsigned char *output,
Gilles Peskinef7a88142018-10-02 22:43:06 +0200998 size_t output_max_len )
Paul Bakkerb3869132013-02-28 17:21:01 +0100999{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001000 int ret;
Gilles Peskinef7a88142018-10-02 22:43:06 +02001001 size_t ilen = ctx->len;
1002 size_t pad_count = 0;
1003 size_t i;
1004 unsigned bad = 0;
1005 unsigned char pad_done = 0;
Gilles Peskinef50ee602018-10-02 22:44:41 +02001006 size_t plaintext_size = 0;
1007 size_t plaintext_max_size = ( output_max_len > ilen - 11 ?
1008 ilen - 11 :
1009 output_max_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskinef7a88142018-10-02 22:43:06 +02001011 unsigned char *p = buf;
Paul Bakkerb3869132013-02-28 17:21:01 +01001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1014 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001015
Paul Bakkerb3869132013-02-28 17:21:01 +01001016 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1020 ? mbedtls_rsa_public( ctx, input, buf )
1021 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001022
1023 if( ret != 0 )
Gilles Peskine8877ec22017-03-23 14:37:37 +01001024 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001025
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001026 /*
1027 * Check and get padding len in "constant-time"
1028 */
1029 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001030
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001031 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001035
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001036 /* Get padding len, but always read till end of buffer
1037 * (minus one, for the 00 byte) */
1038 for( i = 0; i < ilen - 3; i++ )
1039 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001040 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1041 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001042 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001043
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001044 p += pad_count;
1045 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001046 }
1047 else
1048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001050
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001051 /* Get padding len, but always read till end of buffer
1052 * (minus one, for the 00 byte) */
1053 for( i = 0; i < ilen - 3; i++ )
1054 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001055 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001056 pad_count += ( pad_done == 0 );
1057 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001058
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001059 p += pad_count;
1060 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001061 }
1062
Gilles Peskinef50ee602018-10-02 22:44:41 +02001063 /* There must be at least 8 bytes of padding. */
Janos Follathe007c9f2016-02-12 13:30:09 +00001064 bad |= ( pad_count < 8 );
Janos Follatha9583432016-02-08 13:59:25 +00001065
Gilles Peskinef50ee602018-10-02 22:44:41 +02001066 /* Set bad to zero if the padding is valid and
1067 * all-bits-one otherwise. The whole calculation of bad
1068 * is done in such a way to avoid branches. */
1069 bad = unsigned_all_or_nothing( bad );
Paul Bakker8804f692013-02-28 18:06:26 +01001070
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. */
1078 plaintext_size = choose_int_from_mask( bad,
1079 (unsigned) plaintext_max_size,
1080 (unsigned) ( ilen - ( p - buf ) ) );
1081
1082 /* Check if the decrypted plaintext fits in the output buffer.
1083 * If the padding is bad, this will always be the case,
1084 * thus we don't leak the padding validity by trying to produce
1085 * a larger output than what the caller expects. */
1086 if( plaintext_size > output_max_len )
Gilles Peskine8877ec22017-03-23 14:37:37 +01001087 {
1088 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1089 goto cleanup;
1090 }
Paul Bakker060c5682009-01-12 21:48:39 +00001091
Gilles Peskinef50ee602018-10-02 22:44:41 +02001092 /* Set ret to INVALID_PADDING if the padding is bad and to 0
1093 * otherwise. At this point, the variable bad is zero if
1094 * the padding is good and can be any nonzero value otherwise.
1095 * Do this without branches to avoid timing attacks. */
1096 ret = - ( bad & ( - MBEDTLS_ERR_RSA_INVALID_PADDING ) );
1097
1098 /* If the padding is bad, zero the data that we're about to copy
1099 * to the output buffer. We need to copy the same amount of data
1100 * 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++ )
1104 buf[i] &= ~bad;
1105
1106 /* Copy the decrypted plaintext from the end of the buffer. */
1107 memcpy( output, buf + ilen - plaintext_size, plaintext_size );
1108
1109 /* Report the amount of data we copied to the output buffer.
1110 * When the padding is invalid, the value of *olen when this
1111 * function returns is not specified. Making it equivalent to
1112 * the good-padding case limits the risks of leaking the
1113 * padding validity. */
1114 *olen = plaintext_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001115
Gilles Peskine8877ec22017-03-23 14:37:37 +01001116cleanup:
1117 mbedtls_zeroize( buf, sizeof( buf ) );
1118
1119 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001120}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001122
1123/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001124 * Do an RSA operation, then remove the message padding
1125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001127 int (*f_rng)(void *, unsigned char *, size_t),
1128 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001129 int mode, size_t *olen,
1130 const unsigned char *input,
1131 unsigned char *output,
1132 size_t output_max_len)
1133{
1134 switch( ctx->padding )
1135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#if defined(MBEDTLS_PKCS1_V15)
1137 case MBEDTLS_RSA_PKCS_V15:
1138 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001139 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001140#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#if defined(MBEDTLS_PKCS1_V21)
1143 case MBEDTLS_RSA_PKCS_V21:
1144 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001145 olen, input, output,
1146 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001147#endif
1148
1149 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001151 }
1152}
1153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001155/*
1156 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001159 int (*f_rng)(void *, unsigned char *, size_t),
1160 void *p_rng,
1161 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001163 unsigned int hashlen,
1164 const unsigned char *hash,
1165 unsigned char *sig )
1166{
1167 size_t olen;
1168 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001170 unsigned int slen, hlen, offset = 0;
1171 int ret;
1172 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 const mbedtls_md_info_t *md_info;
1174 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1177 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001178
1179 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001181
1182 olen = ctx->len;
1183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001185 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001186 // Gather length of hash to sign
1187 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001189 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001193 }
1194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001196 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001200 slen = hlen;
1201
1202 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001204
1205 memset( sig, 0, olen );
1206
Paul Bakkerb3869132013-02-28 17:21:01 +01001207 // Generate salt of length slen
1208 //
1209 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001211
1212 // Note: EMSA-PSS encoding is over the length of N - 1 bits
1213 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001214 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001215 p += olen - hlen * 2 - 2;
1216 *p++ = 0x01;
1217 memcpy( p, salt, slen );
1218 p += slen;
1219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -07001221 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1222 {
1223 mbedtls_md_free( &md_ctx );
Gilles Peskine74fd8682017-05-05 19:24:06 +02001224 /* No need to zeroize salt: we didn't use it. */
Brian J Murray88c2d222016-06-23 12:57:03 -07001225 return( ret );
1226 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001227
1228 // Generate H = Hash( M' )
1229 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 mbedtls_md_starts( &md_ctx );
1231 mbedtls_md_update( &md_ctx, p, 8 );
1232 mbedtls_md_update( &md_ctx, hash, hashlen );
1233 mbedtls_md_update( &md_ctx, salt, slen );
1234 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine74fd8682017-05-05 19:24:06 +02001235 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001236
1237 // Compensate for boundary condition when applying mask
1238 //
1239 if( msb % 8 == 0 )
1240 offset = 1;
1241
1242 // maskedDB: Apply dbMask to DB
1243 //
1244 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001247
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001248 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001249 sig[0] &= 0xFF >> ( olen * 8 - msb );
1250
1251 p += hlen;
1252 *p++ = 0xBC;
1253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 return( ( mode == MBEDTLS_RSA_PUBLIC )
1255 ? mbedtls_rsa_public( ctx, sig, sig )
1256 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001257}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001261/*
1262 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1263 */
1264/*
1265 * Do an RSA operation to sign the message digest
1266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001268 int (*f_rng)(void *, unsigned char *, size_t),
1269 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001270 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001272 unsigned int hashlen,
1273 const unsigned char *hash,
1274 unsigned char *sig )
1275{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001276 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001277 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001278 const char *oid = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1281 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001282
1283 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001284 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001289 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1293 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001294
Paul Bakkerc70b9822013-04-07 22:00:46 +02001295 nb_pad -= 10 + oid_size;
1296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001298 }
1299
Paul Bakkerc70b9822013-04-07 22:00:46 +02001300 nb_pad -= hashlen;
1301
Paul Bakkerb3869132013-02-28 17:21:01 +01001302 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001304
1305 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001307 memset( p, 0xFF, nb_pad );
1308 p += nb_pad;
1309 *p++ = 0;
1310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001312 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001313 memcpy( p, hash, hashlen );
1314 }
1315 else
1316 {
1317 /*
1318 * DigestInfo ::= SEQUENCE {
1319 * digestAlgorithm DigestAlgorithmIdentifier,
1320 * digest Digest }
1321 *
1322 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1323 *
1324 * Digest ::= OCTET STRING
1325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001327 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001329 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001331 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001332 memcpy( p, oid, oid_size );
1333 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001335 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001337 *p++ = hashlen;
1338 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001339 }
1340
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001341 if( mode == MBEDTLS_RSA_PUBLIC )
1342 return( mbedtls_rsa_public( ctx, sig, sig ) );
1343
Hanno Becker21f83752017-11-06 15:09:33 +00001344 return( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001345}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001347
1348/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001349 * Do an RSA operation to sign the message digest
1350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001352 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001353 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001356 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001357 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 unsigned char *sig )
1359{
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 switch( ctx->padding )
1361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_PKCS1_V15)
1363 case MBEDTLS_RSA_PKCS_V15:
1364 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001365 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001366#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_PKCS1_V21)
1369 case MBEDTLS_RSA_PKCS_V21:
1370 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001371 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001372#endif
1373
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001376 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001377}
1378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001380/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001381 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001384 int (*f_rng)(void *, unsigned char *, size_t),
1385 void *p_rng,
1386 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001388 unsigned int hashlen,
1389 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001391 int expected_salt_len,
1392 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001393{
Paul Bakker23986e52011-04-24 08:57:21 +00001394 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001395 size_t siglen;
1396 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Gilles Peskined0cd8552017-10-17 19:02:13 +02001398 unsigned char *hash_start;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001400 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001401 unsigned int hlen;
Gilles Peskined0cd8552017-10-17 19:02:13 +02001402 size_t observed_salt_len, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 const mbedtls_md_info_t *md_info;
1404 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1407 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001408
Paul Bakker5121ce52009-01-03 21:22:43 +00001409 siglen = ctx->len;
1410
Paul Bakker27fdf462011-06-09 13:55:13 +00001411 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1415 ? mbedtls_rsa_public( ctx, sig, buf )
1416 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
1418 if( ret != 0 )
1419 return( ret );
1420
1421 p = buf;
1422
Paul Bakkerb3869132013-02-28 17:21:01 +01001423 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001427 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001428 // Gather length of hash to sign
1429 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001431 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001435 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001438 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 hlen = mbedtls_md_get_size( md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001442
Paul Bakkerb3869132013-02-28 17:21:01 +01001443 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001444
Paul Bakkerb3869132013-02-28 17:21:01 +01001445 // Note: EMSA-PSS verification is over the length of N - 1 bits
1446 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001447 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001448
Gilles Peskine31a2d142017-10-19 15:23:49 +02001449 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
1450 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1451
1452 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001453 if( msb % 8 == 0 )
1454 {
1455 p++;
1456 siglen -= 1;
1457 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001458
Gilles Peskine9e205822017-10-18 19:03:42 +02001459 if( siglen < hlen + 2 )
1460 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1461 hash_start = p + siglen - hlen - 1;
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -07001464 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1465 {
1466 mbedtls_md_free( &md_ctx );
1467 return( ret );
1468 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001469
Gilles Peskined0cd8552017-10-17 19:02:13 +02001470 mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001471
Paul Bakkerb3869132013-02-28 17:21:01 +01001472 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001473
Gilles Peskined0cd8552017-10-17 19:02:13 +02001474 while( p < hash_start - 1 && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001475 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001476
Gilles Peskine9745cfd2017-10-19 17:46:14 +02001477 if( *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 mbedtls_md_free( &md_ctx );
1480 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001481 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001482
Gilles Peskined0cd8552017-10-17 19:02:13 +02001483 observed_salt_len = hash_start - p;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Gilles Peskined0cd8552017-10-17 19:02:13 +02001486 observed_salt_len != (size_t) expected_salt_len )
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 mbedtls_md_free( &md_ctx );
1489 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001490 }
1491
Paul Bakkerb3869132013-02-28 17:21:01 +01001492 // Generate H = Hash( M' )
1493 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 mbedtls_md_starts( &md_ctx );
1495 mbedtls_md_update( &md_ctx, zeros, 8 );
1496 mbedtls_md_update( &md_ctx, hash, hashlen );
Gilles Peskined0cd8552017-10-17 19:02:13 +02001497 mbedtls_md_update( &md_ctx, p, observed_salt_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001501
Gilles Peskined0cd8552017-10-17 19:02:13 +02001502 if( memcmp( hash_start, result, hlen ) == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001503 return( 0 );
1504 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001506}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001507
1508/*
1509 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001512 int (*f_rng)(void *, unsigned char *, size_t),
1513 void *p_rng,
1514 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001516 unsigned int hashlen,
1517 const unsigned char *hash,
1518 const unsigned char *sig )
1519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1521 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001522 : md_alg;
1523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001525 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001527 sig ) );
1528
1529}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001533/*
1534 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1535 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001537 int (*f_rng)(void *, unsigned char *, size_t),
1538 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001539 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001541 unsigned int hashlen,
1542 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001543 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001544{
1545 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001546 size_t len, siglen, asn1_len;
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001547 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1549 mbedtls_md_type_t msg_md_alg;
1550 const mbedtls_md_info_t *md_info;
1551 mbedtls_asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1554 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001555
1556 siglen = ctx->len;
1557
1558 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1562 ? mbedtls_rsa_public( ctx, sig, buf )
1563 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001564
1565 if( ret != 0 )
1566 return( ret );
1567
1568 p = buf;
1569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1571 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001572
1573 while( *p != 0 )
1574 {
1575 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001577 p++;
1578 }
Manuel Pégourié-Gonnard230ee312017-05-11 12:49:51 +02001579 p++; /* skip 00 byte */
1580
1581 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
1582 if( p - buf < 11 )
1583 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001584
1585 len = siglen - ( p - buf );
1586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001588 {
1589 if( memcmp( p, hash, hashlen ) == 0 )
1590 return( 0 );
1591 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001593 }
1594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001596 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1598 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001599
1600 end = p + len;
1601
Gilles Peskinebd908512017-05-04 12:48:39 +02001602 /*
1603 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
1604 * Insist on 2-byte length tags, to protect against variants of
1605 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
1606 */
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001607 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1609 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1610 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001611 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001613
Gilles Peskinebd908512017-05-04 12:48:39 +02001614 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1616 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1617 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001618 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001620
Gilles Peskinebd908512017-05-04 12:48:39 +02001621 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1623 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001624 if( p != p0 + 2 )
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001625 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001626
1627 oid.p = p;
1628 p += oid.len;
1629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1631 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001632
1633 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001635
1636 /*
1637 * assume the algorithm parameters must be NULL
1638 */
Gilles Peskinebd908512017-05-04 12:48:39 +02001639 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1641 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinebd908512017-05-04 12:48:39 +02001642 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001644
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001645 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001646 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1647 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinefd8f79d2017-05-03 18:32:21 +02001648 if( p != p0 + 2 || asn1_len != hashlen )
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 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001653
1654 p += hashlen;
1655
1656 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001658
1659 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001660}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
1663/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001664 * Do an RSA operation and check the message digest
1665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001667 int (*f_rng)(void *, unsigned char *, size_t),
1668 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001669 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001671 unsigned int hashlen,
1672 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001673 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001674{
1675 switch( ctx->padding )
1676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677#if defined(MBEDTLS_PKCS1_V15)
1678 case MBEDTLS_RSA_PKCS_V15:
1679 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001680 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001681#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#if defined(MBEDTLS_PKCS1_V21)
1684 case MBEDTLS_RSA_PKCS_V21:
1685 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001686 hashlen, hash, sig );
1687#endif
1688
1689 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001691 }
1692}
1693
1694/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001695 * Copy the components of an RSA key
1696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001698{
1699 int ret;
1700
1701 dst->ver = src->ver;
1702 dst->len = src->len;
1703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1705 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1708 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1709 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1710 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1711 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1712 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1715 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1716 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1719 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001720
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001721 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001722 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001723
1724cleanup:
1725 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001726 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001727
1728 return( ret );
1729}
1730
1731/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001732 * Free the components of an RSA key
1733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001735{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1737 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1738 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1739 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1740 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#if defined(MBEDTLS_THREADING_C)
1743 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001744#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001745}
1746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001749#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
1751/*
1752 * Example RSA-1024 keypair, for test purposes
1753 */
1754#define KEY_LEN 128
1755
1756#define RSA_N "9292758453063D803DD603D5E777D788" \
1757 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1758 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1759 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1760 "93A89813FBF3C4F8066D2D800F7C38A8" \
1761 "1AE31942917403FF4946B0A83D3D3E05" \
1762 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1763 "5E94BB77B07507233A0BC7BAC8F90F79"
1764
1765#define RSA_E "10001"
1766
1767#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1768 "66CA472BC44D253102F8B4A9D3BFA750" \
1769 "91386C0077937FE33FA3252D28855837" \
1770 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1771 "DF79C5CE07EE72C7F123142198164234" \
1772 "CABB724CF78B8173B9F880FC86322407" \
1773 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1774 "071513A1E85B5DFA031F21ECAE91A34D"
1775
1776#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1777 "2C01CAD19EA484A87EA4377637E75500" \
1778 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1779 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1780
1781#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1782 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1783 "910E4168387E3C30AA1E00C339A79508" \
1784 "8452DD96A9A5EA5D9DCA68DA636032AF"
1785
1786#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1787 "3C94D22288ACD763FD8E5600ED4A702D" \
1788 "F84198A5F06C2E72236AE490C93F07F8" \
1789 "3CC559CD27BC2D1CA488811730BB5725"
1790
1791#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1792 "D8AAEA56749EA28623272E4F7D0592AF" \
1793 "7C1F1313CAC9471B5C523BFE592F517B" \
1794 "407A1BD76C164B93DA2D32A383E58357"
1795
1796#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1797 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1798 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1799 "A74206CEC169D74BF5A8C50D6F48EA08"
1800
1801#define PT_LEN 24
1802#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1803 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001806static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001807{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001808#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001809 size_t i;
1810
Paul Bakker545570e2010-07-18 09:00:25 +00001811 if( rng_state != NULL )
1812 rng_state = NULL;
1813
Paul Bakkera3d195c2011-11-27 21:07:34 +00001814 for( i = 0; i < len; ++i )
1815 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001816#else
1817 if( rng_state != NULL )
1818 rng_state = NULL;
1819
1820 arc4random_buf( output, len );
1821#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001822
Paul Bakkera3d195c2011-11-27 21:07:34 +00001823 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001824}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001826
Paul Bakker5121ce52009-01-03 21:22:43 +00001827/*
1828 * Checkup routine
1829 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001831{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001832 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001834 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 unsigned char rsa_plaintext[PT_LEN];
1837 unsigned char rsa_decrypted[PT_LEN];
1838 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001840 unsigned char sha1sum[20];
1841#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001844
1845 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1847 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1848 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1849 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1850 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1851 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1852 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1853 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
1855 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1859 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001860 {
1861 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001863
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001864 ret = 1;
1865 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001866 }
1867
1868 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001870
1871 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001874 rsa_plaintext, rsa_ciphertext ) != 0 )
1875 {
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 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001887 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001888 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001889 {
1890 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001892
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001893 ret = 1;
1894 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 }
1896
1897 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1898 {
1899 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001901
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001902 ret = 1;
1903 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 }
1905
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001906 if( verbose != 0 )
1907 mbedtls_printf( "passed\n" );
1908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001910 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001911 mbedtls_printf( "PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001916 sha1sum, rsa_ciphertext ) != 0 )
1917 {
1918 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001920
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001921 ret = 1;
1922 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001923 }
1924
1925 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001926 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001928 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001929 sha1sum, rsa_ciphertext ) != 0 )
1930 {
1931 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001933
Hanno Beckerb81fcd02017-05-03 15:09:31 +01001934 ret = 1;
1935 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001936 }
1937
1938 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001939 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001941
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001942 if( verbose != 0 )
1943 mbedtls_printf( "\n" );
1944
Paul Bakker3d8fb632014-04-17 12:42:41 +02001945cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 mbedtls_rsa_free( &rsa );
1947#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001948 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001950 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001951}
1952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955#endif /* MBEDTLS_RSA_C */