blob: 2baf532575d906287b723218c335439039b36317 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107 /*
108 * find primes P and Q with Q < P so that:
109 * GCD( E, (P-1)*(Q-1) ) == 1
110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113 do
114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, ( nbits + 1 ) >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000116 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, ( nbits + 1 ) >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000119 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
122 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 continue;
126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200128 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 continue;
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
132 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
133 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
134 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138 /*
139 * D = E^-1 mod ((P-1)*(Q-1))
140 * DP = D mod (P - 1)
141 * DQ = D mod (Q - 1)
142 * QP = Q^-1 mod P
143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
145 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
146 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
147 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200149 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151cleanup:
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
155 if( ret != 0 )
156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_rsa_free( ctx );
158 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 }
160
Paul Bakker48377d92013-08-30 12:06:24 +0200161 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162}
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166/*
167 * Check a public RSA key
168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000170{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000171 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000173
Paul Bakker48377d92013-08-30 12:06:24 +0200174 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200178 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
179 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200182 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
184 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
186 return( 0 );
187}
188
189/*
190 * Check a private RSA key
191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000193{
194 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 return( ret );
199
Paul Bakker37940d9f2009-07-10 22:38:58 +0000200 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
204 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
205 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
206 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
209 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
210 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
211 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
212 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
213 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
216 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
217 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
220 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
221 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000222 /*
223 * Check for a valid PKCS1v2 private key
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
226 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
227 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
228 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
229 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
230 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
231 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 }
Paul Bakker48377d92013-08-30 12:06:24 +0200235
Paul Bakker5121ce52009-01-03 21:22:43 +0000236cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
238 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
239 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
240 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000243 return( ret );
244
Paul Bakker6c591fa2011-05-05 11:49:20 +0000245 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000247
248 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249}
250
251/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100252 * Check if contexts holding a public and private key match
253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100255{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
257 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100260 }
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
263 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 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
268 return( 0 );
269}
270
271/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 * Do an RSA public key operation
273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000275 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000276 unsigned char *output )
277{
Paul Bakker23986e52011-04-24 08:57:21 +0000278 int ret;
279 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200284#if defined(MBEDTLS_THREADING_C)
285 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
286 return( ret );
287#endif
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200293 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
294 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 }
296
297 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
299 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300
301cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200303 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
304 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100305#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312 return( 0 );
313}
314
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200315/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200316 * Generate or update blinding values, see section 10 of:
317 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200318 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200319 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200320 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200321static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200322 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
323{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200324 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200325
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200326 if( ctx->Vf.p != NULL )
327 {
328 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
330 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
331 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
332 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200333
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200334 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200335 }
336
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200337 /* Unblinding value: Vf = random number, invertible mod N */
338 do {
339 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
343 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
344 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200345
346 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
348 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 +0200349
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200350
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200351cleanup:
352 return( ret );
353}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200354
Paul Bakker5121ce52009-01-03 21:22:43 +0000355/*
356 * Do an RSA private key operation
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200359 int (*f_rng)(void *, unsigned char *, size_t),
360 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000361 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 unsigned char *output )
363{
Paul Bakker23986e52011-04-24 08:57:21 +0000364 int ret;
365 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_mpi T, T1, T2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000367
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100368 /* Make sure we have private key info, prevent possible misuse */
369 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
370 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200374#if defined(MBEDTLS_THREADING_C)
375 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
376 return( ret );
377#endif
378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
380 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200382 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
383 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 }
385
Paul Bakkerf451bac2013-08-30 15:37:02 +0200386 if( f_rng != NULL )
387 {
388 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200389 * Blinding
390 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200391 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200392 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
393 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200395 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#if defined(MBEDTLS_RSA_NO_CRT)
398 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100399#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200400 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000401 * faster decryption using the CRT
402 *
403 * T1 = input ^ dP mod P
404 * T2 = input ^ dQ mod Q
405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
407 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000408
409 /*
410 * T = (T1 - T2) * (Q^-1 mod P) mod P
411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
413 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
414 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
416 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200417 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
420 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
421#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200422
Paul Bakkerf451bac2013-08-30 15:37:02 +0200423 if( f_rng != NULL )
424 {
425 /*
426 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200427 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200428 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200429 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200431 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000432
433 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200438 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
439 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200440#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000443
444 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447 return( 0 );
448}
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000451/**
452 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
453 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000454 * \param dst buffer to mask
455 * \param dlen length of destination buffer
456 * \param src source of the mask generation
457 * \param slen length of the source buffer
458 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000459 */
Paul Bakker48377d92013-08-30 12:06:24 +0200460static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000462{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000464 unsigned char counter[4];
465 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000466 unsigned int hlen;
467 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000470 memset( counter, 0, 4 );
471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000473
474 // Generate and apply dbMask
475 //
476 p = dst;
477
478 while( dlen > 0 )
479 {
480 use_len = hlen;
481 if( dlen < hlen )
482 use_len = dlen;
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_md_starts( md_ctx );
485 mbedtls_md_update( md_ctx, src, slen );
486 mbedtls_md_update( md_ctx, counter, 4 );
487 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000488
489 for( i = 0; i < use_len; ++i )
490 *p++ ^= mask[i];
491
492 counter[3]++;
493
494 dlen -= use_len;
495 }
496}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100500/*
501 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
502 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100504 int (*f_rng)(void *, unsigned char *, size_t),
505 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100506 int mode,
507 const unsigned char *label, size_t label_len,
508 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100509 const unsigned char *input,
510 unsigned char *output )
511{
512 size_t olen;
513 int ret;
514 unsigned char *p = output;
515 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 const mbedtls_md_info_t *md_info;
517 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
520 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200521
522 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100526 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100528
529 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100531
Janos Follatheddfe8f2016-02-08 14:52:29 +0000532 // first comparison checks for overflow
533 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100535
536 memset( output, 0, olen );
537
538 *p++ = 0;
539
540 // Generate a random octet string seed
541 //
542 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100544
545 p += hlen;
546
547 // Construct DB
548 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100550 p += hlen;
551 p += olen - 2 * hlen - 2 - ilen;
552 *p++ = 1;
553 memcpy( p, input, ilen );
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_init( &md_ctx );
556 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +0100557
558 // maskedDB: Apply dbMask to DB
559 //
560 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
561 &md_ctx );
562
563 // maskedSeed: Apply seedMask to seed
564 //
565 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
566 &md_ctx );
567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 return( ( mode == MBEDTLS_RSA_PUBLIC )
571 ? mbedtls_rsa_public( ctx, output, output )
572 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100577/*
578 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100581 int (*f_rng)(void *, unsigned char *, size_t),
582 void *p_rng,
583 int mode, size_t ilen,
584 const unsigned char *input,
585 unsigned char *output )
586{
587 size_t nb_pad, olen;
588 int ret;
589 unsigned char *p = output;
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
592 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200593
594 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100596
597 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100598
Janos Follatheddfe8f2016-02-08 14:52:29 +0000599 // first comparison checks for overflow
600 if( ilen + 11 < ilen || olen < ilen + 11 )
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 nb_pad = olen - 3 - ilen;
604
605 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100609
610 while( nb_pad-- > 0 )
611 {
612 int rng_dl = 100;
613
614 do {
615 ret = f_rng( p_rng, p, 1 );
616 } while( *p == 0 && --rng_dl && ret == 0 );
617
618 // Check if RNG failed to generate data
619 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200620 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100622
623 p++;
624 }
625 }
626 else
627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100629
630 while( nb_pad-- > 0 )
631 *p++ = 0xFF;
632 }
633
634 *p++ = 0;
635 memcpy( p, input, ilen );
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( ( mode == MBEDTLS_RSA_PUBLIC )
638 ? mbedtls_rsa_public( ctx, output, output )
639 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100640}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100642
Paul Bakker5121ce52009-01-03 21:22:43 +0000643/*
644 * Add the message padding, then do an RSA operation
645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000647 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000648 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000649 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000650 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 unsigned char *output )
652{
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 switch( ctx->padding )
654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_PKCS1_V15)
656 case MBEDTLS_RSA_PKCS_V15:
657 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100658 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200659#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661#if defined(MBEDTLS_PKCS1_V21)
662 case MBEDTLS_RSA_PKCS_V21:
663 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100664 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000665#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000666
667 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000670}
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000673/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100674 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200677 int (*f_rng)(void *, unsigned char *, size_t),
678 void *p_rng,
679 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100680 const unsigned char *label, size_t label_len,
681 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100682 const unsigned char *input,
683 unsigned char *output,
684 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000685{
Paul Bakker23986e52011-04-24 08:57:21 +0000686 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100687 size_t ilen, i, pad_len;
688 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
690 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000691 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 const mbedtls_md_info_t *md_info;
693 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100694
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100695 /*
696 * Parameters sanity checks
697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
699 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
701 ilen = ctx->len;
702
Paul Bakker27fdf462011-06-09 13:55:13 +0000703 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100707 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100709
710 /*
711 * RSA operation
712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 ret = ( mode == MBEDTLS_RSA_PUBLIC )
714 ? mbedtls_rsa_public( ctx, input, buf )
715 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
717 if( ret != 0 )
718 return( ret );
719
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100720 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100721 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_md_init( &md_ctx );
726 mbedtls_md_setup( &md_ctx, md_info, 0 );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100727
728 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100730
731 /* seed: Apply seedMask to maskedSeed */
732 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
733 &md_ctx );
734
735 /* DB: Apply dbMask to maskedDB */
736 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
737 &md_ctx );
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100740
741 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100742 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100743 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000744 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100745 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000746
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100747 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100748
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100749 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100750
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100751 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100752 for( i = 0; i < hlen; i++ )
753 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100754
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100755 /* Get zero-padding len, but always read till end of buffer
756 * (minus one, for the 01 byte) */
757 pad_len = 0;
758 pad_done = 0;
759 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
760 {
761 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100762 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100763 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100764
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100765 p += pad_len;
766 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100767
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100768 /*
769 * The only information "leaked" is whether the padding was correct or not
770 * (eg, no data is copied if it was not correct). This meets the
771 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
772 * the different error conditions.
773 */
774 if( bad != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100776
Paul Bakker66d5d072014-06-17 16:39:18 +0200777 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakkerb3869132013-02-28 17:21:01 +0100779
780 *olen = ilen - (p - buf);
781 memcpy( output, p, *olen );
782
783 return( 0 );
784}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100788/*
789 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200792 int (*f_rng)(void *, unsigned char *, size_t),
793 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100794 int mode, size_t *olen,
795 const unsigned char *input,
796 unsigned char *output,
797 size_t output_max_len)
798{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100799 int ret;
800 size_t ilen, pad_count = 0, i;
801 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
805 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100806
807 ilen = ctx->len;
808
809 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 ret = ( mode == MBEDTLS_RSA_PUBLIC )
813 ? mbedtls_rsa_public( ctx, input, buf )
814 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100815
816 if( ret != 0 )
817 return( ret );
818
819 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100820 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100821
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100822 /*
823 * Check and get padding len in "constant-time"
824 */
825 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100826
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100827 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000831
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100832 /* Get padding len, but always read till end of buffer
833 * (minus one, for the 00 byte) */
834 for( i = 0; i < ilen - 3; i++ )
835 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100836 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
837 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100838 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000839
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100840 p += pad_count;
841 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100842 }
843 else
844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100846
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100847 /* Get padding len, but always read till end of buffer
848 * (minus one, for the 00 byte) */
849 for( i = 0; i < ilen - 3; i++ )
850 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100851 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100852 pad_count += ( pad_done == 0 );
853 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100854
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100855 p += pad_count;
856 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 }
858
Janos Follathb6eb1ca2016-02-08 13:59:25 +0000859 if( pad_count < 8 )
860 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
861
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100862 if( bad )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker8804f692013-02-28 18:06:26 +0100864
Paul Bakker66d5d072014-06-17 16:39:18 +0200865 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000867
Paul Bakker27fdf462011-06-09 13:55:13 +0000868 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 memcpy( output, p, *olen );
870
871 return( 0 );
872}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000874
875/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100876 * Do an RSA operation, then remove the message padding
877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200879 int (*f_rng)(void *, unsigned char *, size_t),
880 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100881 int mode, size_t *olen,
882 const unsigned char *input,
883 unsigned char *output,
884 size_t output_max_len)
885{
886 switch( ctx->padding )
887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888#if defined(MBEDTLS_PKCS1_V15)
889 case MBEDTLS_RSA_PKCS_V15:
890 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +0200891 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +0200892#endif
Paul Bakkerb3869132013-02-28 17:21:01 +0100893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894#if defined(MBEDTLS_PKCS1_V21)
895 case MBEDTLS_RSA_PKCS_V21:
896 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +0200897 olen, input, output,
898 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +0100899#endif
900
901 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100903 }
904}
905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100907/*
908 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100911 int (*f_rng)(void *, unsigned char *, size_t),
912 void *p_rng,
913 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100915 unsigned int hashlen,
916 const unsigned char *hash,
917 unsigned char *sig )
918{
919 size_t olen;
920 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100922 unsigned int slen, hlen, offset = 0;
923 int ret;
924 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 const mbedtls_md_info_t *md_info;
926 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
929 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200930
931 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100933
934 olen = ctx->len;
935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +0100937 {
Paul Bakkerc70b9822013-04-07 22:00:46 +0200938 // Gather length of hash to sign
939 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200941 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100945 }
946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100948 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100952 slen = hlen;
953
954 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100956
957 memset( sig, 0, olen );
958
Paul Bakkerb3869132013-02-28 17:21:01 +0100959 // Generate salt of length slen
960 //
961 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100963
964 // Note: EMSA-PSS encoding is over the length of N - 1 bits
965 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200966 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +0100967 p += olen - hlen * 2 - 2;
968 *p++ = 0x01;
969 memcpy( p, salt, slen );
970 p += slen;
971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_md_init( &md_ctx );
973 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +0100974
975 // Generate H = Hash( M' )
976 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_md_starts( &md_ctx );
978 mbedtls_md_update( &md_ctx, p, 8 );
979 mbedtls_md_update( &md_ctx, hash, hashlen );
980 mbedtls_md_update( &md_ctx, salt, slen );
981 mbedtls_md_finish( &md_ctx, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100982
983 // Compensate for boundary condition when applying mask
984 //
985 if( msb % 8 == 0 )
986 offset = 1;
987
988 // maskedDB: Apply dbMask to DB
989 //
990 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100993
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200994 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +0100995 sig[0] &= 0xFF >> ( olen * 8 - msb );
996
997 p += hlen;
998 *p++ = 0xBC;
999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 return( ( mode == MBEDTLS_RSA_PUBLIC )
1001 ? mbedtls_rsa_public( ctx, sig, sig )
1002 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001007/*
1008 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1009 */
1010/*
1011 * Do an RSA operation to sign the message digest
1012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001014 int (*f_rng)(void *, unsigned char *, size_t),
1015 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001016 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001018 unsigned int hashlen,
1019 const unsigned char *hash,
1020 unsigned char *sig )
1021{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001022 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001023 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001024 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001025 unsigned char *sig_try = NULL, *verif = NULL;
1026 size_t i;
1027 unsigned char diff;
1028 volatile unsigned char diff_no_optimize;
1029 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1032 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001033
1034 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001035 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001040 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1044 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001045
Paul Bakkerc70b9822013-04-07 22:00:46 +02001046 nb_pad -= 10 + oid_size;
1047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001049 }
1050
Paul Bakkerc70b9822013-04-07 22:00:46 +02001051 nb_pad -= hashlen;
1052
Paul Bakkerb3869132013-02-28 17:21:01 +01001053 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001055
1056 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001058 memset( p, 0xFF, nb_pad );
1059 p += nb_pad;
1060 *p++ = 0;
1061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001063 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001064 memcpy( p, hash, hashlen );
1065 }
1066 else
1067 {
1068 /*
1069 * DigestInfo ::= SEQUENCE {
1070 * digestAlgorithm DigestAlgorithmIdentifier,
1071 * digest Digest }
1072 *
1073 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1074 *
1075 * Digest ::= OCTET STRING
1076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001078 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
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) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001082 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001083 memcpy( p, oid, oid_size );
1084 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001086 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001088 *p++ = hashlen;
1089 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001090 }
1091
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001092 if( mode == MBEDTLS_RSA_PUBLIC )
1093 return( mbedtls_rsa_public( ctx, sig, sig ) );
1094
1095 /*
1096 * In order to prevent Lenstra's attack, make the signature in a
1097 * temporary buffer and check it before returning it.
1098 */
1099 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001100 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001101 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1102
Simon Butcher1285ab52016-01-01 21:42:47 +00001103 verif = mbedtls_calloc( 1, ctx->len );
1104 if( verif == NULL )
1105 {
1106 mbedtls_free( sig_try );
1107 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1108 }
1109
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001110 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1111 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1112
1113 /* Compare in constant time just in case */
1114 for( diff = 0, i = 0; i < ctx->len; i++ )
1115 diff |= verif[i] ^ sig[i];
1116 diff_no_optimize = diff;
1117
1118 if( diff_no_optimize != 0 )
1119 {
1120 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1121 goto cleanup;
1122 }
1123
1124 memcpy( sig, sig_try, ctx->len );
1125
1126cleanup:
1127 mbedtls_free( sig_try );
1128 mbedtls_free( verif );
1129
1130 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001133
1134/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 * Do an RSA operation to sign the message digest
1136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001138 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001139 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001140 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001142 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001143 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001144 unsigned char *sig )
1145{
Paul Bakker5121ce52009-01-03 21:22:43 +00001146 switch( ctx->padding )
1147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#if defined(MBEDTLS_PKCS1_V15)
1149 case MBEDTLS_RSA_PKCS_V15:
1150 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001151 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001152#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#if defined(MBEDTLS_PKCS1_V21)
1155 case MBEDTLS_RSA_PKCS_V21:
1156 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001157 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001158#endif
1159
Paul Bakker5121ce52009-01-03 21:22:43 +00001160 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001162 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001163}
1164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001166/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001167 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001170 int (*f_rng)(void *, unsigned char *, size_t),
1171 void *p_rng,
1172 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001174 unsigned int hashlen,
1175 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001177 int expected_salt_len,
1178 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001179{
Paul Bakker23986e52011-04-24 08:57:21 +00001180 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001181 size_t siglen;
1182 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1184 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001185 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001186 unsigned int hlen;
1187 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 const mbedtls_md_info_t *md_info;
1189 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1192 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001193
Paul Bakker5121ce52009-01-03 21:22:43 +00001194 siglen = ctx->len;
1195
Paul Bakker27fdf462011-06-09 13:55:13 +00001196 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1200 ? mbedtls_rsa_public( ctx, sig, buf )
1201 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001202
1203 if( ret != 0 )
1204 return( ret );
1205
1206 p = buf;
1207
Paul Bakkerb3869132013-02-28 17:21:01 +01001208 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001212 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001213 // Gather length of hash to sign
1214 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001216 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001220 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001223 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001227 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001228
Paul Bakkerb3869132013-02-28 17:21:01 +01001229 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001230
Paul Bakkerb3869132013-02-28 17:21:01 +01001231 // Note: EMSA-PSS verification is over the length of N - 1 bits
1232 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001233 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001234
Paul Bakkerb3869132013-02-28 17:21:01 +01001235 // Compensate for boundary condition when applying mask
1236 //
1237 if( msb % 8 == 0 )
1238 {
1239 p++;
1240 siglen -= 1;
1241 }
1242 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 mbedtls_md_init( &md_ctx );
1246 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001247
Paul Bakkerb3869132013-02-28 17:21:01 +01001248 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001249
Paul Bakkerb3869132013-02-28 17:21:01 +01001250 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001251
Paul Bakker4de44aa2013-12-31 11:43:01 +01001252 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001253 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001254
Paul Bakkerb3869132013-02-28 17:21:01 +01001255 if( p == buf + siglen ||
1256 *p++ != 0x01 )
1257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 mbedtls_md_free( &md_ctx );
1259 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001260 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001261
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001262 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001263 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001266 slen != (size_t) expected_salt_len )
1267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 mbedtls_md_free( &md_ctx );
1269 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001270 }
1271
Paul Bakkerb3869132013-02-28 17:21:01 +01001272 // Generate H = Hash( M' )
1273 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 mbedtls_md_starts( &md_ctx );
1275 mbedtls_md_update( &md_ctx, zeros, 8 );
1276 mbedtls_md_update( &md_ctx, hash, hashlen );
1277 mbedtls_md_update( &md_ctx, p, slen );
1278 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001281
Paul Bakkerb3869132013-02-28 17:21:01 +01001282 if( memcmp( p + slen, result, hlen ) == 0 )
1283 return( 0 );
1284 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001286}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001287
1288/*
1289 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001292 int (*f_rng)(void *, unsigned char *, size_t),
1293 void *p_rng,
1294 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001296 unsigned int hashlen,
1297 const unsigned char *hash,
1298 const unsigned char *sig )
1299{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1301 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001302 : md_alg;
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001305 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001307 sig ) );
1308
1309}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001313/*
1314 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001317 int (*f_rng)(void *, unsigned char *, size_t),
1318 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001319 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001321 unsigned int hashlen,
1322 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001323 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001324{
1325 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001326 size_t len, siglen, asn1_len;
1327 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1329 mbedtls_md_type_t msg_md_alg;
1330 const mbedtls_md_info_t *md_info;
1331 mbedtls_asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1334 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001335
1336 siglen = ctx->len;
1337
1338 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1342 ? mbedtls_rsa_public( ctx, sig, buf )
1343 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001344
1345 if( ret != 0 )
1346 return( ret );
1347
1348 p = buf;
1349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1351 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001352
1353 while( *p != 0 )
1354 {
1355 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001357 p++;
1358 }
1359 p++;
1360
1361 len = siglen - ( p - buf );
1362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001364 {
1365 if( memcmp( p, hash, hashlen ) == 0 )
1366 return( 0 );
1367 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 }
1370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001372 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1374 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001375
1376 end = p + len;
1377
1378 // Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1379 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1381 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1382 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001383
1384 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1388 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1389 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001390
1391 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1395 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001396
1397 oid.p = p;
1398 p += oid.len;
1399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1401 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001402
1403 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001405
1406 /*
1407 * assume the algorithm parameters must be NULL
1408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1410 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1413 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001414
1415 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001417
1418 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001420
1421 p += hashlen;
1422
1423 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001425
1426 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001429
1430/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001431 * Do an RSA operation and check the message digest
1432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001434 int (*f_rng)(void *, unsigned char *, size_t),
1435 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001436 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001438 unsigned int hashlen,
1439 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001440 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001441{
1442 switch( ctx->padding )
1443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#if defined(MBEDTLS_PKCS1_V15)
1445 case MBEDTLS_RSA_PKCS_V15:
1446 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001447 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001448#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_PKCS1_V21)
1451 case MBEDTLS_RSA_PKCS_V21:
1452 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001453 hashlen, hash, sig );
1454#endif
1455
1456 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001458 }
1459}
1460
1461/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001462 * Copy the components of an RSA key
1463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001465{
1466 int ret;
1467
1468 dst->ver = src->ver;
1469 dst->len = src->len;
1470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1472 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1475 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1476 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1477 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1478 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1479 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1482 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1483 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1486 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001487
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001488 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001489 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001490
1491cleanup:
1492 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001494
1495 return( ret );
1496}
1497
1498/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001499 * Free the components of an RSA key
1500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001502{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1504 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1505 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1506 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1507 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#if defined(MBEDTLS_THREADING_C)
1510 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001511#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001512}
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001515
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001516#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001517
1518/*
1519 * Example RSA-1024 keypair, for test purposes
1520 */
1521#define KEY_LEN 128
1522
1523#define RSA_N "9292758453063D803DD603D5E777D788" \
1524 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1525 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1526 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1527 "93A89813FBF3C4F8066D2D800F7C38A8" \
1528 "1AE31942917403FF4946B0A83D3D3E05" \
1529 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1530 "5E94BB77B07507233A0BC7BAC8F90F79"
1531
1532#define RSA_E "10001"
1533
1534#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1535 "66CA472BC44D253102F8B4A9D3BFA750" \
1536 "91386C0077937FE33FA3252D28855837" \
1537 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1538 "DF79C5CE07EE72C7F123142198164234" \
1539 "CABB724CF78B8173B9F880FC86322407" \
1540 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1541 "071513A1E85B5DFA031F21ECAE91A34D"
1542
1543#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1544 "2C01CAD19EA484A87EA4377637E75500" \
1545 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1546 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1547
1548#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1549 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1550 "910E4168387E3C30AA1E00C339A79508" \
1551 "8452DD96A9A5EA5D9DCA68DA636032AF"
1552
1553#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1554 "3C94D22288ACD763FD8E5600ED4A702D" \
1555 "F84198A5F06C2E72236AE490C93F07F8" \
1556 "3CC559CD27BC2D1CA488811730BB5725"
1557
1558#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1559 "D8AAEA56749EA28623272E4F7D0592AF" \
1560 "7C1F1313CAC9471B5C523BFE592F517B" \
1561 "407A1BD76C164B93DA2D32A383E58357"
1562
1563#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1564 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1565 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1566 "A74206CEC169D74BF5A8C50D6F48EA08"
1567
1568#define PT_LEN 24
1569#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1570 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001573static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001574{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001575#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001576 size_t i;
1577
Paul Bakker545570e2010-07-18 09:00:25 +00001578 if( rng_state != NULL )
1579 rng_state = NULL;
1580
Paul Bakkera3d195c2011-11-27 21:07:34 +00001581 for( i = 0; i < len; ++i )
1582 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001583#else
1584 if( rng_state != NULL )
1585 rng_state = NULL;
1586
1587 arc4random_buf( output, len );
1588#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001589
Paul Bakkera3d195c2011-11-27 21:07:34 +00001590 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001591}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001593
Paul Bakker5121ce52009-01-03 21:22:43 +00001594/*
1595 * Checkup routine
1596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001598{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001599 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001601 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 unsigned char rsa_plaintext[PT_LEN];
1604 unsigned char rsa_decrypted[PT_LEN];
1605 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001607 unsigned char sha1sum[20];
1608#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001611
1612 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1614 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1615 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1616 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1617 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1618 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1619 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1620 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001621
1622 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001623 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1626 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001627 {
1628 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
1631 return( 1 );
1632 }
1633
1634 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
1637 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001640 rsa_plaintext, rsa_ciphertext ) != 0 )
1641 {
1642 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
1645 return( 1 );
1646 }
1647
1648 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001652 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001653 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001654 {
1655 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
1658 return( 1 );
1659 }
1660
1661 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1662 {
1663 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001665
1666 return( 1 );
1667 }
1668
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001669 if( verbose != 0 )
1670 mbedtls_printf( "passed\n" );
1671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001673 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001674 mbedtls_printf( "PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001679 sha1sum, rsa_ciphertext ) != 0 )
1680 {
1681 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001683
1684 return( 1 );
1685 }
1686
1687 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001691 sha1sum, rsa_ciphertext ) != 0 )
1692 {
1693 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
1696 return( 1 );
1697 }
1698
1699 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001700 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001702
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001703 if( verbose != 0 )
1704 mbedtls_printf( "\n" );
1705
Paul Bakker3d8fb632014-04-17 12:42:41 +02001706cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 mbedtls_rsa_free( &rsa );
1708#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001709 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001711 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001712}
1713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#endif /* MBEDTLS_RSA_C */