blob: 6594e55efafcab6e3318b6f83ec50ae7f8fd65ef [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
Paul Bakker5121ce52009-01-03 21:22:43 +000069/*
70 * Initialize an RSA context
71 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000073 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000074 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_THREADING_C)
81 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020082#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000083}
84
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010085/*
86 * Set padding for an existing RSA context
87 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010089{
90 ctx->padding = padding;
91 ctx->hash_id = hash_id;
92}
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96/*
97 * Generate an RSA keypair
98 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000100 int (*f_rng)(void *, unsigned char *, size_t),
101 void *p_rng,
102 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000103{
104 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Paul Bakker21eb2802010-08-16 11:10:02 +0000107 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Janos Follathef441782016-09-21 13:18:12 +0100110 if( nbits % 2 )
111 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
112
113 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath10c575b2016-02-23 14:42:48 +0000114 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116 /*
117 * find primes P and Q with Q < P so that:
118 * GCD( E, (P-1)*(Q-1) ) == 1
119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
122 do
123 {
Janos Follath10c575b2016-02-23 14:42:48 +0000124 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000125 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Janos Follathef441782016-09-21 13:18:12 +0100127 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000128 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 continue;
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200134 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 continue;
136
Janos Follathef441782016-09-21 13:18:12 +0100137 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
138 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
141 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
142 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
143 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147 /*
148 * D = E^-1 mod ((P-1)*(Q-1))
149 * DP = D mod (P - 1)
150 * DQ = D mod (Q - 1)
151 * QP = Q^-1 mod P
152 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
154 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
155 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
156 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200158 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
160cleanup:
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164 if( ret != 0 )
165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_rsa_free( ctx );
167 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 }
169
Paul Bakker48377d92013-08-30 12:06:24 +0200170 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171}
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175/*
176 * Check a public RSA key
177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000179{
Paul Bakker37940d92009-07-10 22:38:58 +0000180 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000182
Paul Bakker48377d92013-08-30 12:06:24 +0200183 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200187 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
188 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200191 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
193 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194
195 return( 0 );
196}
197
198/*
199 * Check a private RSA key
200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000202{
203 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 return( ret );
208
Paul Bakker37940d92009-07-10 22:38:58 +0000209 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
213 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
214 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
215 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
218 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
219 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
220 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
221 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
222 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
225 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
226 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
229 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
230 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000231 /*
232 * Check for a valid PKCS1v2 private key
233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
235 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
236 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
237 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
238 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
239 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
240 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 }
Paul Bakker48377d92013-08-30 12:06:24 +0200244
Paul Bakker5121ce52009-01-03 21:22:43 +0000245cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
247 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
248 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
249 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000252 return( ret );
253
Paul Bakker6c591fa2011-05-05 11:49:20 +0000254 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000256
257 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000258}
259
260/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100261 * Check if contexts holding a public and private key match
262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100264{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
266 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100269 }
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
272 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100275 }
276
277 return( 0 );
278}
279
280/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 * Do an RSA public key operation
282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000284 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 unsigned char *output )
286{
Paul Bakker23986e52011-04-24 08:57:21 +0000287 int ret;
288 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200293#if defined(MBEDTLS_THREADING_C)
294 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
295 return( ret );
296#endif
297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200302 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
303 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 }
305
306 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
308 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
310cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200312 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
313 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100314#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
321 return( 0 );
322}
323
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200324/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200325 * Generate or update blinding values, see section 10 of:
326 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200327 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200328 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200329 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200330static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200331 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
332{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200333 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200334
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200335 if( ctx->Vf.p != NULL )
336 {
337 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
339 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
340 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
341 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200342
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200343 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200344 }
345
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200346 /* Unblinding value: Vf = random number, invertible mod N */
347 do {
348 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
352 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
353 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200354
355 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
357 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 +0200358
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200359
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200360cleanup:
361 return( ret );
362}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200363
Paul Bakker5121ce52009-01-03 21:22:43 +0000364/*
Janos Follathe81102e2017-03-22 13:38:28 +0000365 * Exponent blinding supposed to prevent side-channel attacks using multiple
366 * traces of measurements to recover the RSA key. The more collisions are there,
367 * the more bits of the key can be recovered. See [3].
368 *
369 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
370 * observations on avarage.
371 *
372 * For example with 28 byte blinding to achieve 2 collisions the adversary has
373 * to make 2^112 observations on avarage.
374 *
375 * (With the currently (as of 2017 April) known best algorithms breaking 2048
376 * bit RSA requires approximately as much time as trying out 2^112 random keys.
377 * Thus in this sense with 28 byte blinding the security is not reduced by
378 * side-channel attacks like the one in [3])
379 *
380 * This countermeasure does not help if the key recovery is possible with a
381 * single trace.
382 */
383#define RSA_EXPONENT_BLINDING 28
384
385/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000386 * Do an RSA private key operation
387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200389 int (*f_rng)(void *, unsigned char *, size_t),
390 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000391 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 unsigned char *output )
393{
Paul Bakker23986e52011-04-24 08:57:21 +0000394 int ret;
395 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_mpi T, T1, T2;
Janos Follathf9203b42017-03-22 15:13:15 +0000397 mbedtls_mpi P1, Q1, R;
Janos Follathe81102e2017-03-22 13:38:28 +0000398#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000399 mbedtls_mpi D_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000400 mbedtls_mpi *D = &ctx->D;
Janos Follathf9203b42017-03-22 15:13:15 +0000401#else
402 mbedtls_mpi DP_blind, DQ_blind;
403 mbedtls_mpi *DP = &ctx->DP;
404 mbedtls_mpi *DQ = &ctx->DQ;
Janos Follathe81102e2017-03-22 13:38:28 +0000405#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000406
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100407 /* Make sure we have private key info, prevent possible misuse */
408 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
409 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000412 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R );
413
414
415 if( f_rng != NULL )
416 {
Janos Follathe81102e2017-03-22 13:38:28 +0000417#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000418 mbedtls_mpi_init( &D_blind );
419#else
420 mbedtls_mpi_init( &DP_blind );
421 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000422#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000423 }
Janos Follathe81102e2017-03-22 13:38:28 +0000424
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200426#if defined(MBEDTLS_THREADING_C)
427 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
428 return( ret );
429#endif
430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
432 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200434 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
435 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 }
437
Paul Bakkerf451bac2013-08-30 15:37:02 +0200438 if( f_rng != NULL )
439 {
440 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200441 * Blinding
442 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200443 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200444 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
445 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000447
Janos Follathe81102e2017-03-22 13:38:28 +0000448 /*
449 * Exponent blinding
450 */
451 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
452 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
453
Janos Follathf9203b42017-03-22 15:13:15 +0000454#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000455 /*
456 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
457 */
458 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
459 f_rng, p_rng ) );
460 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
461 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
462 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
463
464 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000465#else
466 /*
467 * DP_blind = ( P - 1 ) * R + DP
468 */
469 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
470 f_rng, p_rng ) );
471 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
472 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
473 &ctx->DP ) );
474
475 DP = &DP_blind;
476
477 /*
478 * DQ_blind = ( Q - 1 ) * R + DQ
479 */
480 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
481 f_rng, p_rng ) );
482 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
483 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
484 &ctx->DQ ) );
485
486 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000487#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200488 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000491 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100492#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200493 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000494 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000495 *
496 * T1 = input ^ dP mod P
497 * T2 = input ^ dQ mod Q
498 */
Janos Follathf9203b42017-03-22 15:13:15 +0000499 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) );
500 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502 /*
503 * T = (T1 - T2) * (Q^-1 mod P) mod P
504 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
506 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
507 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000508
509 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200510 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
513 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
514#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200515
Paul Bakkerf451bac2013-08-30 15:37:02 +0200516 if( f_rng != NULL )
517 {
518 /*
519 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200520 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200521 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200522 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200524 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000525
526 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200531 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
532 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200533#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathf9203b42017-03-22 15:13:15 +0000536 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R );
537
538 if( f_rng != NULL )
539 {
Janos Follathe81102e2017-03-22 13:38:28 +0000540#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000541 mbedtls_mpi_free( &D_blind );
542#else
543 mbedtls_mpi_free( &DP_blind );
544 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000545#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000546 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000547
548 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
551 return( 0 );
552}
553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000555/**
556 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
557 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000558 * \param dst buffer to mask
559 * \param dlen length of destination buffer
560 * \param src source of the mask generation
561 * \param slen length of the source buffer
562 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000563 */
Paul Bakker48377d92013-08-30 12:06:24 +0200564static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000566{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000568 unsigned char counter[4];
569 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000570 unsigned int hlen;
571 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000574 memset( counter, 0, 4 );
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000577
Simon Butcher02037452016-03-01 21:19:12 +0000578 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000579 p = dst;
580
581 while( dlen > 0 )
582 {
583 use_len = hlen;
584 if( dlen < hlen )
585 use_len = dlen;
586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_md_starts( md_ctx );
588 mbedtls_md_update( md_ctx, src, slen );
589 mbedtls_md_update( md_ctx, counter, 4 );
590 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000591
592 for( i = 0; i < use_len; ++i )
593 *p++ ^= mask[i];
594
595 counter[3]++;
596
597 dlen -= use_len;
598 }
599}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100603/*
604 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100607 int (*f_rng)(void *, unsigned char *, size_t),
608 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100609 int mode,
610 const unsigned char *label, size_t label_len,
611 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100612 const unsigned char *input,
613 unsigned char *output )
614{
615 size_t olen;
616 int ret;
617 unsigned char *p = output;
618 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 const mbedtls_md_info_t *md_info;
620 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
623 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200624
625 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100629 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100631
632 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100634
Simon Butcher02037452016-03-01 21:19:12 +0000635 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000636 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100638
639 memset( output, 0, olen );
640
641 *p++ = 0;
642
Simon Butcher02037452016-03-01 21:19:12 +0000643 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100644 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100646
647 p += hlen;
648
Simon Butcher02037452016-03-01 21:19:12 +0000649 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100651 p += hlen;
652 p += olen - 2 * hlen - 2 - ilen;
653 *p++ = 1;
654 memcpy( p, input, ilen );
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700657 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
658 {
659 mbedtls_md_free( &md_ctx );
660 return( ret );
661 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100662
Simon Butcher02037452016-03-01 21:19:12 +0000663 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100664 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
665 &md_ctx );
666
Simon Butcher02037452016-03-01 21:19:12 +0000667 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100668 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
669 &md_ctx );
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( ( mode == MBEDTLS_RSA_PUBLIC )
674 ? mbedtls_rsa_public( ctx, output, output )
675 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100676}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100680/*
681 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100684 int (*f_rng)(void *, unsigned char *, size_t),
685 void *p_rng,
686 int mode, size_t ilen,
687 const unsigned char *input,
688 unsigned char *output )
689{
690 size_t nb_pad, olen;
691 int ret;
692 unsigned char *p = output;
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
695 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200696
Janos Follath1ed9f992016-03-18 11:45:44 +0000697 // We don't check p_rng because it won't be dereferenced here
698 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100700
701 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100702
Simon Butcher02037452016-03-01 21:19:12 +0000703 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000704 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100706
707 nb_pad = olen - 3 - ilen;
708
709 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100711 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100713
714 while( nb_pad-- > 0 )
715 {
716 int rng_dl = 100;
717
718 do {
719 ret = f_rng( p_rng, p, 1 );
720 } while( *p == 0 && --rng_dl && ret == 0 );
721
Simon Butcher02037452016-03-01 21:19:12 +0000722 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200723 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100725
726 p++;
727 }
728 }
729 else
730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100732
733 while( nb_pad-- > 0 )
734 *p++ = 0xFF;
735 }
736
737 *p++ = 0;
738 memcpy( p, input, ilen );
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 return( ( mode == MBEDTLS_RSA_PUBLIC )
741 ? mbedtls_rsa_public( ctx, output, output )
742 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100745
Paul Bakker5121ce52009-01-03 21:22:43 +0000746/*
747 * Add the message padding, then do an RSA operation
748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000750 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000751 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000752 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000753 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000754 unsigned char *output )
755{
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 switch( ctx->padding )
757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758#if defined(MBEDTLS_PKCS1_V15)
759 case MBEDTLS_RSA_PKCS_V15:
760 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100761 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200762#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764#if defined(MBEDTLS_PKCS1_V21)
765 case MBEDTLS_RSA_PKCS_V21:
766 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100767 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000768#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000769
770 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000773}
774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000776/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100777 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000778 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200780 int (*f_rng)(void *, unsigned char *, size_t),
781 void *p_rng,
782 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100783 const unsigned char *label, size_t label_len,
784 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100785 const unsigned char *input,
786 unsigned char *output,
787 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000788{
Paul Bakker23986e52011-04-24 08:57:21 +0000789 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100790 size_t ilen, i, pad_len;
791 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
793 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000794 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 const mbedtls_md_info_t *md_info;
796 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100797
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100798 /*
799 * Parameters sanity checks
800 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
802 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000803
804 ilen = ctx->len;
805
Paul Bakker27fdf462011-06-09 13:55:13 +0000806 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100810 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100812
Janos Follathc17cda12016-02-11 11:08:18 +0000813 hlen = mbedtls_md_get_size( md_info );
814
815 // checking for integer underflow
816 if( 2 * hlen + 2 > ilen )
817 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
818
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100819 /*
820 * RSA operation
821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 ret = ( mode == MBEDTLS_RSA_PUBLIC )
823 ? mbedtls_rsa_public( ctx, input, buf )
824 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000825
826 if( ret != 0 )
827 return( ret );
828
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100829 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100830 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700833 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
834 {
835 mbedtls_md_free( &md_ctx );
836 return( ret );
837 }
838
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100839
840 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100842
843 /* seed: Apply seedMask to maskedSeed */
844 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
845 &md_ctx );
846
847 /* DB: Apply dbMask to maskedDB */
848 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
849 &md_ctx );
850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100852
853 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100854 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100855 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000856 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100857 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000858
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100859 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100860
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100861 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100862
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100863 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100864 for( i = 0; i < hlen; i++ )
865 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100866
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100867 /* Get zero-padding len, but always read till end of buffer
868 * (minus one, for the 01 byte) */
869 pad_len = 0;
870 pad_done = 0;
871 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
872 {
873 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100874 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100875 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100876
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100877 p += pad_len;
878 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100879
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100880 /*
881 * The only information "leaked" is whether the padding was correct or not
882 * (eg, no data is copied if it was not correct). This meets the
883 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
884 * the different error conditions.
885 */
886 if( bad != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100888
Paul Bakker66d5d072014-06-17 16:39:18 +0200889 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakkerb3869132013-02-28 17:21:01 +0100891
892 *olen = ilen - (p - buf);
893 memcpy( output, p, *olen );
894
895 return( 0 );
896}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100900/*
901 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200904 int (*f_rng)(void *, unsigned char *, size_t),
905 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100906 int mode, size_t *olen,
907 const unsigned char *input,
908 unsigned char *output,
909 size_t output_max_len)
910{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100911 int ret;
912 size_t ilen, pad_count = 0, i;
913 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
917 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100918
919 ilen = ctx->len;
920
921 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 ret = ( mode == MBEDTLS_RSA_PUBLIC )
925 ? mbedtls_rsa_public( ctx, input, buf )
926 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100927
928 if( ret != 0 )
929 return( ret );
930
931 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100932 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100933
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100934 /*
935 * Check and get padding len in "constant-time"
936 */
937 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100938
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100939 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000943
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100944 /* Get padding len, but always read till end of buffer
945 * (minus one, for the 00 byte) */
946 for( i = 0; i < ilen - 3; i++ )
947 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100948 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
949 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100950 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000951
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100952 p += pad_count;
953 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100954 }
955 else
956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100958
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100959 /* Get padding len, but always read till end of buffer
960 * (minus one, for the 00 byte) */
961 for( i = 0; i < ilen - 3; i++ )
962 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100963 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100964 pad_count += ( pad_done == 0 );
965 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100966
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100967 p += pad_count;
968 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +0000969 }
970
Janos Follathc69fa502016-02-12 13:30:09 +0000971 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +0000972
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100973 if( bad )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker8804f692013-02-28 18:06:26 +0100975
Paul Bakker66d5d072014-06-17 16:39:18 +0200976 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000978
Paul Bakker27fdf462011-06-09 13:55:13 +0000979 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000980 memcpy( output, p, *olen );
981
982 return( 0 );
983}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000985
986/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100987 * Do an RSA operation, then remove the message padding
988 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200990 int (*f_rng)(void *, unsigned char *, size_t),
991 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100992 int mode, size_t *olen,
993 const unsigned char *input,
994 unsigned char *output,
995 size_t output_max_len)
996{
997 switch( ctx->padding )
998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999#if defined(MBEDTLS_PKCS1_V15)
1000 case MBEDTLS_RSA_PKCS_V15:
1001 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001002 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001003#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#if defined(MBEDTLS_PKCS1_V21)
1006 case MBEDTLS_RSA_PKCS_V21:
1007 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001008 olen, input, output,
1009 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001010#endif
1011
1012 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001014 }
1015}
1016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001018/*
1019 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001022 int (*f_rng)(void *, unsigned char *, size_t),
1023 void *p_rng,
1024 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001026 unsigned int hashlen,
1027 const unsigned char *hash,
1028 unsigned char *sig )
1029{
1030 size_t olen;
1031 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001033 unsigned int slen, hlen, offset = 0;
1034 int ret;
1035 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 const mbedtls_md_info_t *md_info;
1037 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1040 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001041
1042 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001044
1045 olen = ctx->len;
1046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001048 {
Simon Butcher02037452016-03-01 21:19:12 +00001049 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001051 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001055 }
1056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001058 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001062 slen = hlen;
1063
1064 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001066
1067 memset( sig, 0, olen );
1068
Simon Butcher02037452016-03-01 21:19:12 +00001069 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001070 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001072
Simon Butcher02037452016-03-01 21:19:12 +00001073 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001074 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001075 p += olen - hlen * 2 - 2;
1076 *p++ = 0x01;
1077 memcpy( p, salt, slen );
1078 p += slen;
1079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001081 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1082 {
1083 mbedtls_md_free( &md_ctx );
1084 return( ret );
1085 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001086
Simon Butcher02037452016-03-01 21:19:12 +00001087 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_md_starts( &md_ctx );
1089 mbedtls_md_update( &md_ctx, p, 8 );
1090 mbedtls_md_update( &md_ctx, hash, hashlen );
1091 mbedtls_md_update( &md_ctx, salt, slen );
1092 mbedtls_md_finish( &md_ctx, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001093
Simon Butcher02037452016-03-01 21:19:12 +00001094 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001095 if( msb % 8 == 0 )
1096 offset = 1;
1097
Simon Butcher02037452016-03-01 21:19:12 +00001098 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001099 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001102
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001103 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001104 sig[0] &= 0xFF >> ( olen * 8 - msb );
1105
1106 p += hlen;
1107 *p++ = 0xBC;
1108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 return( ( mode == MBEDTLS_RSA_PUBLIC )
1110 ? mbedtls_rsa_public( ctx, sig, sig )
1111 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001116/*
1117 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1118 */
1119/*
1120 * Do an RSA operation to sign the message digest
1121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001123 int (*f_rng)(void *, unsigned char *, size_t),
1124 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001125 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001127 unsigned int hashlen,
1128 const unsigned char *hash,
1129 unsigned char *sig )
1130{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001131 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001132 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001133 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001134 unsigned char *sig_try = NULL, *verif = NULL;
1135 size_t i;
1136 unsigned char diff;
1137 volatile unsigned char diff_no_optimize;
1138 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1141 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001142
1143 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001144 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001149 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1153 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001154
Paul Bakkerc70b9822013-04-07 22:00:46 +02001155 nb_pad -= 10 + oid_size;
1156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 }
1159
Paul Bakkerc70b9822013-04-07 22:00:46 +02001160 nb_pad -= hashlen;
1161
Paul Bakkerb3869132013-02-28 17:21:01 +01001162 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001164
1165 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001167 memset( p, 0xFF, nb_pad );
1168 p += nb_pad;
1169 *p++ = 0;
1170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001172 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001173 memcpy( p, hash, hashlen );
1174 }
1175 else
1176 {
1177 /*
1178 * DigestInfo ::= SEQUENCE {
1179 * digestAlgorithm DigestAlgorithmIdentifier,
1180 * digest Digest }
1181 *
1182 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1183 *
1184 * Digest ::= OCTET STRING
1185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001187 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001189 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001191 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001192 memcpy( p, oid, oid_size );
1193 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001195 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001197 *p++ = hashlen;
1198 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001199 }
1200
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001201 if( mode == MBEDTLS_RSA_PUBLIC )
1202 return( mbedtls_rsa_public( ctx, sig, sig ) );
1203
1204 /*
1205 * In order to prevent Lenstra's attack, make the signature in a
1206 * temporary buffer and check it before returning it.
1207 */
1208 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001209 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001210 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1211
Simon Butcher1285ab52016-01-01 21:42:47 +00001212 verif = mbedtls_calloc( 1, ctx->len );
1213 if( verif == NULL )
1214 {
1215 mbedtls_free( sig_try );
1216 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1217 }
1218
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001219 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1220 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1221
1222 /* Compare in constant time just in case */
1223 for( diff = 0, i = 0; i < ctx->len; i++ )
1224 diff |= verif[i] ^ sig[i];
1225 diff_no_optimize = diff;
1226
1227 if( diff_no_optimize != 0 )
1228 {
1229 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1230 goto cleanup;
1231 }
1232
1233 memcpy( sig, sig_try, ctx->len );
1234
1235cleanup:
1236 mbedtls_free( sig_try );
1237 mbedtls_free( verif );
1238
1239 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001240}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001242
1243/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001244 * Do an RSA operation to sign the message digest
1245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001247 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001248 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001249 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001251 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001252 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 unsigned char *sig )
1254{
Paul Bakker5121ce52009-01-03 21:22:43 +00001255 switch( ctx->padding )
1256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#if defined(MBEDTLS_PKCS1_V15)
1258 case MBEDTLS_RSA_PKCS_V15:
1259 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001260 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001261#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#if defined(MBEDTLS_PKCS1_V21)
1264 case MBEDTLS_RSA_PKCS_V21:
1265 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001266 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001267#endif
1268
Paul Bakker5121ce52009-01-03 21:22:43 +00001269 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001272}
1273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001275/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001276 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001279 int (*f_rng)(void *, unsigned char *, size_t),
1280 void *p_rng,
1281 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001283 unsigned int hashlen,
1284 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001286 int expected_salt_len,
1287 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001288{
Paul Bakker23986e52011-04-24 08:57:21 +00001289 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001290 size_t siglen;
1291 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001293 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001294 unsigned int hlen;
1295 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 const mbedtls_md_info_t *md_info;
1297 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001298 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1301 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001302
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 siglen = ctx->len;
1304
Paul Bakker27fdf462011-06-09 13:55:13 +00001305 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1309 ? mbedtls_rsa_public( ctx, sig, buf )
1310 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001311
1312 if( ret != 0 )
1313 return( ret );
1314
1315 p = buf;
1316
Paul Bakkerb3869132013-02-28 17:21:01 +01001317 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001321 {
Simon Butcher02037452016-03-01 21:19:12 +00001322 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001324 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001328 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001331 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001335 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001336
Paul Bakkerb3869132013-02-28 17:21:01 +01001337 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001338
Simon Butcher02037452016-03-01 21:19:12 +00001339 /*
1340 * Note: EMSA-PSS verification is over the length of N - 1 bits
1341 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001342 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001343
Simon Butcher02037452016-03-01 21:19:12 +00001344 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001345 if( msb % 8 == 0 )
1346 {
1347 p++;
1348 siglen -= 1;
1349 }
1350 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001354 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1355 {
1356 mbedtls_md_free( &md_ctx );
1357 return( ret );
1358 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001359
Paul Bakkerb3869132013-02-28 17:21:01 +01001360 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001361
Paul Bakkerb3869132013-02-28 17:21:01 +01001362 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001363
Paul Bakker4de44aa2013-12-31 11:43:01 +01001364 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001365 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001366
Paul Bakkerb3869132013-02-28 17:21:01 +01001367 if( p == buf + siglen ||
1368 *p++ != 0x01 )
1369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 mbedtls_md_free( &md_ctx );
1371 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001372 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001373
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001374 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001375 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001378 slen != (size_t) expected_salt_len )
1379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 mbedtls_md_free( &md_ctx );
1381 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001382 }
1383
Simon Butcher02037452016-03-01 21:19:12 +00001384 /*
1385 * Generate H = Hash( M' )
1386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 mbedtls_md_starts( &md_ctx );
1388 mbedtls_md_update( &md_ctx, zeros, 8 );
1389 mbedtls_md_update( &md_ctx, hash, hashlen );
1390 mbedtls_md_update( &md_ctx, p, slen );
1391 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001394
Paul Bakkerb3869132013-02-28 17:21:01 +01001395 if( memcmp( p + slen, result, hlen ) == 0 )
1396 return( 0 );
1397 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001399}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001400
1401/*
1402 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001405 int (*f_rng)(void *, unsigned char *, size_t),
1406 void *p_rng,
1407 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001409 unsigned int hashlen,
1410 const unsigned char *hash,
1411 const unsigned char *sig )
1412{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1414 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001415 : md_alg;
1416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001418 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001420 sig ) );
1421
1422}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001426/*
1427 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001430 int (*f_rng)(void *, unsigned char *, size_t),
1431 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001432 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001434 unsigned int hashlen,
1435 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001436 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001437{
1438 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001439 size_t len, siglen, asn1_len;
1440 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_md_type_t msg_md_alg;
1442 const mbedtls_md_info_t *md_info;
1443 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001444 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1447 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001448
1449 siglen = ctx->len;
1450
1451 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1455 ? mbedtls_rsa_public( ctx, sig, buf )
1456 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001457
1458 if( ret != 0 )
1459 return( ret );
1460
1461 p = buf;
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1464 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001465
1466 while( *p != 0 )
1467 {
1468 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001470 p++;
1471 }
1472 p++;
1473
1474 len = siglen - ( p - buf );
1475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001477 {
1478 if( memcmp( p, hash, hashlen ) == 0 )
1479 return( 0 );
1480 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001482 }
1483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001485 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1487 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001488
1489 end = p + len;
1490
Simon Butcher02037452016-03-01 21:19:12 +00001491 /*
1492 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1495 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1496 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001497
1498 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1502 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1503 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001504
1505 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1509 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001510
1511 oid.p = p;
1512 p += oid.len;
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1515 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001516
1517 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001519
1520 /*
1521 * assume the algorithm parameters must be NULL
1522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1524 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1527 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001528
1529 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001531
1532 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001534
1535 p += hashlen;
1536
1537 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001539
1540 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001543
1544/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001545 * Do an RSA operation and check the message digest
1546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001548 int (*f_rng)(void *, unsigned char *, size_t),
1549 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001550 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001552 unsigned int hashlen,
1553 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001554 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001555{
1556 switch( ctx->padding )
1557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558#if defined(MBEDTLS_PKCS1_V15)
1559 case MBEDTLS_RSA_PKCS_V15:
1560 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001561 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001562#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#if defined(MBEDTLS_PKCS1_V21)
1565 case MBEDTLS_RSA_PKCS_V21:
1566 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001567 hashlen, hash, sig );
1568#endif
1569
1570 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001572 }
1573}
1574
1575/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001576 * Copy the components of an RSA key
1577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001579{
1580 int ret;
1581
1582 dst->ver = src->ver;
1583 dst->len = src->len;
1584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1586 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1589 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1590 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1591 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1592 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1593 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1596 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1597 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1600 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001601
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001602 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001603 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001604
1605cleanup:
1606 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001608
1609 return( ret );
1610}
1611
1612/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 * Free the components of an RSA key
1614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001616{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1618 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1619 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1620 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1621 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623#if defined(MBEDTLS_THREADING_C)
1624 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001625#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001626}
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001629
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001630#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001631
1632/*
1633 * Example RSA-1024 keypair, for test purposes
1634 */
1635#define KEY_LEN 128
1636
1637#define RSA_N "9292758453063D803DD603D5E777D788" \
1638 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1639 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1640 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1641 "93A89813FBF3C4F8066D2D800F7C38A8" \
1642 "1AE31942917403FF4946B0A83D3D3E05" \
1643 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1644 "5E94BB77B07507233A0BC7BAC8F90F79"
1645
1646#define RSA_E "10001"
1647
1648#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1649 "66CA472BC44D253102F8B4A9D3BFA750" \
1650 "91386C0077937FE33FA3252D28855837" \
1651 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1652 "DF79C5CE07EE72C7F123142198164234" \
1653 "CABB724CF78B8173B9F880FC86322407" \
1654 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1655 "071513A1E85B5DFA031F21ECAE91A34D"
1656
1657#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1658 "2C01CAD19EA484A87EA4377637E75500" \
1659 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1660 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1661
1662#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1663 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1664 "910E4168387E3C30AA1E00C339A79508" \
1665 "8452DD96A9A5EA5D9DCA68DA636032AF"
1666
1667#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1668 "3C94D22288ACD763FD8E5600ED4A702D" \
1669 "F84198A5F06C2E72236AE490C93F07F8" \
1670 "3CC559CD27BC2D1CA488811730BB5725"
1671
1672#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1673 "D8AAEA56749EA28623272E4F7D0592AF" \
1674 "7C1F1313CAC9471B5C523BFE592F517B" \
1675 "407A1BD76C164B93DA2D32A383E58357"
1676
1677#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1678 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1679 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1680 "A74206CEC169D74BF5A8C50D6F48EA08"
1681
1682#define PT_LEN 24
1683#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1684 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001687static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001688{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001689#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001690 size_t i;
1691
Paul Bakker545570e2010-07-18 09:00:25 +00001692 if( rng_state != NULL )
1693 rng_state = NULL;
1694
Paul Bakkera3d195c2011-11-27 21:07:34 +00001695 for( i = 0; i < len; ++i )
1696 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001697#else
1698 if( rng_state != NULL )
1699 rng_state = NULL;
1700
1701 arc4random_buf( output, len );
1702#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001703
Paul Bakkera3d195c2011-11-27 21:07:34 +00001704 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001707
Paul Bakker5121ce52009-01-03 21:22:43 +00001708/*
1709 * Checkup routine
1710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001712{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001713 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001715 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001717 unsigned char rsa_plaintext[PT_LEN];
1718 unsigned char rsa_decrypted[PT_LEN];
1719 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001720#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001721 unsigned char sha1sum[20];
1722#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001725
1726 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1728 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1729 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1730 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1731 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1732 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1733 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1734 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001735
1736 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1740 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001741 {
1742 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001744
1745 return( 1 );
1746 }
1747
1748 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
1751 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001754 rsa_plaintext, rsa_ciphertext ) != 0 )
1755 {
1756 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001757 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001758
1759 return( 1 );
1760 }
1761
1762 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001766 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001767 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001768 {
1769 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001770 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001771
1772 return( 1 );
1773 }
1774
1775 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1776 {
1777 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001779
1780 return( 1 );
1781 }
1782
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001783 if( verbose != 0 )
1784 mbedtls_printf( "passed\n" );
1785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001787 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001788 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001792 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 sha1sum, rsa_ciphertext ) != 0 )
1794 {
1795 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
1798 return( 1 );
1799 }
1800
1801 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001804 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001805 sha1sum, rsa_ciphertext ) != 0 )
1806 {
1807 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001808 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001809
1810 return( 1 );
1811 }
1812
1813 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001814 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001816
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001817 if( verbose != 0 )
1818 mbedtls_printf( "\n" );
1819
Paul Bakker3d8fb632014-04-17 12:42:41 +02001820cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 mbedtls_rsa_free( &rsa );
1822#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001823 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001825 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001826}
1827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#endif /* MBEDTLS_RSA_C */