blob: 79f86c3065eef5252426a08ba580c3f17a222737 [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 Follath10c575b2016-02-23 14:42:48 +0000105 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
106 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 /*
109 * find primes P and Q with Q < P so that:
110 * GCD( E, (P-1)*(Q-1) ) == 1
111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114 do
115 {
Janos Follath10c575b2016-02-23 14:42:48 +0000116 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000117 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Janos Follath10c575b2016-02-23 14:42:48 +0000119 if( nbits % 2 )
Simon Butcher3f5c8752016-04-15 19:06:59 +0100120 {
Janos Follath10c575b2016-02-23 14:42:48 +0000121 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, ( nbits >> 1 ) + 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000122 f_rng, p_rng ) );
Simon Butcher3f5c8752016-04-15 19:06:59 +0100123 }
Janos Follath10c575b2016-02-23 14:42:48 +0000124 else
Simon Butcher3f5c8752016-04-15 19:06:59 +0100125 {
Janos Follath10c575b2016-02-23 14:42:48 +0000126 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
127 f_rng, p_rng ) );
Simon Butcher3f5c8752016-04-15 19:06:59 +0100128 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 continue;
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200134 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 continue;
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
138 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
139 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
140 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144 /*
145 * D = E^-1 mod ((P-1)*(Q-1))
146 * DP = D mod (P - 1)
147 * DQ = D mod (Q - 1)
148 * QP = Q^-1 mod P
149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
151 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
152 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
153 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200155 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157cleanup:
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161 if( ret != 0 )
162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_rsa_free( ctx );
164 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 }
166
Paul Bakker48377d92013-08-30 12:06:24 +0200167 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168}
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172/*
173 * Check a public RSA key
174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000176{
Paul Bakker37940d92009-07-10 22:38:58 +0000177 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000179
Paul Bakker48377d92013-08-30 12:06:24 +0200180 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200184 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
185 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200188 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
190 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
192 return( 0 );
193}
194
195/*
196 * Check a private RSA key
197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000199{
200 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 return( ret );
205
Paul Bakker37940d92009-07-10 22:38:58 +0000206 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
210 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
211 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
212 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
215 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
216 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
217 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
218 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
219 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
222 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
223 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
226 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
227 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000228 /*
229 * Check for a valid PKCS1v2 private key
230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
232 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
233 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
234 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
235 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
236 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
237 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 }
Paul Bakker48377d92013-08-30 12:06:24 +0200241
Paul Bakker5121ce52009-01-03 21:22:43 +0000242cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
244 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
245 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
246 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000249 return( ret );
250
Paul Bakker6c591fa2011-05-05 11:49:20 +0000251 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000253
254 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255}
256
257/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100258 * Check if contexts holding a public and private key match
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
263 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100266 }
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
269 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100272 }
273
274 return( 0 );
275}
276
277/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 * Do an RSA public key operation
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000281 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 unsigned char *output )
283{
Paul Bakker23986e52011-04-24 08:57:21 +0000284 int ret;
285 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200290#if defined(MBEDTLS_THREADING_C)
291 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
292 return( ret );
293#endif
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200299 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
300 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 }
302
303 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
305 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200309 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
310 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100311#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
315 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318 return( 0 );
319}
320
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200321/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200322 * Generate or update blinding values, see section 10 of:
323 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200324 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200325 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200326 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200327static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200328 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
329{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200330 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200331
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200332 if( ctx->Vf.p != NULL )
333 {
334 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
336 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
337 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
338 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200339
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200340 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200341 }
342
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200343 /* Unblinding value: Vf = random number, invertible mod N */
344 do {
345 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
349 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
350 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200351
352 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
354 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 +0200355
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200356
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200357cleanup:
358 return( ret );
359}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200360
Paul Bakker5121ce52009-01-03 21:22:43 +0000361/*
362 * Do an RSA private key operation
363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200365 int (*f_rng)(void *, unsigned char *, size_t),
366 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000367 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 unsigned char *output )
369{
Paul Bakker23986e52011-04-24 08:57:21 +0000370 int ret;
371 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_mpi T, T1, T2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100374 /* Make sure we have private key info, prevent possible misuse */
375 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
376 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200380#if defined(MBEDTLS_THREADING_C)
381 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
382 return( ret );
383#endif
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
386 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200388 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
389 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 }
391
Paul Bakkerf451bac2013-08-30 15:37:02 +0200392 if( f_rng != NULL )
393 {
394 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200395 * Blinding
396 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200397 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200398 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
399 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200401 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#if defined(MBEDTLS_RSA_NO_CRT)
404 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100405#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200406 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 * faster decryption using the CRT
408 *
409 * T1 = input ^ dP mod P
410 * T2 = input ^ dQ mod Q
411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
413 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415 /*
416 * T = (T1 - T2) * (Q^-1 mod P) mod P
417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
419 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
420 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
422 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200423 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
426 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
427#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200428
Paul Bakkerf451bac2013-08-30 15:37:02 +0200429 if( f_rng != NULL )
430 {
431 /*
432 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200433 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200434 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200435 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200437 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441
442cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200444 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
445 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200446#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
450 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
453 return( 0 );
454}
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000457/**
458 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
459 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000460 * \param dst buffer to mask
461 * \param dlen length of destination buffer
462 * \param src source of the mask generation
463 * \param slen length of the source buffer
464 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000465 */
Paul Bakker48377d92013-08-30 12:06:24 +0200466static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000468{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000470 unsigned char counter[4];
471 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000472 unsigned int hlen;
473 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000476 memset( counter, 0, 4 );
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000479
480 // Generate and apply dbMask
481 //
482 p = dst;
483
484 while( dlen > 0 )
485 {
486 use_len = hlen;
487 if( dlen < hlen )
488 use_len = dlen;
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_md_starts( md_ctx );
491 mbedtls_md_update( md_ctx, src, slen );
492 mbedtls_md_update( md_ctx, counter, 4 );
493 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000494
495 for( i = 0; i < use_len; ++i )
496 *p++ ^= mask[i];
497
498 counter[3]++;
499
500 dlen -= use_len;
501 }
502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100506/*
507 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100510 int (*f_rng)(void *, unsigned char *, size_t),
511 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100512 int mode,
513 const unsigned char *label, size_t label_len,
514 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100515 const unsigned char *input,
516 unsigned char *output )
517{
518 size_t olen;
519 int ret;
520 unsigned char *p = output;
521 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 const mbedtls_md_info_t *md_info;
523 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
526 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200527
528 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100532 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100534
535 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100537
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200538 if( olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100540
541 memset( output, 0, olen );
542
543 *p++ = 0;
544
545 // Generate a random octet string seed
546 //
547 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100549
550 p += hlen;
551
552 // Construct DB
553 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100555 p += hlen;
556 p += olen - 2 * hlen - 2 - ilen;
557 *p++ = 1;
558 memcpy( p, input, ilen );
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_md_init( &md_ctx );
561 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +0100562
563 // maskedDB: Apply dbMask to DB
564 //
565 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
566 &md_ctx );
567
568 // maskedSeed: Apply seedMask to seed
569 //
570 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
571 &md_ctx );
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 return( ( mode == MBEDTLS_RSA_PUBLIC )
576 ? mbedtls_rsa_public( ctx, output, output )
577 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100578}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100582/*
583 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100586 int (*f_rng)(void *, unsigned char *, size_t),
587 void *p_rng,
588 int mode, size_t ilen,
589 const unsigned char *input,
590 unsigned char *output )
591{
592 size_t nb_pad, olen;
593 int ret;
594 unsigned char *p = output;
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
597 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200598
Janos Follath1ed9f992016-03-18 11:45:44 +0000599 // We don't check p_rng because it won't be dereferenced here
600 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100602
603 olen = ctx->len;
604
605 if( olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100607
608 nb_pad = olen - 3 - ilen;
609
610 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100614
615 while( nb_pad-- > 0 )
616 {
617 int rng_dl = 100;
618
619 do {
620 ret = f_rng( p_rng, p, 1 );
621 } while( *p == 0 && --rng_dl && ret == 0 );
622
623 // Check if RNG failed to generate data
624 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200625 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100627
628 p++;
629 }
630 }
631 else
632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100634
635 while( nb_pad-- > 0 )
636 *p++ = 0xFF;
637 }
638
639 *p++ = 0;
640 memcpy( p, input, ilen );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 return( ( mode == MBEDTLS_RSA_PUBLIC )
643 ? mbedtls_rsa_public( ctx, output, output )
644 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100645}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100647
Paul Bakker5121ce52009-01-03 21:22:43 +0000648/*
649 * Add the message padding, then do an RSA operation
650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000652 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000653 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000654 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000655 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 unsigned char *output )
657{
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 switch( ctx->padding )
659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_PKCS1_V15)
661 case MBEDTLS_RSA_PKCS_V15:
662 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100663 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200664#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#if defined(MBEDTLS_PKCS1_V21)
667 case MBEDTLS_RSA_PKCS_V21:
668 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100669 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000670#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000675}
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000678/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100679 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200682 int (*f_rng)(void *, unsigned char *, size_t),
683 void *p_rng,
684 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100685 const unsigned char *label, size_t label_len,
686 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100687 const unsigned char *input,
688 unsigned char *output,
689 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000690{
Paul Bakker23986e52011-04-24 08:57:21 +0000691 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100692 size_t ilen, i, pad_len;
693 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
695 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000696 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 const mbedtls_md_info_t *md_info;
698 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100699
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100700 /*
701 * Parameters sanity checks
702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
704 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
706 ilen = ctx->len;
707
Paul Bakker27fdf462011-06-09 13:55:13 +0000708 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100712 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100714
715 /*
716 * RSA operation
717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 ret = ( mode == MBEDTLS_RSA_PUBLIC )
719 ? mbedtls_rsa_public( ctx, input, buf )
720 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000721
722 if( ret != 0 )
723 return( ret );
724
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100725 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100726 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_md_init( &md_ctx );
731 mbedtls_md_setup( &md_ctx, md_info, 0 );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100732
733 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100735
736 /* seed: Apply seedMask to maskedSeed */
737 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
738 &md_ctx );
739
740 /* DB: Apply dbMask to maskedDB */
741 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
742 &md_ctx );
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100745
746 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100747 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100748 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100750 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100752 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100753
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100754 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100755
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100756 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100757 for( i = 0; i < hlen; i++ )
758 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100759
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100760 /* Get zero-padding len, but always read till end of buffer
761 * (minus one, for the 01 byte) */
762 pad_len = 0;
763 pad_done = 0;
764 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
765 {
766 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100767 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100768 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100769
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100770 p += pad_len;
771 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100772
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100773 /*
774 * The only information "leaked" is whether the padding was correct or not
775 * (eg, no data is copied if it was not correct). This meets the
776 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
777 * the different error conditions.
778 */
779 if( bad != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100781
Paul Bakker66d5d072014-06-17 16:39:18 +0200782 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakkerb3869132013-02-28 17:21:01 +0100784
785 *olen = ilen - (p - buf);
786 memcpy( output, p, *olen );
787
788 return( 0 );
789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100793/*
794 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200797 int (*f_rng)(void *, unsigned char *, size_t),
798 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100799 int mode, size_t *olen,
800 const unsigned char *input,
801 unsigned char *output,
802 size_t output_max_len)
803{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100804 int ret;
805 size_t ilen, pad_count = 0, i;
806 unsigned char *p, bad, pad_done = 0;
Paul Bakker38d18882016-05-12 12:46:28 +0100807#if defined(__clang_analyzer__)
Nicholas Wilsone7353032016-04-13 11:48:25 +0100808 /* Shut up Clang, mbedtls_rsa_public/private writes to this */
809 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { };
810#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Nicholas Wilsone7353032016-04-13 11:48:25 +0100812#endif
Paul Bakkerb3869132013-02-28 17:21:01 +0100813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
815 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100816
817 ilen = ctx->len;
818
819 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 ret = ( mode == MBEDTLS_RSA_PUBLIC )
823 ? mbedtls_rsa_public( ctx, input, buf )
824 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100825
826 if( ret != 0 )
827 return( ret );
828
829 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100830 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100831
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100832 /*
833 * Check and get padding len in "constant-time"
834 */
835 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100836
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100837 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000841
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100842 /* Get padding len, but always read till end of buffer
843 * (minus one, for the 00 byte) */
844 for( i = 0; i < ilen - 3; i++ )
845 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100846 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
847 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100848 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000849
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100850 p += pad_count;
851 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100852 }
853 else
854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100856
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100857 /* Get padding len, but always read till end of buffer
858 * (minus one, for the 00 byte) */
859 for( i = 0; i < ilen - 3; i++ )
860 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100861 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100862 pad_count += ( pad_done == 0 );
863 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100864
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100865 p += pad_count;
866 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +0000867 }
868
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100869 if( bad )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker8804f692013-02-28 18:06:26 +0100871
Paul Bakker66d5d072014-06-17 16:39:18 +0200872 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000874
Paul Bakker27fdf462011-06-09 13:55:13 +0000875 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000876 memcpy( output, p, *olen );
877
878 return( 0 );
879}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000881
882/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100883 * Do an RSA operation, then remove the message padding
884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200886 int (*f_rng)(void *, unsigned char *, size_t),
887 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100888 int mode, size_t *olen,
889 const unsigned char *input,
890 unsigned char *output,
891 size_t output_max_len)
892{
893 switch( ctx->padding )
894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#if defined(MBEDTLS_PKCS1_V15)
896 case MBEDTLS_RSA_PKCS_V15:
897 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +0200898 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +0200899#endif
Paul Bakkerb3869132013-02-28 17:21:01 +0100900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901#if defined(MBEDTLS_PKCS1_V21)
902 case MBEDTLS_RSA_PKCS_V21:
903 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +0200904 olen, input, output,
905 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +0100906#endif
907
908 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100910 }
911}
912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100914/*
915 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
916 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100918 int (*f_rng)(void *, unsigned char *, size_t),
919 void *p_rng,
920 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100922 unsigned int hashlen,
923 const unsigned char *hash,
924 unsigned char *sig )
925{
926 size_t olen;
927 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100929 unsigned int slen, hlen, offset = 0;
930 int ret;
931 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 const mbedtls_md_info_t *md_info;
933 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
936 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200937
938 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100940
941 olen = ctx->len;
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +0100944 {
Paul Bakkerc70b9822013-04-07 22:00:46 +0200945 // Gather length of hash to sign
946 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200948 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100952 }
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100955 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100959 slen = hlen;
960
961 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100963
964 memset( sig, 0, olen );
965
Paul Bakkerb3869132013-02-28 17:21:01 +0100966 // Generate salt of length slen
967 //
968 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100970
971 // Note: EMSA-PSS encoding is over the length of N - 1 bits
972 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200973 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +0100974 p += olen - hlen * 2 - 2;
975 *p++ = 0x01;
976 memcpy( p, salt, slen );
977 p += slen;
978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_md_init( &md_ctx );
980 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +0100981
982 // Generate H = Hash( M' )
983 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_md_starts( &md_ctx );
985 mbedtls_md_update( &md_ctx, p, 8 );
986 mbedtls_md_update( &md_ctx, hash, hashlen );
987 mbedtls_md_update( &md_ctx, salt, slen );
988 mbedtls_md_finish( &md_ctx, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100989
990 // Compensate for boundary condition when applying mask
991 //
992 if( msb % 8 == 0 )
993 offset = 1;
994
995 // maskedDB: Apply dbMask to DB
996 //
997 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001000
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001001 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001002 sig[0] &= 0xFF >> ( olen * 8 - msb );
1003
1004 p += hlen;
1005 *p++ = 0xBC;
1006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 return( ( mode == MBEDTLS_RSA_PUBLIC )
1008 ? mbedtls_rsa_public( ctx, sig, sig )
1009 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001010}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001014/*
1015 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1016 */
1017/*
1018 * Do an RSA operation to sign the message digest
1019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001021 int (*f_rng)(void *, unsigned char *, size_t),
1022 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001023 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001025 unsigned int hashlen,
1026 const unsigned char *hash,
1027 unsigned char *sig )
1028{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001029 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001030 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001031 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001032 unsigned char *sig_try = NULL, *verif = NULL;
1033 size_t i;
1034 unsigned char diff;
1035 volatile unsigned char diff_no_optimize;
1036 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1039 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001040
1041 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001042 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001047 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1051 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001052
Paul Bakkerc70b9822013-04-07 22:00:46 +02001053 nb_pad -= 10 + oid_size;
1054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001056 }
1057
Paul Bakkerc70b9822013-04-07 22:00:46 +02001058 nb_pad -= hashlen;
1059
Paul Bakkerb3869132013-02-28 17:21:01 +01001060 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001062
1063 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001065 memset( p, 0xFF, nb_pad );
1066 p += nb_pad;
1067 *p++ = 0;
1068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001070 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001071 memcpy( p, hash, hashlen );
1072 }
1073 else
1074 {
1075 /*
1076 * DigestInfo ::= SEQUENCE {
1077 * digestAlgorithm DigestAlgorithmIdentifier,
1078 * digest Digest }
1079 *
1080 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1081 *
1082 * Digest ::= OCTET STRING
1083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001085 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
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) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001089 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001090 memcpy( p, oid, oid_size );
1091 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001093 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001095 *p++ = hashlen;
1096 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001097 }
1098
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001099 if( mode == MBEDTLS_RSA_PUBLIC )
1100 return( mbedtls_rsa_public( ctx, sig, sig ) );
1101
1102 /*
1103 * In order to prevent Lenstra's attack, make the signature in a
1104 * temporary buffer and check it before returning it.
1105 */
1106 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001107 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001108 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1109
Simon Butcher1285ab52016-01-01 21:42:47 +00001110 verif = mbedtls_calloc( 1, ctx->len );
1111 if( verif == NULL )
1112 {
1113 mbedtls_free( sig_try );
1114 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1115 }
1116
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001117 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1118 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1119
1120 /* Compare in constant time just in case */
1121 for( diff = 0, i = 0; i < ctx->len; i++ )
1122 diff |= verif[i] ^ sig[i];
1123 diff_no_optimize = diff;
1124
1125 if( diff_no_optimize != 0 )
1126 {
1127 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1128 goto cleanup;
1129 }
1130
1131 memcpy( sig, sig_try, ctx->len );
1132
1133cleanup:
1134 mbedtls_free( sig_try );
1135 mbedtls_free( verif );
1136
1137 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001138}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001140
1141/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001142 * Do an RSA operation to sign the message digest
1143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001145 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001146 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001149 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001150 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 unsigned char *sig )
1152{
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 switch( ctx->padding )
1154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_PKCS1_V15)
1156 case MBEDTLS_RSA_PKCS_V15:
1157 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001159#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_PKCS1_V21)
1162 case MBEDTLS_RSA_PKCS_V21:
1163 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001164 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001165#endif
1166
Paul Bakker5121ce52009-01-03 21:22:43 +00001167 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001170}
1171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001173/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001174 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001177 int (*f_rng)(void *, unsigned char *, size_t),
1178 void *p_rng,
1179 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001181 unsigned int hashlen,
1182 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001184 int expected_salt_len,
1185 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001186{
Paul Bakker23986e52011-04-24 08:57:21 +00001187 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001188 size_t siglen;
1189 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001191 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001192 unsigned int hlen;
1193 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 const mbedtls_md_info_t *md_info;
1195 mbedtls_md_context_t md_ctx;
Paul Bakker38d18882016-05-12 12:46:28 +01001196#if defined(__clang_analyzer__)
Nicholas Wilsone7353032016-04-13 11:48:25 +01001197 /* Shut up Clang, mbedtls_rsa_public/private writes to this */
1198 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { };
1199#else
1200 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1201#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1204 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001205
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 siglen = ctx->len;
1207
Paul Bakker27fdf462011-06-09 13:55:13 +00001208 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1212 ? mbedtls_rsa_public( ctx, sig, buf )
1213 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
1215 if( ret != 0 )
1216 return( ret );
1217
1218 p = buf;
1219
Paul Bakkerb3869132013-02-28 17:21:01 +01001220 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001224 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001225 // Gather length of hash to sign
1226 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001228 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001232 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001235 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001239 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001240
Paul Bakkerb3869132013-02-28 17:21:01 +01001241 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001242
Paul Bakkerb3869132013-02-28 17:21:01 +01001243 // Note: EMSA-PSS verification is over the length of N - 1 bits
1244 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001245 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001246
Paul Bakkerb3869132013-02-28 17:21:01 +01001247 // Compensate for boundary condition when applying mask
1248 //
1249 if( msb % 8 == 0 )
1250 {
1251 p++;
1252 siglen -= 1;
1253 }
1254 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 mbedtls_md_init( &md_ctx );
1258 mbedtls_md_setup( &md_ctx, md_info, 0 );
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
Paul Bakkerb3869132013-02-28 17:21:01 +01001284 // Generate H = Hash( M' )
1285 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_md_starts( &md_ctx );
1287 mbedtls_md_update( &md_ctx, zeros, 8 );
1288 mbedtls_md_update( &md_ctx, hash, hashlen );
1289 mbedtls_md_update( &md_ctx, p, slen );
1290 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001293
Paul Bakkerb3869132013-02-28 17:21:01 +01001294 if( memcmp( p + slen, result, hlen ) == 0 )
1295 return( 0 );
1296 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001298}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001299
1300/*
1301 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001304 int (*f_rng)(void *, unsigned char *, size_t),
1305 void *p_rng,
1306 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001308 unsigned int hashlen,
1309 const unsigned char *hash,
1310 const unsigned char *sig )
1311{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1313 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001314 : md_alg;
1315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001317 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001319 sig ) );
1320
1321}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001325/*
1326 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001329 int (*f_rng)(void *, unsigned char *, size_t),
1330 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001331 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001333 unsigned int hashlen,
1334 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001335 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001336{
1337 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001338 size_t len, siglen, asn1_len;
1339 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 mbedtls_md_type_t msg_md_alg;
1341 const mbedtls_md_info_t *md_info;
1342 mbedtls_asn1_buf oid;
Paul Bakker38d18882016-05-12 12:46:28 +01001343#if defined(__clang_analyzer__)
Nicholas Wilsone7353032016-04-13 11:48:25 +01001344 /* Shut up Clang, mbedtls_rsa_public/private writes to this */
1345 unsigned char buf[MBEDTLS_MPI_MAX_SIZE] = { };
1346#else
1347 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1348#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1351 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001352
1353 siglen = ctx->len;
1354
1355 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1359 ? mbedtls_rsa_public( ctx, sig, buf )
1360 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001361
1362 if( ret != 0 )
1363 return( ret );
1364
1365 p = buf;
1366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1368 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001369
1370 while( *p != 0 )
1371 {
1372 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001374 p++;
1375 }
1376 p++;
1377
1378 len = siglen - ( p - buf );
1379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001381 {
1382 if( memcmp( p, hash, hashlen ) == 0 )
1383 return( 0 );
1384 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 }
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001389 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1391 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001392
1393 end = p + len;
1394
1395 // Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1396 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1398 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1399 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001400
1401 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1405 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1406 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001407
1408 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1412 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001413
1414 oid.p = p;
1415 p += oid.len;
1416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1418 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001419
1420 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001422
1423 /*
1424 * assume the algorithm parameters must be NULL
1425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1427 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1430 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001431
1432 if( asn1_len != hashlen )
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 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001437
1438 p += hashlen;
1439
1440 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001442
1443 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001446
1447/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001448 * Do an RSA operation and check the message digest
1449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001451 int (*f_rng)(void *, unsigned char *, size_t),
1452 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001453 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001455 unsigned int hashlen,
1456 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001457 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001458{
1459 switch( ctx->padding )
1460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#if defined(MBEDTLS_PKCS1_V15)
1462 case MBEDTLS_RSA_PKCS_V15:
1463 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001464 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001465#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467#if defined(MBEDTLS_PKCS1_V21)
1468 case MBEDTLS_RSA_PKCS_V21:
1469 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001470 hashlen, hash, sig );
1471#endif
1472
1473 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001475 }
1476}
1477
1478/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001479 * Copy the components of an RSA key
1480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001482{
1483 int ret;
1484
1485 dst->ver = src->ver;
1486 dst->len = src->len;
1487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1489 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1492 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1493 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1494 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1495 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1496 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1499 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1500 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1503 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001504
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001505 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001506 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001507
1508cleanup:
1509 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001511
1512 return( ret );
1513}
1514
1515/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001516 * Free the components of an RSA key
1517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001519{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1521 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1522 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1523 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1524 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526#if defined(MBEDTLS_THREADING_C)
1527 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001528#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001529}
1530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001532
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001533#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001534
1535/*
1536 * Example RSA-1024 keypair, for test purposes
1537 */
1538#define KEY_LEN 128
1539
1540#define RSA_N "9292758453063D803DD603D5E777D788" \
1541 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1542 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1543 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1544 "93A89813FBF3C4F8066D2D800F7C38A8" \
1545 "1AE31942917403FF4946B0A83D3D3E05" \
1546 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1547 "5E94BB77B07507233A0BC7BAC8F90F79"
1548
1549#define RSA_E "10001"
1550
1551#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1552 "66CA472BC44D253102F8B4A9D3BFA750" \
1553 "91386C0077937FE33FA3252D28855837" \
1554 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1555 "DF79C5CE07EE72C7F123142198164234" \
1556 "CABB724CF78B8173B9F880FC86322407" \
1557 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1558 "071513A1E85B5DFA031F21ECAE91A34D"
1559
1560#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1561 "2C01CAD19EA484A87EA4377637E75500" \
1562 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1563 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1564
1565#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1566 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1567 "910E4168387E3C30AA1E00C339A79508" \
1568 "8452DD96A9A5EA5D9DCA68DA636032AF"
1569
1570#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1571 "3C94D22288ACD763FD8E5600ED4A702D" \
1572 "F84198A5F06C2E72236AE490C93F07F8" \
1573 "3CC559CD27BC2D1CA488811730BB5725"
1574
1575#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1576 "D8AAEA56749EA28623272E4F7D0592AF" \
1577 "7C1F1313CAC9471B5C523BFE592F517B" \
1578 "407A1BD76C164B93DA2D32A383E58357"
1579
1580#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1581 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1582 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1583 "A74206CEC169D74BF5A8C50D6F48EA08"
1584
1585#define PT_LEN 24
1586#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1587 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001590static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001591{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001592#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001593 size_t i;
1594
Paul Bakker545570e2010-07-18 09:00:25 +00001595 if( rng_state != NULL )
1596 rng_state = NULL;
1597
Paul Bakkera3d195c2011-11-27 21:07:34 +00001598 for( i = 0; i < len; ++i )
1599 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001600#else
1601 if( rng_state != NULL )
1602 rng_state = NULL;
1603
1604 arc4random_buf( output, len );
1605#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001606
Paul Bakkera3d195c2011-11-27 21:07:34 +00001607 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001608}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001610
Paul Bakker5121ce52009-01-03 21:22:43 +00001611/*
1612 * Checkup routine
1613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001615{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001616 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001618 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001620 unsigned char rsa_plaintext[PT_LEN];
1621 unsigned char rsa_decrypted[PT_LEN];
1622 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001624 unsigned char sha1sum[20];
1625#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
1629 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1631 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1632 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1633 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1634 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1635 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1636 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1637 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001638
1639 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1643 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001644 {
1645 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
1648 return( 1 );
1649 }
1650
1651 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
1654 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001657 rsa_plaintext, rsa_ciphertext ) != 0 )
1658 {
1659 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001661
1662 return( 1 );
1663 }
1664
1665 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001669 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001670 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 {
1672 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001674
1675 return( 1 );
1676 }
1677
1678 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1679 {
1680 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001682
1683 return( 1 );
1684 }
1685
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001686 if( verbose != 0 )
1687 mbedtls_printf( "passed\n" );
1688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001690 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001691 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001696 sha1sum, rsa_ciphertext ) != 0 )
1697 {
1698 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001700
1701 return( 1 );
1702 }
1703
1704 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001708 sha1sum, rsa_ciphertext ) != 0 )
1709 {
1710 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001712
1713 return( 1 );
1714 }
1715
1716 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001717 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001719
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001720 if( verbose != 0 )
1721 mbedtls_printf( "\n" );
1722
Paul Bakker3d8fb632014-04-17 12:42:41 +02001723cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724 mbedtls_rsa_free( &rsa );
1725#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001726 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001728 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001729}
1730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#endif /* MBEDTLS_RSA_C */