blob: 2f78ce366d3a9f743587b61a51b3e2b21adc201b [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 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100569static int 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;
Andres Amaya Garcia94682d12017-07-20 14:26:37 +0100577 int ret = 0;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000580 memset( counter, 0, 4 );
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000583
Simon Butcher02037452016-03-01 21:19:12 +0000584 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000585 p = dst;
586
587 while( dlen > 0 )
588 {
589 use_len = hlen;
590 if( dlen < hlen )
591 use_len = dlen;
592
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100593 if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 )
594 goto exit;
595 if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 )
596 goto exit;
597 if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 )
598 goto exit;
599 if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 )
600 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000601
602 for( i = 0; i < use_len; ++i )
603 *p++ ^= mask[i];
604
605 counter[3]++;
606
607 dlen -= use_len;
608 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200609
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100610exit:
Gilles Peskine18ac7162017-05-05 19:24:06 +0200611 mbedtls_zeroize( mask, sizeof( mask ) );
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100612
613 return( ret );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000614}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100618/*
619 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
620 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100622 int (*f_rng)(void *, unsigned char *, size_t),
623 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100624 int mode,
625 const unsigned char *label, size_t label_len,
626 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100627 const unsigned char *input,
628 unsigned char *output )
629{
630 size_t olen;
631 int ret;
632 unsigned char *p = output;
633 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 const mbedtls_md_info_t *md_info;
635 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
638 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200639
640 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100644 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100646
647 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100649
Simon Butcher02037452016-03-01 21:19:12 +0000650 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000651 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100653
654 memset( output, 0, olen );
655
656 *p++ = 0;
657
Simon Butcher02037452016-03-01 21:19:12 +0000658 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100659 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100661
662 p += hlen;
663
Simon Butcher02037452016-03-01 21:19:12 +0000664 /* Construct DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100665 if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 )
666 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100667 p += hlen;
668 p += olen - 2 * hlen - 2 - ilen;
669 *p++ = 1;
670 memcpy( p, input, ilen );
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700673 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100674 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +0100675
Simon Butcher02037452016-03-01 21:19:12 +0000676 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100677 if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
678 &md_ctx ) ) != 0 )
679 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +0100680
Simon Butcher02037452016-03-01 21:19:12 +0000681 /* maskedSeed: Apply seedMask to seed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100682 if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
683 &md_ctx ) ) != 0 )
684 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +0100685
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100686exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100688
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100689 if( ret != 0 )
690 return( ret );
691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 return( ( mode == MBEDTLS_RSA_PUBLIC )
693 ? mbedtls_rsa_public( ctx, output, output )
694 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100695}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100699/*
700 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
701 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100703 int (*f_rng)(void *, unsigned char *, size_t),
704 void *p_rng,
705 int mode, size_t ilen,
706 const unsigned char *input,
707 unsigned char *output )
708{
709 size_t nb_pad, olen;
710 int ret;
711 unsigned char *p = output;
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
714 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200715
Janos Follath1ed9f992016-03-18 11:45:44 +0000716 // We don't check p_rng because it won't be dereferenced here
717 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100719
720 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100721
Simon Butcher02037452016-03-01 21:19:12 +0000722 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000723 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100725
726 nb_pad = olen - 3 - ilen;
727
728 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100732
733 while( nb_pad-- > 0 )
734 {
735 int rng_dl = 100;
736
737 do {
738 ret = f_rng( p_rng, p, 1 );
739 } while( *p == 0 && --rng_dl && ret == 0 );
740
Simon Butcher02037452016-03-01 21:19:12 +0000741 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200742 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100744
745 p++;
746 }
747 }
748 else
749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100751
752 while( nb_pad-- > 0 )
753 *p++ = 0xFF;
754 }
755
756 *p++ = 0;
757 memcpy( p, input, ilen );
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 return( ( mode == MBEDTLS_RSA_PUBLIC )
760 ? mbedtls_rsa_public( ctx, output, output )
761 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100762}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100764
Paul Bakker5121ce52009-01-03 21:22:43 +0000765/*
766 * Add the message padding, then do an RSA operation
767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000769 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000770 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000771 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000772 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000773 unsigned char *output )
774{
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 switch( ctx->padding )
776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#if defined(MBEDTLS_PKCS1_V15)
778 case MBEDTLS_RSA_PKCS_V15:
779 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100780 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200781#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#if defined(MBEDTLS_PKCS1_V21)
784 case MBEDTLS_RSA_PKCS_V21:
785 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100786 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000787#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000788
789 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000791 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000792}
793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000795/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100796 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200799 int (*f_rng)(void *, unsigned char *, size_t),
800 void *p_rng,
801 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100802 const unsigned char *label, size_t label_len,
803 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100804 const unsigned char *input,
805 unsigned char *output,
806 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000807{
Paul Bakker23986e52011-04-24 08:57:21 +0000808 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100809 size_t ilen, i, pad_len;
810 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
812 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000813 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 const mbedtls_md_info_t *md_info;
815 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100816
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100817 /*
818 * Parameters sanity checks
819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
821 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000822
823 ilen = ctx->len;
824
Paul Bakker27fdf462011-06-09 13:55:13 +0000825 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100829 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100831
Janos Follathc17cda12016-02-11 11:08:18 +0000832 hlen = mbedtls_md_get_size( md_info );
833
834 // checking for integer underflow
835 if( 2 * hlen + 2 > ilen )
836 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
837
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100838 /*
839 * RSA operation
840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 ret = ( mode == MBEDTLS_RSA_PUBLIC )
842 ? mbedtls_rsa_public( ctx, input, buf )
843 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000844
845 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100846 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000847
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100848 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100849 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700852 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
853 {
854 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100855 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700856 }
857
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100858 /* seed: Apply seedMask to maskedSeed */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100859 if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
860 &md_ctx ) ) != 0 ||
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100861 /* DB: Apply dbMask to maskedDB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100862 ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
863 &md_ctx ) ) != 0 )
864 {
865 mbedtls_md_free( &md_ctx );
866 goto cleanup;
867 }
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100870
Andres Amaya Garcia698089e2017-06-28 11:46:46 +0100871 /* Generate lHash */
872 if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 )
873 goto cleanup;
874
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100875 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100876 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100877 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000878 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100879 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000880
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100881 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100882
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100883 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100884
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100885 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100886 for( i = 0; i < hlen; i++ )
887 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100888
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100889 /* Get zero-padding len, but always read till end of buffer
890 * (minus one, for the 01 byte) */
891 pad_len = 0;
892 pad_done = 0;
893 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
894 {
895 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100896 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100897 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100898
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100899 p += pad_len;
900 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100901
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100902 /*
903 * The only information "leaked" is whether the padding was correct or not
904 * (eg, no data is copied if it was not correct). This meets the
905 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
906 * the different error conditions.
907 */
908 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100909 {
910 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
911 goto cleanup;
912 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100913
Paul Bakker66d5d072014-06-17 16:39:18 +0200914 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100915 {
916 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
917 goto cleanup;
918 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100919
920 *olen = ilen - (p - buf);
921 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100922 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100923
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100924cleanup:
925 mbedtls_zeroize( buf, sizeof( buf ) );
926 mbedtls_zeroize( lhash, sizeof( lhash ) );
927
928 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100929}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100933/*
934 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
935 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200937 int (*f_rng)(void *, unsigned char *, size_t),
938 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100939 int mode, size_t *olen,
940 const unsigned char *input,
941 unsigned char *output,
942 size_t output_max_len)
943{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100944 int ret;
945 size_t ilen, pad_count = 0, i;
946 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
950 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100951
952 ilen = ctx->len;
953
954 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 ret = ( mode == MBEDTLS_RSA_PUBLIC )
958 ? mbedtls_rsa_public( ctx, input, buf )
959 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100960
961 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100962 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +0100963
964 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100965 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100966
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100967 /*
968 * Check and get padding len in "constant-time"
969 */
970 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100971
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100972 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000976
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 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100981 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
982 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100983 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000984
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100985 p += pad_count;
986 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100987 }
988 else
989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100991
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100992 /* Get padding len, but always read till end of buffer
993 * (minus one, for the 00 byte) */
994 for( i = 0; i < ilen - 3; i++ )
995 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100996 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100997 pad_count += ( pad_done == 0 );
998 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100999
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001000 p += pad_count;
1001 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001002 }
1003
Janos Follathc69fa502016-02-12 13:30:09 +00001004 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001005
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001006 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001007 {
1008 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1009 goto cleanup;
1010 }
Paul Bakker8804f692013-02-28 18:06:26 +01001011
Paul Bakker66d5d072014-06-17 16:39:18 +02001012 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001013 {
1014 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1015 goto cleanup;
1016 }
Paul Bakker060c5682009-01-12 21:48:39 +00001017
Paul Bakker27fdf462011-06-09 13:55:13 +00001018 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001019 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001020 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001021
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001022cleanup:
1023 mbedtls_zeroize( buf, sizeof( buf ) );
1024
1025 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001026}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001028
1029/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001030 * Do an RSA operation, then remove the message padding
1031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001033 int (*f_rng)(void *, unsigned char *, size_t),
1034 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001035 int mode, size_t *olen,
1036 const unsigned char *input,
1037 unsigned char *output,
1038 size_t output_max_len)
1039{
1040 switch( ctx->padding )
1041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_PKCS1_V15)
1043 case MBEDTLS_RSA_PKCS_V15:
1044 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001045 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001046#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_PKCS1_V21)
1049 case MBEDTLS_RSA_PKCS_V21:
1050 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001051 olen, input, output,
1052 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001053#endif
1054
1055 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001057 }
1058}
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001061/*
1062 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1063 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001065 int (*f_rng)(void *, unsigned char *, size_t),
1066 void *p_rng,
1067 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001069 unsigned int hashlen,
1070 const unsigned char *hash,
1071 unsigned char *sig )
1072{
1073 size_t olen;
1074 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001076 unsigned int slen, hlen, offset = 0;
1077 int ret;
1078 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 const mbedtls_md_info_t *md_info;
1080 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1083 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001084
1085 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001087
1088 olen = ctx->len;
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001091 {
Simon Butcher02037452016-03-01 21:19:12 +00001092 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001094 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001098 }
1099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001101 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001105 slen = hlen;
1106
1107 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001109
1110 memset( sig, 0, olen );
1111
Simon Butcher02037452016-03-01 21:19:12 +00001112 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001113 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001115
Simon Butcher02037452016-03-01 21:19:12 +00001116 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001117 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001118 p += olen - hlen * 2 - 2;
1119 *p++ = 0x01;
1120 memcpy( p, salt, slen );
1121 p += slen;
1122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001124 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001125 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001126
Simon Butcher02037452016-03-01 21:19:12 +00001127 /* Generate H = Hash( M' ) */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001128 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1129 goto exit;
1130 if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 )
1131 goto exit;
1132 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1133 goto exit;
1134 if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 )
1135 goto exit;
1136 if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 )
1137 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001138
Simon Butcher02037452016-03-01 21:19:12 +00001139 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001140 if( msb % 8 == 0 )
1141 offset = 1;
1142
Simon Butcher02037452016-03-01 21:19:12 +00001143 /* maskedDB: Apply dbMask to DB */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001144 if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen,
1145 &md_ctx ) ) != 0 )
1146 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001147
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001148 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001149 sig[0] &= 0xFF >> ( olen * 8 - msb );
1150
1151 p += hlen;
1152 *p++ = 0xBC;
1153
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001154 mbedtls_zeroize( salt, sizeof( salt ) );
1155
1156exit:
1157 mbedtls_md_free( &md_ctx );
1158
1159 if( ret != 0 )
1160 return( ret );
1161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 return( ( mode == MBEDTLS_RSA_PUBLIC )
1163 ? mbedtls_rsa_public( ctx, sig, sig )
1164 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001169/*
1170 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1171 */
1172/*
1173 * Do an RSA operation to sign the message digest
1174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001176 int (*f_rng)(void *, unsigned char *, size_t),
1177 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001178 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001180 unsigned int hashlen,
1181 const unsigned char *hash,
1182 unsigned char *sig )
1183{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001184 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001185 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001186 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001187 unsigned char *sig_try = NULL, *verif = NULL;
1188 size_t i;
1189 unsigned char diff;
1190 volatile unsigned char diff_no_optimize;
1191 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1194 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001195
1196 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001197 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001202 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1206 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001207
Paul Bakkerc70b9822013-04-07 22:00:46 +02001208 nb_pad -= 10 + oid_size;
1209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001211 }
1212
Paul Bakkerc70b9822013-04-07 22:00:46 +02001213 nb_pad -= hashlen;
1214
Paul Bakkerb3869132013-02-28 17:21:01 +01001215 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001217
1218 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001220 memset( p, 0xFF, nb_pad );
1221 p += nb_pad;
1222 *p++ = 0;
1223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001225 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001226 memcpy( p, hash, hashlen );
1227 }
1228 else
1229 {
1230 /*
1231 * DigestInfo ::= SEQUENCE {
1232 * digestAlgorithm DigestAlgorithmIdentifier,
1233 * digest Digest }
1234 *
1235 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1236 *
1237 * Digest ::= OCTET STRING
1238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001240 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001242 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001244 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001245 memcpy( p, oid, oid_size );
1246 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001248 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001250 *p++ = hashlen;
1251 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001252 }
1253
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001254 if( mode == MBEDTLS_RSA_PUBLIC )
1255 return( mbedtls_rsa_public( ctx, sig, sig ) );
1256
1257 /*
1258 * In order to prevent Lenstra's attack, make the signature in a
1259 * temporary buffer and check it before returning it.
1260 */
1261 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001262 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001263 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1264
Simon Butcher1285ab52016-01-01 21:42:47 +00001265 verif = mbedtls_calloc( 1, ctx->len );
1266 if( verif == NULL )
1267 {
1268 mbedtls_free( sig_try );
1269 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1270 }
1271
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001272 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1273 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1274
1275 /* Compare in constant time just in case */
1276 for( diff = 0, i = 0; i < ctx->len; i++ )
1277 diff |= verif[i] ^ sig[i];
1278 diff_no_optimize = diff;
1279
1280 if( diff_no_optimize != 0 )
1281 {
1282 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1283 goto cleanup;
1284 }
1285
1286 memcpy( sig, sig_try, ctx->len );
1287
1288cleanup:
1289 mbedtls_free( sig_try );
1290 mbedtls_free( verif );
1291
1292 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001293}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001295
1296/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001297 * Do an RSA operation to sign the message digest
1298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001300 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001301 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001302 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001304 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001305 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001306 unsigned char *sig )
1307{
Paul Bakker5121ce52009-01-03 21:22:43 +00001308 switch( ctx->padding )
1309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_PKCS1_V15)
1311 case MBEDTLS_RSA_PKCS_V15:
1312 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001313 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001314#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316#if defined(MBEDTLS_PKCS1_V21)
1317 case MBEDTLS_RSA_PKCS_V21:
1318 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001319 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001320#endif
1321
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001324 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001325}
1326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001328/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001329 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001332 int (*f_rng)(void *, unsigned char *, size_t),
1333 void *p_rng,
1334 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001336 unsigned int hashlen,
1337 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001339 int expected_salt_len,
1340 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001341{
Paul Bakker23986e52011-04-24 08:57:21 +00001342 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001343 size_t siglen;
1344 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001346 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001347 unsigned int hlen;
1348 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 const mbedtls_md_info_t *md_info;
1350 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001351 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1354 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001355
Paul Bakker5121ce52009-01-03 21:22:43 +00001356 siglen = ctx->len;
1357
Paul Bakker27fdf462011-06-09 13:55:13 +00001358 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1362 ? mbedtls_rsa_public( ctx, sig, buf )
1363 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
1365 if( ret != 0 )
1366 return( ret );
1367
1368 p = buf;
1369
Paul Bakkerb3869132013-02-28 17:21:01 +01001370 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 {
Simon Butcher02037452016-03-01 21:19:12 +00001375 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001377 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001381 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001384 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001388 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001389
Paul Bakkerb3869132013-02-28 17:21:01 +01001390 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001391
Simon Butcher02037452016-03-01 21:19:12 +00001392 /*
1393 * Note: EMSA-PSS verification is over the length of N - 1 bits
1394 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001395 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001396
Simon Butcher02037452016-03-01 21:19:12 +00001397 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001398 if( msb % 8 == 0 )
1399 {
1400 p++;
1401 siglen -= 1;
1402 }
1403 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001407 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001408 goto exit;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001409
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001410 if( ( ret = mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen,
1411 &md_ctx ) ) != 0 )
1412 goto exit;
Paul Bakker02303e82013-01-03 11:08:31 +01001413
Paul Bakkerb3869132013-02-28 17:21:01 +01001414 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001415
Paul Bakker4de44aa2013-12-31 11:43:01 +01001416 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001417 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001418
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001419 if( p == buf + siglen || *p++ != 0x01 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001420 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001421 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1422 goto exit;
Paul Bakkerb3869132013-02-28 17:21:01 +01001423 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001424
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001425 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001426 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001429 slen != (size_t) expected_salt_len )
1430 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001431 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1432 goto exit;
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001433 }
1434
Simon Butcher02037452016-03-01 21:19:12 +00001435 /*
1436 * Generate H = Hash( M' )
1437 */
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001438 if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 )
1439 goto exit;
1440 if( ( ret = mbedtls_md_update( &md_ctx, zeros, 8 ) ) != 0 )
1441 goto exit;
1442 if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 )
1443 goto exit;
1444 if( ( ret = mbedtls_md_update( &md_ctx, p, slen ) ) != 0 )
1445 goto exit;
1446 if( ( ret = mbedtls_md_finish( &md_ctx, result ) ) != 0 )
1447 goto exit;
Paul Bakker53019ae2011-03-25 13:58:48 +00001448
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01001449 if( memcmp( p + slen, result, hlen ) != 0 )
1450 {
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001451 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
Andres Amaya Garciac5c7d762017-07-20 14:42:16 +01001452 goto exit;
1453 }
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001454
1455exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001457
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001458 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001459}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001460
1461/*
1462 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001465 int (*f_rng)(void *, unsigned char *, size_t),
1466 void *p_rng,
1467 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001469 unsigned int hashlen,
1470 const unsigned char *hash,
1471 const unsigned char *sig )
1472{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1474 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001475 : md_alg;
1476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001478 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001480 sig ) );
1481
1482}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001486/*
1487 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001490 int (*f_rng)(void *, unsigned char *, size_t),
1491 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001492 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001494 unsigned int hashlen,
1495 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001496 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001497{
1498 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001499 size_t len, siglen, asn1_len;
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001500 unsigned char *p, *p0, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 mbedtls_md_type_t msg_md_alg;
1502 const mbedtls_md_info_t *md_info;
1503 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001504 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1507 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001508
1509 siglen = ctx->len;
1510
1511 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1515 ? mbedtls_rsa_public( ctx, sig, buf )
1516 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001517
1518 if( ret != 0 )
1519 return( ret );
1520
1521 p = buf;
1522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1524 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001525
1526 while( *p != 0 )
1527 {
1528 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001530 p++;
1531 }
Manuel Pégourié-Gonnardc1380de2017-05-11 12:49:51 +02001532 p++; /* skip 00 byte */
1533
1534 /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */
1535 if( p - buf < 11 )
1536 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001537
1538 len = siglen - ( p - buf );
1539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001541 {
1542 if( memcmp( p, hash, hashlen ) == 0 )
1543 return( 0 );
1544 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001546 }
1547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001549 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1551 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001552
1553 end = p + len;
1554
Simon Butcher02037452016-03-01 21:19:12 +00001555 /*
Gilles Peskinee7e76502017-05-04 12:48:39 +02001556 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure.
1557 * Insist on 2-byte length tags, to protect against variants of
1558 * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification.
Simon Butcher02037452016-03-01 21:19:12 +00001559 */
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001560 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1562 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1563 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001564 if( p != p0 + 2 || asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001566
Gilles Peskinee7e76502017-05-04 12:48:39 +02001567 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1569 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1570 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001571 if( p != p0 + 2 || asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001573
Gilles Peskinee7e76502017-05-04 12:48:39 +02001574 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1576 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001577 if( p != p0 + 2 )
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001578 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001579
1580 oid.p = p;
1581 p += oid.len;
1582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1584 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001585
1586 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001588
1589 /*
1590 * assume the algorithm parameters must be NULL
1591 */
Gilles Peskinee7e76502017-05-04 12:48:39 +02001592 p0 = p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1594 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskinee7e76502017-05-04 12:48:39 +02001595 if( p != p0 + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001597
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001598 p0 = p;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001599 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1600 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Gilles Peskine0e17eb02017-05-03 18:32:21 +02001601 if( p != p0 + 2 || asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001603
1604 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001606
1607 p += hashlen;
1608
1609 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001611
1612 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001615
1616/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001617 * Do an RSA operation and check the message digest
1618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001620 int (*f_rng)(void *, unsigned char *, size_t),
1621 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001622 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001624 unsigned int hashlen,
1625 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001626 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001627{
1628 switch( ctx->padding )
1629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#if defined(MBEDTLS_PKCS1_V15)
1631 case MBEDTLS_RSA_PKCS_V15:
1632 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001633 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001634#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#if defined(MBEDTLS_PKCS1_V21)
1637 case MBEDTLS_RSA_PKCS_V21:
1638 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001639 hashlen, hash, sig );
1640#endif
1641
1642 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001644 }
1645}
1646
1647/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001648 * Copy the components of an RSA key
1649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001651{
1652 int ret;
1653
1654 dst->ver = src->ver;
1655 dst->len = src->len;
1656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1658 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1661 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1662 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1663 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1664 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1665 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1668 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1669 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1672 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001673
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001674 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001675 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001676
1677cleanup:
1678 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001680
1681 return( ret );
1682}
1683
1684/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001685 * Free the components of an RSA key
1686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001688{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1690 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1691 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1692 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1693 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695#if defined(MBEDTLS_THREADING_C)
1696 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001697#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001698}
1699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001701
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001702#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
1704/*
1705 * Example RSA-1024 keypair, for test purposes
1706 */
1707#define KEY_LEN 128
1708
1709#define RSA_N "9292758453063D803DD603D5E777D788" \
1710 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1711 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1712 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1713 "93A89813FBF3C4F8066D2D800F7C38A8" \
1714 "1AE31942917403FF4946B0A83D3D3E05" \
1715 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1716 "5E94BB77B07507233A0BC7BAC8F90F79"
1717
1718#define RSA_E "10001"
1719
1720#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1721 "66CA472BC44D253102F8B4A9D3BFA750" \
1722 "91386C0077937FE33FA3252D28855837" \
1723 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1724 "DF79C5CE07EE72C7F123142198164234" \
1725 "CABB724CF78B8173B9F880FC86322407" \
1726 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1727 "071513A1E85B5DFA031F21ECAE91A34D"
1728
1729#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1730 "2C01CAD19EA484A87EA4377637E75500" \
1731 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1732 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1733
1734#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1735 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1736 "910E4168387E3C30AA1E00C339A79508" \
1737 "8452DD96A9A5EA5D9DCA68DA636032AF"
1738
1739#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1740 "3C94D22288ACD763FD8E5600ED4A702D" \
1741 "F84198A5F06C2E72236AE490C93F07F8" \
1742 "3CC559CD27BC2D1CA488811730BB5725"
1743
1744#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1745 "D8AAEA56749EA28623272E4F7D0592AF" \
1746 "7C1F1313CAC9471B5C523BFE592F517B" \
1747 "407A1BD76C164B93DA2D32A383E58357"
1748
1749#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1750 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1751 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1752 "A74206CEC169D74BF5A8C50D6F48EA08"
1753
1754#define PT_LEN 24
1755#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1756 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001759static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001760{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001761#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001762 size_t i;
1763
Paul Bakker545570e2010-07-18 09:00:25 +00001764 if( rng_state != NULL )
1765 rng_state = NULL;
1766
Paul Bakkera3d195c2011-11-27 21:07:34 +00001767 for( i = 0; i < len; ++i )
1768 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001769#else
1770 if( rng_state != NULL )
1771 rng_state = NULL;
1772
1773 arc4random_buf( output, len );
1774#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001775
Paul Bakkera3d195c2011-11-27 21:07:34 +00001776 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001777}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001779
Paul Bakker5121ce52009-01-03 21:22:43 +00001780/*
1781 * Checkup routine
1782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001784{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001785 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001787 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001789 unsigned char rsa_plaintext[PT_LEN];
1790 unsigned char rsa_decrypted[PT_LEN];
1791 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001793 unsigned char sha1sum[20];
1794#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
1798 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1800 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1801 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1802 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1803 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1804 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1805 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1806 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001807
1808 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001811 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1812 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001813 {
1814 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001816
1817 return( 1 );
1818 }
1819
1820 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001822
1823 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001826 rsa_plaintext, rsa_ciphertext ) != 0 )
1827 {
1828 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001830
1831 return( 1 );
1832 }
1833
1834 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001838 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001839 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001840 {
1841 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
1844 return( 1 );
1845 }
1846
1847 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 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
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001855 if( verbose != 0 )
1856 mbedtls_printf( "passed\n" );
1857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001859 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001860 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001861
Andres Amaya Garcia698089e2017-06-28 11:46:46 +01001862 if( mbedtls_sha1_ext( rsa_plaintext, PT_LEN, sha1sum ) != 0 )
1863 {
1864 if( verbose != 0 )
1865 mbedtls_printf( "failed\n" );
1866
1867 return( 1 );
1868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001871 sha1sum, rsa_ciphertext ) != 0 )
1872 {
1873 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
1876 return( 1 );
1877 }
1878
1879 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 sha1sum, rsa_ciphertext ) != 0 )
1884 {
1885 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
1888 return( 1 );
1889 }
1890
1891 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001892 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001894
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001895 if( verbose != 0 )
1896 mbedtls_printf( "\n" );
1897
Paul Bakker3d8fb632014-04-17 12:42:41 +02001898cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 mbedtls_rsa_free( &rsa );
1900#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001901 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001903 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001904}
1905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#endif /* MBEDTLS_RSA_C */