blob: 56f434563a7896fbd8235e1d834c80001f333636 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000022 * The following sources were referenced in the design of this implementation
23 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000024 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000025 * [1] A method for obtaining digital signatures and public-key cryptosystems
26 * R Rivest, A Shamir, and L Adleman
27 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
28 *
29 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
30 * Menezes, van Oorschot and Vanstone
31 *
Janos Follathe81102e2017-03-22 13:38:28 +000032 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
33 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
34 * Stefan Mangard
35 * https://arxiv.org/abs/1702.08719v2
36 *
Paul Bakker5121ce52009-01-03 21:22:43 +000037 */
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020041#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/rsa.h"
48#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#else
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020065#define mbedtls_calloc calloc
66#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#endif
68
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010069/* Implementation that should never be optimized out by the compiler */
70static void mbedtls_zeroize( void *v, size_t n ) {
71 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
72}
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/*
75 * Initialize an RSA context
76 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000078 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000079 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_THREADING_C)
86 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020087#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000088}
89
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010090/*
91 * Set padding for an existing RSA context
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010094{
95 ctx->padding = padding;
96 ctx->hash_id = hash_id;
97}
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101/*
102 * Generate an RSA keypair
103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000105 int (*f_rng)(void *, unsigned char *, size_t),
106 void *p_rng,
107 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000108{
109 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Paul Bakker21eb2802010-08-16 11:10:02 +0000112 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Janos Follathef441782016-09-21 13:18:12 +0100115 if( nbits % 2 )
116 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
117
118 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath10c575b2016-02-23 14:42:48 +0000119 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 /*
122 * find primes P and Q with Q < P so that:
123 * GCD( E, (P-1)*(Q-1) ) == 1
124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127 do
128 {
Janos Follath10c575b2016-02-23 14:42:48 +0000129 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000130 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Janos Follathef441782016-09-21 13:18:12 +0100132 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000133 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 continue;
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200139 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 continue;
141
Janos Follathef441782016-09-21 13:18:12 +0100142 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
143 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
146 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
147 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
148 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 /*
153 * D = E^-1 mod ((P-1)*(Q-1))
154 * DP = D mod (P - 1)
155 * DQ = D mod (Q - 1)
156 * QP = Q^-1 mod P
157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
159 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
160 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
161 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200163 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
165cleanup:
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 if( ret != 0 )
170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_rsa_free( ctx );
172 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 }
174
Paul Bakker48377d92013-08-30 12:06:24 +0200175 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176}
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180/*
181 * Check a public RSA key
182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000185 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000187
Paul Bakker48377d92013-08-30 12:06:24 +0200188 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200192 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
193 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200196 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
198 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
200 return( 0 );
201}
202
203/*
204 * Check a private RSA key
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000207{
208 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 return( ret );
213
Paul Bakker37940d9f2009-07-10 22:38:58 +0000214 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
218 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
219 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
220 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
223 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
225 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
226 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
227 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
230 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
231 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
234 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
235 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000236 /*
237 * Check for a valid PKCS1v2 private key
238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
240 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
241 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
242 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
243 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
244 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
245 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 }
Paul Bakker48377d92013-08-30 12:06:24 +0200249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
252 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
253 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
254 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000257 return( ret );
258
Paul Bakker6c591fa2011-05-05 11:49:20 +0000259 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000261
262 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263}
264
265/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100266 * Check if contexts holding a public and private key match
267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
271 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100274 }
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
277 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100280 }
281
282 return( 0 );
283}
284
285/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 * Do an RSA public key operation
287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000289 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 unsigned char *output )
291{
Paul Bakker23986e52011-04-24 08:57:21 +0000292 int ret;
293 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200298#if defined(MBEDTLS_THREADING_C)
299 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
300 return( ret );
301#endif
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200307 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
308 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 }
310
311 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
313 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
315cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200317 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
318 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100319#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326 return( 0 );
327}
328
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200329/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200330 * Generate or update blinding values, see section 10 of:
331 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200332 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200333 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200334 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200335static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200336 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
337{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200338 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200339
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200340 if( ctx->Vf.p != NULL )
341 {
342 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
344 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
345 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
346 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200347
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200348 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200349 }
350
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200351 /* Unblinding value: Vf = random number, invertible mod N */
352 do {
353 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
357 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
358 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200359
360 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
362 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200363
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200364
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200365cleanup:
366 return( ret );
367}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200368
Paul Bakker5121ce52009-01-03 21:22:43 +0000369/*
Janos Follathe81102e2017-03-22 13:38:28 +0000370 * Exponent blinding supposed to prevent side-channel attacks using multiple
371 * traces of measurements to recover the RSA key. The more collisions are there,
372 * the more bits of the key can be recovered. See [3].
373 *
374 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
375 * observations on avarage.
376 *
377 * For example with 28 byte blinding to achieve 2 collisions the adversary has
378 * to make 2^112 observations on avarage.
379 *
380 * (With the currently (as of 2017 April) known best algorithms breaking 2048
381 * bit RSA requires approximately as much time as trying out 2^112 random keys.
382 * Thus in this sense with 28 byte blinding the security is not reduced by
383 * side-channel attacks like the one in [3])
384 *
385 * This countermeasure does not help if the key recovery is possible with a
386 * single trace.
387 */
388#define RSA_EXPONENT_BLINDING 28
389
390/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 * Do an RSA private key operation
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200394 int (*f_rng)(void *, unsigned char *, size_t),
395 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000396 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 unsigned char *output )
398{
Paul Bakker23986e52011-04-24 08:57:21 +0000399 int ret;
400 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100401
402 /* Temporary holding the result */
403 mbedtls_mpi T;
404
405 /* Temporaries holding P-1, Q-1 and the
406 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000407 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100408
409#if !defined(MBEDTLS_RSA_NO_CRT)
410 /* Temporaries holding the results mod p resp. mod q. */
411 mbedtls_mpi TP, TQ;
412
413 /* Temporaries holding the blinded exponents for
414 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000415 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100416
417 /* Pointers to actual exponents to be used - either the unblinded
418 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000419 mbedtls_mpi *DP = &ctx->DP;
420 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100421#else
422 /* Temporary holding the blinded exponent (if used). */
423 mbedtls_mpi D_blind;
424
425 /* Pointer to actual exponent to be used - either the unblinded
426 * or the blinded one, depending on the presence of a PRNG. */
427 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100428#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100429
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100430 /* Temporaries holding the initial input and the double
431 * checked result; should be the same in the end. */
432 mbedtls_mpi I, C;
Paul Bakker5121ce52009-01-03 21:22:43 +0000433
Hanno Becker43f94722017-08-25 11:50:00 +0100434 /* Sanity-check that all relevant fields are at least set,
435 * but don't perform a full keycheck. */
Hanno Becker2fdffe02017-09-29 15:19:28 +0100436#if defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker43f94722017-08-25 11:50:00 +0100437 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
Hanno Becker43f94722017-08-25 11:50:00 +0100438 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
Hanno Becker2fdffe02017-09-29 15:19:28 +0100439 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 ||
440 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
441 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 )
Hanno Becker43f94722017-08-25 11:50:00 +0100442 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100443 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker43f94722017-08-25 11:50:00 +0100444 }
Hanno Becker2fdffe02017-09-29 15:19:28 +0100445#else /* ! MBEDTLS_RSA_NO_CRT */
446 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
447 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 ||
448 mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
Hanno Becker2c9f0272017-09-28 11:04:13 +0100449 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
450 mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
Hanno Becker43f94722017-08-25 11:50:00 +0100451 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
452 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
453 {
454 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
455 }
Hanno Becker2fdffe02017-09-29 15:19:28 +0100456#endif /* ! MBEDTLS_RSA_NO_CRT */
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100457
Hanno Becker06811ce2017-05-03 15:10:34 +0100458#if defined(MBEDTLS_THREADING_C)
459 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
460 return( ret );
461#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000462
Hanno Becker06811ce2017-05-03 15:10:34 +0100463 /* MPI Initialization */
464
465 mbedtls_mpi_init( &T );
466
467 mbedtls_mpi_init( &P1 );
468 mbedtls_mpi_init( &Q1 );
469 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000470
471 if( f_rng != NULL )
472 {
Janos Follathe81102e2017-03-22 13:38:28 +0000473#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000474 mbedtls_mpi_init( &D_blind );
475#else
476 mbedtls_mpi_init( &DP_blind );
477 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000478#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000479 }
Janos Follathe81102e2017-03-22 13:38:28 +0000480
Hanno Becker06811ce2017-05-03 15:10:34 +0100481#if !defined(MBEDTLS_RSA_NO_CRT)
482 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200483#endif
484
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100485 mbedtls_mpi_init( &I );
486 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100487
488 /* End of MPI initialization */
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
491 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200493 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
494 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 }
496
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100497 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100498
Paul Bakkerf451bac2013-08-30 15:37:02 +0200499 if( f_rng != NULL )
500 {
501 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200502 * Blinding
503 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200504 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200505 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
506 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000508
Janos Follathe81102e2017-03-22 13:38:28 +0000509 /*
510 * Exponent blinding
511 */
512 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
513 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
514
Janos Follathf9203b42017-03-22 15:13:15 +0000515#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000516 /*
517 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
518 */
519 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
520 f_rng, p_rng ) );
521 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
522 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
523 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
524
525 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000526#else
527 /*
528 * DP_blind = ( P - 1 ) * R + DP
529 */
530 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
531 f_rng, p_rng ) );
532 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
533 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
534 &ctx->DP ) );
535
536 DP = &DP_blind;
537
538 /*
539 * DQ_blind = ( Q - 1 ) * R + DQ
540 */
541 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
542 f_rng, p_rng ) );
543 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
544 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
545 &ctx->DQ ) );
546
547 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000548#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200549 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000552 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100553#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200554 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000555 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100557 * TP = input ^ dP mod P
558 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100560
561 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
562 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100565 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100567 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
568 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
569 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000570
571 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100572 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000573 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100574 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
575 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200577
Paul Bakkerf451bac2013-08-30 15:37:02 +0200578 if( f_rng != NULL )
579 {
580 /*
581 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200582 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200583 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200584 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200586 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000587
Hanno Becker2dec5e82017-10-03 07:49:52 +0100588 /* Verify the result to prevent glitching attacks. */
589 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E,
590 &ctx->N, &ctx->RN ) );
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100591 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +0100592 {
Hanno Becker06811ce2017-05-03 15:10:34 +0100593 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
594 goto cleanup;
595 }
Hanno Becker06811ce2017-05-03 15:10:34 +0100596
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
600cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200602 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
603 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200604#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200605
Hanno Becker06811ce2017-05-03 15:10:34 +0100606 mbedtls_mpi_free( &P1 );
607 mbedtls_mpi_free( &Q1 );
608 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000609
610 if( f_rng != NULL )
611 {
Janos Follathe81102e2017-03-22 13:38:28 +0000612#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000613 mbedtls_mpi_free( &D_blind );
614#else
615 mbedtls_mpi_free( &DP_blind );
616 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000617#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000618 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
Hanno Becker06811ce2017-05-03 15:10:34 +0100620 mbedtls_mpi_free( &T );
621
622#if !defined(MBEDTLS_RSA_NO_CRT)
623 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
624#endif
625
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100626 mbedtls_mpi_free( &C );
627 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +0100628
Paul Bakker5121ce52009-01-03 21:22:43 +0000629 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
632 return( 0 );
633}
634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000636/**
637 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
638 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000639 * \param dst buffer to mask
640 * \param dlen length of destination buffer
641 * \param src source of the mask generation
642 * \param slen length of the source buffer
643 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000644 */
Paul Bakker48377d92013-08-30 12:06:24 +0200645static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000649 unsigned char counter[4];
650 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000651 unsigned int hlen;
652 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000655 memset( counter, 0, 4 );
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000658
Simon Butcher02037452016-03-01 21:19:12 +0000659 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000660 p = dst;
661
662 while( dlen > 0 )
663 {
664 use_len = hlen;
665 if( dlen < hlen )
666 use_len = dlen;
667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 mbedtls_md_starts( md_ctx );
669 mbedtls_md_update( md_ctx, src, slen );
670 mbedtls_md_update( md_ctx, counter, 4 );
671 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000672
673 for( i = 0; i < use_len; ++i )
674 *p++ ^= mask[i];
675
676 counter[3]++;
677
678 dlen -= use_len;
679 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200680
681 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100686/*
687 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100690 int (*f_rng)(void *, unsigned char *, size_t),
691 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100692 int mode,
693 const unsigned char *label, size_t label_len,
694 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100695 const unsigned char *input,
696 unsigned char *output )
697{
698 size_t olen;
699 int ret;
700 unsigned char *p = output;
701 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 const mbedtls_md_info_t *md_info;
703 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
706 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200707
708 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100712 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100714
715 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100717
Simon Butcher02037452016-03-01 21:19:12 +0000718 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000719 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100721
722 memset( output, 0, olen );
723
724 *p++ = 0;
725
Simon Butcher02037452016-03-01 21:19:12 +0000726 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100727 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100729
730 p += hlen;
731
Simon Butcher02037452016-03-01 21:19:12 +0000732 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100734 p += hlen;
735 p += olen - 2 * hlen - 2 - ilen;
736 *p++ = 1;
737 memcpy( p, input, ilen );
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700740 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
741 {
742 mbedtls_md_free( &md_ctx );
743 return( ret );
744 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100745
Simon Butcher02037452016-03-01 21:19:12 +0000746 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100747 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
748 &md_ctx );
749
Simon Butcher02037452016-03-01 21:19:12 +0000750 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100751 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
752 &md_ctx );
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 return( ( mode == MBEDTLS_RSA_PUBLIC )
757 ? mbedtls_rsa_public( ctx, output, output )
758 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100763/*
764 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
765 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100767 int (*f_rng)(void *, unsigned char *, size_t),
768 void *p_rng,
769 int mode, size_t ilen,
770 const unsigned char *input,
771 unsigned char *output )
772{
773 size_t nb_pad, olen;
774 int ret;
775 unsigned char *p = output;
776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
778 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200779
Janos Follath1ed9f992016-03-18 11:45:44 +0000780 // We don't check p_rng because it won't be dereferenced here
781 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100783
784 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100785
Simon Butcher02037452016-03-01 21:19:12 +0000786 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000787 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100789
790 nb_pad = olen - 3 - ilen;
791
792 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100796
797 while( nb_pad-- > 0 )
798 {
799 int rng_dl = 100;
800
801 do {
802 ret = f_rng( p_rng, p, 1 );
803 } while( *p == 0 && --rng_dl && ret == 0 );
804
Simon Butcher02037452016-03-01 21:19:12 +0000805 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200806 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100808
809 p++;
810 }
811 }
812 else
813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100815
816 while( nb_pad-- > 0 )
817 *p++ = 0xFF;
818 }
819
820 *p++ = 0;
821 memcpy( p, input, ilen );
822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 return( ( mode == MBEDTLS_RSA_PUBLIC )
824 ? mbedtls_rsa_public( ctx, output, output )
825 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100826}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100828
Paul Bakker5121ce52009-01-03 21:22:43 +0000829/*
830 * Add the message padding, then do an RSA operation
831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000833 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000834 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000835 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000836 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000837 unsigned char *output )
838{
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 switch( ctx->padding )
840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#if defined(MBEDTLS_PKCS1_V15)
842 case MBEDTLS_RSA_PKCS_V15:
843 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100844 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200845#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847#if defined(MBEDTLS_PKCS1_V21)
848 case MBEDTLS_RSA_PKCS_V21:
849 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100850 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000851#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000852
853 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000855 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000856}
857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000859/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100860 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200863 int (*f_rng)(void *, unsigned char *, size_t),
864 void *p_rng,
865 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100866 const unsigned char *label, size_t label_len,
867 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100868 const unsigned char *input,
869 unsigned char *output,
870 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000871{
Paul Bakker23986e52011-04-24 08:57:21 +0000872 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100873 size_t ilen, i, pad_len;
874 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
876 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000877 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 const mbedtls_md_info_t *md_info;
879 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100880
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100881 /*
882 * Parameters sanity checks
883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
885 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000886
887 ilen = ctx->len;
888
Paul Bakker27fdf462011-06-09 13:55:13 +0000889 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100893 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100895
Janos Follathc17cda12016-02-11 11:08:18 +0000896 hlen = mbedtls_md_get_size( md_info );
897
898 // checking for integer underflow
899 if( 2 * hlen + 2 > ilen )
900 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
901
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100902 /*
903 * RSA operation
904 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 ret = ( mode == MBEDTLS_RSA_PUBLIC )
906 ? mbedtls_rsa_public( ctx, input, buf )
907 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000908
909 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100910 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100912 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100913 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700916 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
917 {
918 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100919 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700920 }
921
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100922
923 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100925
926 /* seed: Apply seedMask to maskedSeed */
927 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
928 &md_ctx );
929
930 /* DB: Apply dbMask to maskedDB */
931 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
932 &md_ctx );
933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100935
936 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100937 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100938 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100940 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000941
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100942 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100943
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100944 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100945
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100946 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100947 for( i = 0; i < hlen; i++ )
948 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100949
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100950 /* Get zero-padding len, but always read till end of buffer
951 * (minus one, for the 01 byte) */
952 pad_len = 0;
953 pad_done = 0;
954 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
955 {
956 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100957 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100958 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100959
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100960 p += pad_len;
961 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100962
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100963 /*
964 * The only information "leaked" is whether the padding was correct or not
965 * (eg, no data is copied if it was not correct). This meets the
966 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
967 * the different error conditions.
968 */
969 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100970 {
971 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
972 goto cleanup;
973 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100974
Paul Bakker66d5d072014-06-17 16:39:18 +0200975 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100976 {
977 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
978 goto cleanup;
979 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100980
981 *olen = ilen - (p - buf);
982 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100983 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100984
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100985cleanup:
986 mbedtls_zeroize( buf, sizeof( buf ) );
987 mbedtls_zeroize( lhash, sizeof( lhash ) );
988
989 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100990}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100994/*
995 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
996 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200998 int (*f_rng)(void *, unsigned char *, size_t),
999 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001000 int mode, size_t *olen,
1001 const unsigned char *input,
1002 unsigned char *output,
1003 size_t output_max_len)
1004{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001005 int ret;
1006 size_t ilen, pad_count = 0, i;
1007 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1011 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001012
1013 ilen = ctx->len;
1014
1015 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1019 ? mbedtls_rsa_public( ctx, input, buf )
1020 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001021
1022 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001023 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001024
1025 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001026 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001027
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001028 /*
1029 * Check and get padding len in "constant-time"
1030 */
1031 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001032
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001033 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001037
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001038 /* Get padding len, but always read till end of buffer
1039 * (minus one, for the 00 byte) */
1040 for( i = 0; i < ilen - 3; i++ )
1041 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001042 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1043 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001044 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001045
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001046 p += pad_count;
1047 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001048 }
1049 else
1050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001052
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001053 /* Get padding len, but always read till end of buffer
1054 * (minus one, for the 00 byte) */
1055 for( i = 0; i < ilen - 3; i++ )
1056 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001057 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001058 pad_count += ( pad_done == 0 );
1059 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001060
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001061 p += pad_count;
1062 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001063 }
1064
Janos Follathc69fa502016-02-12 13:30:09 +00001065 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001066
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001067 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001068 {
1069 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1070 goto cleanup;
1071 }
Paul Bakker8804f692013-02-28 18:06:26 +01001072
Paul Bakker66d5d072014-06-17 16:39:18 +02001073 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001074 {
1075 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1076 goto cleanup;
1077 }
Paul Bakker060c5682009-01-12 21:48:39 +00001078
Paul Bakker27fdf462011-06-09 13:55:13 +00001079 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001080 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001081 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001082
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001083cleanup:
1084 mbedtls_zeroize( buf, sizeof( buf ) );
1085
1086 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001087}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001089
1090/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001091 * Do an RSA operation, then remove the message padding
1092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001094 int (*f_rng)(void *, unsigned char *, size_t),
1095 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001096 int mode, size_t *olen,
1097 const unsigned char *input,
1098 unsigned char *output,
1099 size_t output_max_len)
1100{
1101 switch( ctx->padding )
1102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103#if defined(MBEDTLS_PKCS1_V15)
1104 case MBEDTLS_RSA_PKCS_V15:
1105 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001106 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001107#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#if defined(MBEDTLS_PKCS1_V21)
1110 case MBEDTLS_RSA_PKCS_V21:
1111 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001112 olen, input, output,
1113 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001114#endif
1115
1116 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001118 }
1119}
1120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001122/*
1123 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001126 int (*f_rng)(void *, unsigned char *, size_t),
1127 void *p_rng,
1128 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001130 unsigned int hashlen,
1131 const unsigned char *hash,
1132 unsigned char *sig )
1133{
1134 size_t olen;
1135 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001137 unsigned int slen, hlen, offset = 0;
1138 int ret;
1139 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 const mbedtls_md_info_t *md_info;
1141 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1144 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001145
1146 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001148
1149 olen = ctx->len;
1150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001152 {
Simon Butcher02037452016-03-01 21:19:12 +00001153 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001155 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001159 }
1160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001162 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001166 slen = hlen;
1167
1168 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001170
1171 memset( sig, 0, olen );
1172
Simon Butcher02037452016-03-01 21:19:12 +00001173 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001174 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001176
Simon Butcher02037452016-03-01 21:19:12 +00001177 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001178 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001179 p += olen - hlen * 2 - 2;
1180 *p++ = 0x01;
1181 memcpy( p, salt, slen );
1182 p += slen;
1183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001185 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1186 {
1187 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001188 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001189 return( ret );
1190 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001191
Simon Butcher02037452016-03-01 21:19:12 +00001192 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_md_starts( &md_ctx );
1194 mbedtls_md_update( &md_ctx, p, 8 );
1195 mbedtls_md_update( &md_ctx, hash, hashlen );
1196 mbedtls_md_update( &md_ctx, salt, slen );
1197 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001198 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001199
Simon Butcher02037452016-03-01 21:19:12 +00001200 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001201 if( msb % 8 == 0 )
1202 offset = 1;
1203
Simon Butcher02037452016-03-01 21:19:12 +00001204 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001205 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001208
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001209 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001210 sig[0] &= 0xFF >> ( olen * 8 - msb );
1211
1212 p += hlen;
1213 *p++ = 0xBC;
1214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 return( ( mode == MBEDTLS_RSA_PUBLIC )
1216 ? mbedtls_rsa_public( ctx, sig, sig )
1217 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001222/*
1223 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1224 */
1225/*
1226 * Do an RSA operation to sign the message digest
1227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001229 int (*f_rng)(void *, unsigned char *, size_t),
1230 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001231 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001233 unsigned int hashlen,
1234 const unsigned char *hash,
1235 unsigned char *sig )
1236{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001237 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001238 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001239 const char *oid = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1242 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001243
1244 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001245 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001250 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1254 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001255
Paul Bakkerc70b9822013-04-07 22:00:46 +02001256 nb_pad -= 10 + oid_size;
1257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001259 }
1260
Paul Bakkerc70b9822013-04-07 22:00:46 +02001261 nb_pad -= hashlen;
1262
Paul Bakkerb3869132013-02-28 17:21:01 +01001263 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001265
1266 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001268 memset( p, 0xFF, nb_pad );
1269 p += nb_pad;
1270 *p++ = 0;
1271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001273 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001274 memcpy( p, hash, hashlen );
1275 }
1276 else
1277 {
1278 /*
1279 * DigestInfo ::= SEQUENCE {
1280 * digestAlgorithm DigestAlgorithmIdentifier,
1281 * digest Digest }
1282 *
1283 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1284 *
1285 * Digest ::= OCTET STRING
1286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001288 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001290 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001292 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001293 memcpy( p, oid, oid_size );
1294 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001296 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001298 *p++ = hashlen;
1299 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001300 }
1301
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001302 if( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker43f94722017-08-25 11:50:00 +01001303 return( mbedtls_rsa_public( ctx, sig, sig ) );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001304
Hanno Beckercc209ca2017-08-25 11:51:03 +01001305 return( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001306}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001308
1309/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 * Do an RSA operation to sign the message digest
1311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001313 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001314 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001315 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001317 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001318 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001319 unsigned char *sig )
1320{
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 switch( ctx->padding )
1322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_PKCS1_V15)
1324 case MBEDTLS_RSA_PKCS_V15:
1325 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001326 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001327#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_PKCS1_V21)
1330 case MBEDTLS_RSA_PKCS_V21:
1331 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001332 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001333#endif
1334
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001338}
1339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001341/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001342 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001345 int (*f_rng)(void *, unsigned char *, size_t),
1346 void *p_rng,
1347 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001349 unsigned int hashlen,
1350 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001352 int expected_salt_len,
1353 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001354{
Paul Bakker23986e52011-04-24 08:57:21 +00001355 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001356 size_t siglen;
1357 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001359 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001360 unsigned int hlen;
1361 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 const mbedtls_md_info_t *md_info;
1363 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001364 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1367 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001368
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 siglen = ctx->len;
1370
Paul Bakker27fdf462011-06-09 13:55:13 +00001371 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1375 ? mbedtls_rsa_public( ctx, sig, buf )
1376 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
1378 if( ret != 0 )
1379 return( ret );
1380
1381 p = buf;
1382
Paul Bakkerb3869132013-02-28 17:21:01 +01001383 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001387 {
Simon Butcher02037452016-03-01 21:19:12 +00001388 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001390 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001394 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001397 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001401 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001402
Paul Bakkerb3869132013-02-28 17:21:01 +01001403 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001404
Simon Butcher02037452016-03-01 21:19:12 +00001405 /*
1406 * Note: EMSA-PSS verification is over the length of N - 1 bits
1407 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001408 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001409
Simon Butcher02037452016-03-01 21:19:12 +00001410 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001411 if( msb % 8 == 0 )
1412 {
1413 p++;
1414 siglen -= 1;
1415 }
1416 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001420 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1421 {
1422 mbedtls_md_free( &md_ctx );
1423 return( ret );
1424 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001425
Paul Bakkerb3869132013-02-28 17:21:01 +01001426 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001427
Paul Bakkerb3869132013-02-28 17:21:01 +01001428 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001429
Paul Bakker4de44aa2013-12-31 11:43:01 +01001430 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001431 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001432
Paul Bakkerb3869132013-02-28 17:21:01 +01001433 if( p == buf + siglen ||
1434 *p++ != 0x01 )
1435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 mbedtls_md_free( &md_ctx );
1437 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001438 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001439
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001440 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001441 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001444 slen != (size_t) expected_salt_len )
1445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 mbedtls_md_free( &md_ctx );
1447 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001448 }
1449
Simon Butcher02037452016-03-01 21:19:12 +00001450 /*
1451 * Generate H = Hash( M' )
1452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 mbedtls_md_starts( &md_ctx );
1454 mbedtls_md_update( &md_ctx, zeros, 8 );
1455 mbedtls_md_update( &md_ctx, hash, hashlen );
1456 mbedtls_md_update( &md_ctx, p, slen );
1457 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001460
Paul Bakkerb3869132013-02-28 17:21:01 +01001461 if( memcmp( p + slen, result, hlen ) == 0 )
1462 return( 0 );
1463 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001465}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001466
1467/*
1468 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001471 int (*f_rng)(void *, unsigned char *, size_t),
1472 void *p_rng,
1473 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001475 unsigned int hashlen,
1476 const unsigned char *hash,
1477 const unsigned char *sig )
1478{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1480 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001481 : md_alg;
1482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001484 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001486 sig ) );
1487
1488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001492/*
1493 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001496 int (*f_rng)(void *, unsigned char *, size_t),
1497 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001498 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001500 unsigned int hashlen,
1501 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001502 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001503{
1504 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001505 size_t len, siglen, asn1_len;
1506 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 mbedtls_md_type_t msg_md_alg;
1508 const mbedtls_md_info_t *md_info;
1509 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001510 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1513 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001514
1515 siglen = ctx->len;
1516
1517 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1521 ? mbedtls_rsa_public( ctx, sig, buf )
1522 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001523
1524 if( ret != 0 )
1525 return( ret );
1526
1527 p = buf;
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1530 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001531
1532 while( *p != 0 )
1533 {
1534 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001536 p++;
1537 }
1538 p++;
1539
1540 len = siglen - ( p - buf );
1541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001543 {
1544 if( memcmp( p, hash, hashlen ) == 0 )
1545 return( 0 );
1546 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001548 }
1549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001551 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1553 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001554
1555 end = p + len;
1556
Simon Butcher02037452016-03-01 21:19:12 +00001557 /*
1558 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1561 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1562 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001563
1564 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1568 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1569 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001570
1571 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1575 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001576
1577 oid.p = p;
1578 p += oid.len;
1579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1581 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001582
1583 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001585
1586 /*
1587 * assume the algorithm parameters must be NULL
1588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1590 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1593 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001594
1595 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001597
1598 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001600
1601 p += hashlen;
1602
1603 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001605
1606 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001607}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
1610/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001611 * Do an RSA operation and check the message digest
1612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001614 int (*f_rng)(void *, unsigned char *, size_t),
1615 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001616 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001618 unsigned int hashlen,
1619 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001620 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001621{
1622 switch( ctx->padding )
1623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#if defined(MBEDTLS_PKCS1_V15)
1625 case MBEDTLS_RSA_PKCS_V15:
1626 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001627 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001628#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#if defined(MBEDTLS_PKCS1_V21)
1631 case MBEDTLS_RSA_PKCS_V21:
1632 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001633 hashlen, hash, sig );
1634#endif
1635
1636 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001638 }
1639}
1640
1641/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001642 * Copy the components of an RSA key
1643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001645{
1646 int ret;
1647
1648 dst->ver = src->ver;
1649 dst->len = src->len;
1650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1652 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1655 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1656 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1657 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1658 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1659 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1662 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1663 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1666 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001667
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001668 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001669 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001670
1671cleanup:
1672 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001674
1675 return( ret );
1676}
1677
1678/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 * Free the components of an RSA key
1680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001682{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1684 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1685 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1686 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1687 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#if defined(MBEDTLS_THREADING_C)
1690 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001691#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001692}
1693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001696#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
1698/*
1699 * Example RSA-1024 keypair, for test purposes
1700 */
1701#define KEY_LEN 128
1702
1703#define RSA_N "9292758453063D803DD603D5E777D788" \
1704 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1705 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1706 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1707 "93A89813FBF3C4F8066D2D800F7C38A8" \
1708 "1AE31942917403FF4946B0A83D3D3E05" \
1709 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1710 "5E94BB77B07507233A0BC7BAC8F90F79"
1711
1712#define RSA_E "10001"
1713
1714#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1715 "66CA472BC44D253102F8B4A9D3BFA750" \
1716 "91386C0077937FE33FA3252D28855837" \
1717 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1718 "DF79C5CE07EE72C7F123142198164234" \
1719 "CABB724CF78B8173B9F880FC86322407" \
1720 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1721 "071513A1E85B5DFA031F21ECAE91A34D"
1722
1723#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1724 "2C01CAD19EA484A87EA4377637E75500" \
1725 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1726 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1727
1728#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1729 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1730 "910E4168387E3C30AA1E00C339A79508" \
1731 "8452DD96A9A5EA5D9DCA68DA636032AF"
1732
1733#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1734 "3C94D22288ACD763FD8E5600ED4A702D" \
1735 "F84198A5F06C2E72236AE490C93F07F8" \
1736 "3CC559CD27BC2D1CA488811730BB5725"
1737
1738#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1739 "D8AAEA56749EA28623272E4F7D0592AF" \
1740 "7C1F1313CAC9471B5C523BFE592F517B" \
1741 "407A1BD76C164B93DA2D32A383E58357"
1742
1743#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1744 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1745 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1746 "A74206CEC169D74BF5A8C50D6F48EA08"
1747
1748#define PT_LEN 24
1749#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1750 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001753static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001754{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001755#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001756 size_t i;
1757
Paul Bakker545570e2010-07-18 09:00:25 +00001758 if( rng_state != NULL )
1759 rng_state = NULL;
1760
Paul Bakkera3d195c2011-11-27 21:07:34 +00001761 for( i = 0; i < len; ++i )
1762 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001763#else
1764 if( rng_state != NULL )
1765 rng_state = NULL;
1766
1767 arc4random_buf( output, len );
1768#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001769
Paul Bakkera3d195c2011-11-27 21:07:34 +00001770 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001771}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001772#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001773
Paul Bakker5121ce52009-01-03 21:22:43 +00001774/*
1775 * Checkup routine
1776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001778{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001779 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001781 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001783 unsigned char rsa_plaintext[PT_LEN];
1784 unsigned char rsa_decrypted[PT_LEN];
1785 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001787 unsigned char sha1sum[20];
1788#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
1792 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1794 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1795 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1796 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1797 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1798 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1799 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1800 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001801
1802 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1806 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 {
1808 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001810
Hanno Becker5bc87292017-05-03 15:09:31 +01001811 ret = 1;
1812 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001813 }
1814
1815 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001817
1818 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001821 rsa_plaintext, rsa_ciphertext ) != 0 )
1822 {
1823 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001825
Hanno Becker5bc87292017-05-03 15:09:31 +01001826 ret = 1;
1827 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001828 }
1829
1830 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001833 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001834 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001835 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001836 {
1837 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
Hanno Becker5bc87292017-05-03 15:09:31 +01001840 ret = 1;
1841 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001842 }
1843
1844 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1845 {
1846 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Hanno Becker5bc87292017-05-03 15:09:31 +01001849 ret = 1;
1850 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001851 }
1852
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001853 if( verbose != 0 )
1854 mbedtls_printf( "passed\n" );
1855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001857 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001858 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001863 sha1sum, rsa_ciphertext ) != 0 )
1864 {
1865 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001867
Hanno Becker5bc87292017-05-03 15:09:31 +01001868 ret = 1;
1869 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001870 }
1871
1872 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 sha1sum, rsa_ciphertext ) != 0 )
1877 {
1878 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001880
Hanno Becker5bc87292017-05-03 15:09:31 +01001881 ret = 1;
1882 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001883 }
1884
1885 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001886 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001888
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001889 if( verbose != 0 )
1890 mbedtls_printf( "\n" );
1891
Paul Bakker3d8fb632014-04-17 12:42:41 +02001892cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 mbedtls_rsa_free( &rsa );
1894#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001895 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001897 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001898}
1899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#endif /* MBEDTLS_RSA_C */