blob: e29979bad38a349411349a0e768734705c1a2817 [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 Follathe81102e2017-03-22 13:38:28 +0000397#if defined(MBEDTLS_RSA_NO_CRT)
398 mbedtls_mpi P1, Q1;
399 mbedtls_mpi D_blind, R;
400 mbedtls_mpi *D = &ctx->D;
401#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000402
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100403 /* Make sure we have private key info, prevent possible misuse */
404 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
405 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Janos Follathe81102e2017-03-22 13:38:28 +0000408#if defined(MBEDTLS_RSA_NO_CRT)
409 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
410 mbedtls_mpi_init( &R ); mbedtls_mpi_init( &D_blind );
411#endif
412
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200414#if defined(MBEDTLS_THREADING_C)
415 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
416 return( ret );
417#endif
418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
420 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200422 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
423 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 }
425
Paul Bakkerf451bac2013-08-30 15:37:02 +0200426 if( f_rng != NULL )
427 {
428 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200429 * Blinding
430 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200431 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200432 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
433 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000435
436#if defined(MBEDTLS_RSA_NO_CRT)
437 /*
438 * Exponent blinding
439 */
440 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
441 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
442
443 /*
444 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
445 */
446 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
447 f_rng, p_rng ) );
448 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
449 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
450 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
451
452 D = &D_blind;
453#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200454 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000457 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100458#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200459 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000460 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000461 *
462 * T1 = input ^ dP mod P
463 * T2 = input ^ dQ mod Q
464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
466 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468 /*
469 * T = (T1 - T2) * (Q^-1 mod P) mod P
470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
472 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
473 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200476 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
479 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
480#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200481
Paul Bakkerf451bac2013-08-30 15:37:02 +0200482 if( f_rng != NULL )
483 {
484 /*
485 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200486 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200487 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200488 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200490 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000491
492 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000494
495cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200497 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
498 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200499#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Janos Follathe81102e2017-03-22 13:38:28 +0000502#if defined(MBEDTLS_RSA_NO_CRT)
503 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
504 mbedtls_mpi_free( &R ); mbedtls_mpi_free( &D_blind );
505#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000506
507 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510 return( 0 );
511}
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000514/**
515 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
516 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000517 * \param dst buffer to mask
518 * \param dlen length of destination buffer
519 * \param src source of the mask generation
520 * \param slen length of the source buffer
521 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000522 */
Paul Bakker48377d92013-08-30 12:06:24 +0200523static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000525{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000527 unsigned char counter[4];
528 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000529 unsigned int hlen;
530 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000533 memset( counter, 0, 4 );
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000536
Simon Butcher02037452016-03-01 21:19:12 +0000537 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000538 p = dst;
539
540 while( dlen > 0 )
541 {
542 use_len = hlen;
543 if( dlen < hlen )
544 use_len = dlen;
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_md_starts( md_ctx );
547 mbedtls_md_update( md_ctx, src, slen );
548 mbedtls_md_update( md_ctx, counter, 4 );
549 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000550
551 for( i = 0; i < use_len; ++i )
552 *p++ ^= mask[i];
553
554 counter[3]++;
555
556 dlen -= use_len;
557 }
558}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100562/*
563 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100566 int (*f_rng)(void *, unsigned char *, size_t),
567 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100568 int mode,
569 const unsigned char *label, size_t label_len,
570 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100571 const unsigned char *input,
572 unsigned char *output )
573{
574 size_t olen;
575 int ret;
576 unsigned char *p = output;
577 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 const mbedtls_md_info_t *md_info;
579 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
582 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200583
584 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100588 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100590
591 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100593
Simon Butcher02037452016-03-01 21:19:12 +0000594 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000595 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100597
598 memset( output, 0, olen );
599
600 *p++ = 0;
601
Simon Butcher02037452016-03-01 21:19:12 +0000602 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100603 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100605
606 p += hlen;
607
Simon Butcher02037452016-03-01 21:19:12 +0000608 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100610 p += hlen;
611 p += olen - 2 * hlen - 2 - ilen;
612 *p++ = 1;
613 memcpy( p, input, ilen );
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700616 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
617 {
618 mbedtls_md_free( &md_ctx );
619 return( ret );
620 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100621
Simon Butcher02037452016-03-01 21:19:12 +0000622 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100623 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
624 &md_ctx );
625
Simon Butcher02037452016-03-01 21:19:12 +0000626 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100627 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
628 &md_ctx );
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 return( ( mode == MBEDTLS_RSA_PUBLIC )
633 ? mbedtls_rsa_public( ctx, output, output )
634 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100635}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100639/*
640 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100643 int (*f_rng)(void *, unsigned char *, size_t),
644 void *p_rng,
645 int mode, size_t ilen,
646 const unsigned char *input,
647 unsigned char *output )
648{
649 size_t nb_pad, olen;
650 int ret;
651 unsigned char *p = output;
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
654 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200655
Janos Follath1ed9f992016-03-18 11:45:44 +0000656 // We don't check p_rng because it won't be dereferenced here
657 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100659
660 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100661
Simon Butcher02037452016-03-01 21:19:12 +0000662 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000663 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100665
666 nb_pad = olen - 3 - ilen;
667
668 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100672
673 while( nb_pad-- > 0 )
674 {
675 int rng_dl = 100;
676
677 do {
678 ret = f_rng( p_rng, p, 1 );
679 } while( *p == 0 && --rng_dl && ret == 0 );
680
Simon Butcher02037452016-03-01 21:19:12 +0000681 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200682 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100684
685 p++;
686 }
687 }
688 else
689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100691
692 while( nb_pad-- > 0 )
693 *p++ = 0xFF;
694 }
695
696 *p++ = 0;
697 memcpy( p, input, ilen );
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 return( ( mode == MBEDTLS_RSA_PUBLIC )
700 ? mbedtls_rsa_public( ctx, output, output )
701 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100702}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100704
Paul Bakker5121ce52009-01-03 21:22:43 +0000705/*
706 * Add the message padding, then do an RSA operation
707 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000709 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000710 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000711 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000712 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 unsigned char *output )
714{
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 switch( ctx->padding )
716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#if defined(MBEDTLS_PKCS1_V15)
718 case MBEDTLS_RSA_PKCS_V15:
719 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100720 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200721#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#if defined(MBEDTLS_PKCS1_V21)
724 case MBEDTLS_RSA_PKCS_V21:
725 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100726 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000727#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
729 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000732}
733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000735/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100736 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200739 int (*f_rng)(void *, unsigned char *, size_t),
740 void *p_rng,
741 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100742 const unsigned char *label, size_t label_len,
743 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100744 const unsigned char *input,
745 unsigned char *output,
746 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000747{
Paul Bakker23986e52011-04-24 08:57:21 +0000748 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100749 size_t ilen, i, pad_len;
750 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
752 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000753 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 const mbedtls_md_info_t *md_info;
755 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100756
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100757 /*
758 * Parameters sanity checks
759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
761 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000762
763 ilen = ctx->len;
764
Paul Bakker27fdf462011-06-09 13:55:13 +0000765 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100769 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100771
Janos Follathc17cda12016-02-11 11:08:18 +0000772 hlen = mbedtls_md_get_size( md_info );
773
774 // checking for integer underflow
775 if( 2 * hlen + 2 > ilen )
776 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
777
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100778 /*
779 * RSA operation
780 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 ret = ( mode == MBEDTLS_RSA_PUBLIC )
782 ? mbedtls_rsa_public( ctx, input, buf )
783 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000784
785 if( ret != 0 )
786 return( ret );
787
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100788 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100789 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700792 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
793 {
794 mbedtls_md_free( &md_ctx );
795 return( ret );
796 }
797
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100798
799 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100801
802 /* seed: Apply seedMask to maskedSeed */
803 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
804 &md_ctx );
805
806 /* DB: Apply dbMask to maskedDB */
807 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
808 &md_ctx );
809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100811
812 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100813 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100814 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000815 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100816 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000817
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100818 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100819
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100820 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100821
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100822 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100823 for( i = 0; i < hlen; i++ )
824 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100825
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100826 /* Get zero-padding len, but always read till end of buffer
827 * (minus one, for the 01 byte) */
828 pad_len = 0;
829 pad_done = 0;
830 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
831 {
832 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100833 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100834 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100835
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100836 p += pad_len;
837 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100838
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100839 /*
840 * The only information "leaked" is whether the padding was correct or not
841 * (eg, no data is copied if it was not correct). This meets the
842 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
843 * the different error conditions.
844 */
845 if( bad != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100847
Paul Bakker66d5d072014-06-17 16:39:18 +0200848 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakkerb3869132013-02-28 17:21:01 +0100850
851 *olen = ilen - (p - buf);
852 memcpy( output, p, *olen );
853
854 return( 0 );
855}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100859/*
860 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200863 int (*f_rng)(void *, unsigned char *, size_t),
864 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100865 int mode, size_t *olen,
866 const unsigned char *input,
867 unsigned char *output,
868 size_t output_max_len)
869{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100870 int ret;
871 size_t ilen, pad_count = 0, i;
872 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
876 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100877
878 ilen = ctx->len;
879
880 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 ret = ( mode == MBEDTLS_RSA_PUBLIC )
884 ? mbedtls_rsa_public( ctx, input, buf )
885 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100886
887 if( ret != 0 )
888 return( ret );
889
890 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100891 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100892
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100893 /*
894 * Check and get padding len in "constant-time"
895 */
896 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100897
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100898 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100903 /* Get padding len, but always read till end of buffer
904 * (minus one, for the 00 byte) */
905 for( i = 0; i < ilen - 3; i++ )
906 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100907 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
908 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100909 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000910
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100911 p += pad_count;
912 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100913 }
914 else
915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100917
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100918 /* Get padding len, but always read till end of buffer
919 * (minus one, for the 00 byte) */
920 for( i = 0; i < ilen - 3; i++ )
921 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100922 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100923 pad_count += ( pad_done == 0 );
924 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100925
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100926 p += pad_count;
927 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +0000928 }
929
Janos Follathc69fa502016-02-12 13:30:09 +0000930 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +0000931
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100932 if( bad )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker8804f692013-02-28 18:06:26 +0100934
Paul Bakker66d5d072014-06-17 16:39:18 +0200935 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000937
Paul Bakker27fdf462011-06-09 13:55:13 +0000938 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000939 memcpy( output, p, *olen );
940
941 return( 0 );
942}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000944
945/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100946 * Do an RSA operation, then remove the message padding
947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200949 int (*f_rng)(void *, unsigned char *, size_t),
950 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100951 int mode, size_t *olen,
952 const unsigned char *input,
953 unsigned char *output,
954 size_t output_max_len)
955{
956 switch( ctx->padding )
957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958#if defined(MBEDTLS_PKCS1_V15)
959 case MBEDTLS_RSA_PKCS_V15:
960 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +0200961 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +0200962#endif
Paul Bakkerb3869132013-02-28 17:21:01 +0100963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964#if defined(MBEDTLS_PKCS1_V21)
965 case MBEDTLS_RSA_PKCS_V21:
966 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +0200967 olen, input, output,
968 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +0100969#endif
970
971 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100973 }
974}
975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100977/*
978 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100981 int (*f_rng)(void *, unsigned char *, size_t),
982 void *p_rng,
983 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100985 unsigned int hashlen,
986 const unsigned char *hash,
987 unsigned char *sig )
988{
989 size_t olen;
990 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100992 unsigned int slen, hlen, offset = 0;
993 int ret;
994 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 const mbedtls_md_info_t *md_info;
996 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
999 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001000
1001 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001003
1004 olen = ctx->len;
1005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001007 {
Simon Butcher02037452016-03-01 21:19:12 +00001008 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001010 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001014 }
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001017 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001021 slen = hlen;
1022
1023 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001025
1026 memset( sig, 0, olen );
1027
Simon Butcher02037452016-03-01 21:19:12 +00001028 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001029 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001031
Simon Butcher02037452016-03-01 21:19:12 +00001032 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001033 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001034 p += olen - hlen * 2 - 2;
1035 *p++ = 0x01;
1036 memcpy( p, salt, slen );
1037 p += slen;
1038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001040 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1041 {
1042 mbedtls_md_free( &md_ctx );
1043 return( ret );
1044 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001045
Simon Butcher02037452016-03-01 21:19:12 +00001046 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_md_starts( &md_ctx );
1048 mbedtls_md_update( &md_ctx, p, 8 );
1049 mbedtls_md_update( &md_ctx, hash, hashlen );
1050 mbedtls_md_update( &md_ctx, salt, slen );
1051 mbedtls_md_finish( &md_ctx, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001052
Simon Butcher02037452016-03-01 21:19:12 +00001053 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001054 if( msb % 8 == 0 )
1055 offset = 1;
1056
Simon Butcher02037452016-03-01 21:19:12 +00001057 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001058 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001061
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001062 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001063 sig[0] &= 0xFF >> ( olen * 8 - msb );
1064
1065 p += hlen;
1066 *p++ = 0xBC;
1067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 return( ( mode == MBEDTLS_RSA_PUBLIC )
1069 ? mbedtls_rsa_public( ctx, sig, sig )
1070 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001071}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001075/*
1076 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1077 */
1078/*
1079 * Do an RSA operation to sign the message digest
1080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001082 int (*f_rng)(void *, unsigned char *, size_t),
1083 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001084 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001086 unsigned int hashlen,
1087 const unsigned char *hash,
1088 unsigned char *sig )
1089{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001090 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001091 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001092 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001093 unsigned char *sig_try = NULL, *verif = NULL;
1094 size_t i;
1095 unsigned char diff;
1096 volatile unsigned char diff_no_optimize;
1097 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1100 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001101
1102 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001103 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001108 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1112 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001113
Paul Bakkerc70b9822013-04-07 22:00:46 +02001114 nb_pad -= 10 + oid_size;
1115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001117 }
1118
Paul Bakkerc70b9822013-04-07 22:00:46 +02001119 nb_pad -= hashlen;
1120
Paul Bakkerb3869132013-02-28 17:21:01 +01001121 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001123
1124 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001126 memset( p, 0xFF, nb_pad );
1127 p += nb_pad;
1128 *p++ = 0;
1129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001131 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001132 memcpy( p, hash, hashlen );
1133 }
1134 else
1135 {
1136 /*
1137 * DigestInfo ::= SEQUENCE {
1138 * digestAlgorithm DigestAlgorithmIdentifier,
1139 * digest Digest }
1140 *
1141 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1142 *
1143 * Digest ::= OCTET STRING
1144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001146 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001148 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001150 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001151 memcpy( p, oid, oid_size );
1152 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001154 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001156 *p++ = hashlen;
1157 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 }
1159
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001160 if( mode == MBEDTLS_RSA_PUBLIC )
1161 return( mbedtls_rsa_public( ctx, sig, sig ) );
1162
1163 /*
1164 * In order to prevent Lenstra's attack, make the signature in a
1165 * temporary buffer and check it before returning it.
1166 */
1167 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001168 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001169 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1170
Simon Butcher1285ab52016-01-01 21:42:47 +00001171 verif = mbedtls_calloc( 1, ctx->len );
1172 if( verif == NULL )
1173 {
1174 mbedtls_free( sig_try );
1175 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1176 }
1177
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001178 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1179 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1180
1181 /* Compare in constant time just in case */
1182 for( diff = 0, i = 0; i < ctx->len; i++ )
1183 diff |= verif[i] ^ sig[i];
1184 diff_no_optimize = diff;
1185
1186 if( diff_no_optimize != 0 )
1187 {
1188 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1189 goto cleanup;
1190 }
1191
1192 memcpy( sig, sig_try, ctx->len );
1193
1194cleanup:
1195 mbedtls_free( sig_try );
1196 mbedtls_free( verif );
1197
1198 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001199}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001201
1202/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 * Do an RSA operation to sign the message digest
1204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001206 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001207 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001210 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001211 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001212 unsigned char *sig )
1213{
Paul Bakker5121ce52009-01-03 21:22:43 +00001214 switch( ctx->padding )
1215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216#if defined(MBEDTLS_PKCS1_V15)
1217 case MBEDTLS_RSA_PKCS_V15:
1218 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001219 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001220#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#if defined(MBEDTLS_PKCS1_V21)
1223 case MBEDTLS_RSA_PKCS_V21:
1224 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001225 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001226#endif
1227
Paul Bakker5121ce52009-01-03 21:22:43 +00001228 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001230 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001231}
1232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001234/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001235 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001238 int (*f_rng)(void *, unsigned char *, size_t),
1239 void *p_rng,
1240 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001242 unsigned int hashlen,
1243 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001245 int expected_salt_len,
1246 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001247{
Paul Bakker23986e52011-04-24 08:57:21 +00001248 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001249 size_t siglen;
1250 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001252 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001253 unsigned int hlen;
1254 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 const mbedtls_md_info_t *md_info;
1256 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001257 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1260 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001261
Paul Bakker5121ce52009-01-03 21:22:43 +00001262 siglen = ctx->len;
1263
Paul Bakker27fdf462011-06-09 13:55:13 +00001264 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1268 ? mbedtls_rsa_public( ctx, sig, buf )
1269 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001270
1271 if( ret != 0 )
1272 return( ret );
1273
1274 p = buf;
1275
Paul Bakkerb3869132013-02-28 17:21:01 +01001276 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001280 {
Simon Butcher02037452016-03-01 21:19:12 +00001281 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001283 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001287 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001290 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001294 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001295
Paul Bakkerb3869132013-02-28 17:21:01 +01001296 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001297
Simon Butcher02037452016-03-01 21:19:12 +00001298 /*
1299 * Note: EMSA-PSS verification is over the length of N - 1 bits
1300 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001301 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001302
Simon Butcher02037452016-03-01 21:19:12 +00001303 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001304 if( msb % 8 == 0 )
1305 {
1306 p++;
1307 siglen -= 1;
1308 }
1309 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001313 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1314 {
1315 mbedtls_md_free( &md_ctx );
1316 return( ret );
1317 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001318
Paul Bakkerb3869132013-02-28 17:21:01 +01001319 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001320
Paul Bakkerb3869132013-02-28 17:21:01 +01001321 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001322
Paul Bakker4de44aa2013-12-31 11:43:01 +01001323 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001324 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001325
Paul Bakkerb3869132013-02-28 17:21:01 +01001326 if( p == buf + siglen ||
1327 *p++ != 0x01 )
1328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 mbedtls_md_free( &md_ctx );
1330 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001331 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001332
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001333 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001334 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001337 slen != (size_t) expected_salt_len )
1338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 mbedtls_md_free( &md_ctx );
1340 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001341 }
1342
Simon Butcher02037452016-03-01 21:19:12 +00001343 /*
1344 * Generate H = Hash( M' )
1345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 mbedtls_md_starts( &md_ctx );
1347 mbedtls_md_update( &md_ctx, zeros, 8 );
1348 mbedtls_md_update( &md_ctx, hash, hashlen );
1349 mbedtls_md_update( &md_ctx, p, slen );
1350 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001353
Paul Bakkerb3869132013-02-28 17:21:01 +01001354 if( memcmp( p + slen, result, hlen ) == 0 )
1355 return( 0 );
1356 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001358}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001359
1360/*
1361 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001364 int (*f_rng)(void *, unsigned char *, size_t),
1365 void *p_rng,
1366 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001368 unsigned int hashlen,
1369 const unsigned char *hash,
1370 const unsigned char *sig )
1371{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1373 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001374 : md_alg;
1375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001377 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001379 sig ) );
1380
1381}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001385/*
1386 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001389 int (*f_rng)(void *, unsigned char *, size_t),
1390 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001391 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001393 unsigned int hashlen,
1394 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001395 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001396{
1397 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001398 size_t len, siglen, asn1_len;
1399 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 mbedtls_md_type_t msg_md_alg;
1401 const mbedtls_md_info_t *md_info;
1402 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001403 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1406 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001407
1408 siglen = ctx->len;
1409
1410 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1414 ? mbedtls_rsa_public( ctx, sig, buf )
1415 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001416
1417 if( ret != 0 )
1418 return( ret );
1419
1420 p = buf;
1421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1423 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001424
1425 while( *p != 0 )
1426 {
1427 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001429 p++;
1430 }
1431 p++;
1432
1433 len = siglen - ( p - buf );
1434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001436 {
1437 if( memcmp( p, hash, hashlen ) == 0 )
1438 return( 0 );
1439 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001441 }
1442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001444 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1446 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001447
1448 end = p + len;
1449
Simon Butcher02037452016-03-01 21:19:12 +00001450 /*
1451 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1454 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1455 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001456
1457 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1461 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1462 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001463
1464 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1468 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001469
1470 oid.p = p;
1471 p += oid.len;
1472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1474 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001475
1476 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001478
1479 /*
1480 * assume the algorithm parameters must be NULL
1481 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1483 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1486 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001487
1488 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001490
1491 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001493
1494 p += hashlen;
1495
1496 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001498
1499 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001500}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001502
1503/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001504 * Do an RSA operation and check the message digest
1505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001507 int (*f_rng)(void *, unsigned char *, size_t),
1508 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001509 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001511 unsigned int hashlen,
1512 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001513 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001514{
1515 switch( ctx->padding )
1516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517#if defined(MBEDTLS_PKCS1_V15)
1518 case MBEDTLS_RSA_PKCS_V15:
1519 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001520 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001521#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#if defined(MBEDTLS_PKCS1_V21)
1524 case MBEDTLS_RSA_PKCS_V21:
1525 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001526 hashlen, hash, sig );
1527#endif
1528
1529 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001531 }
1532}
1533
1534/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001535 * Copy the components of an RSA key
1536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001538{
1539 int ret;
1540
1541 dst->ver = src->ver;
1542 dst->len = src->len;
1543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1545 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1548 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1549 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1550 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1551 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1552 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1555 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1556 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1559 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001560
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001561 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001562 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001563
1564cleanup:
1565 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001567
1568 return( ret );
1569}
1570
1571/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001572 * Free the components of an RSA key
1573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001575{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1577 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1578 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1579 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1580 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#if defined(MBEDTLS_THREADING_C)
1583 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001584#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001585}
1586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001589#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001590
1591/*
1592 * Example RSA-1024 keypair, for test purposes
1593 */
1594#define KEY_LEN 128
1595
1596#define RSA_N "9292758453063D803DD603D5E777D788" \
1597 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1598 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1599 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1600 "93A89813FBF3C4F8066D2D800F7C38A8" \
1601 "1AE31942917403FF4946B0A83D3D3E05" \
1602 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1603 "5E94BB77B07507233A0BC7BAC8F90F79"
1604
1605#define RSA_E "10001"
1606
1607#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1608 "66CA472BC44D253102F8B4A9D3BFA750" \
1609 "91386C0077937FE33FA3252D28855837" \
1610 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1611 "DF79C5CE07EE72C7F123142198164234" \
1612 "CABB724CF78B8173B9F880FC86322407" \
1613 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1614 "071513A1E85B5DFA031F21ECAE91A34D"
1615
1616#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1617 "2C01CAD19EA484A87EA4377637E75500" \
1618 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1619 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1620
1621#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1622 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1623 "910E4168387E3C30AA1E00C339A79508" \
1624 "8452DD96A9A5EA5D9DCA68DA636032AF"
1625
1626#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1627 "3C94D22288ACD763FD8E5600ED4A702D" \
1628 "F84198A5F06C2E72236AE490C93F07F8" \
1629 "3CC559CD27BC2D1CA488811730BB5725"
1630
1631#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1632 "D8AAEA56749EA28623272E4F7D0592AF" \
1633 "7C1F1313CAC9471B5C523BFE592F517B" \
1634 "407A1BD76C164B93DA2D32A383E58357"
1635
1636#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1637 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1638 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1639 "A74206CEC169D74BF5A8C50D6F48EA08"
1640
1641#define PT_LEN 24
1642#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1643 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001646static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001647{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001648#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001649 size_t i;
1650
Paul Bakker545570e2010-07-18 09:00:25 +00001651 if( rng_state != NULL )
1652 rng_state = NULL;
1653
Paul Bakkera3d195c2011-11-27 21:07:34 +00001654 for( i = 0; i < len; ++i )
1655 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001656#else
1657 if( rng_state != NULL )
1658 rng_state = NULL;
1659
1660 arc4random_buf( output, len );
1661#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001662
Paul Bakkera3d195c2011-11-27 21:07:34 +00001663 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001664}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001666
Paul Bakker5121ce52009-01-03 21:22:43 +00001667/*
1668 * Checkup routine
1669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001671{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001672 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001674 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001676 unsigned char rsa_plaintext[PT_LEN];
1677 unsigned char rsa_decrypted[PT_LEN];
1678 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001680 unsigned char sha1sum[20];
1681#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
1685 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1687 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1688 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1689 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1690 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1691 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1692 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1693 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
1695 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1699 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001700 {
1701 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
1704 return( 1 );
1705 }
1706
1707 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001709
1710 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001713 rsa_plaintext, rsa_ciphertext ) != 0 )
1714 {
1715 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001717
1718 return( 1 );
1719 }
1720
1721 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001725 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001726 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 {
1728 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001730
1731 return( 1 );
1732 }
1733
1734 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1735 {
1736 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001738
1739 return( 1 );
1740 }
1741
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001742 if( verbose != 0 )
1743 mbedtls_printf( "passed\n" );
1744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001746 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001747 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001751 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001752 sha1sum, rsa_ciphertext ) != 0 )
1753 {
1754 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001756
1757 return( 1 );
1758 }
1759
1760 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001763 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001764 sha1sum, rsa_ciphertext ) != 0 )
1765 {
1766 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001768
1769 return( 1 );
1770 }
1771
1772 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001773 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001775
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001776 if( verbose != 0 )
1777 mbedtls_printf( "\n" );
1778
Paul Bakker3d8fb632014-04-17 12:42:41 +02001779cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001780 mbedtls_rsa_free( &rsa );
1781#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001782 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001784 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001785}
1786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001787#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789#endif /* MBEDTLS_RSA_C */