blob: 40ef2a9480fac22d2c506a4e9fa85184bcfe97b5 [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 *
Paul Bakker5121ce52009-01-03 21:22:43 +000032 */
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/rsa.h"
43#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000044
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000052#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010057#else
Rich Evans00ab4702015-02-06 13:43:58 +000058#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020060#define mbedtls_calloc calloc
61#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#endif
63
Paul Bakker5121ce52009-01-03 21:22:43 +000064/*
65 * Initialize an RSA context
66 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000068 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000069 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_THREADING_C)
76 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020077#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000078}
79
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010080/*
81 * Set padding for an existing RSA context
82 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010084{
85 ctx->padding = padding;
86 ctx->hash_id = hash_id;
87}
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91/*
92 * Generate an RSA keypair
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +000095 int (*f_rng)(void *, unsigned char *, size_t),
96 void *p_rng,
97 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +000098{
99 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Paul Bakker21eb2802010-08-16 11:10:02 +0000102 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Janos Follathef441782016-09-21 13:18:12 +0100105 if( nbits % 2 )
106 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
107
108 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath10c575b2016-02-23 14:42:48 +0000109 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
111 /*
112 * find primes P and Q with Q < P so that:
113 * GCD( E, (P-1)*(Q-1) ) == 1
114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
117 do
118 {
Janos Follath10c575b2016-02-23 14:42:48 +0000119 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000120 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Janos Follathef441782016-09-21 13:18:12 +0100122 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000123 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 continue;
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200129 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 continue;
131
Janos Follathef441782016-09-21 13:18:12 +0100132 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
133 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
136 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
137 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
138 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142 /*
143 * D = E^-1 mod ((P-1)*(Q-1))
144 * DP = D mod (P - 1)
145 * DQ = D mod (Q - 1)
146 * QP = Q^-1 mod P
147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
149 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
150 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
151 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200153 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
155cleanup:
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159 if( ret != 0 )
160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_rsa_free( ctx );
162 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 }
164
Paul Bakker48377d92013-08-30 12:06:24 +0200165 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166}
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170/*
171 * Check a public RSA key
172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000174{
Paul Bakker37940d92009-07-10 22:38:58 +0000175 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000177
Paul Bakker48377d92013-08-30 12:06:24 +0200178 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200182 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
183 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200186 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
188 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
190 return( 0 );
191}
192
193/*
194 * Check a private RSA key
195 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000197{
198 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 return( ret );
203
Paul Bakker37940d92009-07-10 22:38:58 +0000204 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
208 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
209 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
210 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
213 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
214 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
215 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
216 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
217 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
220 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
221 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
225 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000226 /*
227 * Check for a valid PKCS1v2 private key
228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
230 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
231 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
232 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
233 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
234 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
235 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 }
Paul Bakker48377d92013-08-30 12:06:24 +0200239
Paul Bakker5121ce52009-01-03 21:22:43 +0000240cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
242 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
243 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
244 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000247 return( ret );
248
Paul Bakker6c591fa2011-05-05 11:49:20 +0000249 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000251
252 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253}
254
255/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100256 * Check if contexts holding a public and private key match
257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100259{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
261 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100264 }
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
267 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100270 }
271
272 return( 0 );
273}
274
275/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 * Do an RSA public key operation
277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000279 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 unsigned char *output )
281{
Paul Bakker23986e52011-04-24 08:57:21 +0000282 int ret;
283 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200288#if defined(MBEDTLS_THREADING_C)
289 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
290 return( ret );
291#endif
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200297 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
298 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 }
300
301 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
303 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
305cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200307 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
308 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100309#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
313 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
316 return( 0 );
317}
318
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200319/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200320 * Generate or update blinding values, see section 10 of:
321 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200322 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200323 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200324 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200325static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200326 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
327{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200328 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200329
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200330 if( ctx->Vf.p != NULL )
331 {
332 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
334 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
335 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
336 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200337
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200338 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200339 }
340
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200341 /* Unblinding value: Vf = random number, invertible mod N */
342 do {
343 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
347 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
348 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200349
350 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
352 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 +0200353
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200354
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200355cleanup:
356 return( ret );
357}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200358
Paul Bakker5121ce52009-01-03 21:22:43 +0000359/*
360 * Do an RSA private key operation
361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200363 int (*f_rng)(void *, unsigned char *, size_t),
364 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000365 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 unsigned char *output )
367{
Paul Bakker23986e52011-04-24 08:57:21 +0000368 int ret;
369 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_mpi T, T1, T2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100372 /* Make sure we have private key info, prevent possible misuse */
373 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
374 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200378#if defined(MBEDTLS_THREADING_C)
379 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
380 return( ret );
381#endif
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
384 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200386 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
387 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 }
389
Paul Bakkerf451bac2013-08-30 15:37:02 +0200390 if( f_rng != NULL )
391 {
392 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200393 * Blinding
394 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200395 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200396 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
397 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200399 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401#if defined(MBEDTLS_RSA_NO_CRT)
402 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100403#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200404 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 * faster decryption using the CRT
406 *
407 * T1 = input ^ dP mod P
408 * T2 = input ^ dQ mod Q
409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
411 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000412
413 /*
414 * T = (T1 - T2) * (Q^-1 mod P) mod P
415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
417 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
418 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000419
420 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200421 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
424 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
425#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200426
Paul Bakkerf451bac2013-08-30 15:37:02 +0200427 if( f_rng != NULL )
428 {
429 /*
430 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200431 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200432 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200433 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200435 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000436
437 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200442 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
443 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200444#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
448 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
451 return( 0 );
452}
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000455/**
456 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
457 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000458 * \param dst buffer to mask
459 * \param dlen length of destination buffer
460 * \param src source of the mask generation
461 * \param slen length of the source buffer
462 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000463 */
Paul Bakker48377d92013-08-30 12:06:24 +0200464static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000466{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000468 unsigned char counter[4];
469 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000470 unsigned int hlen;
471 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000474 memset( counter, 0, 4 );
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000477
Simon Butcher02037452016-03-01 21:19:12 +0000478 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000479 p = dst;
480
481 while( dlen > 0 )
482 {
483 use_len = hlen;
484 if( dlen < hlen )
485 use_len = dlen;
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_md_starts( md_ctx );
488 mbedtls_md_update( md_ctx, src, slen );
489 mbedtls_md_update( md_ctx, counter, 4 );
490 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000491
492 for( i = 0; i < use_len; ++i )
493 *p++ ^= mask[i];
494
495 counter[3]++;
496
497 dlen -= use_len;
498 }
499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100503/*
504 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100507 int (*f_rng)(void *, unsigned char *, size_t),
508 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100509 int mode,
510 const unsigned char *label, size_t label_len,
511 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100512 const unsigned char *input,
513 unsigned char *output )
514{
515 size_t olen;
516 int ret;
517 unsigned char *p = output;
518 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 const mbedtls_md_info_t *md_info;
520 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
523 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200524
525 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100529 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100531
532 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100534
Simon Butcher02037452016-03-01 21:19:12 +0000535 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000536 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100538
539 memset( output, 0, olen );
540
541 *p++ = 0;
542
Simon Butcher02037452016-03-01 21:19:12 +0000543 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100544 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100546
547 p += hlen;
548
Simon Butcher02037452016-03-01 21:19:12 +0000549 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100551 p += hlen;
552 p += olen - 2 * hlen - 2 - ilen;
553 *p++ = 1;
554 memcpy( p, input, ilen );
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700557 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
558 {
559 mbedtls_md_free( &md_ctx );
560 return( ret );
561 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100562
Simon Butcher02037452016-03-01 21:19:12 +0000563 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100564 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
565 &md_ctx );
566
Simon Butcher02037452016-03-01 21:19:12 +0000567 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100568 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
569 &md_ctx );
570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 return( ( mode == MBEDTLS_RSA_PUBLIC )
574 ? mbedtls_rsa_public( ctx, output, output )
575 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100580/*
581 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100584 int (*f_rng)(void *, unsigned char *, size_t),
585 void *p_rng,
586 int mode, size_t ilen,
587 const unsigned char *input,
588 unsigned char *output )
589{
590 size_t nb_pad, olen;
591 int ret;
592 unsigned char *p = output;
593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
595 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200596
Janos Follath1ed9f992016-03-18 11:45:44 +0000597 // We don't check p_rng because it won't be dereferenced here
598 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100600
601 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100602
Simon Butcher02037452016-03-01 21:19:12 +0000603 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000604 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100606
607 nb_pad = olen - 3 - ilen;
608
609 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100613
614 while( nb_pad-- > 0 )
615 {
616 int rng_dl = 100;
617
618 do {
619 ret = f_rng( p_rng, p, 1 );
620 } while( *p == 0 && --rng_dl && ret == 0 );
621
Simon Butcher02037452016-03-01 21:19:12 +0000622 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200623 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100625
626 p++;
627 }
628 }
629 else
630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100632
633 while( nb_pad-- > 0 )
634 *p++ = 0xFF;
635 }
636
637 *p++ = 0;
638 memcpy( p, input, ilen );
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 return( ( mode == MBEDTLS_RSA_PUBLIC )
641 ? mbedtls_rsa_public( ctx, output, output )
642 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100643}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100645
Paul Bakker5121ce52009-01-03 21:22:43 +0000646/*
647 * Add the message padding, then do an RSA operation
648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000650 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000651 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000652 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000653 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 unsigned char *output )
655{
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 switch( ctx->padding )
657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#if defined(MBEDTLS_PKCS1_V15)
659 case MBEDTLS_RSA_PKCS_V15:
660 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100661 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200662#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_PKCS1_V21)
665 case MBEDTLS_RSA_PKCS_V21:
666 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100667 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000668#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000669
670 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000673}
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000676/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100677 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200680 int (*f_rng)(void *, unsigned char *, size_t),
681 void *p_rng,
682 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100683 const unsigned char *label, size_t label_len,
684 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100685 const unsigned char *input,
686 unsigned char *output,
687 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000688{
Paul Bakker23986e52011-04-24 08:57:21 +0000689 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100690 size_t ilen, i, pad_len;
691 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
693 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000694 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 const mbedtls_md_info_t *md_info;
696 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100697
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100698 /*
699 * Parameters sanity checks
700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
702 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000703
704 ilen = ctx->len;
705
Paul Bakker27fdf462011-06-09 13:55:13 +0000706 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100710 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100712
Janos Follathc17cda12016-02-11 11:08:18 +0000713 hlen = mbedtls_md_get_size( md_info );
714
715 // checking for integer underflow
716 if( 2 * hlen + 2 > ilen )
717 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
718
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100719 /*
720 * RSA operation
721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 ret = ( mode == MBEDTLS_RSA_PUBLIC )
723 ? mbedtls_rsa_public( ctx, input, buf )
724 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
726 if( ret != 0 )
727 return( ret );
728
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100729 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100730 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700733 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
734 {
735 mbedtls_md_free( &md_ctx );
736 return( ret );
737 }
738
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100739
740 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100742
743 /* seed: Apply seedMask to maskedSeed */
744 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
745 &md_ctx );
746
747 /* DB: Apply dbMask to maskedDB */
748 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
749 &md_ctx );
750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100752
753 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100754 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100755 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000756 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100757 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000758
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100759 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100760
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100761 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100762
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100763 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100764 for( i = 0; i < hlen; i++ )
765 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100766
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100767 /* Get zero-padding len, but always read till end of buffer
768 * (minus one, for the 01 byte) */
769 pad_len = 0;
770 pad_done = 0;
771 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
772 {
773 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100774 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100775 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100776
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100777 p += pad_len;
778 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100779
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100780 /*
781 * The only information "leaked" is whether the padding was correct or not
782 * (eg, no data is copied if it was not correct). This meets the
783 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
784 * the different error conditions.
785 */
786 if( bad != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100788
Paul Bakker66d5d072014-06-17 16:39:18 +0200789 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakkerb3869132013-02-28 17:21:01 +0100791
792 *olen = ilen - (p - buf);
793 memcpy( output, p, *olen );
794
795 return( 0 );
796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100800/*
801 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200804 int (*f_rng)(void *, unsigned char *, size_t),
805 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100806 int mode, size_t *olen,
807 const unsigned char *input,
808 unsigned char *output,
809 size_t output_max_len)
810{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100811 int ret;
812 size_t ilen, pad_count = 0, i;
813 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
817 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100818
819 ilen = ctx->len;
820
821 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 ret = ( mode == MBEDTLS_RSA_PUBLIC )
825 ? mbedtls_rsa_public( ctx, input, buf )
826 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100827
828 if( ret != 0 )
829 return( ret );
830
831 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100832 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100833
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100834 /*
835 * Check and get padding len in "constant-time"
836 */
837 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100838
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100839 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000843
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100844 /* Get padding len, but always read till end of buffer
845 * (minus one, for the 00 byte) */
846 for( i = 0; i < ilen - 3; i++ )
847 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100848 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
849 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100850 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000851
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100852 p += pad_count;
853 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100854 }
855 else
856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100858
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100859 /* Get padding len, but always read till end of buffer
860 * (minus one, for the 00 byte) */
861 for( i = 0; i < ilen - 3; i++ )
862 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100863 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100864 pad_count += ( pad_done == 0 );
865 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100866
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100867 p += pad_count;
868 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 }
870
Janos Follathc69fa502016-02-12 13:30:09 +0000871 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +0000872
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100873 if( bad )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker8804f692013-02-28 18:06:26 +0100875
Paul Bakker66d5d072014-06-17 16:39:18 +0200876 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000878
Paul Bakker27fdf462011-06-09 13:55:13 +0000879 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000880 memcpy( output, p, *olen );
881
882 return( 0 );
883}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000885
886/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100887 * Do an RSA operation, then remove the message padding
888 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200890 int (*f_rng)(void *, unsigned char *, size_t),
891 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100892 int mode, size_t *olen,
893 const unsigned char *input,
894 unsigned char *output,
895 size_t output_max_len)
896{
897 switch( ctx->padding )
898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899#if defined(MBEDTLS_PKCS1_V15)
900 case MBEDTLS_RSA_PKCS_V15:
901 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +0200902 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +0200903#endif
Paul Bakkerb3869132013-02-28 17:21:01 +0100904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905#if defined(MBEDTLS_PKCS1_V21)
906 case MBEDTLS_RSA_PKCS_V21:
907 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +0200908 olen, input, output,
909 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +0100910#endif
911
912 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100914 }
915}
916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100918/*
919 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100922 int (*f_rng)(void *, unsigned char *, size_t),
923 void *p_rng,
924 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100926 unsigned int hashlen,
927 const unsigned char *hash,
928 unsigned char *sig )
929{
930 size_t olen;
931 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100933 unsigned int slen, hlen, offset = 0;
934 int ret;
935 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 const mbedtls_md_info_t *md_info;
937 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
940 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200941
942 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100944
945 olen = ctx->len;
946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +0100948 {
Simon Butcher02037452016-03-01 21:19:12 +0000949 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200951 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100955 }
956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100958 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100962 slen = hlen;
963
964 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100966
967 memset( sig, 0, olen );
968
Simon Butcher02037452016-03-01 21:19:12 +0000969 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +0100970 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100972
Simon Butcher02037452016-03-01 21:19:12 +0000973 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200974 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +0100975 p += olen - hlen * 2 - 2;
976 *p++ = 0x01;
977 memcpy( p, salt, slen );
978 p += slen;
979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700981 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
982 {
983 mbedtls_md_free( &md_ctx );
984 return( ret );
985 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100986
Simon Butcher02037452016-03-01 21:19:12 +0000987 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 mbedtls_md_starts( &md_ctx );
989 mbedtls_md_update( &md_ctx, p, 8 );
990 mbedtls_md_update( &md_ctx, hash, hashlen );
991 mbedtls_md_update( &md_ctx, salt, slen );
992 mbedtls_md_finish( &md_ctx, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100993
Simon Butcher02037452016-03-01 21:19:12 +0000994 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +0100995 if( msb % 8 == 0 )
996 offset = 1;
997
Simon Butcher02037452016-03-01 21:19:12 +0000998 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100999 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001002
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001003 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001004 sig[0] &= 0xFF >> ( olen * 8 - msb );
1005
1006 p += hlen;
1007 *p++ = 0xBC;
1008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 return( ( mode == MBEDTLS_RSA_PUBLIC )
1010 ? mbedtls_rsa_public( ctx, sig, sig )
1011 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001012}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001016/*
1017 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1018 */
1019/*
1020 * Do an RSA operation to sign the message digest
1021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001023 int (*f_rng)(void *, unsigned char *, size_t),
1024 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001025 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001027 unsigned int hashlen,
1028 const unsigned char *hash,
1029 unsigned char *sig )
1030{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001031 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001032 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001033 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001034 unsigned char *sig_try = NULL, *verif = NULL;
1035 size_t i;
1036 unsigned char diff;
1037 volatile unsigned char diff_no_optimize;
1038 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1041 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001042
1043 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001044 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001049 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1053 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001054
Paul Bakkerc70b9822013-04-07 22:00:46 +02001055 nb_pad -= 10 + oid_size;
1056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001058 }
1059
Paul Bakkerc70b9822013-04-07 22:00:46 +02001060 nb_pad -= hashlen;
1061
Paul Bakkerb3869132013-02-28 17:21:01 +01001062 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001064
1065 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001067 memset( p, 0xFF, nb_pad );
1068 p += nb_pad;
1069 *p++ = 0;
1070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001072 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001073 memcpy( p, hash, hashlen );
1074 }
1075 else
1076 {
1077 /*
1078 * DigestInfo ::= SEQUENCE {
1079 * digestAlgorithm DigestAlgorithmIdentifier,
1080 * digest Digest }
1081 *
1082 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1083 *
1084 * Digest ::= OCTET STRING
1085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001087 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001089 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001091 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001092 memcpy( p, oid, oid_size );
1093 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001095 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001097 *p++ = hashlen;
1098 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001099 }
1100
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001101 if( mode == MBEDTLS_RSA_PUBLIC )
1102 return( mbedtls_rsa_public( ctx, sig, sig ) );
1103
1104 /*
1105 * In order to prevent Lenstra's attack, make the signature in a
1106 * temporary buffer and check it before returning it.
1107 */
1108 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001109 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001110 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1111
Simon Butcher1285ab52016-01-01 21:42:47 +00001112 verif = mbedtls_calloc( 1, ctx->len );
1113 if( verif == NULL )
1114 {
1115 mbedtls_free( sig_try );
1116 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1117 }
1118
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001119 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1120 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1121
1122 /* Compare in constant time just in case */
1123 for( diff = 0, i = 0; i < ctx->len; i++ )
1124 diff |= verif[i] ^ sig[i];
1125 diff_no_optimize = diff;
1126
1127 if( diff_no_optimize != 0 )
1128 {
1129 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1130 goto cleanup;
1131 }
1132
1133 memcpy( sig, sig_try, ctx->len );
1134
1135cleanup:
1136 mbedtls_free( sig_try );
1137 mbedtls_free( verif );
1138
1139 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001142
1143/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 * Do an RSA operation to sign the message digest
1145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001147 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001148 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001149 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001151 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001152 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 unsigned char *sig )
1154{
Paul Bakker5121ce52009-01-03 21:22:43 +00001155 switch( ctx->padding )
1156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_PKCS1_V15)
1158 case MBEDTLS_RSA_PKCS_V15:
1159 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001160 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001161#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163#if defined(MBEDTLS_PKCS1_V21)
1164 case MBEDTLS_RSA_PKCS_V21:
1165 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001166 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001167#endif
1168
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001172}
1173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001175/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001176 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001179 int (*f_rng)(void *, unsigned char *, size_t),
1180 void *p_rng,
1181 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001183 unsigned int hashlen,
1184 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001186 int expected_salt_len,
1187 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001188{
Paul Bakker23986e52011-04-24 08:57:21 +00001189 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001190 size_t siglen;
1191 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001193 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001194 unsigned int hlen;
1195 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 const mbedtls_md_info_t *md_info;
1197 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001198 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1201 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001202
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 siglen = ctx->len;
1204
Paul Bakker27fdf462011-06-09 13:55:13 +00001205 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1209 ? mbedtls_rsa_public( ctx, sig, buf )
1210 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001211
1212 if( ret != 0 )
1213 return( ret );
1214
1215 p = buf;
1216
Paul Bakkerb3869132013-02-28 17:21:01 +01001217 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 {
Simon Butcher02037452016-03-01 21:19:12 +00001222 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001224 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001228 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001231 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001235 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001236
Paul Bakkerb3869132013-02-28 17:21:01 +01001237 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001238
Simon Butcher02037452016-03-01 21:19:12 +00001239 /*
1240 * Note: EMSA-PSS verification is over the length of N - 1 bits
1241 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001242 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001243
Simon Butcher02037452016-03-01 21:19:12 +00001244 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001245 if( msb % 8 == 0 )
1246 {
1247 p++;
1248 siglen -= 1;
1249 }
1250 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001254 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1255 {
1256 mbedtls_md_free( &md_ctx );
1257 return( ret );
1258 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001259
Paul Bakkerb3869132013-02-28 17:21:01 +01001260 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001261
Paul Bakkerb3869132013-02-28 17:21:01 +01001262 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001263
Paul Bakker4de44aa2013-12-31 11:43:01 +01001264 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001265 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001266
Paul Bakkerb3869132013-02-28 17:21:01 +01001267 if( p == buf + siglen ||
1268 *p++ != 0x01 )
1269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 mbedtls_md_free( &md_ctx );
1271 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001272 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001273
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001274 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001275 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001278 slen != (size_t) expected_salt_len )
1279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 mbedtls_md_free( &md_ctx );
1281 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001282 }
1283
Simon Butcher02037452016-03-01 21:19:12 +00001284 /*
1285 * Generate H = Hash( M' )
1286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_md_starts( &md_ctx );
1288 mbedtls_md_update( &md_ctx, zeros, 8 );
1289 mbedtls_md_update( &md_ctx, hash, hashlen );
1290 mbedtls_md_update( &md_ctx, p, slen );
1291 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001294
Paul Bakkerb3869132013-02-28 17:21:01 +01001295 if( memcmp( p + slen, result, hlen ) == 0 )
1296 return( 0 );
1297 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001299}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001300
1301/*
1302 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001305 int (*f_rng)(void *, unsigned char *, size_t),
1306 void *p_rng,
1307 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001309 unsigned int hashlen,
1310 const unsigned char *hash,
1311 const unsigned char *sig )
1312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1314 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001315 : md_alg;
1316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001318 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001320 sig ) );
1321
1322}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001326/*
1327 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001330 int (*f_rng)(void *, unsigned char *, size_t),
1331 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001332 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001334 unsigned int hashlen,
1335 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001336 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001337{
1338 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001339 size_t len, siglen, asn1_len;
1340 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 mbedtls_md_type_t msg_md_alg;
1342 const mbedtls_md_info_t *md_info;
1343 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001344 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1347 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001348
1349 siglen = ctx->len;
1350
1351 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1355 ? mbedtls_rsa_public( ctx, sig, buf )
1356 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001357
1358 if( ret != 0 )
1359 return( ret );
1360
1361 p = buf;
1362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1364 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001365
1366 while( *p != 0 )
1367 {
1368 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001370 p++;
1371 }
1372 p++;
1373
1374 len = siglen - ( p - buf );
1375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001377 {
1378 if( memcmp( p, hash, hashlen ) == 0 )
1379 return( 0 );
1380 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001382 }
1383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001385 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1387 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001388
1389 end = p + len;
1390
Simon Butcher02037452016-03-01 21:19:12 +00001391 /*
1392 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1393 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1395 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1396 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001397
1398 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1402 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1403 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001404
1405 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1409 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001410
1411 oid.p = p;
1412 p += oid.len;
1413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1415 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001416
1417 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001419
1420 /*
1421 * assume the algorithm parameters must be NULL
1422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1424 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1427 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001428
1429 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001431
1432 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001434
1435 p += hashlen;
1436
1437 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001439
1440 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001441}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001443
1444/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001445 * Do an RSA operation and check the message digest
1446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001448 int (*f_rng)(void *, unsigned char *, size_t),
1449 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001450 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001452 unsigned int hashlen,
1453 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001454 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001455{
1456 switch( ctx->padding )
1457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#if defined(MBEDTLS_PKCS1_V15)
1459 case MBEDTLS_RSA_PKCS_V15:
1460 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001461 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001462#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464#if defined(MBEDTLS_PKCS1_V21)
1465 case MBEDTLS_RSA_PKCS_V21:
1466 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001467 hashlen, hash, sig );
1468#endif
1469
1470 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001472 }
1473}
1474
1475/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001476 * Copy the components of an RSA key
1477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001479{
1480 int ret;
1481
1482 dst->ver = src->ver;
1483 dst->len = src->len;
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1486 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1489 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1490 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1491 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1492 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1493 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1496 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1497 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1500 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001501
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001502 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001503 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001504
1505cleanup:
1506 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001508
1509 return( ret );
1510}
1511
1512/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001513 * Free the components of an RSA key
1514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001516{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1518 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1519 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1520 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1521 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#if defined(MBEDTLS_THREADING_C)
1524 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001525#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001526}
1527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001529
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001530#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001531
1532/*
1533 * Example RSA-1024 keypair, for test purposes
1534 */
1535#define KEY_LEN 128
1536
1537#define RSA_N "9292758453063D803DD603D5E777D788" \
1538 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1539 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1540 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1541 "93A89813FBF3C4F8066D2D800F7C38A8" \
1542 "1AE31942917403FF4946B0A83D3D3E05" \
1543 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1544 "5E94BB77B07507233A0BC7BAC8F90F79"
1545
1546#define RSA_E "10001"
1547
1548#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1549 "66CA472BC44D253102F8B4A9D3BFA750" \
1550 "91386C0077937FE33FA3252D28855837" \
1551 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1552 "DF79C5CE07EE72C7F123142198164234" \
1553 "CABB724CF78B8173B9F880FC86322407" \
1554 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1555 "071513A1E85B5DFA031F21ECAE91A34D"
1556
1557#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1558 "2C01CAD19EA484A87EA4377637E75500" \
1559 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1560 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1561
1562#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1563 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1564 "910E4168387E3C30AA1E00C339A79508" \
1565 "8452DD96A9A5EA5D9DCA68DA636032AF"
1566
1567#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1568 "3C94D22288ACD763FD8E5600ED4A702D" \
1569 "F84198A5F06C2E72236AE490C93F07F8" \
1570 "3CC559CD27BC2D1CA488811730BB5725"
1571
1572#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1573 "D8AAEA56749EA28623272E4F7D0592AF" \
1574 "7C1F1313CAC9471B5C523BFE592F517B" \
1575 "407A1BD76C164B93DA2D32A383E58357"
1576
1577#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1578 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1579 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1580 "A74206CEC169D74BF5A8C50D6F48EA08"
1581
1582#define PT_LEN 24
1583#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1584 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001587static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001588{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001589#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001590 size_t i;
1591
Paul Bakker545570e2010-07-18 09:00:25 +00001592 if( rng_state != NULL )
1593 rng_state = NULL;
1594
Paul Bakkera3d195c2011-11-27 21:07:34 +00001595 for( i = 0; i < len; ++i )
1596 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001597#else
1598 if( rng_state != NULL )
1599 rng_state = NULL;
1600
1601 arc4random_buf( output, len );
1602#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001603
Paul Bakkera3d195c2011-11-27 21:07:34 +00001604 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001605}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001607
Paul Bakker5121ce52009-01-03 21:22:43 +00001608/*
1609 * Checkup routine
1610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001612{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001613 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001615 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 unsigned char rsa_plaintext[PT_LEN];
1618 unsigned char rsa_decrypted[PT_LEN];
1619 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001621 unsigned char sha1sum[20];
1622#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001625
1626 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1628 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1629 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1630 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1631 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1632 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1633 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1634 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
1636 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1640 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001641 {
1642 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
1645 return( 1 );
1646 }
1647
1648 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
1651 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001654 rsa_plaintext, rsa_ciphertext ) != 0 )
1655 {
1656 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
1659 return( 1 );
1660 }
1661
1662 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001666 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001667 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001668 {
1669 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
1672 return( 1 );
1673 }
1674
1675 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1676 {
1677 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001679
1680 return( 1 );
1681 }
1682
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001683 if( verbose != 0 )
1684 mbedtls_printf( "passed\n" );
1685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001687 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001688 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001693 sha1sum, rsa_ciphertext ) != 0 )
1694 {
1695 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
1698 return( 1 );
1699 }
1700
1701 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001705 sha1sum, rsa_ciphertext ) != 0 )
1706 {
1707 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001709
1710 return( 1 );
1711 }
1712
1713 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001714 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001716
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001717 if( verbose != 0 )
1718 mbedtls_printf( "\n" );
1719
Paul Bakker3d8fb632014-04-17 12:42:41 +02001720cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 mbedtls_rsa_free( &rsa );
1722#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001723 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001725 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001726}
1727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#endif /* MBEDTLS_RSA_C */