blob: 06f96ef2355e92cac4ec31c8ae98107e91e0d8bb [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
Simon Butcher02037452016-03-01 21:19:12 +0000474 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000475 p = dst;
476
477 while( dlen > 0 )
478 {
479 use_len = hlen;
480 if( dlen < hlen )
481 use_len = dlen;
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 mbedtls_md_starts( md_ctx );
484 mbedtls_md_update( md_ctx, src, slen );
485 mbedtls_md_update( md_ctx, counter, 4 );
486 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000487
488 for( i = 0; i < use_len; ++i )
489 *p++ ^= mask[i];
490
491 counter[3]++;
492
493 dlen -= use_len;
494 }
495}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100499/*
500 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100503 int (*f_rng)(void *, unsigned char *, size_t),
504 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100505 int mode,
506 const unsigned char *label, size_t label_len,
507 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100508 const unsigned char *input,
509 unsigned char *output )
510{
511 size_t olen;
512 int ret;
513 unsigned char *p = output;
514 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 const mbedtls_md_info_t *md_info;
516 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
519 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200520
521 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100525 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100527
528 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100530
Simon Butcher02037452016-03-01 21:19:12 +0000531 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000532 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
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 memset( output, 0, olen );
536
537 *p++ = 0;
538
Simon Butcher02037452016-03-01 21:19:12 +0000539 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100540 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100542
543 p += hlen;
544
Simon Butcher02037452016-03-01 21:19:12 +0000545 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100547 p += hlen;
548 p += olen - 2 * hlen - 2 - ilen;
549 *p++ = 1;
550 memcpy( p, input, ilen );
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_md_init( &md_ctx );
553 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +0100554
Simon Butcher02037452016-03-01 21:19:12 +0000555 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100556 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
557 &md_ctx );
558
Simon Butcher02037452016-03-01 21:19:12 +0000559 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100560 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
561 &md_ctx );
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 return( ( mode == MBEDTLS_RSA_PUBLIC )
566 ? mbedtls_rsa_public( ctx, output, output )
567 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100568}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100572/*
573 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100576 int (*f_rng)(void *, unsigned char *, size_t),
577 void *p_rng,
578 int mode, size_t ilen,
579 const unsigned char *input,
580 unsigned char *output )
581{
582 size_t nb_pad, olen;
583 int ret;
584 unsigned char *p = output;
585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
587 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200588
589 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100591
592 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100593
Simon Butcher02037452016-03-01 21:19:12 +0000594 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000595 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100597
598 nb_pad = olen - 3 - ilen;
599
600 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100604
605 while( nb_pad-- > 0 )
606 {
607 int rng_dl = 100;
608
609 do {
610 ret = f_rng( p_rng, p, 1 );
611 } while( *p == 0 && --rng_dl && ret == 0 );
612
Simon Butcher02037452016-03-01 21:19:12 +0000613 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200614 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100616
617 p++;
618 }
619 }
620 else
621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100623
624 while( nb_pad-- > 0 )
625 *p++ = 0xFF;
626 }
627
628 *p++ = 0;
629 memcpy( p, input, ilen );
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 return( ( mode == MBEDTLS_RSA_PUBLIC )
632 ? mbedtls_rsa_public( ctx, output, output )
633 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100634}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100636
Paul Bakker5121ce52009-01-03 21:22:43 +0000637/*
638 * Add the message padding, then do an RSA operation
639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000641 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000642 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000643 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000644 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 unsigned char *output )
646{
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 switch( ctx->padding )
648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_PKCS1_V15)
650 case MBEDTLS_RSA_PKCS_V15:
651 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100652 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200653#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#if defined(MBEDTLS_PKCS1_V21)
656 case MBEDTLS_RSA_PKCS_V21:
657 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100658 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000659#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
661 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000663 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000664}
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000667/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100668 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200671 int (*f_rng)(void *, unsigned char *, size_t),
672 void *p_rng,
673 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100674 const unsigned char *label, size_t label_len,
675 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100676 const unsigned char *input,
677 unsigned char *output,
678 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000679{
Paul Bakker23986e52011-04-24 08:57:21 +0000680 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100681 size_t ilen, i, pad_len;
682 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
684 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000685 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 const mbedtls_md_info_t *md_info;
687 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100688
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100689 /*
690 * Parameters sanity checks
691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
693 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000694
695 ilen = ctx->len;
696
Paul Bakker27fdf462011-06-09 13:55:13 +0000697 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100701 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100703
Janos Follathc17cda12016-02-11 11:08:18 +0000704 hlen = mbedtls_md_get_size( md_info );
705
706 // checking for integer underflow
707 if( 2 * hlen + 2 > ilen )
708 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
709
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100710 /*
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 mbedtls_md_init( &md_ctx );
724 mbedtls_md_setup( &md_ctx, md_info, 0 );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100725
726 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100728
729 /* seed: Apply seedMask to maskedSeed */
730 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
731 &md_ctx );
732
733 /* DB: Apply dbMask to maskedDB */
734 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
735 &md_ctx );
736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100738
739 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100740 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100741 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100743 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000744
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100745 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100746
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100747 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100748
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100749 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100750 for( i = 0; i < hlen; i++ )
751 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100752
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100753 /* Get zero-padding len, but always read till end of buffer
754 * (minus one, for the 01 byte) */
755 pad_len = 0;
756 pad_done = 0;
757 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
758 {
759 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100760 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100761 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100762
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100763 p += pad_len;
764 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100765
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100766 /*
767 * The only information "leaked" is whether the padding was correct or not
768 * (eg, no data is copied if it was not correct). This meets the
769 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
770 * the different error conditions.
771 */
772 if( bad != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100774
Paul Bakker66d5d072014-06-17 16:39:18 +0200775 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakkerb3869132013-02-28 17:21:01 +0100777
778 *olen = ilen - (p - buf);
779 memcpy( output, p, *olen );
780
781 return( 0 );
782}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100786/*
787 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200790 int (*f_rng)(void *, unsigned char *, size_t),
791 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100792 int mode, size_t *olen,
793 const unsigned char *input,
794 unsigned char *output,
795 size_t output_max_len)
796{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100797 int ret;
798 size_t ilen, pad_count = 0, i;
799 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
803 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100804
805 ilen = ctx->len;
806
807 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 ret = ( mode == MBEDTLS_RSA_PUBLIC )
811 ? mbedtls_rsa_public( ctx, input, buf )
812 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100813
814 if( ret != 0 )
815 return( ret );
816
817 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100818 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100819
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100820 /*
821 * Check and get padding len in "constant-time"
822 */
823 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100824
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100825 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000829
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100830 /* Get padding len, but always read till end of buffer
831 * (minus one, for the 00 byte) */
832 for( i = 0; i < ilen - 3; i++ )
833 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100834 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
835 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100836 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000837
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100838 p += pad_count;
839 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100840 }
841 else
842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100844
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100845 /* Get padding len, but always read till end of buffer
846 * (minus one, for the 00 byte) */
847 for( i = 0; i < ilen - 3; i++ )
848 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100849 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100850 pad_count += ( pad_done == 0 );
851 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100852
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100853 p += pad_count;
854 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +0000855 }
856
Janos Follathc69fa502016-02-12 13:30:09 +0000857 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +0000858
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100859 if( bad )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker8804f692013-02-28 18:06:26 +0100861
Paul Bakker66d5d072014-06-17 16:39:18 +0200862 if( ilen - ( p - buf ) > output_max_len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Paul Bakker060c5682009-01-12 21:48:39 +0000864
Paul Bakker27fdf462011-06-09 13:55:13 +0000865 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000866 memcpy( output, p, *olen );
867
868 return( 0 );
869}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000871
872/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100873 * Do an RSA operation, then remove the message padding
874 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200876 int (*f_rng)(void *, unsigned char *, size_t),
877 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100878 int mode, size_t *olen,
879 const unsigned char *input,
880 unsigned char *output,
881 size_t output_max_len)
882{
883 switch( ctx->padding )
884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885#if defined(MBEDTLS_PKCS1_V15)
886 case MBEDTLS_RSA_PKCS_V15:
887 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +0200888 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +0200889#endif
Paul Bakkerb3869132013-02-28 17:21:01 +0100890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891#if defined(MBEDTLS_PKCS1_V21)
892 case MBEDTLS_RSA_PKCS_V21:
893 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +0200894 olen, input, output,
895 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +0100896#endif
897
898 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100900 }
901}
902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100904/*
905 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
906 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100908 int (*f_rng)(void *, unsigned char *, size_t),
909 void *p_rng,
910 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100912 unsigned int hashlen,
913 const unsigned char *hash,
914 unsigned char *sig )
915{
916 size_t olen;
917 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100919 unsigned int slen, hlen, offset = 0;
920 int ret;
921 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 const mbedtls_md_info_t *md_info;
923 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
926 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200927
928 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100930
931 olen = ctx->len;
932
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +0100934 {
Simon Butcher02037452016-03-01 21:19:12 +0000935 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200937 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100941 }
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100944 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100948 slen = hlen;
949
950 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100952
953 memset( sig, 0, olen );
954
Simon Butcher02037452016-03-01 21:19:12 +0000955 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +0100956 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100958
Simon Butcher02037452016-03-01 21:19:12 +0000959 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200960 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +0100961 p += olen - hlen * 2 - 2;
962 *p++ = 0x01;
963 memcpy( p, salt, slen );
964 p += slen;
965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_md_init( &md_ctx );
967 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakkerb3869132013-02-28 17:21:01 +0100968
Simon Butcher02037452016-03-01 21:19:12 +0000969 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_md_starts( &md_ctx );
971 mbedtls_md_update( &md_ctx, p, 8 );
972 mbedtls_md_update( &md_ctx, hash, hashlen );
973 mbedtls_md_update( &md_ctx, salt, slen );
974 mbedtls_md_finish( &md_ctx, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100975
Simon Butcher02037452016-03-01 21:19:12 +0000976 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +0100977 if( msb % 8 == 0 )
978 offset = 1;
979
Simon Butcher02037452016-03-01 21:19:12 +0000980 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100981 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100984
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200985 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +0100986 sig[0] &= 0xFF >> ( olen * 8 - msb );
987
988 p += hlen;
989 *p++ = 0xBC;
990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 return( ( mode == MBEDTLS_RSA_PUBLIC )
992 ? mbedtls_rsa_public( ctx, sig, sig )
993 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100994}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100998/*
999 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1000 */
1001/*
1002 * Do an RSA operation to sign the message digest
1003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001005 int (*f_rng)(void *, unsigned char *, size_t),
1006 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001007 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001009 unsigned int hashlen,
1010 const unsigned char *hash,
1011 unsigned char *sig )
1012{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001013 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001014 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001015 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001016 unsigned char *sig_try = NULL, *verif = NULL;
1017 size_t i;
1018 unsigned char diff;
1019 volatile unsigned char diff_no_optimize;
1020 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1023 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001024
1025 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001026 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001031 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1035 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001036
Paul Bakkerc70b9822013-04-07 22:00:46 +02001037 nb_pad -= 10 + oid_size;
1038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001040 }
1041
Paul Bakkerc70b9822013-04-07 22:00:46 +02001042 nb_pad -= hashlen;
1043
Paul Bakkerb3869132013-02-28 17:21:01 +01001044 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001046
1047 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001049 memset( p, 0xFF, nb_pad );
1050 p += nb_pad;
1051 *p++ = 0;
1052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001054 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001055 memcpy( p, hash, hashlen );
1056 }
1057 else
1058 {
1059 /*
1060 * DigestInfo ::= SEQUENCE {
1061 * digestAlgorithm DigestAlgorithmIdentifier,
1062 * digest Digest }
1063 *
1064 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1065 *
1066 * Digest ::= OCTET STRING
1067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001069 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001071 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001073 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001074 memcpy( p, oid, oid_size );
1075 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001077 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001079 *p++ = hashlen;
1080 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001081 }
1082
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001083 if( mode == MBEDTLS_RSA_PUBLIC )
1084 return( mbedtls_rsa_public( ctx, sig, sig ) );
1085
1086 /*
1087 * In order to prevent Lenstra's attack, make the signature in a
1088 * temporary buffer and check it before returning it.
1089 */
1090 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001091 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001092 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1093
Simon Butcher1285ab52016-01-01 21:42:47 +00001094 verif = mbedtls_calloc( 1, ctx->len );
1095 if( verif == NULL )
1096 {
1097 mbedtls_free( sig_try );
1098 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1099 }
1100
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001101 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1102 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1103
1104 /* Compare in constant time just in case */
1105 for( diff = 0, i = 0; i < ctx->len; i++ )
1106 diff |= verif[i] ^ sig[i];
1107 diff_no_optimize = diff;
1108
1109 if( diff_no_optimize != 0 )
1110 {
1111 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1112 goto cleanup;
1113 }
1114
1115 memcpy( sig, sig_try, ctx->len );
1116
1117cleanup:
1118 mbedtls_free( sig_try );
1119 mbedtls_free( verif );
1120
1121 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001122}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001124
1125/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001126 * Do an RSA operation to sign the message digest
1127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001129 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001130 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001133 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001134 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001135 unsigned char *sig )
1136{
Paul Bakker5121ce52009-01-03 21:22:43 +00001137 switch( ctx->padding )
1138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139#if defined(MBEDTLS_PKCS1_V15)
1140 case MBEDTLS_RSA_PKCS_V15:
1141 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001142 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001143#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#if defined(MBEDTLS_PKCS1_V21)
1146 case MBEDTLS_RSA_PKCS_V21:
1147 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001148 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001149#endif
1150
Paul Bakker5121ce52009-01-03 21:22:43 +00001151 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001153 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001154}
1155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001157/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001158 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001161 int (*f_rng)(void *, unsigned char *, size_t),
1162 void *p_rng,
1163 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001165 unsigned int hashlen,
1166 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001168 int expected_salt_len,
1169 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001170{
Paul Bakker23986e52011-04-24 08:57:21 +00001171 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001172 size_t siglen;
1173 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1175 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001176 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001177 unsigned int hlen;
1178 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 const mbedtls_md_info_t *md_info;
1180 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1183 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001184
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 siglen = ctx->len;
1186
Paul Bakker27fdf462011-06-09 13:55:13 +00001187 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1191 ? mbedtls_rsa_public( ctx, sig, buf )
1192 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001193
1194 if( ret != 0 )
1195 return( ret );
1196
1197 p = buf;
1198
Paul Bakkerb3869132013-02-28 17:21:01 +01001199 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 {
Simon Butcher02037452016-03-01 21:19:12 +00001204 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001206 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001210 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001213 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001217 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001218
Paul Bakkerb3869132013-02-28 17:21:01 +01001219 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001220
Simon Butcher02037452016-03-01 21:19:12 +00001221 /*
1222 * Note: EMSA-PSS verification is over the length of N - 1 bits
1223 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001224 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001225
Simon Butcher02037452016-03-01 21:19:12 +00001226 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001227 if( msb % 8 == 0 )
1228 {
1229 p++;
1230 siglen -= 1;
1231 }
1232 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 mbedtls_md_init( &md_ctx );
1236 mbedtls_md_setup( &md_ctx, md_info, 0 );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001237
Paul Bakkerb3869132013-02-28 17:21:01 +01001238 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001239
Paul Bakkerb3869132013-02-28 17:21:01 +01001240 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001241
Paul Bakker4de44aa2013-12-31 11:43:01 +01001242 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001243 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001244
Paul Bakkerb3869132013-02-28 17:21:01 +01001245 if( p == buf + siglen ||
1246 *p++ != 0x01 )
1247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_md_free( &md_ctx );
1249 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001250 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001251
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001252 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001253 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001256 slen != (size_t) expected_salt_len )
1257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 mbedtls_md_free( &md_ctx );
1259 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001260 }
1261
Simon Butcher02037452016-03-01 21:19:12 +00001262 /*
1263 * Generate H = Hash( M' )
1264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 mbedtls_md_starts( &md_ctx );
1266 mbedtls_md_update( &md_ctx, zeros, 8 );
1267 mbedtls_md_update( &md_ctx, hash, hashlen );
1268 mbedtls_md_update( &md_ctx, p, slen );
1269 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001272
Paul Bakkerb3869132013-02-28 17:21:01 +01001273 if( memcmp( p + slen, result, hlen ) == 0 )
1274 return( 0 );
1275 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001277}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001278
1279/*
1280 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001283 int (*f_rng)(void *, unsigned char *, size_t),
1284 void *p_rng,
1285 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001287 unsigned int hashlen,
1288 const unsigned char *hash,
1289 const unsigned char *sig )
1290{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1292 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001293 : md_alg;
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001296 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001298 sig ) );
1299
1300}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001304/*
1305 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001308 int (*f_rng)(void *, unsigned char *, size_t),
1309 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001310 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001312 unsigned int hashlen,
1313 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001314 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001315{
1316 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001317 size_t len, siglen, asn1_len;
1318 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1320 mbedtls_md_type_t msg_md_alg;
1321 const mbedtls_md_info_t *md_info;
1322 mbedtls_asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1325 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001326
1327 siglen = ctx->len;
1328
1329 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1333 ? mbedtls_rsa_public( ctx, sig, buf )
1334 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001335
1336 if( ret != 0 )
1337 return( ret );
1338
1339 p = buf;
1340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1342 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001343
1344 while( *p != 0 )
1345 {
1346 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001348 p++;
1349 }
1350 p++;
1351
1352 len = siglen - ( p - buf );
1353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001355 {
1356 if( memcmp( p, hash, hashlen ) == 0 )
1357 return( 0 );
1358 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 }
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001363 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1365 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001366
1367 end = p + len;
1368
Simon Butcher02037452016-03-01 21:19:12 +00001369 /*
1370 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1373 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1374 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001375
1376 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1380 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1381 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001382
1383 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1387 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001388
1389 oid.p = p;
1390 p += oid.len;
1391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1393 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001394
1395 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001397
1398 /*
1399 * assume the algorithm parameters must be NULL
1400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1402 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1405 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001406
1407 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001409
1410 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001412
1413 p += hashlen;
1414
1415 if( p != end )
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 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001419}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
1422/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001423 * Do an RSA operation and check the message digest
1424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001426 int (*f_rng)(void *, unsigned char *, size_t),
1427 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001428 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001430 unsigned int hashlen,
1431 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001432 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001433{
1434 switch( ctx->padding )
1435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_PKCS1_V15)
1437 case MBEDTLS_RSA_PKCS_V15:
1438 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001439 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001440#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#if defined(MBEDTLS_PKCS1_V21)
1443 case MBEDTLS_RSA_PKCS_V21:
1444 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001445 hashlen, hash, sig );
1446#endif
1447
1448 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001450 }
1451}
1452
1453/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001454 * Copy the components of an RSA key
1455 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001457{
1458 int ret;
1459
1460 dst->ver = src->ver;
1461 dst->len = src->len;
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1464 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1467 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1468 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1469 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1470 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1471 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1474 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1475 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1478 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001479
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001480 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001481 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001482
1483cleanup:
1484 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001486
1487 return( ret );
1488}
1489
1490/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001491 * Free the components of an RSA key
1492 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001494{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1496 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1497 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1498 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1499 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#if defined(MBEDTLS_THREADING_C)
1502 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001503#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001504}
1505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001507
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001508#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001509
1510/*
1511 * Example RSA-1024 keypair, for test purposes
1512 */
1513#define KEY_LEN 128
1514
1515#define RSA_N "9292758453063D803DD603D5E777D788" \
1516 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1517 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1518 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1519 "93A89813FBF3C4F8066D2D800F7C38A8" \
1520 "1AE31942917403FF4946B0A83D3D3E05" \
1521 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1522 "5E94BB77B07507233A0BC7BAC8F90F79"
1523
1524#define RSA_E "10001"
1525
1526#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1527 "66CA472BC44D253102F8B4A9D3BFA750" \
1528 "91386C0077937FE33FA3252D28855837" \
1529 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1530 "DF79C5CE07EE72C7F123142198164234" \
1531 "CABB724CF78B8173B9F880FC86322407" \
1532 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1533 "071513A1E85B5DFA031F21ECAE91A34D"
1534
1535#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1536 "2C01CAD19EA484A87EA4377637E75500" \
1537 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1538 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1539
1540#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1541 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1542 "910E4168387E3C30AA1E00C339A79508" \
1543 "8452DD96A9A5EA5D9DCA68DA636032AF"
1544
1545#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1546 "3C94D22288ACD763FD8E5600ED4A702D" \
1547 "F84198A5F06C2E72236AE490C93F07F8" \
1548 "3CC559CD27BC2D1CA488811730BB5725"
1549
1550#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1551 "D8AAEA56749EA28623272E4F7D0592AF" \
1552 "7C1F1313CAC9471B5C523BFE592F517B" \
1553 "407A1BD76C164B93DA2D32A383E58357"
1554
1555#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1556 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1557 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1558 "A74206CEC169D74BF5A8C50D6F48EA08"
1559
1560#define PT_LEN 24
1561#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1562 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001565static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001566{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001567#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001568 size_t i;
1569
Paul Bakker545570e2010-07-18 09:00:25 +00001570 if( rng_state != NULL )
1571 rng_state = NULL;
1572
Paul Bakkera3d195c2011-11-27 21:07:34 +00001573 for( i = 0; i < len; ++i )
1574 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001575#else
1576 if( rng_state != NULL )
1577 rng_state = NULL;
1578
1579 arc4random_buf( output, len );
1580#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001581
Paul Bakkera3d195c2011-11-27 21:07:34 +00001582 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001585
Paul Bakker5121ce52009-01-03 21:22:43 +00001586/*
1587 * Checkup routine
1588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001590{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001591 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001593 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 unsigned char rsa_plaintext[PT_LEN];
1596 unsigned char rsa_decrypted[PT_LEN];
1597 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001599 unsigned char sha1sum[20];
1600#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001603
1604 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1606 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1607 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1608 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1609 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1610 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1611 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1612 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001613
1614 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1618 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001619 {
1620 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001622
1623 return( 1 );
1624 }
1625
1626 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001628
1629 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001632 rsa_plaintext, rsa_ciphertext ) != 0 )
1633 {
1634 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001636
1637 return( 1 );
1638 }
1639
1640 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001644 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001645 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 {
1647 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001649
1650 return( 1 );
1651 }
1652
1653 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1654 {
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
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001661 if( verbose != 0 )
1662 mbedtls_printf( "passed\n" );
1663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001665 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001666 mbedtls_printf( "PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001671 sha1sum, rsa_ciphertext ) != 0 )
1672 {
1673 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001675
1676 return( 1 );
1677 }
1678
1679 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001683 sha1sum, rsa_ciphertext ) != 0 )
1684 {
1685 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001687
1688 return( 1 );
1689 }
1690
1691 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001692 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001694
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001695 if( verbose != 0 )
1696 mbedtls_printf( "\n" );
1697
Paul Bakker3d8fb632014-04-17 12:42:41 +02001698cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 mbedtls_rsa_free( &rsa );
1700#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001701 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001703 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001704}
1705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#endif /* MBEDTLS_RSA_C */