blob: 18fc7021230d3ec827331cdb7f2dd526aa61502c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000022 * The following sources were referenced in the design of this implementation
23 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000024 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000025 * [1] A method for obtaining digital signatures and public-key cryptosystems
26 * R Rivest, A Shamir, and L Adleman
27 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
28 *
29 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
30 * Menezes, van Oorschot and Vanstone
31 *
Paul Bakker5121ce52009-01-03 21:22:43 +000032 */
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/rsa.h"
43#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000044
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <string.h>
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000052#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010057#else
Rich Evans00ab4702015-02-06 13:43:58 +000058#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020060#define mbedtls_calloc calloc
61#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#endif
63
Paul Bakker5121ce52009-01-03 21:22:43 +000064/*
65 * Initialize an RSA context
66 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000068 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000069 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_THREADING_C)
76 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020077#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000078}
79
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010080/*
81 * Set padding for an existing RSA context
82 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010084{
85 ctx->padding = padding;
86 ctx->hash_id = hash_id;
87}
88
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91/*
92 * Generate an RSA keypair
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +000095 int (*f_rng)(void *, unsigned char *, size_t),
96 void *p_rng,
97 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +000098{
99 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Paul Bakker21eb2802010-08-16 11:10:02 +0000102 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Janos Follath10c575b2016-02-23 14:42:48 +0000105 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
106 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 /*
109 * find primes P and Q with Q < P so that:
110 * GCD( E, (P-1)*(Q-1) ) == 1
111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114 do
115 {
Janos Follath10c575b2016-02-23 14:42:48 +0000116 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000117 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Janos Follath10c575b2016-02-23 14:42:48 +0000119 if( nbits % 2 )
Simon Butcher3f5c8752016-04-15 19:06:59 +0100120 {
Janos Follath10c575b2016-02-23 14:42:48 +0000121 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, ( nbits >> 1 ) + 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000122 f_rng, p_rng ) );
Simon Butcher3f5c8752016-04-15 19:06:59 +0100123 }
Janos Follath10c575b2016-02-23 14:42:48 +0000124 else
Simon Butcher3f5c8752016-04-15 19:06:59 +0100125 {
Janos Follath10c575b2016-02-23 14:42:48 +0000126 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
127 f_rng, p_rng ) );
Simon Butcher3f5c8752016-04-15 19:06:59 +0100128 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 continue;
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200134 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 continue;
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
138 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
139 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
140 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144 /*
145 * D = E^-1 mod ((P-1)*(Q-1))
146 * DP = D mod (P - 1)
147 * DQ = D mod (Q - 1)
148 * QP = Q^-1 mod P
149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
151 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
152 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
153 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200155 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157cleanup:
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161 if( ret != 0 )
162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_rsa_free( ctx );
164 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 }
166
Paul Bakker48377d92013-08-30 12:06:24 +0200167 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168}
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172/*
173 * Check a public RSA key
174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000176{
Paul Bakker37940d92009-07-10 22:38:58 +0000177 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000179
Paul Bakker48377d92013-08-30 12:06:24 +0200180 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200184 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
185 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200188 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
190 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
192 return( 0 );
193}
194
195/*
196 * Check a private RSA key
197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000199{
200 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 return( ret );
205
Paul Bakker37940d92009-07-10 22:38:58 +0000206 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
210 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
211 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
212 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
215 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
216 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
217 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
218 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
219 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
222 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
223 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
226 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
227 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000228 /*
229 * Check for a valid PKCS1v2 private key
230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
232 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
233 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
234 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
235 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
236 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
237 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 }
Paul Bakker48377d92013-08-30 12:06:24 +0200241
Paul Bakker5121ce52009-01-03 21:22:43 +0000242cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
244 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
245 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
246 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000249 return( ret );
250
Paul Bakker6c591fa2011-05-05 11:49:20 +0000251 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000253
254 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255}
256
257/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100258 * Check if contexts holding a public and private key match
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
263 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100266 }
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
269 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100272 }
273
274 return( 0 );
275}
276
277/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 * Do an RSA public key operation
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000281 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 unsigned char *output )
283{
Paul Bakker23986e52011-04-24 08:57:21 +0000284 int ret;
285 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200290#if defined(MBEDTLS_THREADING_C)
291 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
292 return( ret );
293#endif
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200299 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
300 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 }
302
303 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
305 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
307cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200309 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
310 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100311#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
315 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318 return( 0 );
319}
320
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200321/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200322 * Generate or update blinding values, see section 10 of:
323 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200324 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200325 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200326 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200327static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200328 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
329{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200330 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200331
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200332 if( ctx->Vf.p != NULL )
333 {
334 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
336 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
337 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
338 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200339
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200340 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200341 }
342
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200343 /* Unblinding value: Vf = random number, invertible mod N */
344 do {
345 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
349 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
350 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200351
352 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
354 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200355
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200356
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200357cleanup:
358 return( ret );
359}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200360
Paul Bakker5121ce52009-01-03 21:22:43 +0000361/*
362 * Do an RSA private key operation
363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200365 int (*f_rng)(void *, unsigned char *, size_t),
366 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000367 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 unsigned char *output )
369{
Paul Bakker23986e52011-04-24 08:57:21 +0000370 int ret;
371 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_mpi T, T1, T2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100374 /* Make sure we have private key info, prevent possible misuse */
375 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
376 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000379
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200380#if defined(MBEDTLS_THREADING_C)
381 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
382 return( ret );
383#endif
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
386 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200388 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
389 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 }
391
Paul Bakkerf451bac2013-08-30 15:37:02 +0200392 if( f_rng != NULL )
393 {
394 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200395 * Blinding
396 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200397 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200398 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
399 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200401 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#if defined(MBEDTLS_RSA_NO_CRT)
404 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100405#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200406 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 * faster decryption using the CRT
408 *
409 * T1 = input ^ dP mod P
410 * T2 = input ^ dQ mod Q
411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
413 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415 /*
416 * T = (T1 - T2) * (Q^-1 mod P) mod P
417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
419 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
420 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000421
422 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200423 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
426 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
427#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200428
Paul Bakkerf451bac2013-08-30 15:37:02 +0200429 if( f_rng != NULL )
430 {
431 /*
432 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200433 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200434 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200435 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200437 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000441
442cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200444 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
445 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200446#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
450 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
453 return( 0 );
454}
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000457/**
458 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
459 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000460 * \param dst buffer to mask
461 * \param dlen length of destination buffer
462 * \param src source of the mask generation
463 * \param slen length of the source buffer
464 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000465 */
Paul Bakker48377d92013-08-30 12:06:24 +0200466static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000468{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000470 unsigned char counter[4];
471 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000472 unsigned int hlen;
473 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000476 memset( counter, 0, 4 );
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000479
480 // Generate and apply dbMask
481 //
482 p = dst;
483
484 while( dlen > 0 )
485 {
486 use_len = hlen;
487 if( dlen < hlen )
488 use_len = dlen;
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_md_starts( md_ctx );
491 mbedtls_md_update( md_ctx, src, slen );
492 mbedtls_md_update( md_ctx, counter, 4 );
493 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000494
495 for( i = 0; i < use_len; ++i )
496 *p++ ^= mask[i];
497
498 counter[3]++;
499
500 dlen -= use_len;
501 }
502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100506/*
507 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100510 int (*f_rng)(void *, unsigned char *, size_t),
511 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100512 int mode,
513 const unsigned char *label, size_t label_len,
514 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100515 const unsigned char *input,
516 unsigned char *output )
517{
518 size_t olen;
519 int ret;
520 unsigned char *p = output;
521 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 const mbedtls_md_info_t *md_info;
523 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
526 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200527
528 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100532 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100534
535 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100537
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200538 if( olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100540
541 memset( output, 0, olen );
542
543 *p++ = 0;
544
545 // Generate a random octet string seed
546 //
547 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100549
550 p += hlen;
551
552 // Construct DB
553 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100555 p += hlen;
556 p += olen - 2 * hlen - 2 - ilen;
557 *p++ = 1;
558 memcpy( p, input, ilen );
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_md_init( &md_ctx );
561 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +0100562
563 // maskedDB: Apply dbMask to DB
564 //
565 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
566 &md_ctx );
567
568 // maskedSeed: Apply seedMask to seed
569 //
570 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
571 &md_ctx );
572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 return( ( mode == MBEDTLS_RSA_PUBLIC )
576 ? mbedtls_rsa_public( ctx, output, output )
577 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100578}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100582/*
583 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100586 int (*f_rng)(void *, unsigned char *, size_t),
587 void *p_rng,
588 int mode, size_t ilen,
589 const unsigned char *input,
590 unsigned char *output )
591{
592 size_t nb_pad, olen;
593 int ret;
594 unsigned char *p = output;
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
597 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200598
Janos Follath1ed9f992016-03-18 11:45:44 +0000599 // We don't check p_rng because it won't be dereferenced here
600 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100602
603 olen = ctx->len;
604
605 if( olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100607
608 nb_pad = olen - 3 - ilen;
609
610 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100614
615 while( nb_pad-- > 0 )
616 {
617 int rng_dl = 100;
618
619 do {
620 ret = f_rng( p_rng, p, 1 );
621 } while( *p == 0 && --rng_dl && ret == 0 );
622
623 // Check if RNG failed to generate data
624 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200625 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100627
628 p++;
629 }
630 }
631 else
632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100634
635 while( nb_pad-- > 0 )
636 *p++ = 0xFF;
637 }
638
639 *p++ = 0;
640 memcpy( p, input, ilen );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 return( ( mode == MBEDTLS_RSA_PUBLIC )
643 ? mbedtls_rsa_public( ctx, output, output )
644 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100645}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100647
Paul Bakker5121ce52009-01-03 21:22:43 +0000648/*
649 * Add the message padding, then do an RSA operation
650 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000652 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000653 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000654 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000655 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000656 unsigned char *output )
657{
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 switch( ctx->padding )
659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660#if defined(MBEDTLS_PKCS1_V15)
661 case MBEDTLS_RSA_PKCS_V15:
662 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100663 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200664#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#if defined(MBEDTLS_PKCS1_V21)
667 case MBEDTLS_RSA_PKCS_V21:
668 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100669 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000670#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000675}
676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000678/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100679 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200682 int (*f_rng)(void *, unsigned char *, size_t),
683 void *p_rng,
684 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100685 const unsigned char *label, size_t label_len,
686 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100687 const unsigned char *input,
688 unsigned char *output,
689 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000690{
Paul Bakker23986e52011-04-24 08:57:21 +0000691 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100692 size_t ilen, i, pad_len;
693 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
695 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000696 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 const mbedtls_md_info_t *md_info;
698 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100699
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100700 /*
701 * Parameters sanity checks
702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
704 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
706 ilen = ctx->len;
707
Paul Bakker27fdf462011-06-09 13:55:13 +0000708 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100712 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100714
715 /*
716 * RSA operation
717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 ret = ( mode == MBEDTLS_RSA_PUBLIC )
719 ? mbedtls_rsa_public( ctx, input, buf )
720 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000721
722 if( ret != 0 )
723 return( ret );
724
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100725 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100726 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_md_init( &md_ctx );
731 mbedtls_md_setup( &md_ctx, md_info, 0 );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100732
733 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100735
736 /* seed: Apply seedMask to maskedSeed */
737 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
738 &md_ctx );
739
740 /* DB: Apply dbMask to maskedDB */
741 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
742 &md_ctx );
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100745
746 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100747 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100748 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000749 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100750 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000751
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100752 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100753
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100754 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100755
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100756 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100757 for( i = 0; i < hlen; i++ )
758 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100759
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100760 /* Get zero-padding len, but always read till end of buffer
761 * (minus one, for the 01 byte) */
762 pad_len = 0;
763 pad_done = 0;
764 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
765 {
766 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100767 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100768 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100769
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100770 p += pad_len;
771 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100772
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100773 /*
774 * The only information "leaked" is whether the padding was correct or not
775 * (eg, no data is copied if it was not correct). This meets the
776 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
777 * the different error conditions.
778 */
779 if( bad != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100781
Paul Bakker66d5d072014-06-17 16:39:18 +0200782 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakkerb3869132013-02-28 17:21:01 +0100784
785 *olen = ilen - (p - buf);
786 memcpy( output, p, *olen );
787
788 return( 0 );
789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100793/*
794 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200797 int (*f_rng)(void *, unsigned char *, size_t),
798 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100799 int mode, size_t *olen,
800 const unsigned char *input,
801 unsigned char *output,
802 size_t output_max_len)
803{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100804 int ret;
805 size_t ilen, pad_count = 0, i;
806 unsigned char *p, bad, pad_done = 0;
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
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100864 if( bad )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker8804f692013-02-28 18:06:26 +0100866
Paul Bakker66d5d072014-06-17 16:39:18 +0200867 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000869
Paul Bakker27fdf462011-06-09 13:55:13 +0000870 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 memcpy( output, p, *olen );
872
873 return( 0 );
874}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000876
877/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100878 * Do an RSA operation, then remove the message padding
879 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200881 int (*f_rng)(void *, unsigned char *, size_t),
882 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100883 int mode, size_t *olen,
884 const unsigned char *input,
885 unsigned char *output,
886 size_t output_max_len)
887{
888 switch( ctx->padding )
889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890#if defined(MBEDTLS_PKCS1_V15)
891 case MBEDTLS_RSA_PKCS_V15:
892 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +0200893 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +0200894#endif
Paul Bakkerb3869132013-02-28 17:21:01 +0100895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896#if defined(MBEDTLS_PKCS1_V21)
897 case MBEDTLS_RSA_PKCS_V21:
898 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +0200899 olen, input, output,
900 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +0100901#endif
902
903 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100905 }
906}
907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100909/*
910 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
911 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100913 int (*f_rng)(void *, unsigned char *, size_t),
914 void *p_rng,
915 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100917 unsigned int hashlen,
918 const unsigned char *hash,
919 unsigned char *sig )
920{
921 size_t olen;
922 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100924 unsigned int slen, hlen, offset = 0;
925 int ret;
926 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 const mbedtls_md_info_t *md_info;
928 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
931 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200932
933 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100935
936 olen = ctx->len;
937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +0100939 {
Paul Bakkerc70b9822013-04-07 22:00:46 +0200940 // Gather length of hash to sign
941 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200943 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100947 }
948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100950 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100954 slen = hlen;
955
956 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100958
959 memset( sig, 0, olen );
960
Paul Bakkerb3869132013-02-28 17:21:01 +0100961 // Generate salt of length slen
962 //
963 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
966 // Note: EMSA-PSS encoding is over the length of N - 1 bits
967 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200968 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +0100969 p += olen - hlen * 2 - 2;
970 *p++ = 0x01;
971 memcpy( p, salt, slen );
972 p += slen;
973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_md_init( &md_ctx );
975 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +0100976
977 // Generate H = Hash( M' )
978 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_md_starts( &md_ctx );
980 mbedtls_md_update( &md_ctx, p, 8 );
981 mbedtls_md_update( &md_ctx, hash, hashlen );
982 mbedtls_md_update( &md_ctx, salt, slen );
983 mbedtls_md_finish( &md_ctx, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100984
985 // Compensate for boundary condition when applying mask
986 //
987 if( msb % 8 == 0 )
988 offset = 1;
989
990 // maskedDB: Apply dbMask to DB
991 //
992 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100995
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200996 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +0100997 sig[0] &= 0xFF >> ( olen * 8 - msb );
998
999 p += hlen;
1000 *p++ = 0xBC;
1001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 return( ( mode == MBEDTLS_RSA_PUBLIC )
1003 ? mbedtls_rsa_public( ctx, sig, sig )
1004 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001005}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001009/*
1010 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1011 */
1012/*
1013 * Do an RSA operation to sign the message digest
1014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001016 int (*f_rng)(void *, unsigned char *, size_t),
1017 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001018 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001020 unsigned int hashlen,
1021 const unsigned char *hash,
1022 unsigned char *sig )
1023{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001024 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001025 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001026 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001027 unsigned char *sig_try = NULL, *verif = NULL;
1028 size_t i;
1029 unsigned char diff;
1030 volatile unsigned char diff_no_optimize;
1031 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1034 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001035
1036 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001037 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001042 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1046 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001047
Paul Bakkerc70b9822013-04-07 22:00:46 +02001048 nb_pad -= 10 + oid_size;
1049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001051 }
1052
Paul Bakkerc70b9822013-04-07 22:00:46 +02001053 nb_pad -= hashlen;
1054
Paul Bakkerb3869132013-02-28 17:21:01 +01001055 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001057
1058 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001060 memset( p, 0xFF, nb_pad );
1061 p += nb_pad;
1062 *p++ = 0;
1063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001065 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001066 memcpy( p, hash, hashlen );
1067 }
1068 else
1069 {
1070 /*
1071 * DigestInfo ::= SEQUENCE {
1072 * digestAlgorithm DigestAlgorithmIdentifier,
1073 * digest Digest }
1074 *
1075 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1076 *
1077 * Digest ::= OCTET STRING
1078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001080 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001082 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001084 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001085 memcpy( p, oid, oid_size );
1086 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001088 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001090 *p++ = hashlen;
1091 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001092 }
1093
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001094 if( mode == MBEDTLS_RSA_PUBLIC )
1095 return( mbedtls_rsa_public( ctx, sig, sig ) );
1096
1097 /*
1098 * In order to prevent Lenstra's attack, make the signature in a
1099 * temporary buffer and check it before returning it.
1100 */
1101 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001102 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001103 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1104
Simon Butcher1285ab52016-01-01 21:42:47 +00001105 verif = mbedtls_calloc( 1, ctx->len );
1106 if( verif == NULL )
1107 {
1108 mbedtls_free( sig_try );
1109 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1110 }
1111
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001112 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1113 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1114
1115 /* Compare in constant time just in case */
1116 for( diff = 0, i = 0; i < ctx->len; i++ )
1117 diff |= verif[i] ^ sig[i];
1118 diff_no_optimize = diff;
1119
1120 if( diff_no_optimize != 0 )
1121 {
1122 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1123 goto cleanup;
1124 }
1125
1126 memcpy( sig, sig_try, ctx->len );
1127
1128cleanup:
1129 mbedtls_free( sig_try );
1130 mbedtls_free( verif );
1131
1132 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001135
1136/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 * Do an RSA operation to sign the message digest
1138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001140 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001141 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001142 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001144 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001145 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 unsigned char *sig )
1147{
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 switch( ctx->padding )
1149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#if defined(MBEDTLS_PKCS1_V15)
1151 case MBEDTLS_RSA_PKCS_V15:
1152 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001153 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001154#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_PKCS1_V21)
1157 case MBEDTLS_RSA_PKCS_V21:
1158 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001159 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001160#endif
1161
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001165}
1166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001168/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001169 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001172 int (*f_rng)(void *, unsigned char *, size_t),
1173 void *p_rng,
1174 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001176 unsigned int hashlen,
1177 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001179 int expected_salt_len,
1180 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001181{
Paul Bakker23986e52011-04-24 08:57:21 +00001182 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001183 size_t siglen;
1184 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1186 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001187 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001188 unsigned int hlen;
1189 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 const mbedtls_md_info_t *md_info;
1191 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1194 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001195
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 siglen = ctx->len;
1197
Paul Bakker27fdf462011-06-09 13:55:13 +00001198 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1202 ? mbedtls_rsa_public( ctx, sig, buf )
1203 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001204
1205 if( ret != 0 )
1206 return( ret );
1207
1208 p = buf;
1209
Paul Bakkerb3869132013-02-28 17:21:01 +01001210 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001214 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001215 // Gather length of hash to sign
1216 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001218 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001222 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001225 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001229 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001230
Paul Bakkerb3869132013-02-28 17:21:01 +01001231 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001232
Paul Bakkerb3869132013-02-28 17:21:01 +01001233 // Note: EMSA-PSS verification is over the length of N - 1 bits
1234 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001235 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001236
Paul Bakkerb3869132013-02-28 17:21:01 +01001237 // Compensate for boundary condition when applying mask
1238 //
1239 if( msb % 8 == 0 )
1240 {
1241 p++;
1242 siglen -= 1;
1243 }
1244 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 mbedtls_md_init( &md_ctx );
1248 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001249
Paul Bakkerb3869132013-02-28 17:21:01 +01001250 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001251
Paul Bakkerb3869132013-02-28 17:21:01 +01001252 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001253
Paul Bakker4de44aa2013-12-31 11:43:01 +01001254 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001255 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001256
Paul Bakkerb3869132013-02-28 17:21:01 +01001257 if( p == buf + siglen ||
1258 *p++ != 0x01 )
1259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 mbedtls_md_free( &md_ctx );
1261 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001262 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001263
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001264 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001265 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001268 slen != (size_t) expected_salt_len )
1269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 mbedtls_md_free( &md_ctx );
1271 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001272 }
1273
Paul Bakkerb3869132013-02-28 17:21:01 +01001274 // Generate H = Hash( M' )
1275 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_md_starts( &md_ctx );
1277 mbedtls_md_update( &md_ctx, zeros, 8 );
1278 mbedtls_md_update( &md_ctx, hash, hashlen );
1279 mbedtls_md_update( &md_ctx, p, slen );
1280 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001283
Paul Bakkerb3869132013-02-28 17:21:01 +01001284 if( memcmp( p + slen, result, hlen ) == 0 )
1285 return( 0 );
1286 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001288}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001289
1290/*
1291 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001294 int (*f_rng)(void *, unsigned char *, size_t),
1295 void *p_rng,
1296 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001298 unsigned int hashlen,
1299 const unsigned char *hash,
1300 const unsigned char *sig )
1301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1303 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001304 : md_alg;
1305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001307 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001309 sig ) );
1310
1311}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001315/*
1316 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001319 int (*f_rng)(void *, unsigned char *, size_t),
1320 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001321 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001323 unsigned int hashlen,
1324 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001325 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001326{
1327 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001328 size_t len, siglen, asn1_len;
1329 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1331 mbedtls_md_type_t msg_md_alg;
1332 const mbedtls_md_info_t *md_info;
1333 mbedtls_asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1336 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001337
1338 siglen = ctx->len;
1339
1340 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1344 ? mbedtls_rsa_public( ctx, sig, buf )
1345 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001346
1347 if( ret != 0 )
1348 return( ret );
1349
1350 p = buf;
1351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1353 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001354
1355 while( *p != 0 )
1356 {
1357 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001359 p++;
1360 }
1361 p++;
1362
1363 len = siglen - ( p - buf );
1364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001366 {
1367 if( memcmp( p, hash, hashlen ) == 0 )
1368 return( 0 );
1369 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 }
1372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001374 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1376 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001377
1378 end = p + len;
1379
1380 // Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1381 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1383 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1384 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001385
1386 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1390 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1391 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001392
1393 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1397 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001398
1399 oid.p = p;
1400 p += oid.len;
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1403 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001404
1405 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001407
1408 /*
1409 * assume the algorithm parameters must be NULL
1410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1412 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1415 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001416
1417 if( asn1_len != hashlen )
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 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001422
1423 p += hashlen;
1424
1425 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001427
1428 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001429}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001431
1432/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001433 * Do an RSA operation and check the message digest
1434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001436 int (*f_rng)(void *, unsigned char *, size_t),
1437 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001438 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001440 unsigned int hashlen,
1441 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001442 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001443{
1444 switch( ctx->padding )
1445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#if defined(MBEDTLS_PKCS1_V15)
1447 case MBEDTLS_RSA_PKCS_V15:
1448 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001449 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001450#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#if defined(MBEDTLS_PKCS1_V21)
1453 case MBEDTLS_RSA_PKCS_V21:
1454 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001455 hashlen, hash, sig );
1456#endif
1457
1458 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001460 }
1461}
1462
1463/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001464 * Copy the components of an RSA key
1465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001467{
1468 int ret;
1469
1470 dst->ver = src->ver;
1471 dst->len = src->len;
1472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1474 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1477 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1478 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1479 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1480 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1481 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1484 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1485 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1488 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001489
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001490 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001491 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001492
1493cleanup:
1494 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001496
1497 return( ret );
1498}
1499
1500/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 * Free the components of an RSA key
1502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001504{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1506 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1507 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1508 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1509 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511#if defined(MBEDTLS_THREADING_C)
1512 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001513#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001514}
1515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001517
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001518#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001519
1520/*
1521 * Example RSA-1024 keypair, for test purposes
1522 */
1523#define KEY_LEN 128
1524
1525#define RSA_N "9292758453063D803DD603D5E777D788" \
1526 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1527 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1528 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1529 "93A89813FBF3C4F8066D2D800F7C38A8" \
1530 "1AE31942917403FF4946B0A83D3D3E05" \
1531 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1532 "5E94BB77B07507233A0BC7BAC8F90F79"
1533
1534#define RSA_E "10001"
1535
1536#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1537 "66CA472BC44D253102F8B4A9D3BFA750" \
1538 "91386C0077937FE33FA3252D28855837" \
1539 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1540 "DF79C5CE07EE72C7F123142198164234" \
1541 "CABB724CF78B8173B9F880FC86322407" \
1542 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1543 "071513A1E85B5DFA031F21ECAE91A34D"
1544
1545#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1546 "2C01CAD19EA484A87EA4377637E75500" \
1547 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1548 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1549
1550#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1551 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1552 "910E4168387E3C30AA1E00C339A79508" \
1553 "8452DD96A9A5EA5D9DCA68DA636032AF"
1554
1555#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1556 "3C94D22288ACD763FD8E5600ED4A702D" \
1557 "F84198A5F06C2E72236AE490C93F07F8" \
1558 "3CC559CD27BC2D1CA488811730BB5725"
1559
1560#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1561 "D8AAEA56749EA28623272E4F7D0592AF" \
1562 "7C1F1313CAC9471B5C523BFE592F517B" \
1563 "407A1BD76C164B93DA2D32A383E58357"
1564
1565#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1566 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1567 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1568 "A74206CEC169D74BF5A8C50D6F48EA08"
1569
1570#define PT_LEN 24
1571#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1572 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001575static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001576{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001577#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001578 size_t i;
1579
Paul Bakker545570e2010-07-18 09:00:25 +00001580 if( rng_state != NULL )
1581 rng_state = NULL;
1582
Paul Bakkera3d195c2011-11-27 21:07:34 +00001583 for( i = 0; i < len; ++i )
1584 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001585#else
1586 if( rng_state != NULL )
1587 rng_state = NULL;
1588
1589 arc4random_buf( output, len );
1590#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001591
Paul Bakkera3d195c2011-11-27 21:07:34 +00001592 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001593}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001595
Paul Bakker5121ce52009-01-03 21:22:43 +00001596/*
1597 * Checkup routine
1598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001600{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001601 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001603 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001605 unsigned char rsa_plaintext[PT_LEN];
1606 unsigned char rsa_decrypted[PT_LEN];
1607 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001609 unsigned char sha1sum[20];
1610#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
1614 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1616 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1617 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1618 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1619 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1620 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1621 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1622 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001623
1624 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1628 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001629 {
1630 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001632
1633 return( 1 );
1634 }
1635
1636 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001638
1639 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001642 rsa_plaintext, rsa_ciphertext ) != 0 )
1643 {
1644 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001646
1647 return( 1 );
1648 }
1649
1650 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001654 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001655 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001656 {
1657 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
1660 return( 1 );
1661 }
1662
1663 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1664 {
1665 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
1668 return( 1 );
1669 }
1670
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001671 if( verbose != 0 )
1672 mbedtls_printf( "passed\n" );
1673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001675 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001676 mbedtls_printf( "PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001681 sha1sum, rsa_ciphertext ) != 0 )
1682 {
1683 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001685
1686 return( 1 );
1687 }
1688
1689 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001693 sha1sum, rsa_ciphertext ) != 0 )
1694 {
1695 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001697
1698 return( 1 );
1699 }
1700
1701 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001702 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001704
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001705 if( verbose != 0 )
1706 mbedtls_printf( "\n" );
1707
Paul Bakker3d8fb632014-04-17 12:42:41 +02001708cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 mbedtls_rsa_free( &rsa );
1710#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001711 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001713 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001714}
1715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#endif /* MBEDTLS_RSA_C */