blob: 7c1f658cd48c82ac1d142812316fa7b28a18094d [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 Bakker37940d9f2009-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 Bakker37940d9f2009-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 Bakker37940d9f2009-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 Bakker37940d9f2009-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
Simon Butcher02037452016-03-01 21:19:12 +0000480 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000481 p = dst;
482
483 while( dlen > 0 )
484 {
485 use_len = hlen;
486 if( dlen < hlen )
487 use_len = dlen;
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_md_starts( md_ctx );
490 mbedtls_md_update( md_ctx, src, slen );
491 mbedtls_md_update( md_ctx, counter, 4 );
492 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000493
494 for( i = 0; i < use_len; ++i )
495 *p++ ^= mask[i];
496
497 counter[3]++;
498
499 dlen -= use_len;
500 }
501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100505/*
506 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100509 int (*f_rng)(void *, unsigned char *, size_t),
510 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100511 int mode,
512 const unsigned char *label, size_t label_len,
513 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100514 const unsigned char *input,
515 unsigned char *output )
516{
517 size_t olen;
518 int ret;
519 unsigned char *p = output;
520 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 const mbedtls_md_info_t *md_info;
522 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
525 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200526
527 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100531 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100533
534 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100536
Simon Butcher02037452016-03-01 21:19:12 +0000537 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000538 if( ilen + 2 * hlen + 2 < ilen || 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
Simon Butcher02037452016-03-01 21:19:12 +0000545 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100546 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100548
549 p += hlen;
550
Simon Butcher02037452016-03-01 21:19:12 +0000551 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100553 p += hlen;
554 p += olen - 2 * hlen - 2 - ilen;
555 *p++ = 1;
556 memcpy( p, input, ilen );
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_md_init( &md_ctx );
559 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +0100560
Simon Butcher02037452016-03-01 21:19:12 +0000561 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100562 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
563 &md_ctx );
564
Simon Butcher02037452016-03-01 21:19:12 +0000565 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100566 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
567 &md_ctx );
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 return( ( mode == MBEDTLS_RSA_PUBLIC )
572 ? mbedtls_rsa_public( ctx, output, output )
573 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100578/*
579 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100582 int (*f_rng)(void *, unsigned char *, size_t),
583 void *p_rng,
584 int mode, size_t ilen,
585 const unsigned char *input,
586 unsigned char *output )
587{
588 size_t nb_pad, olen;
589 int ret;
590 unsigned char *p = output;
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
593 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200594
Janos Follath1ed9f992016-03-18 11:45:44 +0000595 // We don't check p_rng because it won't be dereferenced here
596 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100598
599 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100600
Simon Butcher02037452016-03-01 21:19:12 +0000601 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000602 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100604
605 nb_pad = olen - 3 - ilen;
606
607 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100611
612 while( nb_pad-- > 0 )
613 {
614 int rng_dl = 100;
615
616 do {
617 ret = f_rng( p_rng, p, 1 );
618 } while( *p == 0 && --rng_dl && ret == 0 );
619
Simon Butcher02037452016-03-01 21:19:12 +0000620 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200621 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100623
624 p++;
625 }
626 }
627 else
628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100630
631 while( nb_pad-- > 0 )
632 *p++ = 0xFF;
633 }
634
635 *p++ = 0;
636 memcpy( p, input, ilen );
637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 return( ( mode == MBEDTLS_RSA_PUBLIC )
639 ? mbedtls_rsa_public( ctx, output, output )
640 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100641}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100643
Paul Bakker5121ce52009-01-03 21:22:43 +0000644/*
645 * Add the message padding, then do an RSA operation
646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000648 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000649 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000650 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000651 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000652 unsigned char *output )
653{
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 switch( ctx->padding )
655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656#if defined(MBEDTLS_PKCS1_V15)
657 case MBEDTLS_RSA_PKCS_V15:
658 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100659 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200660#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_PKCS1_V21)
663 case MBEDTLS_RSA_PKCS_V21:
664 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100665 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000666#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
668 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000670 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000671}
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000674/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100675 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000676 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200678 int (*f_rng)(void *, unsigned char *, size_t),
679 void *p_rng,
680 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100681 const unsigned char *label, size_t label_len,
682 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100683 const unsigned char *input,
684 unsigned char *output,
685 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000686{
Paul Bakker23986e52011-04-24 08:57:21 +0000687 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100688 size_t ilen, i, pad_len;
689 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
691 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000692 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 const mbedtls_md_info_t *md_info;
694 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100695
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100696 /*
697 * Parameters sanity checks
698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
700 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000701
702 ilen = ctx->len;
703
Paul Bakker27fdf462011-06-09 13:55:13 +0000704 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100708 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100710
Janos Follathc17cda12016-02-11 11:08:18 +0000711 hlen = mbedtls_md_get_size( md_info );
712
713 // checking for integer underflow
714 if( 2 * hlen + 2 > ilen )
715 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
716
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100717 /*
718 * RSA operation
719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 ret = ( mode == MBEDTLS_RSA_PUBLIC )
721 ? mbedtls_rsa_public( ctx, input, buf )
722 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000723
724 if( ret != 0 )
725 return( ret );
726
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100727 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100728 * Unmask data and generate lHash
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;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
810 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100811
812 ilen = ctx->len;
813
814 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 ret = ( mode == MBEDTLS_RSA_PUBLIC )
818 ? mbedtls_rsa_public( ctx, input, buf )
819 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100820
821 if( ret != 0 )
822 return( ret );
823
824 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100825 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100826
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100827 /*
828 * Check and get padding len in "constant-time"
829 */
830 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100831
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100832 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000836
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100837 /* Get padding len, but always read till end of buffer
838 * (minus one, for the 00 byte) */
839 for( i = 0; i < ilen - 3; i++ )
840 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100841 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
842 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100843 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000844
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100845 p += pad_count;
846 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100847 }
848 else
849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100851
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100852 /* Get padding len, but always read till end of buffer
853 * (minus one, for the 00 byte) */
854 for( i = 0; i < ilen - 3; i++ )
855 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100856 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100857 pad_count += ( pad_done == 0 );
858 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100859
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100860 p += pad_count;
861 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +0000862 }
863
Janos Follathc69fa502016-02-12 13:30:09 +0000864 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +0000865
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100866 if( bad )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker8804f692013-02-28 18:06:26 +0100868
Paul Bakker66d5d072014-06-17 16:39:18 +0200869 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000871
Paul Bakker27fdf462011-06-09 13:55:13 +0000872 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000873 memcpy( output, p, *olen );
874
875 return( 0 );
876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000878
879/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100880 * Do an RSA operation, then remove the message padding
881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200883 int (*f_rng)(void *, unsigned char *, size_t),
884 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100885 int mode, size_t *olen,
886 const unsigned char *input,
887 unsigned char *output,
888 size_t output_max_len)
889{
890 switch( ctx->padding )
891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892#if defined(MBEDTLS_PKCS1_V15)
893 case MBEDTLS_RSA_PKCS_V15:
894 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +0200895 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +0200896#endif
Paul Bakkerb3869132013-02-28 17:21:01 +0100897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898#if defined(MBEDTLS_PKCS1_V21)
899 case MBEDTLS_RSA_PKCS_V21:
900 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +0200901 olen, input, output,
902 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +0100903#endif
904
905 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100907 }
908}
909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100911/*
912 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100915 int (*f_rng)(void *, unsigned char *, size_t),
916 void *p_rng,
917 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100919 unsigned int hashlen,
920 const unsigned char *hash,
921 unsigned char *sig )
922{
923 size_t olen;
924 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100926 unsigned int slen, hlen, offset = 0;
927 int ret;
928 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 const mbedtls_md_info_t *md_info;
930 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
933 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200934
935 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100937
938 olen = ctx->len;
939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +0100941 {
Simon Butcher02037452016-03-01 21:19:12 +0000942 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200944 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100948 }
949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100951 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100955 slen = hlen;
956
957 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100959
960 memset( sig, 0, olen );
961
Simon Butcher02037452016-03-01 21:19:12 +0000962 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +0100963 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100965
Simon Butcher02037452016-03-01 21:19:12 +0000966 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200967 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +0100968 p += olen - hlen * 2 - 2;
969 *p++ = 0x01;
970 memcpy( p, salt, slen );
971 p += slen;
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_md_init( &md_ctx );
974 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +0100975
Simon Butcher02037452016-03-01 21:19:12 +0000976 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_md_starts( &md_ctx );
978 mbedtls_md_update( &md_ctx, p, 8 );
979 mbedtls_md_update( &md_ctx, hash, hashlen );
980 mbedtls_md_update( &md_ctx, salt, slen );
981 mbedtls_md_finish( &md_ctx, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100982
Simon Butcher02037452016-03-01 21:19:12 +0000983 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +0100984 if( msb % 8 == 0 )
985 offset = 1;
986
Simon Butcher02037452016-03-01 21:19:12 +0000987 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100988 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100991
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200992 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +0100993 sig[0] &= 0xFF >> ( olen * 8 - msb );
994
995 p += hlen;
996 *p++ = 0xBC;
997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 return( ( mode == MBEDTLS_RSA_PUBLIC )
999 ? mbedtls_rsa_public( ctx, sig, sig )
1000 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001005/*
1006 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1007 */
1008/*
1009 * Do an RSA operation to sign the message digest
1010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001012 int (*f_rng)(void *, unsigned char *, size_t),
1013 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001014 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001016 unsigned int hashlen,
1017 const unsigned char *hash,
1018 unsigned char *sig )
1019{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001020 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001021 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001022 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001023 unsigned char *sig_try = NULL, *verif = NULL;
1024 size_t i;
1025 unsigned char diff;
1026 volatile unsigned char diff_no_optimize;
1027 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1030 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001031
1032 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001033 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001038 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1042 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001043
Paul Bakkerc70b9822013-04-07 22:00:46 +02001044 nb_pad -= 10 + oid_size;
1045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001047 }
1048
Paul Bakkerc70b9822013-04-07 22:00:46 +02001049 nb_pad -= hashlen;
1050
Paul Bakkerb3869132013-02-28 17:21:01 +01001051 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001053
1054 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001056 memset( p, 0xFF, nb_pad );
1057 p += nb_pad;
1058 *p++ = 0;
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001061 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001062 memcpy( p, hash, hashlen );
1063 }
1064 else
1065 {
1066 /*
1067 * DigestInfo ::= SEQUENCE {
1068 * digestAlgorithm DigestAlgorithmIdentifier,
1069 * digest Digest }
1070 *
1071 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1072 *
1073 * Digest ::= OCTET STRING
1074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001076 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001078 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001080 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001081 memcpy( p, oid, oid_size );
1082 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001084 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001086 *p++ = hashlen;
1087 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001088 }
1089
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001090 if( mode == MBEDTLS_RSA_PUBLIC )
1091 return( mbedtls_rsa_public( ctx, sig, sig ) );
1092
1093 /*
1094 * In order to prevent Lenstra's attack, make the signature in a
1095 * temporary buffer and check it before returning it.
1096 */
1097 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001098 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001099 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1100
Simon Butcher1285ab52016-01-01 21:42:47 +00001101 verif = mbedtls_calloc( 1, ctx->len );
1102 if( verif == NULL )
1103 {
1104 mbedtls_free( sig_try );
1105 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1106 }
1107
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001108 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1109 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1110
1111 /* Compare in constant time just in case */
1112 for( diff = 0, i = 0; i < ctx->len; i++ )
1113 diff |= verif[i] ^ sig[i];
1114 diff_no_optimize = diff;
1115
1116 if( diff_no_optimize != 0 )
1117 {
1118 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1119 goto cleanup;
1120 }
1121
1122 memcpy( sig, sig_try, ctx->len );
1123
1124cleanup:
1125 mbedtls_free( sig_try );
1126 mbedtls_free( verif );
1127
1128 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001131
1132/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001133 * Do an RSA operation to sign the message digest
1134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001136 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001137 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001138 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001140 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001141 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001142 unsigned char *sig )
1143{
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 switch( ctx->padding )
1145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146#if defined(MBEDTLS_PKCS1_V15)
1147 case MBEDTLS_RSA_PKCS_V15:
1148 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001149 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001150#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152#if defined(MBEDTLS_PKCS1_V21)
1153 case MBEDTLS_RSA_PKCS_V21:
1154 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001155 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001156#endif
1157
Paul Bakker5121ce52009-01-03 21:22:43 +00001158 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001160 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001161}
1162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001164/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001165 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001168 int (*f_rng)(void *, unsigned char *, size_t),
1169 void *p_rng,
1170 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001172 unsigned int hashlen,
1173 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001175 int expected_salt_len,
1176 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001177{
Paul Bakker23986e52011-04-24 08:57:21 +00001178 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001179 size_t siglen;
1180 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1182 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001183 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001184 unsigned int hlen;
1185 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 const mbedtls_md_info_t *md_info;
1187 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1190 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001191
Paul Bakker5121ce52009-01-03 21:22:43 +00001192 siglen = ctx->len;
1193
Paul Bakker27fdf462011-06-09 13:55:13 +00001194 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1198 ? mbedtls_rsa_public( ctx, sig, buf )
1199 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
1201 if( ret != 0 )
1202 return( ret );
1203
1204 p = buf;
1205
Paul Bakkerb3869132013-02-28 17:21:01 +01001206 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001210 {
Simon Butcher02037452016-03-01 21:19:12 +00001211 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001213 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001217 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001220 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001224 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001225
Paul Bakkerb3869132013-02-28 17:21:01 +01001226 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001227
Simon Butcher02037452016-03-01 21:19:12 +00001228 /*
1229 * Note: EMSA-PSS verification is over the length of N - 1 bits
1230 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001231 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001232
Simon Butcher02037452016-03-01 21:19:12 +00001233 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001234 if( msb % 8 == 0 )
1235 {
1236 p++;
1237 siglen -= 1;
1238 }
1239 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_md_init( &md_ctx );
1243 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001244
Paul Bakkerb3869132013-02-28 17:21:01 +01001245 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001246
Paul Bakkerb3869132013-02-28 17:21:01 +01001247 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001248
Paul Bakker4de44aa2013-12-31 11:43:01 +01001249 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001250 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001251
Paul Bakkerb3869132013-02-28 17:21:01 +01001252 if( p == buf + siglen ||
1253 *p++ != 0x01 )
1254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 mbedtls_md_free( &md_ctx );
1256 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001257 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001258
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001259 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001260 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001263 slen != (size_t) expected_salt_len )
1264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 mbedtls_md_free( &md_ctx );
1266 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001267 }
1268
Simon Butcher02037452016-03-01 21:19:12 +00001269 /*
1270 * Generate H = Hash( M' )
1271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 mbedtls_md_starts( &md_ctx );
1273 mbedtls_md_update( &md_ctx, zeros, 8 );
1274 mbedtls_md_update( &md_ctx, hash, hashlen );
1275 mbedtls_md_update( &md_ctx, p, slen );
1276 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001279
Paul Bakkerb3869132013-02-28 17:21:01 +01001280 if( memcmp( p + slen, result, hlen ) == 0 )
1281 return( 0 );
1282 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001284}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001285
1286/*
1287 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001290 int (*f_rng)(void *, unsigned char *, size_t),
1291 void *p_rng,
1292 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001294 unsigned int hashlen,
1295 const unsigned char *hash,
1296 const unsigned char *sig )
1297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1299 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001300 : md_alg;
1301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001303 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001305 sig ) );
1306
1307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001311/*
1312 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001315 int (*f_rng)(void *, unsigned char *, size_t),
1316 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001317 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001319 unsigned int hashlen,
1320 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001321 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001322{
1323 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001324 size_t len, siglen, asn1_len;
1325 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1327 mbedtls_md_type_t msg_md_alg;
1328 const mbedtls_md_info_t *md_info;
1329 mbedtls_asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1332 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001333
1334 siglen = ctx->len;
1335
1336 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1340 ? mbedtls_rsa_public( ctx, sig, buf )
1341 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001342
1343 if( ret != 0 )
1344 return( ret );
1345
1346 p = buf;
1347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1349 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001350
1351 while( *p != 0 )
1352 {
1353 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001355 p++;
1356 }
1357 p++;
1358
1359 len = siglen - ( p - buf );
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001362 {
1363 if( memcmp( p, hash, hashlen ) == 0 )
1364 return( 0 );
1365 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 }
1368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001370 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1372 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001373
1374 end = p + len;
1375
Simon Butcher02037452016-03-01 21:19:12 +00001376 /*
1377 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1380 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1381 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001382
1383 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1387 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1388 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001389
1390 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1394 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001395
1396 oid.p = p;
1397 p += oid.len;
1398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1400 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001401
1402 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001404
1405 /*
1406 * assume the algorithm parameters must be NULL
1407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1409 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, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1412 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001413
1414 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001416
1417 if( memcmp( p, hash, hashlen ) != 0 )
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 p += hashlen;
1421
1422 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001424
1425 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001426}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001428
1429/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001430 * Do an RSA operation and check the message digest
1431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001433 int (*f_rng)(void *, unsigned char *, size_t),
1434 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001435 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001437 unsigned int hashlen,
1438 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001439 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001440{
1441 switch( ctx->padding )
1442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443#if defined(MBEDTLS_PKCS1_V15)
1444 case MBEDTLS_RSA_PKCS_V15:
1445 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001446 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001447#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_PKCS1_V21)
1450 case MBEDTLS_RSA_PKCS_V21:
1451 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001452 hashlen, hash, sig );
1453#endif
1454
1455 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001457 }
1458}
1459
1460/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001461 * Copy the components of an RSA key
1462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001464{
1465 int ret;
1466
1467 dst->ver = src->ver;
1468 dst->len = src->len;
1469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1471 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1474 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1475 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1476 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1477 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1478 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1481 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1482 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1485 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001486
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001487 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001488 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001489
1490cleanup:
1491 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001493
1494 return( ret );
1495}
1496
1497/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001498 * Free the components of an RSA key
1499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001501{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1503 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1504 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1505 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1506 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#if defined(MBEDTLS_THREADING_C)
1509 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001510#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001511}
1512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001514
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001515#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001516
1517/*
1518 * Example RSA-1024 keypair, for test purposes
1519 */
1520#define KEY_LEN 128
1521
1522#define RSA_N "9292758453063D803DD603D5E777D788" \
1523 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1524 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1525 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1526 "93A89813FBF3C4F8066D2D800F7C38A8" \
1527 "1AE31942917403FF4946B0A83D3D3E05" \
1528 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1529 "5E94BB77B07507233A0BC7BAC8F90F79"
1530
1531#define RSA_E "10001"
1532
1533#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1534 "66CA472BC44D253102F8B4A9D3BFA750" \
1535 "91386C0077937FE33FA3252D28855837" \
1536 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1537 "DF79C5CE07EE72C7F123142198164234" \
1538 "CABB724CF78B8173B9F880FC86322407" \
1539 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1540 "071513A1E85B5DFA031F21ECAE91A34D"
1541
1542#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1543 "2C01CAD19EA484A87EA4377637E75500" \
1544 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1545 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1546
1547#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1548 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1549 "910E4168387E3C30AA1E00C339A79508" \
1550 "8452DD96A9A5EA5D9DCA68DA636032AF"
1551
1552#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1553 "3C94D22288ACD763FD8E5600ED4A702D" \
1554 "F84198A5F06C2E72236AE490C93F07F8" \
1555 "3CC559CD27BC2D1CA488811730BB5725"
1556
1557#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1558 "D8AAEA56749EA28623272E4F7D0592AF" \
1559 "7C1F1313CAC9471B5C523BFE592F517B" \
1560 "407A1BD76C164B93DA2D32A383E58357"
1561
1562#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1563 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1564 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1565 "A74206CEC169D74BF5A8C50D6F48EA08"
1566
1567#define PT_LEN 24
1568#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1569 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001572static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001573{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001574#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001575 size_t i;
1576
Paul Bakker545570e2010-07-18 09:00:25 +00001577 if( rng_state != NULL )
1578 rng_state = NULL;
1579
Paul Bakkera3d195c2011-11-27 21:07:34 +00001580 for( i = 0; i < len; ++i )
1581 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001582#else
1583 if( rng_state != NULL )
1584 rng_state = NULL;
1585
1586 arc4random_buf( output, len );
1587#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001588
Paul Bakkera3d195c2011-11-27 21:07:34 +00001589 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001590}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001592
Paul Bakker5121ce52009-01-03 21:22:43 +00001593/*
1594 * Checkup routine
1595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001597{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001598 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001600 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 unsigned char rsa_plaintext[PT_LEN];
1603 unsigned char rsa_decrypted[PT_LEN];
1604 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001606 unsigned char sha1sum[20];
1607#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
1611 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1613 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1614 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1615 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1616 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1617 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1618 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1619 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001620
1621 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1625 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001626 {
1627 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001629
1630 return( 1 );
1631 }
1632
1633 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001635
1636 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 rsa_plaintext, rsa_ciphertext ) != 0 )
1640 {
1641 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001643
1644 return( 1 );
1645 }
1646
1647 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001651 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001652 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001653 {
1654 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
1657 return( 1 );
1658 }
1659
1660 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1661 {
1662 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
1665 return( 1 );
1666 }
1667
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001668 if( verbose != 0 )
1669 mbedtls_printf( "passed\n" );
1670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001672 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001673 mbedtls_printf( "PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001678 sha1sum, rsa_ciphertext ) != 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
1686 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001690 sha1sum, rsa_ciphertext ) != 0 )
1691 {
1692 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
1695 return( 1 );
1696 }
1697
1698 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001699 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001701
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001702 if( verbose != 0 )
1703 mbedtls_printf( "\n" );
1704
Paul Bakker3d8fb632014-04-17 12:42:41 +02001705cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706 mbedtls_rsa_free( &rsa );
1707#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001708 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001710 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001711}
1712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#endif /* MBEDTLS_RSA_C */