blob: d866c7aa3c58b6056fbaac660354c708b5f2a291 [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/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000022 * The following sources were referenced in the design of this implementation
23 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000024 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000025 * [1] A method for obtaining digital signatures and public-key cryptosystems
26 * R Rivest, A Shamir, and L Adleman
27 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
28 *
29 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
30 * Menezes, van Oorschot and Vanstone
31 *
Janos Follathe81102e2017-03-22 13:38:28 +000032 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
33 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
34 * Stefan Mangard
35 * https://arxiv.org/abs/1702.08719v2
36 *
Paul Bakker5121ce52009-01-03 21:22:43 +000037 */
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020041#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/rsa.h"
48#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#else
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020065#define mbedtls_calloc calloc
66#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#endif
68
Hanno Beckera988a272017-09-07 11:32:04 +010069#if !defined(MBEDTLS_RSA_FORCE_BLINDING) && \
70 defined(MBEDTLS_DEPRECATED_WARNING)
71#warning Not enforcing blinding checks for RSA private key operations\
72 is deprecated. Please uncomment MBEDTLS_RSA_FORCE_BLINDING\
73 in config.h to enforce blinding checks.
74#endif
75
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010076/* Implementation that should never be optimized out by the compiler */
77static void mbedtls_zeroize( void *v, size_t n ) {
78 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
79}
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/*
82 * Initialize an RSA context
83 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000085 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000086 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if defined(MBEDTLS_THREADING_C)
93 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020094#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000095}
96
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010097/*
98 * Set padding for an existing RSA context
99 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100101{
102 ctx->padding = padding;
103 ctx->hash_id = hash_id;
104}
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108/*
109 * Generate an RSA keypair
110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000112 int (*f_rng)(void *, unsigned char *, size_t),
113 void *p_rng,
114 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000115{
116 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Paul Bakker21eb2802010-08-16 11:10:02 +0000119 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Janos Follathef441782016-09-21 13:18:12 +0100122 if( nbits % 2 )
123 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
124
125 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath10c575b2016-02-23 14:42:48 +0000126 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
128 /*
129 * find primes P and Q with Q < P so that:
130 * GCD( E, (P-1)*(Q-1) ) == 1
131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134 do
135 {
Janos Follath10c575b2016-02-23 14:42:48 +0000136 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000137 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Janos Follathef441782016-09-21 13:18:12 +0100139 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000140 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 continue;
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200146 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 continue;
148
Janos Follathef441782016-09-21 13:18:12 +0100149 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
150 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
153 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
154 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
155 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159 /*
160 * D = E^-1 mod ((P-1)*(Q-1))
161 * DP = D mod (P - 1)
162 * DQ = D mod (Q - 1)
163 * QP = Q^-1 mod P
164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
166 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
167 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
168 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200170 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172cleanup:
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
176 if( ret != 0 )
177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_rsa_free( ctx );
179 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 }
181
Paul Bakker48377d92013-08-30 12:06:24 +0200182 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183}
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
187/*
188 * Check a public RSA key
189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000191{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000192 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000194
Paul Bakker48377d92013-08-30 12:06:24 +0200195 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200199 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
200 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200203 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
205 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
207 return( 0 );
208}
209
210/*
211 * Check a private RSA key
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000214{
215 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 return( ret );
220
Paul Bakker37940d9f2009-07-10 22:38:58 +0000221 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
225 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
226 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
227 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
230 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
231 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
232 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
233 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
234 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
237 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
238 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
241 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
242 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000243 /*
244 * Check for a valid PKCS1v2 private key
245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
247 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
248 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
249 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
250 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
251 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
252 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 }
Paul Bakker48377d92013-08-30 12:06:24 +0200256
Paul Bakker5121ce52009-01-03 21:22:43 +0000257cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
259 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
260 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
261 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000264 return( ret );
265
Paul Bakker6c591fa2011-05-05 11:49:20 +0000266 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000268
269 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000270}
271
272/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100273 * Check if contexts holding a public and private key match
274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100276{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
278 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100281 }
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
284 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100287 }
288
289 return( 0 );
290}
291
292/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 * Do an RSA public key operation
294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000296 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 unsigned char *output )
298{
Paul Bakker23986e52011-04-24 08:57:21 +0000299 int ret;
300 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200305#if defined(MBEDTLS_THREADING_C)
306 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
307 return( ret );
308#endif
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200314 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
315 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 }
317
318 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
320 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
322cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200324 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
325 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100326#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
333 return( 0 );
334}
335
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200336/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200337 * Generate or update blinding values, see section 10 of:
338 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200339 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200340 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200341 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200342static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200343 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
344{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200345 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200346
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200347 if( ctx->Vf.p != NULL )
348 {
349 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
351 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
352 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
353 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200354
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200355 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200356 }
357
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200358 /* Unblinding value: Vf = random number, invertible mod N */
359 do {
360 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
364 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
365 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200366
367 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
369 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 +0200370
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200371
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200372cleanup:
373 return( ret );
374}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376/*
Janos Follathe81102e2017-03-22 13:38:28 +0000377 * Exponent blinding supposed to prevent side-channel attacks using multiple
378 * traces of measurements to recover the RSA key. The more collisions are there,
379 * the more bits of the key can be recovered. See [3].
380 *
381 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
382 * observations on avarage.
383 *
384 * For example with 28 byte blinding to achieve 2 collisions the adversary has
385 * to make 2^112 observations on avarage.
386 *
387 * (With the currently (as of 2017 April) known best algorithms breaking 2048
388 * bit RSA requires approximately as much time as trying out 2^112 random keys.
389 * Thus in this sense with 28 byte blinding the security is not reduced by
390 * side-channel attacks like the one in [3])
391 *
392 * This countermeasure does not help if the key recovery is possible with a
393 * single trace.
394 */
395#define RSA_EXPONENT_BLINDING 28
396
397/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 * Do an RSA private key operation
399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200401 int (*f_rng)(void *, unsigned char *, size_t),
402 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000403 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 unsigned char *output )
405{
Paul Bakker23986e52011-04-24 08:57:21 +0000406 int ret;
407 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100408
409 /* Temporary holding the result */
410 mbedtls_mpi T;
411
412 /* Temporaries holding P-1, Q-1 and the
413 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000414 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100415
416#if !defined(MBEDTLS_RSA_NO_CRT)
417 /* Temporaries holding the results mod p resp. mod q. */
418 mbedtls_mpi TP, TQ;
419
420 /* Temporaries holding the blinded exponents for
421 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000422 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100423
424 /* Pointers to actual exponents to be used - either the unblinded
425 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000426 mbedtls_mpi *DP = &ctx->DP;
427 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100428#else
429 /* Temporary holding the blinded exponent (if used). */
430 mbedtls_mpi D_blind;
431
432 /* Pointer to actual exponent to be used - either the unblinded
433 * or the blinded one, depending on the presence of a PRNG. */
434 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100435#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100436
437#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100438 /* Temporaries holding the initial input and the double
439 * checked result; should be the same in the end. */
440 mbedtls_mpi I, C;
Hanno Becker06811ce2017-05-03 15:10:34 +0100441#endif
442
443#if defined(MBEDTLS_RSA_FORCE_BLINDING)
444 if( f_rng == NULL )
445 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Janos Follathe81102e2017-03-22 13:38:28 +0000446#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
Hanno Becker43f94722017-08-25 11:50:00 +0100448 /* Sanity-check that all relevant fields are at least set,
449 * but don't perform a full keycheck. */
Hanno Becker2fdffe02017-09-29 15:19:28 +0100450#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker43f94722017-08-25 11:50:00 +0100451 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
Hanno Becker43f94722017-08-25 11:50:00 +0100452 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
Hanno Becker2fdffe02017-09-29 15:19:28 +0100453 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 ||
454 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
455 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 )
Hanno Becker43f94722017-08-25 11:50:00 +0100456 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100457 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker43f94722017-08-25 11:50:00 +0100458 }
Hanno Becker2fdffe02017-09-29 15:19:28 +0100459#else /* ! MBEDTLS_RSA_NO_CRT */
460 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
461 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 ||
462 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
Hanno Becker2c9f0272017-09-28 11:04:13 +0100463 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
464 mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
Hanno Becker43f94722017-08-25 11:50:00 +0100465 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
466 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
467 {
468 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
469 }
Hanno Becker2fdffe02017-09-29 15:19:28 +0100470#endif /* ! MBEDTLS_RSA_NO_CRT */
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100471
Hanno Becker06811ce2017-05-03 15:10:34 +0100472#if defined(MBEDTLS_THREADING_C)
473 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
474 return( ret );
475#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000476
Hanno Becker06811ce2017-05-03 15:10:34 +0100477 /* MPI Initialization */
478
479 mbedtls_mpi_init( &T );
480
481 mbedtls_mpi_init( &P1 );
482 mbedtls_mpi_init( &Q1 );
483 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000484
485 if( f_rng != NULL )
486 {
Janos Follathe81102e2017-03-22 13:38:28 +0000487#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000488 mbedtls_mpi_init( &D_blind );
489#else
490 mbedtls_mpi_init( &DP_blind );
491 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000492#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000493 }
Janos Follathe81102e2017-03-22 13:38:28 +0000494
Hanno Becker06811ce2017-05-03 15:10:34 +0100495#if !defined(MBEDTLS_RSA_NO_CRT)
496 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200497#endif
498
Hanno Becker06811ce2017-05-03 15:10:34 +0100499#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100500 mbedtls_mpi_init( &I );
501 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100502#endif
503
504 /* End of MPI initialization */
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
507 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200509 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
510 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 }
512
Hanno Becker06811ce2017-05-03 15:10:34 +0100513#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100514 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100515#endif
516
Paul Bakkerf451bac2013-08-30 15:37:02 +0200517 if( f_rng != NULL )
518 {
519 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200520 * Blinding
521 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200522 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200523 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
524 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000526
Janos Follathe81102e2017-03-22 13:38:28 +0000527 /*
528 * Exponent blinding
529 */
530 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
531 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
532
Janos Follathf9203b42017-03-22 15:13:15 +0000533#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000534 /*
535 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
536 */
537 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
538 f_rng, p_rng ) );
539 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
540 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
541 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
542
543 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000544#else
545 /*
546 * DP_blind = ( P - 1 ) * R + DP
547 */
548 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
549 f_rng, p_rng ) );
550 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
551 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
552 &ctx->DP ) );
553
554 DP = &DP_blind;
555
556 /*
557 * DQ_blind = ( Q - 1 ) * R + DQ
558 */
559 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
560 f_rng, p_rng ) );
561 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
562 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
563 &ctx->DQ ) );
564
565 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000566#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200567 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000570 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100571#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200572 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000573 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100575 * TP = input ^ dP mod P
576 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100578
579 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
580 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100583 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100585 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
586 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
587 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
589 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100590 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100592 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
593 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200595
Paul Bakkerf451bac2013-08-30 15:37:02 +0200596 if( f_rng != NULL )
597 {
598 /*
599 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200600 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200601 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200602 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200604 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100606 /* If requested by the config, verify the result to prevent glitching attacks. */
Hanno Becker06811ce2017-05-03 15:10:34 +0100607#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100608 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E, &ctx->N, &ctx->RN ) );
609 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +0100610 {
Hanno Becker06811ce2017-05-03 15:10:34 +0100611 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
612 goto cleanup;
613 }
614#endif /* MBEDTLS_RSA_REQUIRE_VERIFICATION */
615
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000618
619cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200621 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
622 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200623#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200624
Hanno Becker06811ce2017-05-03 15:10:34 +0100625 mbedtls_mpi_free( &P1 );
626 mbedtls_mpi_free( &Q1 );
627 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000628
629 if( f_rng != NULL )
630 {
Janos Follathe81102e2017-03-22 13:38:28 +0000631#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000632 mbedtls_mpi_free( &D_blind );
633#else
634 mbedtls_mpi_free( &DP_blind );
635 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000636#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000637 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
Hanno Becker06811ce2017-05-03 15:10:34 +0100639 mbedtls_mpi_free( &T );
640
641#if !defined(MBEDTLS_RSA_NO_CRT)
642 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
643#endif
644
645#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100646 mbedtls_mpi_free( &C );
647 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +0100648#endif
649
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000652
653 return( 0 );
654}
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000657/**
658 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
659 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000660 * \param dst buffer to mask
661 * \param dlen length of destination buffer
662 * \param src source of the mask generation
663 * \param slen length of the source buffer
664 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000665 */
Paul Bakker48377d92013-08-30 12:06:24 +0200666static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000668{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000670 unsigned char counter[4];
671 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000672 unsigned int hlen;
673 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000676 memset( counter, 0, 4 );
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000679
Simon Butcher02037452016-03-01 21:19:12 +0000680 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000681 p = dst;
682
683 while( dlen > 0 )
684 {
685 use_len = hlen;
686 if( dlen < hlen )
687 use_len = dlen;
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 mbedtls_md_starts( md_ctx );
690 mbedtls_md_update( md_ctx, src, slen );
691 mbedtls_md_update( md_ctx, counter, 4 );
692 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000693
694 for( i = 0; i < use_len; ++i )
695 *p++ ^= mask[i];
696
697 counter[3]++;
698
699 dlen -= use_len;
700 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200701
702 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000703}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100707/*
708 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100711 int (*f_rng)(void *, unsigned char *, size_t),
712 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100713 int mode,
714 const unsigned char *label, size_t label_len,
715 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100716 const unsigned char *input,
717 unsigned char *output )
718{
719 size_t olen;
720 int ret;
721 unsigned char *p = output;
722 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 const mbedtls_md_info_t *md_info;
724 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
727 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200728
729 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100733 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100735
736 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100738
Simon Butcher02037452016-03-01 21:19:12 +0000739 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000740 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100742
743 memset( output, 0, olen );
744
745 *p++ = 0;
746
Simon Butcher02037452016-03-01 21:19:12 +0000747 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100748 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100750
751 p += hlen;
752
Simon Butcher02037452016-03-01 21:19:12 +0000753 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100755 p += hlen;
756 p += olen - 2 * hlen - 2 - ilen;
757 *p++ = 1;
758 memcpy( p, input, ilen );
759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700761 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
762 {
763 mbedtls_md_free( &md_ctx );
764 return( ret );
765 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100766
Simon Butcher02037452016-03-01 21:19:12 +0000767 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100768 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
769 &md_ctx );
770
Simon Butcher02037452016-03-01 21:19:12 +0000771 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100772 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
773 &md_ctx );
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 return( ( mode == MBEDTLS_RSA_PUBLIC )
778 ? mbedtls_rsa_public( ctx, output, output )
779 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100780}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100784/*
785 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100788 int (*f_rng)(void *, unsigned char *, size_t),
789 void *p_rng,
790 int mode, size_t ilen,
791 const unsigned char *input,
792 unsigned char *output )
793{
794 size_t nb_pad, olen;
795 int ret;
796 unsigned char *p = output;
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
799 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200800
Janos Follath1ed9f992016-03-18 11:45:44 +0000801 // We don't check p_rng because it won't be dereferenced here
802 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100804
805 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100806
Simon Butcher02037452016-03-01 21:19:12 +0000807 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000808 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100810
811 nb_pad = olen - 3 - ilen;
812
813 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100817
818 while( nb_pad-- > 0 )
819 {
820 int rng_dl = 100;
821
822 do {
823 ret = f_rng( p_rng, p, 1 );
824 } while( *p == 0 && --rng_dl && ret == 0 );
825
Simon Butcher02037452016-03-01 21:19:12 +0000826 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200827 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100829
830 p++;
831 }
832 }
833 else
834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100836
837 while( nb_pad-- > 0 )
838 *p++ = 0xFF;
839 }
840
841 *p++ = 0;
842 memcpy( p, input, ilen );
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 return( ( mode == MBEDTLS_RSA_PUBLIC )
845 ? mbedtls_rsa_public( ctx, output, output )
846 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100847}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100849
Paul Bakker5121ce52009-01-03 21:22:43 +0000850/*
851 * Add the message padding, then do an RSA operation
852 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000854 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000855 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000856 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000857 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000858 unsigned char *output )
859{
Paul Bakker5121ce52009-01-03 21:22:43 +0000860 switch( ctx->padding )
861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_PKCS1_V15)
863 case MBEDTLS_RSA_PKCS_V15:
864 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100865 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200866#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_PKCS1_V21)
869 case MBEDTLS_RSA_PKCS_V21:
870 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100871 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000872#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000873
874 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000877}
878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000880/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100881 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200884 int (*f_rng)(void *, unsigned char *, size_t),
885 void *p_rng,
886 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100887 const unsigned char *label, size_t label_len,
888 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100889 const unsigned char *input,
890 unsigned char *output,
891 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000892{
Paul Bakker23986e52011-04-24 08:57:21 +0000893 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100894 size_t ilen, i, pad_len;
895 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
897 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000898 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 const mbedtls_md_info_t *md_info;
900 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100901
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100902 /*
903 * Parameters sanity checks
904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
906 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000907
908 ilen = ctx->len;
909
Paul Bakker27fdf462011-06-09 13:55:13 +0000910 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100914 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100916
Janos Follathc17cda12016-02-11 11:08:18 +0000917 hlen = mbedtls_md_get_size( md_info );
918
919 // checking for integer underflow
920 if( 2 * hlen + 2 > ilen )
921 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
922
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100923 /*
924 * RSA operation
925 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 ret = ( mode == MBEDTLS_RSA_PUBLIC )
927 ? mbedtls_rsa_public( ctx, input, buf )
928 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000929
930 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100931 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000932
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100933 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100934 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100935 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700937 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
938 {
939 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100940 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700941 }
942
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100943
944 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100946
947 /* seed: Apply seedMask to maskedSeed */
948 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
949 &md_ctx );
950
951 /* DB: Apply dbMask to maskedDB */
952 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
953 &md_ctx );
954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100956
957 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100958 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100959 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000960 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100961 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000962
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100963 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100964
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100965 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100966
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100967 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100968 for( i = 0; i < hlen; i++ )
969 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100970
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100971 /* Get zero-padding len, but always read till end of buffer
972 * (minus one, for the 01 byte) */
973 pad_len = 0;
974 pad_done = 0;
975 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
976 {
977 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100978 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100979 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100980
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100981 p += pad_len;
982 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100983
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100984 /*
985 * The only information "leaked" is whether the padding was correct or not
986 * (eg, no data is copied if it was not correct). This meets the
987 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
988 * the different error conditions.
989 */
990 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100991 {
992 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
993 goto cleanup;
994 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100995
Paul Bakker66d5d072014-06-17 16:39:18 +0200996 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100997 {
998 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
999 goto cleanup;
1000 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001001
1002 *olen = ilen - (p - buf);
1003 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001004 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001005
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001006cleanup:
1007 mbedtls_zeroize( buf, sizeof( buf ) );
1008 mbedtls_zeroize( lhash, sizeof( lhash ) );
1009
1010 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001011}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001015/*
1016 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001019 int (*f_rng)(void *, unsigned char *, size_t),
1020 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001021 int mode, size_t *olen,
1022 const unsigned char *input,
1023 unsigned char *output,
1024 size_t output_max_len)
1025{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001026 int ret;
1027 size_t ilen, pad_count = 0, i;
1028 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1032 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001033
1034 ilen = ctx->len;
1035
1036 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1040 ? mbedtls_rsa_public( ctx, input, buf )
1041 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001042
1043 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001044 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001045
1046 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001047 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001048
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001049 /*
1050 * Check and get padding len in "constant-time"
1051 */
1052 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001053
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001054 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001058
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001059 /* Get padding len, but always read till end of buffer
1060 * (minus one, for the 00 byte) */
1061 for( i = 0; i < ilen - 3; i++ )
1062 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001063 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1064 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001065 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001066
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001067 p += pad_count;
1068 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001069 }
1070 else
1071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001073
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001074 /* Get padding len, but always read till end of buffer
1075 * (minus one, for the 00 byte) */
1076 for( i = 0; i < ilen - 3; i++ )
1077 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001078 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001079 pad_count += ( pad_done == 0 );
1080 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001081
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001082 p += pad_count;
1083 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001084 }
1085
Janos Follathc69fa502016-02-12 13:30:09 +00001086 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001087
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001088 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001089 {
1090 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1091 goto cleanup;
1092 }
Paul Bakker8804f692013-02-28 18:06:26 +01001093
Paul Bakker66d5d072014-06-17 16:39:18 +02001094 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001095 {
1096 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1097 goto cleanup;
1098 }
Paul Bakker060c5682009-01-12 21:48:39 +00001099
Paul Bakker27fdf462011-06-09 13:55:13 +00001100 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001101 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001102 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001103
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001104cleanup:
1105 mbedtls_zeroize( buf, sizeof( buf ) );
1106
1107 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001110
1111/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001112 * Do an RSA operation, then remove the message padding
1113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001115 int (*f_rng)(void *, unsigned char *, size_t),
1116 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001117 int mode, size_t *olen,
1118 const unsigned char *input,
1119 unsigned char *output,
1120 size_t output_max_len)
1121{
1122 switch( ctx->padding )
1123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124#if defined(MBEDTLS_PKCS1_V15)
1125 case MBEDTLS_RSA_PKCS_V15:
1126 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001127 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001128#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#if defined(MBEDTLS_PKCS1_V21)
1131 case MBEDTLS_RSA_PKCS_V21:
1132 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001133 olen, input, output,
1134 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001135#endif
1136
1137 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001139 }
1140}
1141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001143/*
1144 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001147 int (*f_rng)(void *, unsigned char *, size_t),
1148 void *p_rng,
1149 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001151 unsigned int hashlen,
1152 const unsigned char *hash,
1153 unsigned char *sig )
1154{
1155 size_t olen;
1156 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 unsigned int slen, hlen, offset = 0;
1159 int ret;
1160 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 const mbedtls_md_info_t *md_info;
1162 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1165 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001166
1167 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001169
1170 olen = ctx->len;
1171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001173 {
Simon Butcher02037452016-03-01 21:19:12 +00001174 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001176 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001180 }
1181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001183 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001187 slen = hlen;
1188
1189 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001191
1192 memset( sig, 0, olen );
1193
Simon Butcher02037452016-03-01 21:19:12 +00001194 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001195 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001197
Simon Butcher02037452016-03-01 21:19:12 +00001198 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001199 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001200 p += olen - hlen * 2 - 2;
1201 *p++ = 0x01;
1202 memcpy( p, salt, slen );
1203 p += slen;
1204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001206 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1207 {
1208 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001209 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001210 return( ret );
1211 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001212
Simon Butcher02037452016-03-01 21:19:12 +00001213 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_md_starts( &md_ctx );
1215 mbedtls_md_update( &md_ctx, p, 8 );
1216 mbedtls_md_update( &md_ctx, hash, hashlen );
1217 mbedtls_md_update( &md_ctx, salt, slen );
1218 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001219 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001220
Simon Butcher02037452016-03-01 21:19:12 +00001221 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001222 if( msb % 8 == 0 )
1223 offset = 1;
1224
Simon Butcher02037452016-03-01 21:19:12 +00001225 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001226 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001229
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001230 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001231 sig[0] &= 0xFF >> ( olen * 8 - msb );
1232
1233 p += hlen;
1234 *p++ = 0xBC;
1235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 return( ( mode == MBEDTLS_RSA_PUBLIC )
1237 ? mbedtls_rsa_public( ctx, sig, sig )
1238 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001239}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001243/*
1244 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1245 */
1246/*
1247 * Do an RSA operation to sign the message digest
1248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001250 int (*f_rng)(void *, unsigned char *, size_t),
1251 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001252 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001254 unsigned int hashlen,
1255 const unsigned char *hash,
1256 unsigned char *sig )
1257{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001258 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001259 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001260 const char *oid = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1263 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001264
1265 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001266 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001271 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1275 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001276
Paul Bakkerc70b9822013-04-07 22:00:46 +02001277 nb_pad -= 10 + oid_size;
1278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001280 }
1281
Paul Bakkerc70b9822013-04-07 22:00:46 +02001282 nb_pad -= hashlen;
1283
Paul Bakkerb3869132013-02-28 17:21:01 +01001284 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001286
1287 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001289 memset( p, 0xFF, nb_pad );
1290 p += nb_pad;
1291 *p++ = 0;
1292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001294 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001295 memcpy( p, hash, hashlen );
1296 }
1297 else
1298 {
1299 /*
1300 * DigestInfo ::= SEQUENCE {
1301 * digestAlgorithm DigestAlgorithmIdentifier,
1302 * digest Digest }
1303 *
1304 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1305 *
1306 * Digest ::= OCTET STRING
1307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001309 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001311 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001313 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001314 memcpy( p, oid, oid_size );
1315 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001317 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001319 *p++ = hashlen;
1320 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001321 }
1322
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001323 if( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker43f94722017-08-25 11:50:00 +01001324 return( mbedtls_rsa_public( ctx, sig, sig ) );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001325
Hanno Beckercc209ca2017-08-25 11:51:03 +01001326 return( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001327}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001329
1330/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 * Do an RSA operation to sign the message digest
1332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001334 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001335 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001336 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001338 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001339 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 unsigned char *sig )
1341{
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 switch( ctx->padding )
1343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#if defined(MBEDTLS_PKCS1_V15)
1345 case MBEDTLS_RSA_PKCS_V15:
1346 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001347 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001348#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#if defined(MBEDTLS_PKCS1_V21)
1351 case MBEDTLS_RSA_PKCS_V21:
1352 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001353 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001354#endif
1355
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001359}
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001362/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001363 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001366 int (*f_rng)(void *, unsigned char *, size_t),
1367 void *p_rng,
1368 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001370 unsigned int hashlen,
1371 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001373 int expected_salt_len,
1374 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001375{
Paul Bakker23986e52011-04-24 08:57:21 +00001376 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001377 size_t siglen;
1378 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001380 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001381 unsigned int hlen;
1382 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 const mbedtls_md_info_t *md_info;
1384 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001385 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1388 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001389
Paul Bakker5121ce52009-01-03 21:22:43 +00001390 siglen = ctx->len;
1391
Paul Bakker27fdf462011-06-09 13:55:13 +00001392 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1396 ? mbedtls_rsa_public( ctx, sig, buf )
1397 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001398
1399 if( ret != 0 )
1400 return( ret );
1401
1402 p = buf;
1403
Paul Bakkerb3869132013-02-28 17:21:01 +01001404 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 {
Simon Butcher02037452016-03-01 21:19:12 +00001409 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001411 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001415 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001418 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001422 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001423
Paul Bakkerb3869132013-02-28 17:21:01 +01001424 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001425
Simon Butcher02037452016-03-01 21:19:12 +00001426 /*
1427 * Note: EMSA-PSS verification is over the length of N - 1 bits
1428 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001429 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001430
Simon Butcher02037452016-03-01 21:19:12 +00001431 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001432 if( msb % 8 == 0 )
1433 {
1434 p++;
1435 siglen -= 1;
1436 }
1437 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001441 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1442 {
1443 mbedtls_md_free( &md_ctx );
1444 return( ret );
1445 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001446
Paul Bakkerb3869132013-02-28 17:21:01 +01001447 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001448
Paul Bakkerb3869132013-02-28 17:21:01 +01001449 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001450
Paul Bakker4de44aa2013-12-31 11:43:01 +01001451 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001452 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001453
Paul Bakkerb3869132013-02-28 17:21:01 +01001454 if( p == buf + siglen ||
1455 *p++ != 0x01 )
1456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 mbedtls_md_free( &md_ctx );
1458 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001459 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001460
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001461 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001462 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001465 slen != (size_t) expected_salt_len )
1466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_md_free( &md_ctx );
1468 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001469 }
1470
Simon Butcher02037452016-03-01 21:19:12 +00001471 /*
1472 * Generate H = Hash( M' )
1473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 mbedtls_md_starts( &md_ctx );
1475 mbedtls_md_update( &md_ctx, zeros, 8 );
1476 mbedtls_md_update( &md_ctx, hash, hashlen );
1477 mbedtls_md_update( &md_ctx, p, slen );
1478 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001481
Paul Bakkerb3869132013-02-28 17:21:01 +01001482 if( memcmp( p + slen, result, hlen ) == 0 )
1483 return( 0 );
1484 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001486}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001487
1488/*
1489 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1490 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001492 int (*f_rng)(void *, unsigned char *, size_t),
1493 void *p_rng,
1494 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001496 unsigned int hashlen,
1497 const unsigned char *hash,
1498 const unsigned char *sig )
1499{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1501 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001502 : md_alg;
1503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001505 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001507 sig ) );
1508
1509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001513/*
1514 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001517 int (*f_rng)(void *, unsigned char *, size_t),
1518 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001519 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001521 unsigned int hashlen,
1522 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001523 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001524{
1525 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001526 size_t len, siglen, asn1_len;
1527 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 mbedtls_md_type_t msg_md_alg;
1529 const mbedtls_md_info_t *md_info;
1530 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001531 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1534 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001535
1536 siglen = ctx->len;
1537
1538 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1542 ? mbedtls_rsa_public( ctx, sig, buf )
1543 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001544
1545 if( ret != 0 )
1546 return( ret );
1547
1548 p = buf;
1549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1551 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001552
1553 while( *p != 0 )
1554 {
1555 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001557 p++;
1558 }
1559 p++;
1560
1561 len = siglen - ( p - buf );
1562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001564 {
1565 if( memcmp( p, hash, hashlen ) == 0 )
1566 return( 0 );
1567 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001569 }
1570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001572 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1574 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001575
1576 end = p + len;
1577
Simon Butcher02037452016-03-01 21:19:12 +00001578 /*
1579 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1582 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1583 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001584
1585 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1589 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1590 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001591
1592 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1596 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001597
1598 oid.p = p;
1599 p += oid.len;
1600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1602 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001603
1604 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001606
1607 /*
1608 * assume the algorithm parameters must be NULL
1609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1611 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1614 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001615
1616 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001618
1619 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001621
1622 p += hashlen;
1623
1624 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001626
1627 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001628}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
1631/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001632 * Do an RSA operation and check the message digest
1633 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001635 int (*f_rng)(void *, unsigned char *, size_t),
1636 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001637 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001639 unsigned int hashlen,
1640 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001641 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001642{
1643 switch( ctx->padding )
1644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_PKCS1_V15)
1646 case MBEDTLS_RSA_PKCS_V15:
1647 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001648 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001649#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#if defined(MBEDTLS_PKCS1_V21)
1652 case MBEDTLS_RSA_PKCS_V21:
1653 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001654 hashlen, hash, sig );
1655#endif
1656
1657 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001659 }
1660}
1661
1662/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001663 * Copy the components of an RSA key
1664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001666{
1667 int ret;
1668
1669 dst->ver = src->ver;
1670 dst->len = src->len;
1671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1673 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1676 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1677 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1678 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1679 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1680 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1683 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1684 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1687 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001688
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001689 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001690 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001691
1692cleanup:
1693 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001695
1696 return( ret );
1697}
1698
1699/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 * Free the components of an RSA key
1701 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1705 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1706 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1707 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1708 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#if defined(MBEDTLS_THREADING_C)
1711 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001712#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001713}
1714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001716
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001717#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001718
1719/*
1720 * Example RSA-1024 keypair, for test purposes
1721 */
1722#define KEY_LEN 128
1723
1724#define RSA_N "9292758453063D803DD603D5E777D788" \
1725 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1726 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1727 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1728 "93A89813FBF3C4F8066D2D800F7C38A8" \
1729 "1AE31942917403FF4946B0A83D3D3E05" \
1730 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1731 "5E94BB77B07507233A0BC7BAC8F90F79"
1732
1733#define RSA_E "10001"
1734
1735#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1736 "66CA472BC44D253102F8B4A9D3BFA750" \
1737 "91386C0077937FE33FA3252D28855837" \
1738 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1739 "DF79C5CE07EE72C7F123142198164234" \
1740 "CABB724CF78B8173B9F880FC86322407" \
1741 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1742 "071513A1E85B5DFA031F21ECAE91A34D"
1743
1744#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1745 "2C01CAD19EA484A87EA4377637E75500" \
1746 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1747 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1748
1749#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1750 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1751 "910E4168387E3C30AA1E00C339A79508" \
1752 "8452DD96A9A5EA5D9DCA68DA636032AF"
1753
1754#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1755 "3C94D22288ACD763FD8E5600ED4A702D" \
1756 "F84198A5F06C2E72236AE490C93F07F8" \
1757 "3CC559CD27BC2D1CA488811730BB5725"
1758
1759#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1760 "D8AAEA56749EA28623272E4F7D0592AF" \
1761 "7C1F1313CAC9471B5C523BFE592F517B" \
1762 "407A1BD76C164B93DA2D32A383E58357"
1763
1764#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1765 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1766 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1767 "A74206CEC169D74BF5A8C50D6F48EA08"
1768
1769#define PT_LEN 24
1770#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1771 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001774static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001775{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001776#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001777 size_t i;
1778
Paul Bakker545570e2010-07-18 09:00:25 +00001779 if( rng_state != NULL )
1780 rng_state = NULL;
1781
Paul Bakkera3d195c2011-11-27 21:07:34 +00001782 for( i = 0; i < len; ++i )
1783 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001784#else
1785 if( rng_state != NULL )
1786 rng_state = NULL;
1787
1788 arc4random_buf( output, len );
1789#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001790
Paul Bakkera3d195c2011-11-27 21:07:34 +00001791 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001792}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001794
Paul Bakker5121ce52009-01-03 21:22:43 +00001795/*
1796 * Checkup routine
1797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001799{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001800 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001802 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001804 unsigned char rsa_plaintext[PT_LEN];
1805 unsigned char rsa_decrypted[PT_LEN];
1806 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001808 unsigned char sha1sum[20];
1809#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001812
1813 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1815 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1816 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1817 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1818 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1819 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1820 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1821 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
1823 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1827 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 {
1829 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001831
Hanno Becker5bc87292017-05-03 15:09:31 +01001832 ret = 1;
1833 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001834 }
1835
1836 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001838
1839 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 rsa_plaintext, rsa_ciphertext ) != 0 )
1843 {
1844 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
Hanno Becker5bc87292017-05-03 15:09:31 +01001847 ret = 1;
1848 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001849 }
1850
1851 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001855 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001856 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001857 {
1858 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001860
Hanno Becker5bc87292017-05-03 15:09:31 +01001861 ret = 1;
1862 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 }
1864
1865 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1866 {
1867 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
Hanno Becker5bc87292017-05-03 15:09:31 +01001870 ret = 1;
1871 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 }
1873
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001874 if( verbose != 0 )
1875 mbedtls_printf( "passed\n" );
1876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001878 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001879 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001884 sha1sum, rsa_ciphertext ) != 0 )
1885 {
1886 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001888
Hanno Becker5bc87292017-05-03 15:09:31 +01001889 ret = 1;
1890 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001891 }
1892
1893 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001897 sha1sum, rsa_ciphertext ) != 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 Becker5bc87292017-05-03 15:09:31 +01001902 ret = 1;
1903 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001904 }
1905
1906 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001907 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001909
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001910 if( verbose != 0 )
1911 mbedtls_printf( "\n" );
1912
Paul Bakker3d8fb632014-04-17 12:42:41 +02001913cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 mbedtls_rsa_free( &rsa );
1915#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001916 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001918 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001919}
1920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#endif /* MBEDTLS_RSA_C */