blob: 9b7d346c245a83c85f479dd6f54fff177ed38728 [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 Bakker37940d92009-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 Bakker37940d92009-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 Bakker37940d92009-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 Bakker37940d92009-07-10 22:38:58 +0000216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
218 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
219 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
220 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
223 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
225 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
226 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
227 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
230 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
231 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
234 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
235 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000236 /*
237 * Check for a valid PKCS1v2 private key
238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
240 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
241 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
242 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
243 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
244 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
245 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 }
Paul Bakker48377d92013-08-30 12:06:24 +0200249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
252 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
253 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
254 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000257 return( ret );
258
Paul Bakker6c591fa2011-05-05 11:49:20 +0000259 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000261
262 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263}
264
265/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100266 * Check if contexts holding a public and private key match
267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
271 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100274 }
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
277 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100280 }
281
282 return( 0 );
283}
284
285/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 * Do an RSA public key operation
287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000289 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 unsigned char *output )
291{
Paul Bakker23986e52011-04-24 08:57:21 +0000292 int ret;
293 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200298#if defined(MBEDTLS_THREADING_C)
299 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
300 return( ret );
301#endif
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200307 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
308 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 }
310
311 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
313 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
315cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200317 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
318 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100319#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326 return( 0 );
327}
328
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200329/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200330 * Generate or update blinding values, see section 10 of:
331 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200332 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200333 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200334 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200335static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200336 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
337{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200338 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200339
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200340 if( ctx->Vf.p != NULL )
341 {
342 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
344 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
345 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
346 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200347
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200348 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200349 }
350
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200351 /* Unblinding value: Vf = random number, invertible mod N */
352 do {
353 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
357 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
358 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200359
360 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
362 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200363
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200364
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200365cleanup:
366 return( ret );
367}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200368
Paul Bakker5121ce52009-01-03 21:22:43 +0000369/*
Janos Follathe81102e2017-03-22 13:38:28 +0000370 * Exponent blinding supposed to prevent side-channel attacks using multiple
371 * traces of measurements to recover the RSA key. The more collisions are there,
372 * the more bits of the key can be recovered. See [3].
373 *
374 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
375 * observations on avarage.
376 *
377 * For example with 28 byte blinding to achieve 2 collisions the adversary has
378 * to make 2^112 observations on avarage.
379 *
380 * (With the currently (as of 2017 April) known best algorithms breaking 2048
381 * bit RSA requires approximately as much time as trying out 2^112 random keys.
382 * Thus in this sense with 28 byte blinding the security is not reduced by
383 * side-channel attacks like the one in [3])
384 *
385 * This countermeasure does not help if the key recovery is possible with a
386 * single trace.
387 */
388#define RSA_EXPONENT_BLINDING 28
389
390/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 * Do an RSA private key operation
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200394 int (*f_rng)(void *, unsigned char *, size_t),
395 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000396 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 unsigned char *output )
398{
Paul Bakker23986e52011-04-24 08:57:21 +0000399 int ret;
400 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100401
402 /* Temporary holding the result */
403 mbedtls_mpi T;
404
405 /* Temporaries holding P-1, Q-1 and the
406 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000407 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100408
409#if !defined(MBEDTLS_RSA_NO_CRT)
410 /* Temporaries holding the results mod p resp. mod q. */
411 mbedtls_mpi TP, TQ;
412
413 /* Temporaries holding the blinded exponents for
414 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000415 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100416
417 /* Pointers to actual exponents to be used - either the unblinded
418 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000419 mbedtls_mpi *DP = &ctx->DP;
420 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100421#else
422 /* Temporary holding the blinded exponent (if used). */
423 mbedtls_mpi D_blind;
424
425 /* Pointer to actual exponent to be used - either the unblinded
426 * or the blinded one, depending on the presence of a PRNG. */
427 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100428#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100429
430#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100431 /* Temporaries holding the initial input and the double
432 * checked result; should be the same in the end. */
433 mbedtls_mpi I, C;
Hanno Becker06811ce2017-05-03 15:10:34 +0100434#endif
435
436#if defined(MBEDTLS_RSA_FORCE_BLINDING)
437 if( f_rng == NULL )
438 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Janos Follathe81102e2017-03-22 13:38:28 +0000439#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
Hanno Becker43f94722017-08-25 11:50:00 +0100441 /* Sanity-check that all relevant fields are at least set,
442 * but don't perform a full keycheck. */
443 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
444 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
445 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
446 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
447 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
448 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100449 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker43f94722017-08-25 11:50:00 +0100450 }
451#if !defined(MBEDTLS_RSA_NO_CRT)
452 if( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
453 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
454 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
455 {
456 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
457 }
458#endif /* MBEDTLS_RSA_NO_CRT */
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100459
Hanno Becker06811ce2017-05-03 15:10:34 +0100460#if defined(MBEDTLS_THREADING_C)
461 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
462 return( ret );
463#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000464
Hanno Becker06811ce2017-05-03 15:10:34 +0100465 /* MPI Initialization */
466
467 mbedtls_mpi_init( &T );
468
469 mbedtls_mpi_init( &P1 );
470 mbedtls_mpi_init( &Q1 );
471 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000472
473 if( f_rng != NULL )
474 {
Janos Follathe81102e2017-03-22 13:38:28 +0000475#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000476 mbedtls_mpi_init( &D_blind );
477#else
478 mbedtls_mpi_init( &DP_blind );
479 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000480#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000481 }
Janos Follathe81102e2017-03-22 13:38:28 +0000482
Hanno Becker06811ce2017-05-03 15:10:34 +0100483#if !defined(MBEDTLS_RSA_NO_CRT)
484 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200485#endif
486
Hanno Becker06811ce2017-05-03 15:10:34 +0100487#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100488 mbedtls_mpi_init( &I );
489 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100490#endif
491
492 /* End of MPI initialization */
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
495 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200497 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
498 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000499 }
500
Hanno Becker06811ce2017-05-03 15:10:34 +0100501#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100502 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100503#endif
504
Paul Bakkerf451bac2013-08-30 15:37:02 +0200505 if( f_rng != NULL )
506 {
507 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200508 * Blinding
509 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200510 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200511 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
512 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000514
Janos Follathe81102e2017-03-22 13:38:28 +0000515 /*
516 * Exponent blinding
517 */
518 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
519 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
520
Janos Follathf9203b42017-03-22 15:13:15 +0000521#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000522 /*
523 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
524 */
525 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
526 f_rng, p_rng ) );
527 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
528 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
529 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
530
531 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000532#else
533 /*
534 * DP_blind = ( P - 1 ) * R + DP
535 */
536 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
537 f_rng, p_rng ) );
538 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
539 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
540 &ctx->DP ) );
541
542 DP = &DP_blind;
543
544 /*
545 * DQ_blind = ( Q - 1 ) * R + DQ
546 */
547 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
548 f_rng, p_rng ) );
549 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
550 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
551 &ctx->DQ ) );
552
553 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000554#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200555 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000558 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100559#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200560 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000561 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100563 * TP = input ^ dP mod P
564 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100566
567 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
568 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100571 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100573 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
574 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
575 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100578 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100580 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
581 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200583
Paul Bakkerf451bac2013-08-30 15:37:02 +0200584 if( f_rng != NULL )
585 {
586 /*
587 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200588 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200589 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200590 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200592 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100594 /* If requested by the config, verify the result to prevent glitching attacks. */
Hanno Becker06811ce2017-05-03 15:10:34 +0100595#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100596 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E, &ctx->N, &ctx->RN ) );
597 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +0100598 {
Hanno Becker06811ce2017-05-03 15:10:34 +0100599 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
600 goto cleanup;
601 }
602#endif /* MBEDTLS_RSA_REQUIRE_VERIFICATION */
603
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
607cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200609 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
610 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200611#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200612
Hanno Becker06811ce2017-05-03 15:10:34 +0100613 mbedtls_mpi_free( &P1 );
614 mbedtls_mpi_free( &Q1 );
615 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000616
617 if( f_rng != NULL )
618 {
Janos Follathe81102e2017-03-22 13:38:28 +0000619#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000620 mbedtls_mpi_free( &D_blind );
621#else
622 mbedtls_mpi_free( &DP_blind );
623 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000624#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000625 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Hanno Becker06811ce2017-05-03 15:10:34 +0100627 mbedtls_mpi_free( &T );
628
629#if !defined(MBEDTLS_RSA_NO_CRT)
630 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
631#endif
632
633#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100634 mbedtls_mpi_free( &C );
635 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +0100636#endif
637
Paul Bakker5121ce52009-01-03 21:22:43 +0000638 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000640
641 return( 0 );
642}
643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000645/**
646 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
647 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000648 * \param dst buffer to mask
649 * \param dlen length of destination buffer
650 * \param src source of the mask generation
651 * \param slen length of the source buffer
652 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000653 */
Paul Bakker48377d92013-08-30 12:06:24 +0200654static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000656{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000658 unsigned char counter[4];
659 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000660 unsigned int hlen;
661 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000664 memset( counter, 0, 4 );
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000667
Simon Butcher02037452016-03-01 21:19:12 +0000668 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000669 p = dst;
670
671 while( dlen > 0 )
672 {
673 use_len = hlen;
674 if( dlen < hlen )
675 use_len = dlen;
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 mbedtls_md_starts( md_ctx );
678 mbedtls_md_update( md_ctx, src, slen );
679 mbedtls_md_update( md_ctx, counter, 4 );
680 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000681
682 for( i = 0; i < use_len; ++i )
683 *p++ ^= mask[i];
684
685 counter[3]++;
686
687 dlen -= use_len;
688 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200689
690 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000691}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100695/*
696 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100699 int (*f_rng)(void *, unsigned char *, size_t),
700 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100701 int mode,
702 const unsigned char *label, size_t label_len,
703 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100704 const unsigned char *input,
705 unsigned char *output )
706{
707 size_t olen;
708 int ret;
709 unsigned char *p = output;
710 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 const mbedtls_md_info_t *md_info;
712 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
715 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200716
717 if( f_rng == 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100721 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100723
724 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100726
Simon Butcher02037452016-03-01 21:19:12 +0000727 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000728 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100730
731 memset( output, 0, olen );
732
733 *p++ = 0;
734
Simon Butcher02037452016-03-01 21:19:12 +0000735 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100736 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100738
739 p += hlen;
740
Simon Butcher02037452016-03-01 21:19:12 +0000741 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100743 p += hlen;
744 p += olen - 2 * hlen - 2 - ilen;
745 *p++ = 1;
746 memcpy( p, input, ilen );
747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700749 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
750 {
751 mbedtls_md_free( &md_ctx );
752 return( ret );
753 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100754
Simon Butcher02037452016-03-01 21:19:12 +0000755 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100756 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
757 &md_ctx );
758
Simon Butcher02037452016-03-01 21:19:12 +0000759 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100760 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
761 &md_ctx );
762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 return( ( mode == MBEDTLS_RSA_PUBLIC )
766 ? mbedtls_rsa_public( ctx, output, output )
767 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100768}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100772/*
773 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100776 int (*f_rng)(void *, unsigned char *, size_t),
777 void *p_rng,
778 int mode, size_t ilen,
779 const unsigned char *input,
780 unsigned char *output )
781{
782 size_t nb_pad, olen;
783 int ret;
784 unsigned char *p = output;
785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
787 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200788
Janos Follath1ed9f992016-03-18 11:45:44 +0000789 // We don't check p_rng because it won't be dereferenced here
790 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100792
793 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100794
Simon Butcher02037452016-03-01 21:19:12 +0000795 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000796 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100798
799 nb_pad = olen - 3 - ilen;
800
801 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100803 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100805
806 while( nb_pad-- > 0 )
807 {
808 int rng_dl = 100;
809
810 do {
811 ret = f_rng( p_rng, p, 1 );
812 } while( *p == 0 && --rng_dl && ret == 0 );
813
Simon Butcher02037452016-03-01 21:19:12 +0000814 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200815 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100817
818 p++;
819 }
820 }
821 else
822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100824
825 while( nb_pad-- > 0 )
826 *p++ = 0xFF;
827 }
828
829 *p++ = 0;
830 memcpy( p, input, ilen );
831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 return( ( mode == MBEDTLS_RSA_PUBLIC )
833 ? mbedtls_rsa_public( ctx, output, output )
834 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100837
Paul Bakker5121ce52009-01-03 21:22:43 +0000838/*
839 * Add the message padding, then do an RSA operation
840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000842 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000843 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000844 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000845 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000846 unsigned char *output )
847{
Paul Bakker5121ce52009-01-03 21:22:43 +0000848 switch( ctx->padding )
849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#if defined(MBEDTLS_PKCS1_V15)
851 case MBEDTLS_RSA_PKCS_V15:
852 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100853 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200854#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#if defined(MBEDTLS_PKCS1_V21)
857 case MBEDTLS_RSA_PKCS_V21:
858 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100859 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000860#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000861
862 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000864 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000865}
866
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000868/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100869 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200872 int (*f_rng)(void *, unsigned char *, size_t),
873 void *p_rng,
874 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100875 const unsigned char *label, size_t label_len,
876 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100877 const unsigned char *input,
878 unsigned char *output,
879 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000880{
Paul Bakker23986e52011-04-24 08:57:21 +0000881 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100882 size_t ilen, i, pad_len;
883 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
885 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000886 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 const mbedtls_md_info_t *md_info;
888 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100889
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100890 /*
891 * Parameters sanity checks
892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
894 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000895
896 ilen = ctx->len;
897
Paul Bakker27fdf462011-06-09 13:55:13 +0000898 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100902 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100904
Janos Follathc17cda12016-02-11 11:08:18 +0000905 hlen = mbedtls_md_get_size( md_info );
906
907 // checking for integer underflow
908 if( 2 * hlen + 2 > ilen )
909 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
910
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100911 /*
912 * RSA operation
913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 ret = ( mode == MBEDTLS_RSA_PUBLIC )
915 ? mbedtls_rsa_public( ctx, input, buf )
916 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000917
918 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100919 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000920
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100921 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100922 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100923 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700925 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
926 {
927 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100928 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700929 }
930
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100931
932 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100934
935 /* seed: Apply seedMask to maskedSeed */
936 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
937 &md_ctx );
938
939 /* DB: Apply dbMask to maskedDB */
940 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
941 &md_ctx );
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100944
945 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100946 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100947 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000948 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100949 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000950
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100951 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100952
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100953 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100954
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100955 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100956 for( i = 0; i < hlen; i++ )
957 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100958
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100959 /* Get zero-padding len, but always read till end of buffer
960 * (minus one, for the 01 byte) */
961 pad_len = 0;
962 pad_done = 0;
963 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
964 {
965 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100966 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100967 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100968
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100969 p += pad_len;
970 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100971
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100972 /*
973 * The only information "leaked" is whether the padding was correct or not
974 * (eg, no data is copied if it was not correct). This meets the
975 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
976 * the different error conditions.
977 */
978 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100979 {
980 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
981 goto cleanup;
982 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100983
Paul Bakker66d5d072014-06-17 16:39:18 +0200984 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100985 {
986 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
987 goto cleanup;
988 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100989
990 *olen = ilen - (p - buf);
991 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100992 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100993
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100994cleanup:
995 mbedtls_zeroize( buf, sizeof( buf ) );
996 mbedtls_zeroize( lhash, sizeof( lhash ) );
997
998 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001003/*
1004 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1005 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001007 int (*f_rng)(void *, unsigned char *, size_t),
1008 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001009 int mode, size_t *olen,
1010 const unsigned char *input,
1011 unsigned char *output,
1012 size_t output_max_len)
1013{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001014 int ret;
1015 size_t ilen, pad_count = 0, i;
1016 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1020 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001021
1022 ilen = ctx->len;
1023
1024 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1028 ? mbedtls_rsa_public( ctx, input, buf )
1029 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001030
1031 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001032 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001033
1034 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001035 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001036
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001037 /*
1038 * Check and get padding len in "constant-time"
1039 */
1040 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001041
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001042 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001046
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001047 /* Get padding len, but always read till end of buffer
1048 * (minus one, for the 00 byte) */
1049 for( i = 0; i < ilen - 3; i++ )
1050 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001051 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1052 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001053 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001054
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001055 p += pad_count;
1056 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001057 }
1058 else
1059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001061
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001062 /* Get padding len, but always read till end of buffer
1063 * (minus one, for the 00 byte) */
1064 for( i = 0; i < ilen - 3; i++ )
1065 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001066 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001067 pad_count += ( pad_done == 0 );
1068 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001069
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001070 p += pad_count;
1071 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001072 }
1073
Janos Follathc69fa502016-02-12 13:30:09 +00001074 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001075
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001076 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001077 {
1078 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1079 goto cleanup;
1080 }
Paul Bakker8804f692013-02-28 18:06:26 +01001081
Paul Bakker66d5d072014-06-17 16:39:18 +02001082 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001083 {
1084 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1085 goto cleanup;
1086 }
Paul Bakker060c5682009-01-12 21:48:39 +00001087
Paul Bakker27fdf462011-06-09 13:55:13 +00001088 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001089 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001090 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001091
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001092cleanup:
1093 mbedtls_zeroize( buf, sizeof( buf ) );
1094
1095 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
1099/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001100 * Do an RSA operation, then remove the message padding
1101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001103 int (*f_rng)(void *, unsigned char *, size_t),
1104 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001105 int mode, size_t *olen,
1106 const unsigned char *input,
1107 unsigned char *output,
1108 size_t output_max_len)
1109{
1110 switch( ctx->padding )
1111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#if defined(MBEDTLS_PKCS1_V15)
1113 case MBEDTLS_RSA_PKCS_V15:
1114 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001115 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001116#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118#if defined(MBEDTLS_PKCS1_V21)
1119 case MBEDTLS_RSA_PKCS_V21:
1120 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001121 olen, input, output,
1122 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001123#endif
1124
1125 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001127 }
1128}
1129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001131/*
1132 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001135 int (*f_rng)(void *, unsigned char *, size_t),
1136 void *p_rng,
1137 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001139 unsigned int hashlen,
1140 const unsigned char *hash,
1141 unsigned char *sig )
1142{
1143 size_t olen;
1144 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001146 unsigned int slen, hlen, offset = 0;
1147 int ret;
1148 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 const mbedtls_md_info_t *md_info;
1150 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1153 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001154
1155 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001157
1158 olen = ctx->len;
1159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001161 {
Simon Butcher02037452016-03-01 21:19:12 +00001162 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001164 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001168 }
1169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001171 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001175 slen = hlen;
1176
1177 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001179
1180 memset( sig, 0, olen );
1181
Simon Butcher02037452016-03-01 21:19:12 +00001182 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001183 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001185
Simon Butcher02037452016-03-01 21:19:12 +00001186 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001187 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001188 p += olen - hlen * 2 - 2;
1189 *p++ = 0x01;
1190 memcpy( p, salt, slen );
1191 p += slen;
1192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001194 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1195 {
1196 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001197 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001198 return( ret );
1199 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001200
Simon Butcher02037452016-03-01 21:19:12 +00001201 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 mbedtls_md_starts( &md_ctx );
1203 mbedtls_md_update( &md_ctx, p, 8 );
1204 mbedtls_md_update( &md_ctx, hash, hashlen );
1205 mbedtls_md_update( &md_ctx, salt, slen );
1206 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001207 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001208
Simon Butcher02037452016-03-01 21:19:12 +00001209 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001210 if( msb % 8 == 0 )
1211 offset = 1;
1212
Simon Butcher02037452016-03-01 21:19:12 +00001213 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001214 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001217
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001218 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001219 sig[0] &= 0xFF >> ( olen * 8 - msb );
1220
1221 p += hlen;
1222 *p++ = 0xBC;
1223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 return( ( mode == MBEDTLS_RSA_PUBLIC )
1225 ? mbedtls_rsa_public( ctx, sig, sig )
1226 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001231/*
1232 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1233 */
1234/*
1235 * Do an RSA operation to sign the message digest
1236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001238 int (*f_rng)(void *, unsigned char *, size_t),
1239 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001240 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001242 unsigned int hashlen,
1243 const unsigned char *hash,
1244 unsigned char *sig )
1245{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001246 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001247 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001248 const char *oid = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1251 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001252
1253 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001254 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001259 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1263 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001264
Paul Bakkerc70b9822013-04-07 22:00:46 +02001265 nb_pad -= 10 + oid_size;
1266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001268 }
1269
Paul Bakkerc70b9822013-04-07 22:00:46 +02001270 nb_pad -= hashlen;
1271
Paul Bakkerb3869132013-02-28 17:21:01 +01001272 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001274
1275 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001277 memset( p, 0xFF, nb_pad );
1278 p += nb_pad;
1279 *p++ = 0;
1280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001282 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001283 memcpy( p, hash, hashlen );
1284 }
1285 else
1286 {
1287 /*
1288 * DigestInfo ::= SEQUENCE {
1289 * digestAlgorithm DigestAlgorithmIdentifier,
1290 * digest Digest }
1291 *
1292 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1293 *
1294 * Digest ::= OCTET STRING
1295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001297 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001299 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001301 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001302 memcpy( p, oid, oid_size );
1303 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001305 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001307 *p++ = hashlen;
1308 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001309 }
1310
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001311 if( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker43f94722017-08-25 11:50:00 +01001312 return( mbedtls_rsa_public( ctx, sig, sig ) );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001313
1314 /*
1315 * In order to prevent Lenstra's attack, make the signature in a
1316 * temporary buffer and check it before returning it.
1317 */
1318 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001319 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001320 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1321
Simon Butcher1285ab52016-01-01 21:42:47 +00001322 verif = mbedtls_calloc( 1, ctx->len );
1323 if( verif == NULL )
1324 {
1325 mbedtls_free( sig_try );
1326 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1327 }
1328
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001329 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1330 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1331
1332 /* Compare in constant time just in case */
1333 for( diff = 0, i = 0; i < ctx->len; i++ )
1334 diff |= verif[i] ^ sig[i];
1335 diff_no_optimize = diff;
1336
1337 if( diff_no_optimize != 0 )
1338 {
1339 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1340 goto cleanup;
1341 }
1342
1343 memcpy( sig, sig_try, ctx->len );
1344
1345cleanup:
1346 mbedtls_free( sig_try );
1347 mbedtls_free( verif );
1348
1349 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001352
1353/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 * Do an RSA operation to sign the message digest
1355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001357 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001358 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001361 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001362 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 unsigned char *sig )
1364{
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 switch( ctx->padding )
1366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_PKCS1_V15)
1368 case MBEDTLS_RSA_PKCS_V15:
1369 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001370 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001371#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#if defined(MBEDTLS_PKCS1_V21)
1374 case MBEDTLS_RSA_PKCS_V21:
1375 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001376 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001377#endif
1378
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001382}
1383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001385/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001386 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001389 int (*f_rng)(void *, unsigned char *, size_t),
1390 void *p_rng,
1391 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001393 unsigned int hashlen,
1394 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001396 int expected_salt_len,
1397 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001398{
Paul Bakker23986e52011-04-24 08:57:21 +00001399 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001400 size_t siglen;
1401 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001403 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001404 unsigned int hlen;
1405 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 const mbedtls_md_info_t *md_info;
1407 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001408 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1411 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001412
Paul Bakker5121ce52009-01-03 21:22:43 +00001413 siglen = ctx->len;
1414
Paul Bakker27fdf462011-06-09 13:55:13 +00001415 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1419 ? mbedtls_rsa_public( ctx, sig, buf )
1420 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
1422 if( ret != 0 )
1423 return( ret );
1424
1425 p = buf;
1426
Paul Bakkerb3869132013-02-28 17:21:01 +01001427 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001431 {
Simon Butcher02037452016-03-01 21:19:12 +00001432 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001434 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001438 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001441 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001445 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001446
Paul Bakkerb3869132013-02-28 17:21:01 +01001447 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001448
Simon Butcher02037452016-03-01 21:19:12 +00001449 /*
1450 * Note: EMSA-PSS verification is over the length of N - 1 bits
1451 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001452 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001453
Simon Butcher02037452016-03-01 21:19:12 +00001454 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001455 if( msb % 8 == 0 )
1456 {
1457 p++;
1458 siglen -= 1;
1459 }
1460 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001464 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1465 {
1466 mbedtls_md_free( &md_ctx );
1467 return( ret );
1468 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001469
Paul Bakkerb3869132013-02-28 17:21:01 +01001470 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001471
Paul Bakkerb3869132013-02-28 17:21:01 +01001472 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001473
Paul Bakker4de44aa2013-12-31 11:43:01 +01001474 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001475 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001476
Paul Bakkerb3869132013-02-28 17:21:01 +01001477 if( p == buf + siglen ||
1478 *p++ != 0x01 )
1479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 mbedtls_md_free( &md_ctx );
1481 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001482 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001483
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001484 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001485 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001488 slen != (size_t) expected_salt_len )
1489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 mbedtls_md_free( &md_ctx );
1491 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001492 }
1493
Simon Butcher02037452016-03-01 21:19:12 +00001494 /*
1495 * Generate H = Hash( M' )
1496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 mbedtls_md_starts( &md_ctx );
1498 mbedtls_md_update( &md_ctx, zeros, 8 );
1499 mbedtls_md_update( &md_ctx, hash, hashlen );
1500 mbedtls_md_update( &md_ctx, p, slen );
1501 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001504
Paul Bakkerb3869132013-02-28 17:21:01 +01001505 if( memcmp( p + slen, result, hlen ) == 0 )
1506 return( 0 );
1507 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001509}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001510
1511/*
1512 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1513 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001515 int (*f_rng)(void *, unsigned char *, size_t),
1516 void *p_rng,
1517 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001519 unsigned int hashlen,
1520 const unsigned char *hash,
1521 const unsigned char *sig )
1522{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1524 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001525 : md_alg;
1526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001528 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001530 sig ) );
1531
1532}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001536/*
1537 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001540 int (*f_rng)(void *, unsigned char *, size_t),
1541 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001542 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001544 unsigned int hashlen,
1545 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001546 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001547{
1548 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001549 size_t len, siglen, asn1_len;
1550 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 mbedtls_md_type_t msg_md_alg;
1552 const mbedtls_md_info_t *md_info;
1553 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001554 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1557 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001558
1559 siglen = ctx->len;
1560
1561 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1565 ? mbedtls_rsa_public( ctx, sig, buf )
1566 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001567
1568 if( ret != 0 )
1569 return( ret );
1570
1571 p = buf;
1572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1574 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001575
1576 while( *p != 0 )
1577 {
1578 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001580 p++;
1581 }
1582 p++;
1583
1584 len = siglen - ( p - buf );
1585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001587 {
1588 if( memcmp( p, hash, hashlen ) == 0 )
1589 return( 0 );
1590 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001592 }
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001595 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1597 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001598
1599 end = p + len;
1600
Simon Butcher02037452016-03-01 21:19:12 +00001601 /*
1602 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1605 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1606 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001607
1608 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1612 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1613 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001614
1615 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001618 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1619 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001620
1621 oid.p = p;
1622 p += oid.len;
1623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1625 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001626
1627 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001629
1630 /*
1631 * assume the algorithm parameters must be NULL
1632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1634 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1637 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001638
1639 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001641
1642 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001644
1645 p += hashlen;
1646
1647 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001649
1650 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001651}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
1654/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001655 * Do an RSA operation and check the message digest
1656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001658 int (*f_rng)(void *, unsigned char *, size_t),
1659 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001660 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001662 unsigned int hashlen,
1663 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001664 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001665{
1666 switch( ctx->padding )
1667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#if defined(MBEDTLS_PKCS1_V15)
1669 case MBEDTLS_RSA_PKCS_V15:
1670 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001671 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001672#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674#if defined(MBEDTLS_PKCS1_V21)
1675 case MBEDTLS_RSA_PKCS_V21:
1676 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001677 hashlen, hash, sig );
1678#endif
1679
1680 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001682 }
1683}
1684
1685/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001686 * Copy the components of an RSA key
1687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001689{
1690 int ret;
1691
1692 dst->ver = src->ver;
1693 dst->len = src->len;
1694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1696 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1699 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1700 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1701 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1702 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1703 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1706 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1707 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1710 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001711
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001712 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001713 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001714
1715cleanup:
1716 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001718
1719 return( ret );
1720}
1721
1722/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001723 * Free the components of an RSA key
1724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001726{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1728 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1729 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1730 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1731 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#if defined(MBEDTLS_THREADING_C)
1734 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001735#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001736}
1737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001738#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001739
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001740#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001741
1742/*
1743 * Example RSA-1024 keypair, for test purposes
1744 */
1745#define KEY_LEN 128
1746
1747#define RSA_N "9292758453063D803DD603D5E777D788" \
1748 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1749 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1750 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1751 "93A89813FBF3C4F8066D2D800F7C38A8" \
1752 "1AE31942917403FF4946B0A83D3D3E05" \
1753 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1754 "5E94BB77B07507233A0BC7BAC8F90F79"
1755
1756#define RSA_E "10001"
1757
1758#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1759 "66CA472BC44D253102F8B4A9D3BFA750" \
1760 "91386C0077937FE33FA3252D28855837" \
1761 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1762 "DF79C5CE07EE72C7F123142198164234" \
1763 "CABB724CF78B8173B9F880FC86322407" \
1764 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1765 "071513A1E85B5DFA031F21ECAE91A34D"
1766
1767#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1768 "2C01CAD19EA484A87EA4377637E75500" \
1769 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1770 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1771
1772#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1773 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1774 "910E4168387E3C30AA1E00C339A79508" \
1775 "8452DD96A9A5EA5D9DCA68DA636032AF"
1776
1777#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1778 "3C94D22288ACD763FD8E5600ED4A702D" \
1779 "F84198A5F06C2E72236AE490C93F07F8" \
1780 "3CC559CD27BC2D1CA488811730BB5725"
1781
1782#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1783 "D8AAEA56749EA28623272E4F7D0592AF" \
1784 "7C1F1313CAC9471B5C523BFE592F517B" \
1785 "407A1BD76C164B93DA2D32A383E58357"
1786
1787#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1788 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1789 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1790 "A74206CEC169D74BF5A8C50D6F48EA08"
1791
1792#define PT_LEN 24
1793#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1794 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001797static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001798{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001799#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001800 size_t i;
1801
Paul Bakker545570e2010-07-18 09:00:25 +00001802 if( rng_state != NULL )
1803 rng_state = NULL;
1804
Paul Bakkera3d195c2011-11-27 21:07:34 +00001805 for( i = 0; i < len; ++i )
1806 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001807#else
1808 if( rng_state != NULL )
1809 rng_state = NULL;
1810
1811 arc4random_buf( output, len );
1812#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001813
Paul Bakkera3d195c2011-11-27 21:07:34 +00001814 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001817
Paul Bakker5121ce52009-01-03 21:22:43 +00001818/*
1819 * Checkup routine
1820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001822{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001823 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001825 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001827 unsigned char rsa_plaintext[PT_LEN];
1828 unsigned char rsa_decrypted[PT_LEN];
1829 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001831 unsigned char sha1sum[20];
1832#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001835
1836 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1838 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1839 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1840 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1841 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1842 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1843 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1844 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001845
1846 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1850 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001851 {
1852 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001854
Hanno Becker5bc87292017-05-03 15:09:31 +01001855 ret = 1;
1856 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001857 }
1858
1859 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001861
1862 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001865 rsa_plaintext, rsa_ciphertext ) != 0 )
1866 {
1867 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001869
Hanno Becker5bc87292017-05-03 15:09:31 +01001870 ret = 1;
1871 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001872 }
1873
1874 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001878 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001879 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001880 {
1881 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
Hanno Becker5bc87292017-05-03 15:09:31 +01001884 ret = 1;
1885 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 }
1887
1888 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1889 {
1890 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001892
Hanno Becker5bc87292017-05-03 15:09:31 +01001893 ret = 1;
1894 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001895 }
1896
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001897 if( verbose != 0 )
1898 mbedtls_printf( "passed\n" );
1899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001901 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001902 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001907 sha1sum, rsa_ciphertext ) != 0 )
1908 {
1909 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001911
Hanno Becker5bc87292017-05-03 15:09:31 +01001912 ret = 1;
1913 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001914 }
1915
1916 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001919 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001920 sha1sum, rsa_ciphertext ) != 0 )
1921 {
1922 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001924
Hanno Becker5bc87292017-05-03 15:09:31 +01001925 ret = 1;
1926 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001927 }
1928
1929 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001930 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001932
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001933 if( verbose != 0 )
1934 mbedtls_printf( "\n" );
1935
Paul Bakker3d8fb632014-04-17 12:42:41 +02001936cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 mbedtls_rsa_free( &rsa );
1938#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001939 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001941 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001942}
1943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946#endif /* MBEDTLS_RSA_C */