blob: bdd2538c3a07906121b978d65687cb3cafc6d33b [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;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +0000402 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +0000403#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000404 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000405 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +0000406#else
407 mbedtls_mpi DP_blind, DQ_blind;
408 mbedtls_mpi *DP = &ctx->DP;
409 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +0000410#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100412 /* Make sure we have private key info, prevent possible misuse */
413 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
414 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000417 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
418
419
420 if( f_rng != NULL )
421 {
Janos Follathe81102e2017-03-22 13:38:28 +0000422#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000423 mbedtls_mpi_init( &D_blind );
424#else
425 mbedtls_mpi_init( &DP_blind );
426 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000427#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000428 }
Janos Follathe81102e2017-03-22 13:38:28 +0000429
Paul Bakker5121ce52009-01-03 21:22:43 +0000430
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200431#if defined(MBEDTLS_THREADING_C)
432 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
433 return( ret );
434#endif
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
437 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200439 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
440 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000441 }
442
Paul Bakkerf451bac2013-08-30 15:37:02 +0200443 if( f_rng != NULL )
444 {
445 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200446 * Blinding
447 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200448 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200449 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
450 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000452
Janos Follathe81102e2017-03-22 13:38:28 +0000453 /*
454 * Exponent blinding
455 */
456 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
457 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
458
Janos Follathf9203b42017-03-22 15:13:15 +0000459#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000460 /*
461 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
462 */
463 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
464 f_rng, p_rng ) );
465 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
466 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
467 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
468
469 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000470#else
471 /*
472 * DP_blind = ( P - 1 ) * R + DP
473 */
474 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
475 f_rng, p_rng ) );
476 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
477 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
478 &ctx->DP ) );
479
480 DP = &DP_blind;
481
482 /*
483 * DQ_blind = ( Q - 1 ) * R + DQ
484 */
485 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
486 f_rng, p_rng ) );
487 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
488 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
489 &ctx->DQ ) );
490
491 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000492#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200493 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000496 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100497#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200498 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000499 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000500 *
501 * T1 = input ^ dP mod P
502 * T2 = input ^ dQ mod Q
503 */
Janos Follathf9203b42017-03-22 15:13:15 +0000504 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
505 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
507 /*
508 * T = (T1 - T2) * (Q^-1 mod P) mod P
509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
511 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
512 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
514 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200515 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
518 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
519#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200520
Paul Bakkerf451bac2013-08-30 15:37:02 +0200521 if( f_rng != NULL )
522 {
523 /*
524 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200525 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200526 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200527 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200529 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000530
531 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
534cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200536 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
537 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200538#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000541 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
542
543 if( f_rng != NULL )
544 {
Janos Follathe81102e2017-03-22 13:38:28 +0000545#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000546 mbedtls_mpi_free( &D_blind );
547#else
548 mbedtls_mpi_free( &DP_blind );
549 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000550#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000551 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556 return( 0 );
557}
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000560/**
561 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
562 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000563 * \param dst buffer to mask
564 * \param dlen length of destination buffer
565 * \param src source of the mask generation
566 * \param slen length of the source buffer
567 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000568 */
Paul Bakker48377d92013-08-30 12:06:24 +0200569static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000571{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000573 unsigned char counter[4];
574 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000575 unsigned int hlen;
576 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000579 memset( counter, 0, 4 );
580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000582
Simon Butcher02037452016-03-01 21:19:12 +0000583 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000584 p = dst;
585
586 while( dlen > 0 )
587 {
588 use_len = hlen;
589 if( dlen < hlen )
590 use_len = dlen;
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_md_starts( md_ctx );
593 mbedtls_md_update( md_ctx, src, slen );
594 mbedtls_md_update( md_ctx, counter, 4 );
595 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000596
597 for( i = 0; i < use_len; ++i )
598 *p++ ^= mask[i];
599
600 counter[3]++;
601
602 dlen -= use_len;
603 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200604
605 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000606}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100610/*
611 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100614 int (*f_rng)(void *, unsigned char *, size_t),
615 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100616 int mode,
617 const unsigned char *label, size_t label_len,
618 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100619 const unsigned char *input,
620 unsigned char *output )
621{
622 size_t olen;
623 int ret;
624 unsigned char *p = output;
625 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 const mbedtls_md_info_t *md_info;
627 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
630 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200631
632 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100636 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100638
639 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100641
Simon Butcher02037452016-03-01 21:19:12 +0000642 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000643 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100645
646 memset( output, 0, olen );
647
648 *p++ = 0;
649
Simon Butcher02037452016-03-01 21:19:12 +0000650 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100651 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100653
654 p += hlen;
655
Simon Butcher02037452016-03-01 21:19:12 +0000656 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100658 p += hlen;
659 p += olen - 2 * hlen - 2 - ilen;
660 *p++ = 1;
661 memcpy( p, input, ilen );
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700664 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
665 {
666 mbedtls_md_free( &md_ctx );
667 return( ret );
668 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100669
Simon Butcher02037452016-03-01 21:19:12 +0000670 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100671 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
672 &md_ctx );
673
Simon Butcher02037452016-03-01 21:19:12 +0000674 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100675 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
676 &md_ctx );
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( ( mode == MBEDTLS_RSA_PUBLIC )
681 ? mbedtls_rsa_public( ctx, output, output )
682 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100683}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100687/*
688 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
689 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100691 int (*f_rng)(void *, unsigned char *, size_t),
692 void *p_rng,
693 int mode, size_t ilen,
694 const unsigned char *input,
695 unsigned char *output )
696{
697 size_t nb_pad, olen;
698 int ret;
699 unsigned char *p = output;
700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
702 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200703
Janos Follath1ed9f992016-03-18 11:45:44 +0000704 // We don't check p_rng because it won't be dereferenced here
705 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100707
708 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100709
Simon Butcher02037452016-03-01 21:19:12 +0000710 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000711 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100713
714 nb_pad = olen - 3 - ilen;
715
716 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100720
721 while( nb_pad-- > 0 )
722 {
723 int rng_dl = 100;
724
725 do {
726 ret = f_rng( p_rng, p, 1 );
727 } while( *p == 0 && --rng_dl && ret == 0 );
728
Simon Butcher02037452016-03-01 21:19:12 +0000729 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200730 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100732
733 p++;
734 }
735 }
736 else
737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100739
740 while( nb_pad-- > 0 )
741 *p++ = 0xFF;
742 }
743
744 *p++ = 0;
745 memcpy( p, input, ilen );
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 return( ( mode == MBEDTLS_RSA_PUBLIC )
748 ? mbedtls_rsa_public( ctx, output, output )
749 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100750}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100752
Paul Bakker5121ce52009-01-03 21:22:43 +0000753/*
754 * Add the message padding, then do an RSA operation
755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000757 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000758 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000759 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000760 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 unsigned char *output )
762{
Paul Bakker5121ce52009-01-03 21:22:43 +0000763 switch( ctx->padding )
764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#if defined(MBEDTLS_PKCS1_V15)
766 case MBEDTLS_RSA_PKCS_V15:
767 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100768 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200769#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#if defined(MBEDTLS_PKCS1_V21)
772 case MBEDTLS_RSA_PKCS_V21:
773 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100774 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000775#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000776
777 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000779 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000780}
781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000783/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100784 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200787 int (*f_rng)(void *, unsigned char *, size_t),
788 void *p_rng,
789 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100790 const unsigned char *label, size_t label_len,
791 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100792 const unsigned char *input,
793 unsigned char *output,
794 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000795{
Paul Bakker23986e52011-04-24 08:57:21 +0000796 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100797 size_t ilen, i, pad_len;
798 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
800 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000801 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 const mbedtls_md_info_t *md_info;
803 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100804
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100805 /*
806 * Parameters sanity checks
807 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
809 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000810
811 ilen = ctx->len;
812
Paul Bakker27fdf462011-06-09 13:55:13 +0000813 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100817 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100819
Janos Follathc17cda12016-02-11 11:08:18 +0000820 hlen = mbedtls_md_get_size( md_info );
821
822 // checking for integer underflow
823 if( 2 * hlen + 2 > ilen )
824 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
825
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100826 /*
827 * RSA operation
828 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 ret = ( mode == MBEDTLS_RSA_PUBLIC )
830 ? mbedtls_rsa_public( ctx, input, buf )
831 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000832
833 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100834 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100836 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100837 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100838 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700840 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
841 {
842 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100843 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700844 }
845
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100846
847 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100849
850 /* seed: Apply seedMask to maskedSeed */
851 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
852 &md_ctx );
853
854 /* DB: Apply dbMask to maskedDB */
855 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
856 &md_ctx );
857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100859
860 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100861 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100862 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100864 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000865
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100866 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100867
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100868 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100869
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100870 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100871 for( i = 0; i < hlen; i++ )
872 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100873
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100874 /* Get zero-padding len, but always read till end of buffer
875 * (minus one, for the 01 byte) */
876 pad_len = 0;
877 pad_done = 0;
878 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
879 {
880 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100881 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100882 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100883
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100884 p += pad_len;
885 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100886
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100887 /*
888 * The only information "leaked" is whether the padding was correct or not
889 * (eg, no data is copied if it was not correct). This meets the
890 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
891 * the different error conditions.
892 */
893 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100894 {
895 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
896 goto cleanup;
897 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100898
Paul Bakker66d5d072014-06-17 16:39:18 +0200899 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100900 {
901 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
902 goto cleanup;
903 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100904
905 *olen = ilen - (p - buf);
906 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100907 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100908
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100909cleanup:
910 mbedtls_zeroize( buf, sizeof( buf ) );
911 mbedtls_zeroize( lhash, sizeof( lhash ) );
912
913 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100918/*
919 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200922 int (*f_rng)(void *, unsigned char *, size_t),
923 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100924 int mode, size_t *olen,
925 const unsigned char *input,
926 unsigned char *output,
927 size_t output_max_len)
928{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100929 int ret;
930 size_t ilen, pad_count = 0, i;
931 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
935 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100936
937 ilen = ctx->len;
938
939 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 ret = ( mode == MBEDTLS_RSA_PUBLIC )
943 ? mbedtls_rsa_public( ctx, input, buf )
944 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100945
946 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100947 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +0100948
949 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100950 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100951
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100952 /*
953 * Check and get padding len in "constant-time"
954 */
955 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100956
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100957 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000961
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100962 /* Get padding len, but always read till end of buffer
963 * (minus one, for the 00 byte) */
964 for( i = 0; i < ilen - 3; i++ )
965 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100966 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
967 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100968 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000969
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100970 p += pad_count;
971 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100972 }
973 else
974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100976
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100977 /* Get padding len, but always read till end of buffer
978 * (minus one, for the 00 byte) */
979 for( i = 0; i < ilen - 3; i++ )
980 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100981 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100982 pad_count += ( pad_done == 0 );
983 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100984
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100985 p += pad_count;
986 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +0000987 }
988
Janos Follathc69fa502016-02-12 13:30:09 +0000989 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +0000990
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100991 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100992 {
993 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
994 goto cleanup;
995 }
Paul Bakker8804f692013-02-28 18:06:26 +0100996
Paul Bakker66d5d072014-06-17 16:39:18 +0200997 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100998 {
999 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1000 goto cleanup;
1001 }
Paul Bakker060c5682009-01-12 21:48:39 +00001002
Paul Bakker27fdf462011-06-09 13:55:13 +00001003 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001004 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001005 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001006
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001007cleanup:
1008 mbedtls_zeroize( buf, sizeof( buf ) );
1009
1010 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001011}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001013
1014/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001015 * Do an RSA operation, then remove the message padding
1016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001018 int (*f_rng)(void *, unsigned char *, size_t),
1019 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001020 int mode, size_t *olen,
1021 const unsigned char *input,
1022 unsigned char *output,
1023 size_t output_max_len)
1024{
1025 switch( ctx->padding )
1026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#if defined(MBEDTLS_PKCS1_V15)
1028 case MBEDTLS_RSA_PKCS_V15:
1029 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001030 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001031#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033#if defined(MBEDTLS_PKCS1_V21)
1034 case MBEDTLS_RSA_PKCS_V21:
1035 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001036 olen, input, output,
1037 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001038#endif
1039
1040 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001042 }
1043}
1044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001046/*
1047 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001050 int (*f_rng)(void *, unsigned char *, size_t),
1051 void *p_rng,
1052 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001054 unsigned int hashlen,
1055 const unsigned char *hash,
1056 unsigned char *sig )
1057{
1058 size_t olen;
1059 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001061 unsigned int slen, hlen, offset = 0;
1062 int ret;
1063 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 const mbedtls_md_info_t *md_info;
1065 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1068 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001069
1070 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001072
1073 olen = ctx->len;
1074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001076 {
Simon Butcher02037452016-03-01 21:19:12 +00001077 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001079 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001083 }
1084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001086 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001090 slen = hlen;
1091
1092 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001094
1095 memset( sig, 0, olen );
1096
Simon Butcher02037452016-03-01 21:19:12 +00001097 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001098 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001100
Simon Butcher02037452016-03-01 21:19:12 +00001101 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001102 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001103 p += olen - hlen * 2 - 2;
1104 *p++ = 0x01;
1105 memcpy( p, salt, slen );
1106 p += slen;
1107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001109 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1110 {
1111 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001112 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001113 return( ret );
1114 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001115
Simon Butcher02037452016-03-01 21:19:12 +00001116 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 mbedtls_md_starts( &md_ctx );
1118 mbedtls_md_update( &md_ctx, p, 8 );
1119 mbedtls_md_update( &md_ctx, hash, hashlen );
1120 mbedtls_md_update( &md_ctx, salt, slen );
1121 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001122 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001123
Simon Butcher02037452016-03-01 21:19:12 +00001124 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001125 if( msb % 8 == 0 )
1126 offset = 1;
1127
Simon Butcher02037452016-03-01 21:19:12 +00001128 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001129 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001132
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001133 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001134 sig[0] &= 0xFF >> ( olen * 8 - msb );
1135
1136 p += hlen;
1137 *p++ = 0xBC;
1138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 return( ( mode == MBEDTLS_RSA_PUBLIC )
1140 ? mbedtls_rsa_public( ctx, sig, sig )
1141 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001146/*
1147 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1148 */
1149/*
1150 * Do an RSA operation to sign the message digest
1151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001153 int (*f_rng)(void *, unsigned char *, size_t),
1154 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001155 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001157 unsigned int hashlen,
1158 const unsigned char *hash,
1159 unsigned char *sig )
1160{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001161 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001162 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001163 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001164 unsigned char *sig_try = NULL, *verif = NULL;
1165 size_t i;
1166 unsigned char diff;
1167 volatile unsigned char diff_no_optimize;
1168 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1171 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001172
1173 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001174 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001179 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1183 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001184
Paul Bakkerc70b9822013-04-07 22:00:46 +02001185 nb_pad -= 10 + oid_size;
1186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001188 }
1189
Paul Bakkerc70b9822013-04-07 22:00:46 +02001190 nb_pad -= hashlen;
1191
Paul Bakkerb3869132013-02-28 17:21:01 +01001192 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001194
1195 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001197 memset( p, 0xFF, nb_pad );
1198 p += nb_pad;
1199 *p++ = 0;
1200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001202 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001203 memcpy( p, hash, hashlen );
1204 }
1205 else
1206 {
1207 /*
1208 * DigestInfo ::= SEQUENCE {
1209 * digestAlgorithm DigestAlgorithmIdentifier,
1210 * digest Digest }
1211 *
1212 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1213 *
1214 * Digest ::= OCTET STRING
1215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001217 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001219 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001221 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001222 memcpy( p, oid, oid_size );
1223 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001225 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001227 *p++ = hashlen;
1228 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001229 }
1230
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001231 if( mode == MBEDTLS_RSA_PUBLIC )
1232 return( mbedtls_rsa_public( ctx, sig, sig ) );
1233
1234 /*
1235 * In order to prevent Lenstra's attack, make the signature in a
1236 * temporary buffer and check it before returning it.
1237 */
1238 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001239 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001240 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1241
Simon Butcher1285ab52016-01-01 21:42:47 +00001242 verif = mbedtls_calloc( 1, ctx->len );
1243 if( verif == NULL )
1244 {
1245 mbedtls_free( sig_try );
1246 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1247 }
1248
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001249 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1250 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1251
1252 /* Compare in constant time just in case */
1253 for( diff = 0, i = 0; i < ctx->len; i++ )
1254 diff |= verif[i] ^ sig[i];
1255 diff_no_optimize = diff;
1256
1257 if( diff_no_optimize != 0 )
1258 {
1259 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1260 goto cleanup;
1261 }
1262
1263 memcpy( sig, sig_try, ctx->len );
1264
1265cleanup:
1266 mbedtls_free( sig_try );
1267 mbedtls_free( verif );
1268
1269 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001270}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001272
1273/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001274 * Do an RSA operation to sign the message digest
1275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001277 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001278 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001279 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001281 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001282 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001283 unsigned char *sig )
1284{
Paul Bakker5121ce52009-01-03 21:22:43 +00001285 switch( ctx->padding )
1286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#if defined(MBEDTLS_PKCS1_V15)
1288 case MBEDTLS_RSA_PKCS_V15:
1289 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001290 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001291#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#if defined(MBEDTLS_PKCS1_V21)
1294 case MBEDTLS_RSA_PKCS_V21:
1295 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001296 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001297#endif
1298
Paul Bakker5121ce52009-01-03 21:22:43 +00001299 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001301 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001302}
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001305/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001306 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001309 int (*f_rng)(void *, unsigned char *, size_t),
1310 void *p_rng,
1311 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001313 unsigned int hashlen,
1314 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001316 int expected_salt_len,
1317 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001318{
Paul Bakker23986e52011-04-24 08:57:21 +00001319 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001320 size_t siglen;
1321 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001323 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001324 unsigned int hlen;
1325 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 const mbedtls_md_info_t *md_info;
1327 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001328 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1331 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001332
Paul Bakker5121ce52009-01-03 21:22:43 +00001333 siglen = ctx->len;
1334
Paul Bakker27fdf462011-06-09 13:55:13 +00001335 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1339 ? mbedtls_rsa_public( ctx, sig, buf )
1340 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001341
1342 if( ret != 0 )
1343 return( ret );
1344
1345 p = buf;
1346
Paul Bakkerb3869132013-02-28 17:21:01 +01001347 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 {
Simon Butcher02037452016-03-01 21:19:12 +00001352 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001354 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001358 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001361 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001365 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001366
Paul Bakkerb3869132013-02-28 17:21:01 +01001367 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001368
Simon Butcher02037452016-03-01 21:19:12 +00001369 /*
1370 * Note: EMSA-PSS verification is over the length of N - 1 bits
1371 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001372 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001373
Simon Butcher02037452016-03-01 21:19:12 +00001374 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001375 if( msb % 8 == 0 )
1376 {
1377 p++;
1378 siglen -= 1;
1379 }
1380 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001384 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1385 {
1386 mbedtls_md_free( &md_ctx );
1387 return( ret );
1388 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001389
Paul Bakkerb3869132013-02-28 17:21:01 +01001390 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001391
Paul Bakkerb3869132013-02-28 17:21:01 +01001392 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001393
Paul Bakker4de44aa2013-12-31 11:43:01 +01001394 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001395 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001396
Paul Bakkerb3869132013-02-28 17:21:01 +01001397 if( p == buf + siglen ||
1398 *p++ != 0x01 )
1399 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 mbedtls_md_free( &md_ctx );
1401 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001402 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001403
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001404 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001405 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001408 slen != (size_t) expected_salt_len )
1409 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 mbedtls_md_free( &md_ctx );
1411 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001412 }
1413
Simon Butcher02037452016-03-01 21:19:12 +00001414 /*
1415 * Generate H = Hash( M' )
1416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 mbedtls_md_starts( &md_ctx );
1418 mbedtls_md_update( &md_ctx, zeros, 8 );
1419 mbedtls_md_update( &md_ctx, hash, hashlen );
1420 mbedtls_md_update( &md_ctx, p, slen );
1421 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001424
Paul Bakkerb3869132013-02-28 17:21:01 +01001425 if( memcmp( p + slen, result, hlen ) == 0 )
1426 return( 0 );
1427 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001429}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001430
1431/*
1432 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001435 int (*f_rng)(void *, unsigned char *, size_t),
1436 void *p_rng,
1437 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001439 unsigned int hashlen,
1440 const unsigned char *hash,
1441 const unsigned char *sig )
1442{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1444 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001445 : md_alg;
1446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001448 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001450 sig ) );
1451
1452}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001455#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001456/*
1457 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001460 int (*f_rng)(void *, unsigned char *, size_t),
1461 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001462 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001464 unsigned int hashlen,
1465 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001466 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001467{
1468 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001469 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001470 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 mbedtls_md_type_t msg_md_alg;
1472 const mbedtls_md_info_t *md_info;
1473 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001474 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1477 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001478
1479 siglen = ctx->len;
1480
1481 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1485 ? mbedtls_rsa_public( ctx, sig, buf )
1486 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001487
1488 if( ret != 0 )
1489 return( ret );
1490
1491 p = buf;
1492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1494 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001495
1496 while( *p != 0 )
1497 {
1498 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001500 p++;
1501 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02001502 p++; /* skip 00 byte */
1503
1504 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
1505 if( p - buf < 11 )
1506 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001507
1508 len = siglen - ( p - buf );
1509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001511 {
1512 if( memcmp( p, hash, hashlen ) == 0 )
1513 return( 0 );
1514 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001516 }
1517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001519 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1521 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001522
1523 end = p + len;
1524
Simon Butcher02037452016-03-01 21:19:12 +00001525 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02001526 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
1527 * Insist on 2-byte length tags, to protect against variants of
1528 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00001529 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001530 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1532 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1533 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001534 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001536
Gilles Peskinee7e76502017-05-04 12:48:39 +02001537 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1539 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1540 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001541 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001543
Gilles Peskinee7e76502017-05-04 12:48:39 +02001544 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1546 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001547 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001548 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001549
1550 oid.p = p;
1551 p += oid.len;
1552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1554 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001555
1556 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001558
1559 /*
1560 * assume the algorithm parameters must be NULL
1561 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02001562 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1564 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001565 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001567
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001568 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001569 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1570 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001571 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001573
1574 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001576
1577 p += hashlen;
1578
1579 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001581
1582 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
1586/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001587 * Do an RSA operation and check the message digest
1588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001590 int (*f_rng)(void *, unsigned char *, size_t),
1591 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001592 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001594 unsigned int hashlen,
1595 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001596 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001597{
1598 switch( ctx->padding )
1599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#if defined(MBEDTLS_PKCS1_V15)
1601 case MBEDTLS_RSA_PKCS_V15:
1602 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001603 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001604#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_PKCS1_V21)
1607 case MBEDTLS_RSA_PKCS_V21:
1608 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001609 hashlen, hash, sig );
1610#endif
1611
1612 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001614 }
1615}
1616
1617/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001618 * Copy the components of an RSA key
1619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001621{
1622 int ret;
1623
1624 dst->ver = src->ver;
1625 dst->len = src->len;
1626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1628 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1631 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1632 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1633 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1634 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1635 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1638 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1639 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1642 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001643
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001644 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001645 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001646
1647cleanup:
1648 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001650
1651 return( ret );
1652}
1653
1654/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001655 * Free the components of an RSA key
1656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001658{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1660 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1661 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1662 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1663 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#if defined(MBEDTLS_THREADING_C)
1666 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001667#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001668}
1669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001672#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001673
1674/*
1675 * Example RSA-1024 keypair, for test purposes
1676 */
1677#define KEY_LEN 128
1678
1679#define RSA_N "9292758453063D803DD603D5E777D788" \
1680 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1681 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1682 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1683 "93A89813FBF3C4F8066D2D800F7C38A8" \
1684 "1AE31942917403FF4946B0A83D3D3E05" \
1685 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1686 "5E94BB77B07507233A0BC7BAC8F90F79"
1687
1688#define RSA_E "10001"
1689
1690#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1691 "66CA472BC44D253102F8B4A9D3BFA750" \
1692 "91386C0077937FE33FA3252D28855837" \
1693 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1694 "DF79C5CE07EE72C7F123142198164234" \
1695 "CABB724CF78B8173B9F880FC86322407" \
1696 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1697 "071513A1E85B5DFA031F21ECAE91A34D"
1698
1699#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1700 "2C01CAD19EA484A87EA4377637E75500" \
1701 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1702 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1703
1704#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1705 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1706 "910E4168387E3C30AA1E00C339A79508" \
1707 "8452DD96A9A5EA5D9DCA68DA636032AF"
1708
1709#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1710 "3C94D22288ACD763FD8E5600ED4A702D" \
1711 "F84198A5F06C2E72236AE490C93F07F8" \
1712 "3CC559CD27BC2D1CA488811730BB5725"
1713
1714#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1715 "D8AAEA56749EA28623272E4F7D0592AF" \
1716 "7C1F1313CAC9471B5C523BFE592F517B" \
1717 "407A1BD76C164B93DA2D32A383E58357"
1718
1719#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1720 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1721 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1722 "A74206CEC169D74BF5A8C50D6F48EA08"
1723
1724#define PT_LEN 24
1725#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1726 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001729static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001730{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001731#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001732 size_t i;
1733
Paul Bakker545570e2010-07-18 09:00:25 +00001734 if( rng_state != NULL )
1735 rng_state = NULL;
1736
Paul Bakkera3d195c2011-11-27 21:07:34 +00001737 for( i = 0; i < len; ++i )
1738 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001739#else
1740 if( rng_state != NULL )
1741 rng_state = NULL;
1742
1743 arc4random_buf( output, len );
1744#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001745
Paul Bakkera3d195c2011-11-27 21:07:34 +00001746 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001747}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001748#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001749
Paul Bakker5121ce52009-01-03 21:22:43 +00001750/*
1751 * Checkup routine
1752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001754{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001755 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001757 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001759 unsigned char rsa_plaintext[PT_LEN];
1760 unsigned char rsa_decrypted[PT_LEN];
1761 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001762#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001763 unsigned char sha1sum[20];
1764#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001767
1768 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1770 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1771 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1772 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1773 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1774 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1775 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1776 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001777
1778 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001779 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1782 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 {
1784 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001786
1787 return( 1 );
1788 }
1789
1790 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
1793 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001796 rsa_plaintext, rsa_ciphertext ) != 0 )
1797 {
1798 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001800
1801 return( 1 );
1802 }
1803
1804 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001808 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001809 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001810 {
1811 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001813
1814 return( 1 );
1815 }
1816
1817 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1818 {
1819 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001821
1822 return( 1 );
1823 }
1824
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001825 if( verbose != 0 )
1826 mbedtls_printf( "passed\n" );
1827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001830 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001835 sha1sum, rsa_ciphertext ) != 0 )
1836 {
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
1840 return( 1 );
1841 }
1842
1843 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001846 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001847 sha1sum, rsa_ciphertext ) != 0 )
1848 {
1849 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001851
1852 return( 1 );
1853 }
1854
1855 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001856 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001859 if( verbose != 0 )
1860 mbedtls_printf( "\n" );
1861
Paul Bakker3d8fb632014-04-17 12:42:41 +02001862cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 mbedtls_rsa_free( &rsa );
1864#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001865 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001867 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001868}
1869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#endif /* MBEDTLS_RSA_C */