blob: 0c5bc4fb5de7e5b24f3d817c2f50df857b5b85d3 [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
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010069/* Implementation that should never be optimized out by the compiler */
70static void mbedtls_zeroize( void *v, size_t n ) {
71 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
72}
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/*
75 * Initialize an RSA context
76 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000078 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000079 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_THREADING_C)
86 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020087#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000088}
89
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010090/*
91 * Set padding for an existing RSA context
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010094{
95 ctx->padding = padding;
96 ctx->hash_id = hash_id;
97}
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101/*
102 * Generate an RSA keypair
103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000105 int (*f_rng)(void *, unsigned char *, size_t),
106 void *p_rng,
107 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000108{
109 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Paul Bakker21eb2802010-08-16 11:10:02 +0000112 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Janos Follathef441782016-09-21 13:18:12 +0100115 if( nbits % 2 )
116 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
117
118 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath10c575b2016-02-23 14:42:48 +0000119 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 /*
122 * find primes P and Q with Q < P so that:
123 * GCD( E, (P-1)*(Q-1) ) == 1
124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127 do
128 {
Janos Follath10c575b2016-02-23 14:42:48 +0000129 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000130 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Janos Follathef441782016-09-21 13:18:12 +0100132 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000133 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 continue;
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200139 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 continue;
141
Janos Follathef441782016-09-21 13:18:12 +0100142 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
143 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
146 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
147 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
148 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 /*
153 * D = E^-1 mod ((P-1)*(Q-1))
154 * DP = D mod (P - 1)
155 * DQ = D mod (Q - 1)
156 * QP = Q^-1 mod P
157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
159 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
160 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
161 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200163 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
165cleanup:
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 if( ret != 0 )
170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_rsa_free( ctx );
172 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 }
174
Paul Bakker48377d92013-08-30 12:06:24 +0200175 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176}
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180/*
181 * Check a public RSA key
182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000185 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000187
Paul Bakker48377d92013-08-30 12:06:24 +0200188 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200192 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
193 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200196 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
198 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
200 return( 0 );
201}
202
203/*
204 * Check a private RSA key
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000207{
208 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 return( ret );
213
Paul Bakker37940d9f2009-07-10 22:38:58 +0000214 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
218 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
219 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
220 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
223 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
225 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
226 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
227 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
230 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
231 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
234 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
235 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000236 /*
237 * Check for a valid PKCS1v2 private key
238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
240 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
241 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
242 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
243 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
244 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
245 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 }
Paul Bakker48377d92013-08-30 12:06:24 +0200249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
252 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
253 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
254 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000257 return( ret );
258
Paul Bakker6c591fa2011-05-05 11:49:20 +0000259 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000261
262 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263}
264
265/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100266 * Check if contexts holding a public and private key match
267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
271 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100274 }
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
277 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100280 }
281
282 return( 0 );
283}
284
285/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 * Do an RSA public key operation
287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000289 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 unsigned char *output )
291{
Paul Bakker23986e52011-04-24 08:57:21 +0000292 int ret;
293 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200298#if defined(MBEDTLS_THREADING_C)
299 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
300 return( ret );
301#endif
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200307 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
308 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 }
310
311 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
313 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
315cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200317 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
318 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100319#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326 return( 0 );
327}
328
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200329/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200330 * Generate or update blinding values, see section 10 of:
331 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200332 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200333 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200334 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200335static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200336 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
337{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200338 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200339
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200340 if( ctx->Vf.p != NULL )
341 {
342 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
344 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
345 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
346 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200347
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200348 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200349 }
350
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200351 /* Unblinding value: Vf = random number, invertible mod N */
352 do {
353 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
357 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
358 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200359
360 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
362 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 +0200363
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200364
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200365cleanup:
366 return( ret );
367}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200368
Paul Bakker5121ce52009-01-03 21:22:43 +0000369/*
Janos Follathe81102e2017-03-22 13:38:28 +0000370 * Exponent blinding supposed to prevent side-channel attacks using multiple
371 * traces of measurements to recover the RSA key. The more collisions are there,
372 * the more bits of the key can be recovered. See [3].
373 *
374 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
375 * observations on avarage.
376 *
377 * For example with 28 byte blinding to achieve 2 collisions the adversary has
378 * to make 2^112 observations on avarage.
379 *
380 * (With the currently (as of 2017 April) known best algorithms breaking 2048
381 * bit RSA requires approximately as much time as trying out 2^112 random keys.
382 * Thus in this sense with 28 byte blinding the security is not reduced by
383 * side-channel attacks like the one in [3])
384 *
385 * This countermeasure does not help if the key recovery is possible with a
386 * single trace.
387 */
388#define RSA_EXPONENT_BLINDING 28
389
390/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 * Do an RSA private key operation
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200394 int (*f_rng)(void *, unsigned char *, size_t),
395 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000396 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 unsigned char *output )
398{
Paul Bakker23986e52011-04-24 08:57:21 +0000399 int ret;
400 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100401
402 /* Temporary holding the result */
403 mbedtls_mpi T;
404
405 /* Temporaries holding P-1, Q-1 and the
406 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000407 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100408
409#if !defined(MBEDTLS_RSA_NO_CRT)
410 /* Temporaries holding the results mod p resp. mod q. */
411 mbedtls_mpi TP, TQ;
412
413 /* Temporaries holding the blinded exponents for
414 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000415 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100416
417 /* Pointers to actual exponents to be used - either the unblinded
418 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000419 mbedtls_mpi *DP = &ctx->DP;
420 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100421#else
422 /* Temporary holding the blinded exponent (if used). */
423 mbedtls_mpi D_blind;
424
425 /* Pointer to actual exponent to be used - either the unblinded
426 * or the blinded one, depending on the presence of a PRNG. */
427 mbedtls_mpi *D = &ctx->D;
428#endif
429
430#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100431 /* Temporaries holding the initial input and the double
432 * checked result; should be the same in the end. */
433 mbedtls_mpi I, C;
Hanno Becker06811ce2017-05-03 15:10:34 +0100434#endif
435
436#if defined(MBEDTLS_RSA_FORCE_BLINDING)
437 if( f_rng == NULL )
438 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Janos Follathe81102e2017-03-22 13:38:28 +0000439#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100441 /* Make sure we have private key info, prevent possible misuse */
442 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
443 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
444
Hanno Becker06811ce2017-05-03 15:10:34 +0100445#if defined(MBEDTLS_THREADING_C)
446 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
447 return( ret );
448#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000449
Hanno Becker06811ce2017-05-03 15:10:34 +0100450 /* MPI Initialization */
451
452 mbedtls_mpi_init( &T );
453
454 mbedtls_mpi_init( &P1 );
455 mbedtls_mpi_init( &Q1 );
456 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000457
458 if( f_rng != NULL )
459 {
Janos Follathe81102e2017-03-22 13:38:28 +0000460#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000461 mbedtls_mpi_init( &D_blind );
462#else
463 mbedtls_mpi_init( &DP_blind );
464 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000465#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000466 }
Janos Follathe81102e2017-03-22 13:38:28 +0000467
Hanno Becker06811ce2017-05-03 15:10:34 +0100468#if !defined(MBEDTLS_RSA_NO_CRT)
469 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200470#endif
471
Hanno Becker06811ce2017-05-03 15:10:34 +0100472#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100473 mbedtls_mpi_init( &I );
474 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100475#endif
476
477 /* End of MPI initialization */
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
480 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200482 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
483 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 }
485
Hanno Becker06811ce2017-05-03 15:10:34 +0100486#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100487 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100488#endif
489
Paul Bakkerf451bac2013-08-30 15:37:02 +0200490 if( f_rng != NULL )
491 {
492 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200493 * Blinding
494 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200495 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200496 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
497 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000499
Janos Follathe81102e2017-03-22 13:38:28 +0000500 /*
501 * Exponent blinding
502 */
503 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
504 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
505
Janos Follathf9203b42017-03-22 15:13:15 +0000506#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000507 /*
508 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
509 */
510 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
511 f_rng, p_rng ) );
512 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
513 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
514 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
515
516 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000517#else
518 /*
519 * DP_blind = ( P - 1 ) * R + DP
520 */
521 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
522 f_rng, p_rng ) );
523 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
524 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
525 &ctx->DP ) );
526
527 DP = &DP_blind;
528
529 /*
530 * DQ_blind = ( Q - 1 ) * R + DQ
531 */
532 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
533 f_rng, p_rng ) );
534 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
535 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
536 &ctx->DQ ) );
537
538 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000539#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200540 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000543 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100544#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200545 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000546 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000547 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100548 * TP = input ^ dP mod P
549 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100551
552 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
553 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000554
555 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100556 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100558 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
559 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
560 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
562 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100563 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100565 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
566 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200568
Paul Bakkerf451bac2013-08-30 15:37:02 +0200569 if( f_rng != NULL )
570 {
571 /*
572 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200573 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200574 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200575 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200577 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000578
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100579 /* If requested by the config, verify the result to prevent glitching attacks. */
Hanno Becker06811ce2017-05-03 15:10:34 +0100580#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100581 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E, &ctx->N, &ctx->RN ) );
582 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +0100583 {
Hanno Becker06811ce2017-05-03 15:10:34 +0100584 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
585 goto cleanup;
586 }
587#endif /* MBEDTLS_RSA_REQUIRE_VERIFICATION */
588
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
592cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200594 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
595 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200596#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200597
Hanno Becker06811ce2017-05-03 15:10:34 +0100598 mbedtls_mpi_free( &P1 );
599 mbedtls_mpi_free( &Q1 );
600 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000601
602 if( f_rng != NULL )
603 {
Janos Follathe81102e2017-03-22 13:38:28 +0000604#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000605 mbedtls_mpi_free( &D_blind );
606#else
607 mbedtls_mpi_free( &DP_blind );
608 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000609#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000610 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
Hanno Becker06811ce2017-05-03 15:10:34 +0100612 mbedtls_mpi_free( &T );
613
614#if !defined(MBEDTLS_RSA_NO_CRT)
615 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
616#endif
617
618#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100619 mbedtls_mpi_free( &C );
620 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +0100621#endif
622
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626 return( 0 );
627}
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000630/**
631 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
632 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000633 * \param dst buffer to mask
634 * \param dlen length of destination buffer
635 * \param src source of the mask generation
636 * \param slen length of the source buffer
637 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000638 */
Paul Bakker48377d92013-08-30 12:06:24 +0200639static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000641{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000643 unsigned char counter[4];
644 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000645 unsigned int hlen;
646 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000649 memset( counter, 0, 4 );
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000652
Simon Butcher02037452016-03-01 21:19:12 +0000653 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000654 p = dst;
655
656 while( dlen > 0 )
657 {
658 use_len = hlen;
659 if( dlen < hlen )
660 use_len = dlen;
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_md_starts( md_ctx );
663 mbedtls_md_update( md_ctx, src, slen );
664 mbedtls_md_update( md_ctx, counter, 4 );
665 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000666
667 for( i = 0; i < use_len; ++i )
668 *p++ ^= mask[i];
669
670 counter[3]++;
671
672 dlen -= use_len;
673 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200674
675 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000676}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100680/*
681 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100684 int (*f_rng)(void *, unsigned char *, size_t),
685 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100686 int mode,
687 const unsigned char *label, size_t label_len,
688 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100689 const unsigned char *input,
690 unsigned char *output )
691{
692 size_t olen;
693 int ret;
694 unsigned char *p = output;
695 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 const mbedtls_md_info_t *md_info;
697 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
700 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200701
702 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100706 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100708
709 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100711
Simon Butcher02037452016-03-01 21:19:12 +0000712 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000713 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100715
716 memset( output, 0, olen );
717
718 *p++ = 0;
719
Simon Butcher02037452016-03-01 21:19:12 +0000720 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100721 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100723
724 p += hlen;
725
Simon Butcher02037452016-03-01 21:19:12 +0000726 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100728 p += hlen;
729 p += olen - 2 * hlen - 2 - ilen;
730 *p++ = 1;
731 memcpy( p, input, ilen );
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700734 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
735 {
736 mbedtls_md_free( &md_ctx );
737 return( ret );
738 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100739
Simon Butcher02037452016-03-01 21:19:12 +0000740 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100741 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
742 &md_ctx );
743
Simon Butcher02037452016-03-01 21:19:12 +0000744 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100745 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
746 &md_ctx );
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 return( ( mode == MBEDTLS_RSA_PUBLIC )
751 ? mbedtls_rsa_public( ctx, output, output )
752 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100757/*
758 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100761 int (*f_rng)(void *, unsigned char *, size_t),
762 void *p_rng,
763 int mode, size_t ilen,
764 const unsigned char *input,
765 unsigned char *output )
766{
767 size_t nb_pad, olen;
768 int ret;
769 unsigned char *p = output;
770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
772 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200773
Janos Follath1ed9f992016-03-18 11:45:44 +0000774 // We don't check p_rng because it won't be dereferenced here
775 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100777
778 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100779
Simon Butcher02037452016-03-01 21:19:12 +0000780 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000781 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100783
784 nb_pad = olen - 3 - ilen;
785
786 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100790
791 while( nb_pad-- > 0 )
792 {
793 int rng_dl = 100;
794
795 do {
796 ret = f_rng( p_rng, p, 1 );
797 } while( *p == 0 && --rng_dl && ret == 0 );
798
Simon Butcher02037452016-03-01 21:19:12 +0000799 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200800 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100802
803 p++;
804 }
805 }
806 else
807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100809
810 while( nb_pad-- > 0 )
811 *p++ = 0xFF;
812 }
813
814 *p++ = 0;
815 memcpy( p, input, ilen );
816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 return( ( mode == MBEDTLS_RSA_PUBLIC )
818 ? mbedtls_rsa_public( ctx, output, output )
819 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100820}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200821#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100822
Paul Bakker5121ce52009-01-03 21:22:43 +0000823/*
824 * Add the message padding, then do an RSA operation
825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000827 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000828 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000829 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000830 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000831 unsigned char *output )
832{
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 switch( ctx->padding )
834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#if defined(MBEDTLS_PKCS1_V15)
836 case MBEDTLS_RSA_PKCS_V15:
837 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100838 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200839#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#if defined(MBEDTLS_PKCS1_V21)
842 case MBEDTLS_RSA_PKCS_V21:
843 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100844 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000845#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
847 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000849 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000850}
851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000853/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100854 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000855 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200857 int (*f_rng)(void *, unsigned char *, size_t),
858 void *p_rng,
859 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100860 const unsigned char *label, size_t label_len,
861 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100862 const unsigned char *input,
863 unsigned char *output,
864 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000865{
Paul Bakker23986e52011-04-24 08:57:21 +0000866 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100867 size_t ilen, i, pad_len;
868 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
870 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000871 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 const mbedtls_md_info_t *md_info;
873 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100874
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100875 /*
876 * Parameters sanity checks
877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
879 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000880
881 ilen = ctx->len;
882
Paul Bakker27fdf462011-06-09 13:55:13 +0000883 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100887 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100889
Janos Follathc17cda12016-02-11 11:08:18 +0000890 hlen = mbedtls_md_get_size( md_info );
891
892 // checking for integer underflow
893 if( 2 * hlen + 2 > ilen )
894 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
895
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100896 /*
897 * RSA operation
898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 ret = ( mode == MBEDTLS_RSA_PUBLIC )
900 ? mbedtls_rsa_public( ctx, input, buf )
901 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
903 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100904 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000905
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100906 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100907 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100908 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700910 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
911 {
912 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100913 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700914 }
915
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100916
917 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100919
920 /* seed: Apply seedMask to maskedSeed */
921 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
922 &md_ctx );
923
924 /* DB: Apply dbMask to maskedDB */
925 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
926 &md_ctx );
927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100929
930 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100931 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100932 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100934 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000935
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100936 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100937
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100938 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100939
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100940 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100941 for( i = 0; i < hlen; i++ )
942 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100943
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100944 /* Get zero-padding len, but always read till end of buffer
945 * (minus one, for the 01 byte) */
946 pad_len = 0;
947 pad_done = 0;
948 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
949 {
950 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100951 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100952 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100953
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100954 p += pad_len;
955 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100956
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100957 /*
958 * The only information "leaked" is whether the padding was correct or not
959 * (eg, no data is copied if it was not correct). This meets the
960 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
961 * the different error conditions.
962 */
963 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100964 {
965 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
966 goto cleanup;
967 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100968
Paul Bakker66d5d072014-06-17 16:39:18 +0200969 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100970 {
971 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
972 goto cleanup;
973 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100974
975 *olen = ilen - (p - buf);
976 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100977 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100978
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100979cleanup:
980 mbedtls_zeroize( buf, sizeof( buf ) );
981 mbedtls_zeroize( lhash, sizeof( lhash ) );
982
983 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100984}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100988/*
989 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200992 int (*f_rng)(void *, unsigned char *, size_t),
993 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100994 int mode, size_t *olen,
995 const unsigned char *input,
996 unsigned char *output,
997 size_t output_max_len)
998{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100999 int ret;
1000 size_t ilen, pad_count = 0, i;
1001 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1005 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001006
1007 ilen = ctx->len;
1008
1009 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1013 ? mbedtls_rsa_public( ctx, input, buf )
1014 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001015
1016 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001017 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001018
1019 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001020 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001021
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001022 /*
1023 * Check and get padding len in "constant-time"
1024 */
1025 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001026
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001027 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001031
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001032 /* Get padding len, but always read till end of buffer
1033 * (minus one, for the 00 byte) */
1034 for( i = 0; i < ilen - 3; i++ )
1035 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001036 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1037 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001038 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001039
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001040 p += pad_count;
1041 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001042 }
1043 else
1044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001046
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001047 /* Get padding len, but always read till end of buffer
1048 * (minus one, for the 00 byte) */
1049 for( i = 0; i < ilen - 3; i++ )
1050 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001051 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001052 pad_count += ( pad_done == 0 );
1053 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001054
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001055 p += pad_count;
1056 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 }
1058
Janos Follathc69fa502016-02-12 13:30:09 +00001059 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001060
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001061 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001062 {
1063 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1064 goto cleanup;
1065 }
Paul Bakker8804f692013-02-28 18:06:26 +01001066
Paul Bakker66d5d072014-06-17 16:39:18 +02001067 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001068 {
1069 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1070 goto cleanup;
1071 }
Paul Bakker060c5682009-01-12 21:48:39 +00001072
Paul Bakker27fdf462011-06-09 13:55:13 +00001073 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001074 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001075 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001076
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001077cleanup:
1078 mbedtls_zeroize( buf, sizeof( buf ) );
1079
1080 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001083
1084/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001085 * Do an RSA operation, then remove the message padding
1086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001088 int (*f_rng)(void *, unsigned char *, size_t),
1089 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001090 int mode, size_t *olen,
1091 const unsigned char *input,
1092 unsigned char *output,
1093 size_t output_max_len)
1094{
1095 switch( ctx->padding )
1096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_PKCS1_V15)
1098 case MBEDTLS_RSA_PKCS_V15:
1099 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001100 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001101#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#if defined(MBEDTLS_PKCS1_V21)
1104 case MBEDTLS_RSA_PKCS_V21:
1105 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001106 olen, input, output,
1107 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001108#endif
1109
1110 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001112 }
1113}
1114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001116/*
1117 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001120 int (*f_rng)(void *, unsigned char *, size_t),
1121 void *p_rng,
1122 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001124 unsigned int hashlen,
1125 const unsigned char *hash,
1126 unsigned char *sig )
1127{
1128 size_t olen;
1129 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001131 unsigned int slen, hlen, offset = 0;
1132 int ret;
1133 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 const mbedtls_md_info_t *md_info;
1135 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1138 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001139
1140 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001142
1143 olen = ctx->len;
1144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001146 {
Simon Butcher02037452016-03-01 21:19:12 +00001147 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001149 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001153 }
1154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001156 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001160 slen = hlen;
1161
1162 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001164
1165 memset( sig, 0, olen );
1166
Simon Butcher02037452016-03-01 21:19:12 +00001167 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001168 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001170
Simon Butcher02037452016-03-01 21:19:12 +00001171 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001172 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001173 p += olen - hlen * 2 - 2;
1174 *p++ = 0x01;
1175 memcpy( p, salt, slen );
1176 p += slen;
1177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001179 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1180 {
1181 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001182 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001183 return( ret );
1184 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001185
Simon Butcher02037452016-03-01 21:19:12 +00001186 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 mbedtls_md_starts( &md_ctx );
1188 mbedtls_md_update( &md_ctx, p, 8 );
1189 mbedtls_md_update( &md_ctx, hash, hashlen );
1190 mbedtls_md_update( &md_ctx, salt, slen );
1191 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001192 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001193
Simon Butcher02037452016-03-01 21:19:12 +00001194 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001195 if( msb % 8 == 0 )
1196 offset = 1;
1197
Simon Butcher02037452016-03-01 21:19:12 +00001198 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001199 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001202
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001203 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001204 sig[0] &= 0xFF >> ( olen * 8 - msb );
1205
1206 p += hlen;
1207 *p++ = 0xBC;
1208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 return( ( mode == MBEDTLS_RSA_PUBLIC )
1210 ? mbedtls_rsa_public( ctx, sig, sig )
1211 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001216/*
1217 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1218 */
1219/*
1220 * Do an RSA operation to sign the message digest
1221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001223 int (*f_rng)(void *, unsigned char *, size_t),
1224 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001225 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001227 unsigned int hashlen,
1228 const unsigned char *hash,
1229 unsigned char *sig )
1230{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001231 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001232 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001233 const char *oid = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1236 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001237
1238 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001239 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001242 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001244 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1248 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001249
Paul Bakkerc70b9822013-04-07 22:00:46 +02001250 nb_pad -= 10 + oid_size;
1251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001253 }
1254
Paul Bakkerc70b9822013-04-07 22:00:46 +02001255 nb_pad -= hashlen;
1256
Paul Bakkerb3869132013-02-28 17:21:01 +01001257 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001259
1260 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001262 memset( p, 0xFF, nb_pad );
1263 p += nb_pad;
1264 *p++ = 0;
1265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001267 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001268 memcpy( p, hash, hashlen );
1269 }
1270 else
1271 {
1272 /*
1273 * DigestInfo ::= SEQUENCE {
1274 * digestAlgorithm DigestAlgorithmIdentifier,
1275 * digest Digest }
1276 *
1277 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1278 *
1279 * Digest ::= OCTET STRING
1280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001282 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001284 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001286 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001287 memcpy( p, oid, oid_size );
1288 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001290 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001292 *p++ = hashlen;
1293 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001294 }
1295
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001296 if( mode == MBEDTLS_RSA_PUBLIC )
1297 return( mbedtls_rsa_public( ctx, sig, sig ) );
1298
1299 /*
1300 * In order to prevent Lenstra's attack, make the signature in a
1301 * temporary buffer and check it before returning it.
1302 */
1303 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001304 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001305 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1306
Simon Butcher1285ab52016-01-01 21:42:47 +00001307 verif = mbedtls_calloc( 1, ctx->len );
1308 if( verif == NULL )
1309 {
1310 mbedtls_free( sig_try );
1311 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1312 }
1313
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001314 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1315 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1316
1317 /* Compare in constant time just in case */
1318 for( diff = 0, i = 0; i < ctx->len; i++ )
1319 diff |= verif[i] ^ sig[i];
1320 diff_no_optimize = diff;
1321
1322 if( diff_no_optimize != 0 )
1323 {
1324 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1325 goto cleanup;
1326 }
1327
1328 memcpy( sig, sig_try, ctx->len );
1329
1330cleanup:
1331 mbedtls_free( sig_try );
1332 mbedtls_free( verif );
1333
1334 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001335}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001337
1338/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 * Do an RSA operation to sign the message digest
1340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001342 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001343 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001346 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001347 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001348 unsigned char *sig )
1349{
Paul Bakker5121ce52009-01-03 21:22:43 +00001350 switch( ctx->padding )
1351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#if defined(MBEDTLS_PKCS1_V15)
1353 case MBEDTLS_RSA_PKCS_V15:
1354 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001355 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001356#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#if defined(MBEDTLS_PKCS1_V21)
1359 case MBEDTLS_RSA_PKCS_V21:
1360 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001361 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001362#endif
1363
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001367}
1368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001370/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001371 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001374 int (*f_rng)(void *, unsigned char *, size_t),
1375 void *p_rng,
1376 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001378 unsigned int hashlen,
1379 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001381 int expected_salt_len,
1382 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001383{
Paul Bakker23986e52011-04-24 08:57:21 +00001384 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001385 size_t siglen;
1386 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001388 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001389 unsigned int hlen;
1390 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 const mbedtls_md_info_t *md_info;
1392 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001393 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1396 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001397
Paul Bakker5121ce52009-01-03 21:22:43 +00001398 siglen = ctx->len;
1399
Paul Bakker27fdf462011-06-09 13:55:13 +00001400 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1404 ? mbedtls_rsa_public( ctx, sig, buf )
1405 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001406
1407 if( ret != 0 )
1408 return( ret );
1409
1410 p = buf;
1411
Paul Bakkerb3869132013-02-28 17:21:01 +01001412 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001416 {
Simon Butcher02037452016-03-01 21:19:12 +00001417 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001419 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001423 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001426 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001430 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001431
Paul Bakkerb3869132013-02-28 17:21:01 +01001432 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001433
Simon Butcher02037452016-03-01 21:19:12 +00001434 /*
1435 * Note: EMSA-PSS verification is over the length of N - 1 bits
1436 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001437 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001438
Simon Butcher02037452016-03-01 21:19:12 +00001439 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001440 if( msb % 8 == 0 )
1441 {
1442 p++;
1443 siglen -= 1;
1444 }
1445 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001449 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1450 {
1451 mbedtls_md_free( &md_ctx );
1452 return( ret );
1453 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001454
Paul Bakkerb3869132013-02-28 17:21:01 +01001455 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001456
Paul Bakkerb3869132013-02-28 17:21:01 +01001457 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001458
Paul Bakker4de44aa2013-12-31 11:43:01 +01001459 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001460 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001461
Paul Bakkerb3869132013-02-28 17:21:01 +01001462 if( p == buf + siglen ||
1463 *p++ != 0x01 )
1464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 mbedtls_md_free( &md_ctx );
1466 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001467 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001468
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001469 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001470 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001473 slen != (size_t) expected_salt_len )
1474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 mbedtls_md_free( &md_ctx );
1476 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001477 }
1478
Simon Butcher02037452016-03-01 21:19:12 +00001479 /*
1480 * Generate H = Hash( M' )
1481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 mbedtls_md_starts( &md_ctx );
1483 mbedtls_md_update( &md_ctx, zeros, 8 );
1484 mbedtls_md_update( &md_ctx, hash, hashlen );
1485 mbedtls_md_update( &md_ctx, p, slen );
1486 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001489
Paul Bakkerb3869132013-02-28 17:21:01 +01001490 if( memcmp( p + slen, result, hlen ) == 0 )
1491 return( 0 );
1492 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001494}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001495
1496/*
1497 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001500 int (*f_rng)(void *, unsigned char *, size_t),
1501 void *p_rng,
1502 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001504 unsigned int hashlen,
1505 const unsigned char *hash,
1506 const unsigned char *sig )
1507{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1509 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001510 : md_alg;
1511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001513 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001515 sig ) );
1516
1517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001521/*
1522 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001525 int (*f_rng)(void *, unsigned char *, size_t),
1526 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001527 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001529 unsigned int hashlen,
1530 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001531 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001532{
1533 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001534 size_t len, siglen, asn1_len;
1535 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 mbedtls_md_type_t msg_md_alg;
1537 const mbedtls_md_info_t *md_info;
1538 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001539 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1542 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001543
1544 siglen = ctx->len;
1545
1546 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001549 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1550 ? mbedtls_rsa_public( ctx, sig, buf )
1551 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001552
1553 if( ret != 0 )
1554 return( ret );
1555
1556 p = buf;
1557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1559 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001560
1561 while( *p != 0 )
1562 {
1563 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001565 p++;
1566 }
1567 p++;
1568
1569 len = siglen - ( p - buf );
1570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001572 {
1573 if( memcmp( p, hash, hashlen ) == 0 )
1574 return( 0 );
1575 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001577 }
1578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001580 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1582 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001583
1584 end = p + len;
1585
Simon Butcher02037452016-03-01 21:19:12 +00001586 /*
1587 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1590 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1591 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001592
1593 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1597 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1598 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001599
1600 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1604 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001605
1606 oid.p = p;
1607 p += oid.len;
1608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1610 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001611
1612 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001614
1615 /*
1616 * assume the algorithm parameters must be NULL
1617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1619 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1622 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001623
1624 if( asn1_len != hashlen )
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 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001629
1630 p += hashlen;
1631
1632 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001634
1635 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001636}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001638
1639/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001640 * Do an RSA operation and check the message digest
1641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001643 int (*f_rng)(void *, unsigned char *, size_t),
1644 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001645 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001647 unsigned int hashlen,
1648 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001649 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001650{
1651 switch( ctx->padding )
1652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653#if defined(MBEDTLS_PKCS1_V15)
1654 case MBEDTLS_RSA_PKCS_V15:
1655 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001656 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001657#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#if defined(MBEDTLS_PKCS1_V21)
1660 case MBEDTLS_RSA_PKCS_V21:
1661 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001662 hashlen, hash, sig );
1663#endif
1664
1665 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001667 }
1668}
1669
1670/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001671 * Copy the components of an RSA key
1672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001674{
1675 int ret;
1676
1677 dst->ver = src->ver;
1678 dst->len = src->len;
1679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1681 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1684 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1685 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1686 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1687 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1688 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1691 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1692 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1695 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001696
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001697 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001698 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001699
1700cleanup:
1701 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001703
1704 return( ret );
1705}
1706
1707/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001708 * Free the components of an RSA key
1709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001711{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1713 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1714 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1715 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1716 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#if defined(MBEDTLS_THREADING_C)
1719 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001720#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001721}
1722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001724
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001725#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001726
1727/*
1728 * Example RSA-1024 keypair, for test purposes
1729 */
1730#define KEY_LEN 128
1731
1732#define RSA_N "9292758453063D803DD603D5E777D788" \
1733 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1734 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1735 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1736 "93A89813FBF3C4F8066D2D800F7C38A8" \
1737 "1AE31942917403FF4946B0A83D3D3E05" \
1738 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1739 "5E94BB77B07507233A0BC7BAC8F90F79"
1740
1741#define RSA_E "10001"
1742
1743#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1744 "66CA472BC44D253102F8B4A9D3BFA750" \
1745 "91386C0077937FE33FA3252D28855837" \
1746 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1747 "DF79C5CE07EE72C7F123142198164234" \
1748 "CABB724CF78B8173B9F880FC86322407" \
1749 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1750 "071513A1E85B5DFA031F21ECAE91A34D"
1751
1752#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1753 "2C01CAD19EA484A87EA4377637E75500" \
1754 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1755 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1756
1757#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1758 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1759 "910E4168387E3C30AA1E00C339A79508" \
1760 "8452DD96A9A5EA5D9DCA68DA636032AF"
1761
1762#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1763 "3C94D22288ACD763FD8E5600ED4A702D" \
1764 "F84198A5F06C2E72236AE490C93F07F8" \
1765 "3CC559CD27BC2D1CA488811730BB5725"
1766
1767#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1768 "D8AAEA56749EA28623272E4F7D0592AF" \
1769 "7C1F1313CAC9471B5C523BFE592F517B" \
1770 "407A1BD76C164B93DA2D32A383E58357"
1771
1772#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1773 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1774 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1775 "A74206CEC169D74BF5A8C50D6F48EA08"
1776
1777#define PT_LEN 24
1778#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1779 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001782static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001783{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001784#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001785 size_t i;
1786
Paul Bakker545570e2010-07-18 09:00:25 +00001787 if( rng_state != NULL )
1788 rng_state = NULL;
1789
Paul Bakkera3d195c2011-11-27 21:07:34 +00001790 for( i = 0; i < len; ++i )
1791 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001792#else
1793 if( rng_state != NULL )
1794 rng_state = NULL;
1795
1796 arc4random_buf( output, len );
1797#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001798
Paul Bakkera3d195c2011-11-27 21:07:34 +00001799 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001800}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001801#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001802
Paul Bakker5121ce52009-01-03 21:22:43 +00001803/*
1804 * Checkup routine
1805 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001807{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001808 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001810 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001812 unsigned char rsa_plaintext[PT_LEN];
1813 unsigned char rsa_decrypted[PT_LEN];
1814 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001816 unsigned char sha1sum[20];
1817#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
1821 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1823 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1824 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1825 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1826 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1827 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1828 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1829 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001830
1831 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1835 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 {
1837 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
Hanno Becker5bc87292017-05-03 15:09:31 +01001840 ret = 1;
1841 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 }
1843
1844 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001845 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
1847 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001850 rsa_plaintext, rsa_ciphertext ) != 0 )
1851 {
1852 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
Hanno Becker5bc87292017-05-03 15:09:31 +01001855 ret = 1;
1856 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001857 }
1858
1859 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001863 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001864 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 {
1866 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001868
Hanno Becker5bc87292017-05-03 15:09:31 +01001869 ret = 1;
1870 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001871 }
1872
1873 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1874 {
1875 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
Hanno Becker5bc87292017-05-03 15:09:31 +01001878 ret = 1;
1879 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001880 }
1881
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001882 if( verbose != 0 )
1883 mbedtls_printf( "passed\n" );
1884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001887 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 sha1sum, rsa_ciphertext ) != 0 )
1893 {
1894 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
Hanno Becker5bc87292017-05-03 15:09:31 +01001897 ret = 1;
1898 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 }
1900
1901 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 sha1sum, rsa_ciphertext ) != 0 )
1906 {
1907 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001909
Hanno Becker5bc87292017-05-03 15:09:31 +01001910 ret = 1;
1911 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001912 }
1913
1914 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001915 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001918 if( verbose != 0 )
1919 mbedtls_printf( "\n" );
1920
Paul Bakker3d8fb632014-04-17 12:42:41 +02001921cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 mbedtls_rsa_free( &rsa );
1923#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001924 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001925#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001926 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001927}
1928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#endif /* MBEDTLS_RSA_C */