blob: 9fc80cdc624d66dadfe61eccade0a5759d279f56 [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
599 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100601
602 olen = ctx->len;
603
604 if( olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100606
607 nb_pad = olen - 3 - ilen;
608
609 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100613
614 while( nb_pad-- > 0 )
615 {
616 int rng_dl = 100;
617
618 do {
619 ret = f_rng( p_rng, p, 1 );
620 } while( *p == 0 && --rng_dl && ret == 0 );
621
622 // Check if RNG failed to generate data
623 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200624 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100626
627 p++;
628 }
629 }
630 else
631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100633
634 while( nb_pad-- > 0 )
635 *p++ = 0xFF;
636 }
637
638 *p++ = 0;
639 memcpy( p, input, ilen );
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 return( ( mode == MBEDTLS_RSA_PUBLIC )
642 ? mbedtls_rsa_public( ctx, output, output )
643 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100644}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100646
Paul Bakker5121ce52009-01-03 21:22:43 +0000647/*
648 * Add the message padding, then do an RSA operation
649 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000651 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000652 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000653 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000654 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 unsigned char *output )
656{
Paul Bakker5121ce52009-01-03 21:22:43 +0000657 switch( ctx->padding )
658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#if defined(MBEDTLS_PKCS1_V15)
660 case MBEDTLS_RSA_PKCS_V15:
661 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100662 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200663#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#if defined(MBEDTLS_PKCS1_V21)
666 case MBEDTLS_RSA_PKCS_V21:
667 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100668 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000669#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000670
671 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000673 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000674}
675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000677/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100678 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200681 int (*f_rng)(void *, unsigned char *, size_t),
682 void *p_rng,
683 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100684 const unsigned char *label, size_t label_len,
685 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100686 const unsigned char *input,
687 unsigned char *output,
688 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000689{
Paul Bakker23986e52011-04-24 08:57:21 +0000690 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100691 size_t ilen, i, pad_len;
692 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
694 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000695 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 const mbedtls_md_info_t *md_info;
697 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100698
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100699 /*
700 * Parameters sanity checks
701 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
703 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
705 ilen = ctx->len;
706
Paul Bakker27fdf462011-06-09 13:55:13 +0000707 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100711 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100713
714 /*
715 * RSA operation
716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 ret = ( mode == MBEDTLS_RSA_PUBLIC )
718 ? mbedtls_rsa_public( ctx, input, buf )
719 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000720
721 if( ret != 0 )
722 return( ret );
723
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100724 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100725 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_md_init( &md_ctx );
730 mbedtls_md_setup( &md_ctx, md_info, 0 );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100731
732 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100734
735 /* seed: Apply seedMask to maskedSeed */
736 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
737 &md_ctx );
738
739 /* DB: Apply dbMask to maskedDB */
740 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
741 &md_ctx );
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100744
745 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100746 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100747 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000748 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100749 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100751 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100752
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100753 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100754
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100755 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100756 for( i = 0; i < hlen; i++ )
757 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100758
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100759 /* Get zero-padding len, but always read till end of buffer
760 * (minus one, for the 01 byte) */
761 pad_len = 0;
762 pad_done = 0;
763 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
764 {
765 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100766 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100767 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100768
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100769 p += pad_len;
770 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100771
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100772 /*
773 * The only information "leaked" is whether the padding was correct or not
774 * (eg, no data is copied if it was not correct). This meets the
775 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
776 * the different error conditions.
777 */
778 if( bad != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100780
Paul Bakker66d5d072014-06-17 16:39:18 +0200781 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakkerb3869132013-02-28 17:21:01 +0100783
784 *olen = ilen - (p - buf);
785 memcpy( output, p, *olen );
786
787 return( 0 );
788}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100792/*
793 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200796 int (*f_rng)(void *, unsigned char *, size_t),
797 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100798 int mode, size_t *olen,
799 const unsigned char *input,
800 unsigned char *output,
801 size_t output_max_len)
802{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100803 int ret;
804 size_t ilen, pad_count = 0, i;
805 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
809 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100810
811 ilen = ctx->len;
812
813 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 ret = ( mode == MBEDTLS_RSA_PUBLIC )
817 ? mbedtls_rsa_public( ctx, input, buf )
818 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100819
820 if( ret != 0 )
821 return( ret );
822
823 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100824 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100825
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100826 /*
827 * Check and get padding len in "constant-time"
828 */
829 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100830
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100831 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000835
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100836 /* Get padding len, but always read till end of buffer
837 * (minus one, for the 00 byte) */
838 for( i = 0; i < ilen - 3; i++ )
839 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100840 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
841 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100842 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000843
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100844 p += pad_count;
845 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100846 }
847 else
848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100850
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100851 /* Get padding len, but always read till end of buffer
852 * (minus one, for the 00 byte) */
853 for( i = 0; i < ilen - 3; i++ )
854 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100855 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100856 pad_count += ( pad_done == 0 );
857 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100858
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100859 p += pad_count;
860 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +0000861 }
862
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100863 if( bad )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker8804f692013-02-28 18:06:26 +0100865
Paul Bakker66d5d072014-06-17 16:39:18 +0200866 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000868
Paul Bakker27fdf462011-06-09 13:55:13 +0000869 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000870 memcpy( output, p, *olen );
871
872 return( 0 );
873}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000875
876/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100877 * Do an RSA operation, then remove the message padding
878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200880 int (*f_rng)(void *, unsigned char *, size_t),
881 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100882 int mode, size_t *olen,
883 const unsigned char *input,
884 unsigned char *output,
885 size_t output_max_len)
886{
887 switch( ctx->padding )
888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_PKCS1_V15)
890 case MBEDTLS_RSA_PKCS_V15:
891 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +0200892 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +0200893#endif
Paul Bakkerb3869132013-02-28 17:21:01 +0100894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895#if defined(MBEDTLS_PKCS1_V21)
896 case MBEDTLS_RSA_PKCS_V21:
897 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +0200898 olen, input, output,
899 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +0100900#endif
901
902 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100904 }
905}
906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100908/*
909 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100912 int (*f_rng)(void *, unsigned char *, size_t),
913 void *p_rng,
914 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100916 unsigned int hashlen,
917 const unsigned char *hash,
918 unsigned char *sig )
919{
920 size_t olen;
921 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100923 unsigned int slen, hlen, offset = 0;
924 int ret;
925 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 const mbedtls_md_info_t *md_info;
927 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
930 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200931
932 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100934
935 olen = ctx->len;
936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +0100938 {
Paul Bakkerc70b9822013-04-07 22:00:46 +0200939 // Gather length of hash to sign
940 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200942 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100946 }
947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100949 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100953 slen = hlen;
954
955 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100957
958 memset( sig, 0, olen );
959
Paul Bakkerb3869132013-02-28 17:21:01 +0100960 // Generate salt of length slen
961 //
962 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100964
965 // Note: EMSA-PSS encoding is over the length of N - 1 bits
966 //
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
976 // Generate H = Hash( M' )
977 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 mbedtls_md_starts( &md_ctx );
979 mbedtls_md_update( &md_ctx, p, 8 );
980 mbedtls_md_update( &md_ctx, hash, hashlen );
981 mbedtls_md_update( &md_ctx, salt, slen );
982 mbedtls_md_finish( &md_ctx, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100983
984 // Compensate for boundary condition when applying mask
985 //
986 if( msb % 8 == 0 )
987 offset = 1;
988
989 // maskedDB: Apply dbMask to DB
990 //
991 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100994
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200995 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +0100996 sig[0] &= 0xFF >> ( olen * 8 - msb );
997
998 p += hlen;
999 *p++ = 0xBC;
1000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 return( ( mode == MBEDTLS_RSA_PUBLIC )
1002 ? mbedtls_rsa_public( ctx, sig, sig )
1003 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001004}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001008/*
1009 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1010 */
1011/*
1012 * Do an RSA operation to sign the message digest
1013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001015 int (*f_rng)(void *, unsigned char *, size_t),
1016 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001017 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001019 unsigned int hashlen,
1020 const unsigned char *hash,
1021 unsigned char *sig )
1022{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001023 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001024 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001025 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001026 unsigned char *sig_try = NULL, *verif = NULL;
1027 size_t i;
1028 unsigned char diff;
1029 volatile unsigned char diff_no_optimize;
1030 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1033 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001034
1035 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001036 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001039 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001041 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1045 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001046
Paul Bakkerc70b9822013-04-07 22:00:46 +02001047 nb_pad -= 10 + oid_size;
1048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001050 }
1051
Paul Bakkerc70b9822013-04-07 22:00:46 +02001052 nb_pad -= hashlen;
1053
Paul Bakkerb3869132013-02-28 17:21:01 +01001054 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001056
1057 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001059 memset( p, 0xFF, nb_pad );
1060 p += nb_pad;
1061 *p++ = 0;
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001064 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001065 memcpy( p, hash, hashlen );
1066 }
1067 else
1068 {
1069 /*
1070 * DigestInfo ::= SEQUENCE {
1071 * digestAlgorithm DigestAlgorithmIdentifier,
1072 * digest Digest }
1073 *
1074 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1075 *
1076 * Digest ::= OCTET STRING
1077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001079 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001081 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001083 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001084 memcpy( p, oid, oid_size );
1085 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001087 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001089 *p++ = hashlen;
1090 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001091 }
1092
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001093 if( mode == MBEDTLS_RSA_PUBLIC )
1094 return( mbedtls_rsa_public( ctx, sig, sig ) );
1095
1096 /*
1097 * In order to prevent Lenstra's attack, make the signature in a
1098 * temporary buffer and check it before returning it.
1099 */
1100 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001101 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001102 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1103
Simon Butcher1285ab52016-01-01 21:42:47 +00001104 verif = mbedtls_calloc( 1, ctx->len );
1105 if( verif == NULL )
1106 {
1107 mbedtls_free( sig_try );
1108 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1109 }
1110
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001111 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1112 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1113
1114 /* Compare in constant time just in case */
1115 for( diff = 0, i = 0; i < ctx->len; i++ )
1116 diff |= verif[i] ^ sig[i];
1117 diff_no_optimize = diff;
1118
1119 if( diff_no_optimize != 0 )
1120 {
1121 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1122 goto cleanup;
1123 }
1124
1125 memcpy( sig, sig_try, ctx->len );
1126
1127cleanup:
1128 mbedtls_free( sig_try );
1129 mbedtls_free( verif );
1130
1131 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001132}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001134
1135/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001136 * Do an RSA operation to sign the message digest
1137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001139 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001140 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001141 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001143 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001144 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001145 unsigned char *sig )
1146{
Paul Bakker5121ce52009-01-03 21:22:43 +00001147 switch( ctx->padding )
1148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#if defined(MBEDTLS_PKCS1_V15)
1150 case MBEDTLS_RSA_PKCS_V15:
1151 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001152 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001153#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_PKCS1_V21)
1156 case MBEDTLS_RSA_PKCS_V21:
1157 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001159#endif
1160
Paul Bakker5121ce52009-01-03 21:22:43 +00001161 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001163 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001164}
1165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001167/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001168 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001169 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001171 int (*f_rng)(void *, unsigned char *, size_t),
1172 void *p_rng,
1173 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001175 unsigned int hashlen,
1176 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001178 int expected_salt_len,
1179 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001180{
Paul Bakker23986e52011-04-24 08:57:21 +00001181 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001182 size_t siglen;
1183 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1185 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001186 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001187 unsigned int hlen;
1188 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 const mbedtls_md_info_t *md_info;
1190 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1193 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001194
Paul Bakker5121ce52009-01-03 21:22:43 +00001195 siglen = ctx->len;
1196
Paul Bakker27fdf462011-06-09 13:55:13 +00001197 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1201 ? mbedtls_rsa_public( ctx, sig, buf )
1202 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001203
1204 if( ret != 0 )
1205 return( ret );
1206
1207 p = buf;
1208
Paul Bakkerb3869132013-02-28 17:21:01 +01001209 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001214 // Gather length of hash to sign
1215 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001217 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001221 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001224 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001228 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001229
Paul Bakkerb3869132013-02-28 17:21:01 +01001230 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001231
Paul Bakkerb3869132013-02-28 17:21:01 +01001232 // Note: EMSA-PSS verification is over the length of N - 1 bits
1233 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001234 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001235
Paul Bakkerb3869132013-02-28 17:21:01 +01001236 // Compensate for boundary condition when applying mask
1237 //
1238 if( msb % 8 == 0 )
1239 {
1240 p++;
1241 siglen -= 1;
1242 }
1243 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_md_init( &md_ctx );
1247 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001248
Paul Bakkerb3869132013-02-28 17:21:01 +01001249 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001250
Paul Bakkerb3869132013-02-28 17:21:01 +01001251 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001252
Paul Bakker4de44aa2013-12-31 11:43:01 +01001253 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001254 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001255
Paul Bakkerb3869132013-02-28 17:21:01 +01001256 if( p == buf + siglen ||
1257 *p++ != 0x01 )
1258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 mbedtls_md_free( &md_ctx );
1260 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001261 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001262
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001263 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001264 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001267 slen != (size_t) expected_salt_len )
1268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 mbedtls_md_free( &md_ctx );
1270 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001271 }
1272
Paul Bakkerb3869132013-02-28 17:21:01 +01001273 // Generate H = Hash( M' )
1274 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 mbedtls_md_starts( &md_ctx );
1276 mbedtls_md_update( &md_ctx, zeros, 8 );
1277 mbedtls_md_update( &md_ctx, hash, hashlen );
1278 mbedtls_md_update( &md_ctx, p, slen );
1279 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001282
Paul Bakkerb3869132013-02-28 17:21:01 +01001283 if( memcmp( p + slen, result, hlen ) == 0 )
1284 return( 0 );
1285 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001287}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001288
1289/*
1290 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001293 int (*f_rng)(void *, unsigned char *, size_t),
1294 void *p_rng,
1295 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001297 unsigned int hashlen,
1298 const unsigned char *hash,
1299 const unsigned char *sig )
1300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1302 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001303 : md_alg;
1304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001306 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001308 sig ) );
1309
1310}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001314/*
1315 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001318 int (*f_rng)(void *, unsigned char *, size_t),
1319 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001320 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001322 unsigned int hashlen,
1323 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001324 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001325{
1326 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001327 size_t len, siglen, asn1_len;
1328 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1330 mbedtls_md_type_t msg_md_alg;
1331 const mbedtls_md_info_t *md_info;
1332 mbedtls_asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1335 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001336
1337 siglen = ctx->len;
1338
1339 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1343 ? mbedtls_rsa_public( ctx, sig, buf )
1344 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001345
1346 if( ret != 0 )
1347 return( ret );
1348
1349 p = buf;
1350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1352 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001353
1354 while( *p != 0 )
1355 {
1356 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001358 p++;
1359 }
1360 p++;
1361
1362 len = siglen - ( p - buf );
1363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001365 {
1366 if( memcmp( p, hash, hashlen ) == 0 )
1367 return( 0 );
1368 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001370 }
1371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001373 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1375 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001376
1377 end = p + len;
1378
1379 // Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1380 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1382 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1383 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001384
1385 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1389 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1390 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001391
1392 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1396 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001397
1398 oid.p = p;
1399 p += oid.len;
1400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1402 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001403
1404 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001406
1407 /*
1408 * assume the algorithm parameters must be NULL
1409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1411 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1414 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001415
1416 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001418
1419 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001421
1422 p += hashlen;
1423
1424 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001426
1427 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001428}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001430
1431/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001432 * Do an RSA operation and check the message digest
1433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001435 int (*f_rng)(void *, unsigned char *, size_t),
1436 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001437 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001439 unsigned int hashlen,
1440 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001441 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001442{
1443 switch( ctx->padding )
1444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#if defined(MBEDTLS_PKCS1_V15)
1446 case MBEDTLS_RSA_PKCS_V15:
1447 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001448 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001449#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451#if defined(MBEDTLS_PKCS1_V21)
1452 case MBEDTLS_RSA_PKCS_V21:
1453 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001454 hashlen, hash, sig );
1455#endif
1456
1457 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001459 }
1460}
1461
1462/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001463 * Copy the components of an RSA key
1464 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001466{
1467 int ret;
1468
1469 dst->ver = src->ver;
1470 dst->len = src->len;
1471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1473 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1476 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1477 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1478 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1479 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1480 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1483 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1484 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1487 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001488
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001489 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001490 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001491
1492cleanup:
1493 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001495
1496 return( ret );
1497}
1498
1499/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001500 * Free the components of an RSA key
1501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001503{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1505 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1506 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1507 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1508 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#if defined(MBEDTLS_THREADING_C)
1511 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001512#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001513}
1514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001516
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001517#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001518
1519/*
1520 * Example RSA-1024 keypair, for test purposes
1521 */
1522#define KEY_LEN 128
1523
1524#define RSA_N "9292758453063D803DD603D5E777D788" \
1525 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1526 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1527 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1528 "93A89813FBF3C4F8066D2D800F7C38A8" \
1529 "1AE31942917403FF4946B0A83D3D3E05" \
1530 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1531 "5E94BB77B07507233A0BC7BAC8F90F79"
1532
1533#define RSA_E "10001"
1534
1535#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1536 "66CA472BC44D253102F8B4A9D3BFA750" \
1537 "91386C0077937FE33FA3252D28855837" \
1538 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1539 "DF79C5CE07EE72C7F123142198164234" \
1540 "CABB724CF78B8173B9F880FC86322407" \
1541 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1542 "071513A1E85B5DFA031F21ECAE91A34D"
1543
1544#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1545 "2C01CAD19EA484A87EA4377637E75500" \
1546 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1547 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1548
1549#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1550 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1551 "910E4168387E3C30AA1E00C339A79508" \
1552 "8452DD96A9A5EA5D9DCA68DA636032AF"
1553
1554#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1555 "3C94D22288ACD763FD8E5600ED4A702D" \
1556 "F84198A5F06C2E72236AE490C93F07F8" \
1557 "3CC559CD27BC2D1CA488811730BB5725"
1558
1559#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1560 "D8AAEA56749EA28623272E4F7D0592AF" \
1561 "7C1F1313CAC9471B5C523BFE592F517B" \
1562 "407A1BD76C164B93DA2D32A383E58357"
1563
1564#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1565 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1566 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1567 "A74206CEC169D74BF5A8C50D6F48EA08"
1568
1569#define PT_LEN 24
1570#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1571 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001574static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001575{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001576#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001577 size_t i;
1578
Paul Bakker545570e2010-07-18 09:00:25 +00001579 if( rng_state != NULL )
1580 rng_state = NULL;
1581
Paul Bakkera3d195c2011-11-27 21:07:34 +00001582 for( i = 0; i < len; ++i )
1583 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001584#else
1585 if( rng_state != NULL )
1586 rng_state = NULL;
1587
1588 arc4random_buf( output, len );
1589#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001590
Paul Bakkera3d195c2011-11-27 21:07:34 +00001591 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001592}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001594
Paul Bakker5121ce52009-01-03 21:22:43 +00001595/*
1596 * Checkup routine
1597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001599{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001600 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001602 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001604 unsigned char rsa_plaintext[PT_LEN];
1605 unsigned char rsa_decrypted[PT_LEN];
1606 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001608 unsigned char sha1sum[20];
1609#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001612
1613 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1615 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1616 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1617 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1618 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1619 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1620 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1621 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001622
1623 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1627 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001628 {
1629 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001631
1632 return( 1 );
1633 }
1634
1635 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001637
1638 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001641 rsa_plaintext, rsa_ciphertext ) != 0 )
1642 {
1643 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001645
1646 return( 1 );
1647 }
1648
1649 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001653 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001654 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001655 {
1656 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
1659 return( 1 );
1660 }
1661
1662 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1663 {
1664 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
1667 return( 1 );
1668 }
1669
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001670 if( verbose != 0 )
1671 mbedtls_printf( "passed\n" );
1672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001674 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001675 mbedtls_printf( "PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001680 sha1sum, rsa_ciphertext ) != 0 )
1681 {
1682 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001684
1685 return( 1 );
1686 }
1687
1688 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001692 sha1sum, rsa_ciphertext ) != 0 )
1693 {
1694 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001696
1697 return( 1 );
1698 }
1699
1700 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001701 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001704 if( verbose != 0 )
1705 mbedtls_printf( "\n" );
1706
Paul Bakker3d8fb632014-04-17 12:42:41 +02001707cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708 mbedtls_rsa_free( &rsa );
1709#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001710 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001712 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001713}
1714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#endif /* MBEDTLS_RSA_C */