blob: 250f96fcdf360e745a60930d6d1ff1809df3584a [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/*
22 * RSA was designed by Ron Rivest, Adi Shamir and Len Adleman.
23 *
24 * http://theory.lcs.mit.edu/~rivest/rsapaper.pdf
25 * http://www.cacr.math.uwaterloo.ca/hac/about/chap8.pdf
26 */
27
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/rsa.h"
37#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000038
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <string.h>
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000046#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000047#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010051#else
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020054#define mbedtls_calloc calloc
55#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010056#endif
57
Gilles Peskine8877ec22017-03-23 14:37:37 +010058/* Implementation that should never be optimized out by the compiler */
59static void mbedtls_zeroize( void *v, size_t n ) {
60 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
61}
62
Paul Bakker5121ce52009-01-03 21:22:43 +000063/*
64 * Initialize an RSA context
65 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000067 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000068 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000069{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_THREADING_C)
75 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020076#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000077}
78
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010079/*
80 * Set padding for an existing RSA context
81 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010083{
84 ctx->padding = padding;
85 ctx->hash_id = hash_id;
86}
87
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90/*
91 * Generate an RSA keypair
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +000094 int (*f_rng)(void *, unsigned char *, size_t),
95 void *p_rng,
96 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +000097{
98 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Paul Bakker21eb2802010-08-16 11:10:02 +0000101 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Janos Follath95b30362016-09-21 13:18:12 +0100104 if( nbits % 2 )
105 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
106
107 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath1a59a502016-02-23 14:42:48 +0000108 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
110 /*
111 * find primes P and Q with Q < P so that:
112 * GCD( E, (P-1)*(Q-1) ) == 1
113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116 do
117 {
Janos Follath1a59a502016-02-23 14:42:48 +0000118 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000119 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
Janos Follath95b30362016-09-21 13:18:12 +0100121 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000122 f_rng, p_rng ) );
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
Janos Follath95b30362016-09-21 13:18:12 +0100131 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
132 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
135 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
136 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
137 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141 /*
142 * D = E^-1 mod ((P-1)*(Q-1))
143 * DP = D mod (P - 1)
144 * DQ = D mod (Q - 1)
145 * QP = Q^-1 mod P
146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
148 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
149 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
150 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200152 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
154cleanup:
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158 if( ret != 0 )
159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_rsa_free( ctx );
161 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 }
163
Paul Bakker48377d92013-08-30 12:06:24 +0200164 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165}
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169/*
170 * Check a public RSA key
171 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000173{
Paul Bakker37940d9f2009-07-10 22:38:58 +0000174 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000176
Paul Bakker48377d92013-08-30 12:06:24 +0200177 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200181 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
182 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200185 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
187 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
189 return( 0 );
190}
191
192/*
193 * Check a private RSA key
194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000196{
197 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 return( ret );
202
Paul Bakker37940d9f2009-07-10 22:38:58 +0000203 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d9f2009-07-10 22:38:58 +0000205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
207 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
208 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
209 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
212 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
213 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
214 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
215 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
216 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
219 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
220 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
223 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000225 /*
226 * Check for a valid PKCS1v2 private key
227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
229 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
230 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
231 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
232 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
233 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
234 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 }
Paul Bakker48377d92013-08-30 12:06:24 +0200238
Paul Bakker5121ce52009-01-03 21:22:43 +0000239cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
241 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
242 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
243 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000246 return( ret );
247
Paul Bakker6c591fa2011-05-05 11:49:20 +0000248 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000250
251 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000252}
253
254/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100255 * Check if contexts holding a public and private key match
256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100258{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
260 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100263 }
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
266 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100269 }
270
271 return( 0 );
272}
273
274/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 * Do an RSA public key operation
276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000278 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 unsigned char *output )
280{
Paul Bakker23986e52011-04-24 08:57:21 +0000281 int ret;
282 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200287#if defined(MBEDTLS_THREADING_C)
288 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
289 return( ret );
290#endif
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200296 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
297 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 }
299
300 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
302 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
304cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200306 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
307 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100308#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
312 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
315 return( 0 );
316}
317
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200318/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200319 * Generate or update blinding values, see section 10 of:
320 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200321 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200322 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200323 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200324static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200325 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
326{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200327 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200328
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200329 if( ctx->Vf.p != NULL )
330 {
331 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
333 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
334 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
335 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200336
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200337 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200338 }
339
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200340 /* Unblinding value: Vf = random number, invertible mod N */
341 do {
342 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
346 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
347 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200348
349 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
351 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 +0200352
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200353
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200354cleanup:
355 return( ret );
356}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200357
Paul Bakker5121ce52009-01-03 21:22:43 +0000358/*
359 * Do an RSA private key operation
360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200362 int (*f_rng)(void *, unsigned char *, size_t),
363 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000364 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 unsigned char *output )
366{
Paul Bakker23986e52011-04-24 08:57:21 +0000367 int ret;
368 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_mpi T, T1, T2;
Paul Bakker5121ce52009-01-03 21:22:43 +0000370
Manuel Pégourié-Gonnard9f44a802015-10-30 10:56:25 +0100371 /* Make sure we have private key info, prevent possible misuse */
372 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
373 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200377#if defined(MBEDTLS_THREADING_C)
378 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
379 return( ret );
380#endif
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
383 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200385 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
386 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 }
388
Paul Bakkerf451bac2013-08-30 15:37:02 +0200389 if( f_rng != NULL )
390 {
391 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200392 * Blinding
393 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200394 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200395 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
396 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200398 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#if defined(MBEDTLS_RSA_NO_CRT)
401 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100402#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200403 /*
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 * faster decryption using the CRT
405 *
406 * T1 = input ^ dP mod P
407 * T2 = input ^ dQ mod Q
408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
410 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000411
412 /*
413 * T = (T1 - T2) * (Q^-1 mod P) mod P
414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
416 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
417 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419 /*
Paul Bakkerf451bac2013-08-30 15:37:02 +0200420 * T = T2 + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
423 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
424#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200425
Paul Bakkerf451bac2013-08-30 15:37:02 +0200426 if( f_rng != NULL )
427 {
428 /*
429 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200430 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200431 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200432 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200434 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000435
436 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200441 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
442 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200443#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
447 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000449
450 return( 0 );
451}
452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000454/**
455 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
456 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000457 * \param dst buffer to mask
458 * \param dlen length of destination buffer
459 * \param src source of the mask generation
460 * \param slen length of the source buffer
461 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000462 */
Paul Bakker48377d92013-08-30 12:06:24 +0200463static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000465{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000467 unsigned char counter[4];
468 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000469 unsigned int hlen;
470 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000473 memset( counter, 0, 4 );
474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000476
477 // Generate and apply dbMask
478 //
479 p = dst;
480
481 while( dlen > 0 )
482 {
483 use_len = hlen;
484 if( dlen < hlen )
485 use_len = dlen;
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 mbedtls_md_starts( md_ctx );
488 mbedtls_md_update( md_ctx, src, slen );
489 mbedtls_md_update( md_ctx, counter, 4 );
490 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000491
492 for( i = 0; i < use_len; ++i )
493 *p++ ^= mask[i];
494
495 counter[3]++;
496
497 dlen -= use_len;
498 }
499}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100503/*
504 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100507 int (*f_rng)(void *, unsigned char *, size_t),
508 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100509 int mode,
510 const unsigned char *label, size_t label_len,
511 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100512 const unsigned char *input,
513 unsigned char *output )
514{
515 size_t olen;
516 int ret;
517 unsigned char *p = output;
518 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 const mbedtls_md_info_t *md_info;
520 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
523 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200524
525 if( f_rng == 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100529 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100531
532 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100534
Janos Follathe33f5592016-02-08 14:52:29 +0000535 // first comparison checks for overflow
536 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100538
539 memset( output, 0, olen );
540
541 *p++ = 0;
542
543 // Generate a random octet string seed
544 //
545 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100547
548 p += hlen;
549
550 // Construct DB
551 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100553 p += hlen;
554 p += olen - 2 * hlen - 2 - ilen;
555 *p++ = 1;
556 memcpy( p, input, ilen );
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -0700559 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
560 {
561 mbedtls_md_free( &md_ctx );
562 return( ret );
563 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100564
565 // maskedDB: Apply dbMask to DB
566 //
567 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
568 &md_ctx );
569
570 // maskedSeed: Apply seedMask to seed
571 //
572 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
573 &md_ctx );
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 return( ( mode == MBEDTLS_RSA_PUBLIC )
578 ? mbedtls_rsa_public( ctx, output, output )
579 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100580}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100584/*
585 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100588 int (*f_rng)(void *, unsigned char *, size_t),
589 void *p_rng,
590 int mode, size_t ilen,
591 const unsigned char *input,
592 unsigned char *output )
593{
594 size_t nb_pad, olen;
595 int ret;
596 unsigned char *p = output;
597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
599 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200600
Janos Follath689a6272016-03-18 11:45:44 +0000601 // We don't check p_rng because it won't be dereferenced here
602 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100604
605 olen = ctx->len;
Janos Follathe33f5592016-02-08 14:52:29 +0000606
607 // first comparison checks for overflow
608 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100610
611 nb_pad = olen - 3 - ilen;
612
613 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100617
618 while( nb_pad-- > 0 )
619 {
620 int rng_dl = 100;
621
622 do {
623 ret = f_rng( p_rng, p, 1 );
624 } while( *p == 0 && --rng_dl && ret == 0 );
625
626 // Check if RNG failed to generate data
627 //
Paul Bakker66d5d072014-06-17 16:39:18 +0200628 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100630
631 p++;
632 }
633 }
634 else
635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100637
638 while( nb_pad-- > 0 )
639 *p++ = 0xFF;
640 }
641
642 *p++ = 0;
643 memcpy( p, input, ilen );
644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 return( ( mode == MBEDTLS_RSA_PUBLIC )
646 ? mbedtls_rsa_public( ctx, output, output )
647 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100650
Paul Bakker5121ce52009-01-03 21:22:43 +0000651/*
652 * Add the message padding, then do an RSA operation
653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000655 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000656 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000657 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000658 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000659 unsigned char *output )
660{
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 switch( ctx->padding )
662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_PKCS1_V15)
664 case MBEDTLS_RSA_PKCS_V15:
665 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100666 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200667#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#if defined(MBEDTLS_PKCS1_V21)
670 case MBEDTLS_RSA_PKCS_V21:
671 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100672 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000673#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000678}
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000681/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100682 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200685 int (*f_rng)(void *, unsigned char *, size_t),
686 void *p_rng,
687 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100688 const unsigned char *label, size_t label_len,
689 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100690 const unsigned char *input,
691 unsigned char *output,
692 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000693{
Paul Bakker23986e52011-04-24 08:57:21 +0000694 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100695 size_t ilen, i, pad_len;
696 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
698 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000699 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 const mbedtls_md_info_t *md_info;
701 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100702
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100703 /*
704 * Parameters sanity checks
705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
707 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000708
709 ilen = ctx->len;
710
Paul Bakker27fdf462011-06-09 13:55:13 +0000711 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100715 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100717
Janos Follath25da9b32016-02-11 11:08:18 +0000718 hlen = mbedtls_md_get_size( md_info );
719
720 // checking for integer underflow
721 if( 2 * hlen + 2 > ilen )
722 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
723
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100724 /*
725 * RSA operation
726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 ret = ( mode == MBEDTLS_RSA_PUBLIC )
728 ? mbedtls_rsa_public( ctx, input, buf )
729 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000730
731 if( ret != 0 )
Gilles Peskine8877ec22017-03-23 14:37:37 +0100732 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000733
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100734 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100735 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -0700738 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
739 {
740 mbedtls_md_free( &md_ctx );
Gilles Peskine8877ec22017-03-23 14:37:37 +0100741 goto cleanup;
Brian J Murray88c2d222016-06-23 12:57:03 -0700742 }
743
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100744
745 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100747
748 /* seed: Apply seedMask to maskedSeed */
749 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
750 &md_ctx );
751
752 /* DB: Apply dbMask to maskedDB */
753 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
754 &md_ctx );
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100757
758 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100759 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100760 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000761 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100762 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000763
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100764 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100765
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100766 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100767
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100768 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100769 for( i = 0; i < hlen; i++ )
770 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100771
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100772 /* Get zero-padding len, but always read till end of buffer
773 * (minus one, for the 01 byte) */
774 pad_len = 0;
775 pad_done = 0;
776 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
777 {
778 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100779 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100780 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100781
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100782 p += pad_len;
783 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100784
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100785 /*
786 * The only information "leaked" is whether the padding was correct or not
787 * (eg, no data is copied if it was not correct). This meets the
788 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
789 * the different error conditions.
790 */
791 if( bad != 0 )
Gilles Peskine8877ec22017-03-23 14:37:37 +0100792 {
793 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
794 goto cleanup;
795 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100796
Paul Bakker66d5d072014-06-17 16:39:18 +0200797 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine8877ec22017-03-23 14:37:37 +0100798 {
799 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
800 goto cleanup;
801 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100802
803 *olen = ilen - (p - buf);
804 memcpy( output, p, *olen );
Gilles Peskine8877ec22017-03-23 14:37:37 +0100805 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100806
Gilles Peskine8877ec22017-03-23 14:37:37 +0100807cleanup:
808 mbedtls_zeroize( buf, sizeof( buf ) );
809 mbedtls_zeroize( lhash, sizeof( lhash ) );
810
811 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100816/*
817 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200820 int (*f_rng)(void *, unsigned char *, size_t),
821 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100822 int mode, size_t *olen,
823 const unsigned char *input,
824 unsigned char *output,
825 size_t output_max_len)
826{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100827 int ret;
828 size_t ilen, pad_count = 0, i;
829 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
833 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100834
835 ilen = ctx->len;
836
837 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 ret = ( mode == MBEDTLS_RSA_PUBLIC )
841 ? mbedtls_rsa_public( ctx, input, buf )
842 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +0100843
844 if( ret != 0 )
Gilles Peskine8877ec22017-03-23 14:37:37 +0100845 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +0100846
847 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100848 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100849
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100850 /*
851 * Check and get padding len in "constant-time"
852 */
853 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100854
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100855 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +0000857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +0000859
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100860 /* Get padding len, but always read till end of buffer
861 * (minus one, for the 00 byte) */
862 for( i = 0; i < ilen - 3; i++ )
863 {
Pascal Junodb99183d2015-03-11 16:49:45 +0100864 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
865 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100866 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +0000867
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100868 p += pad_count;
869 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +0100870 }
871 else
872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100874
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100875 /* Get padding len, but always read till end of buffer
876 * (minus one, for the 00 byte) */
877 for( i = 0; i < ilen - 3; i++ )
878 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +0100879 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100880 pad_count += ( pad_done == 0 );
881 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100882
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100883 p += pad_count;
884 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +0000885 }
886
Janos Follathe007c9f2016-02-12 13:30:09 +0000887 bad |= ( pad_count < 8 );
Janos Follatha9583432016-02-08 13:59:25 +0000888
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +0100889 if( bad )
Gilles Peskine8877ec22017-03-23 14:37:37 +0100890 {
891 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
892 goto cleanup;
893 }
Paul Bakker8804f692013-02-28 18:06:26 +0100894
Paul Bakker66d5d072014-06-17 16:39:18 +0200895 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine8877ec22017-03-23 14:37:37 +0100896 {
897 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
898 goto cleanup;
899 }
Paul Bakker060c5682009-01-12 21:48:39 +0000900
Paul Bakker27fdf462011-06-09 13:55:13 +0000901 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000902 memcpy( output, p, *olen );
Gilles Peskine8877ec22017-03-23 14:37:37 +0100903 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000904
Gilles Peskine8877ec22017-03-23 14:37:37 +0100905cleanup:
906 mbedtls_zeroize( buf, sizeof( buf ) );
907
908 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000909}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000911
912/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100913 * Do an RSA operation, then remove the message padding
914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200916 int (*f_rng)(void *, unsigned char *, size_t),
917 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +0100918 int mode, size_t *olen,
919 const unsigned char *input,
920 unsigned char *output,
921 size_t output_max_len)
922{
923 switch( ctx->padding )
924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#if defined(MBEDTLS_PKCS1_V15)
926 case MBEDTLS_RSA_PKCS_V15:
927 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +0200928 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +0200929#endif
Paul Bakkerb3869132013-02-28 17:21:01 +0100930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931#if defined(MBEDTLS_PKCS1_V21)
932 case MBEDTLS_RSA_PKCS_V21:
933 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +0200934 olen, input, output,
935 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +0100936#endif
937
938 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +0100940 }
941}
942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100944/*
945 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
946 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100948 int (*f_rng)(void *, unsigned char *, size_t),
949 void *p_rng,
950 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +0100952 unsigned int hashlen,
953 const unsigned char *hash,
954 unsigned char *sig )
955{
956 size_t olen;
957 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +0100959 unsigned int slen, hlen, offset = 0;
960 int ret;
961 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 const mbedtls_md_info_t *md_info;
963 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
966 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200967
968 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100970
971 olen = ctx->len;
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +0100974 {
Paul Bakkerc70b9822013-04-07 22:00:46 +0200975 // Gather length of hash to sign
976 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200978 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +0200980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100982 }
983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100985 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100989 slen = hlen;
990
991 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100993
994 memset( sig, 0, olen );
995
Paul Bakkerb3869132013-02-28 17:21:01 +0100996 // Generate salt of length slen
997 //
998 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001000
1001 // Note: EMSA-PSS encoding is over the length of N - 1 bits
1002 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001003 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001004 p += olen - hlen * 2 - 2;
1005 *p++ = 0x01;
1006 memcpy( p, salt, slen );
1007 p += slen;
1008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -07001010 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1011 {
1012 mbedtls_md_free( &md_ctx );
1013 return( ret );
1014 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001015
1016 // Generate H = Hash( M' )
1017 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_md_starts( &md_ctx );
1019 mbedtls_md_update( &md_ctx, p, 8 );
1020 mbedtls_md_update( &md_ctx, hash, hashlen );
1021 mbedtls_md_update( &md_ctx, salt, slen );
1022 mbedtls_md_finish( &md_ctx, p );
Paul Bakkerb3869132013-02-28 17:21:01 +01001023
1024 // Compensate for boundary condition when applying mask
1025 //
1026 if( msb % 8 == 0 )
1027 offset = 1;
1028
1029 // maskedDB: Apply dbMask to DB
1030 //
1031 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001034
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001035 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001036 sig[0] &= 0xFF >> ( olen * 8 - msb );
1037
1038 p += hlen;
1039 *p++ = 0xBC;
1040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 return( ( mode == MBEDTLS_RSA_PUBLIC )
1042 ? mbedtls_rsa_public( ctx, sig, sig )
1043 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001044}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001048/*
1049 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1050 */
1051/*
1052 * Do an RSA operation to sign the message digest
1053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001055 int (*f_rng)(void *, unsigned char *, size_t),
1056 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001057 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001059 unsigned int hashlen,
1060 const unsigned char *hash,
1061 unsigned char *sig )
1062{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001063 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001064 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001065 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001066 unsigned char *sig_try = NULL, *verif = NULL;
1067 size_t i;
1068 unsigned char diff;
1069 volatile unsigned char diff_no_optimize;
1070 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1073 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001074
1075 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001076 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001081 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1085 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001086
Paul Bakkerc70b9822013-04-07 22:00:46 +02001087 nb_pad -= 10 + oid_size;
1088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001090 }
1091
Paul Bakkerc70b9822013-04-07 22:00:46 +02001092 nb_pad -= hashlen;
1093
Paul Bakkerb3869132013-02-28 17:21:01 +01001094 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001096
1097 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001099 memset( p, 0xFF, nb_pad );
1100 p += nb_pad;
1101 *p++ = 0;
1102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001104 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001105 memcpy( p, hash, hashlen );
1106 }
1107 else
1108 {
1109 /*
1110 * DigestInfo ::= SEQUENCE {
1111 * digestAlgorithm DigestAlgorithmIdentifier,
1112 * digest Digest }
1113 *
1114 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1115 *
1116 * Digest ::= OCTET STRING
1117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001119 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001121 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001123 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001124 memcpy( p, oid, oid_size );
1125 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001127 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001129 *p++ = hashlen;
1130 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001131 }
1132
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001133 if( mode == MBEDTLS_RSA_PUBLIC )
1134 return( mbedtls_rsa_public( ctx, sig, sig ) );
1135
1136 /*
1137 * In order to prevent Lenstra's attack, make the signature in a
1138 * temporary buffer and check it before returning it.
1139 */
1140 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher318daf02016-01-01 21:42:47 +00001141 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001142 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1143
Simon Butcher318daf02016-01-01 21:42:47 +00001144 verif = mbedtls_calloc( 1, ctx->len );
1145 if( verif == NULL )
1146 {
1147 mbedtls_free( sig_try );
1148 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1149 }
1150
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001151 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1152 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1153
1154 /* Compare in constant time just in case */
1155 for( diff = 0, i = 0; i < ctx->len; i++ )
1156 diff |= verif[i] ^ sig[i];
1157 diff_no_optimize = diff;
1158
1159 if( diff_no_optimize != 0 )
1160 {
1161 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1162 goto cleanup;
1163 }
1164
1165 memcpy( sig, sig_try, ctx->len );
1166
1167cleanup:
1168 mbedtls_free( sig_try );
1169 mbedtls_free( verif );
1170
1171 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001172}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001174
1175/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001176 * Do an RSA operation to sign the message digest
1177 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001179 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001180 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001181 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001183 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001184 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 unsigned char *sig )
1186{
Paul Bakker5121ce52009-01-03 21:22:43 +00001187 switch( ctx->padding )
1188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#if defined(MBEDTLS_PKCS1_V15)
1190 case MBEDTLS_RSA_PKCS_V15:
1191 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001192 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001193#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195#if defined(MBEDTLS_PKCS1_V21)
1196 case MBEDTLS_RSA_PKCS_V21:
1197 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001198 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001199#endif
1200
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001203 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001204}
1205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001207/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001208 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001211 int (*f_rng)(void *, unsigned char *, size_t),
1212 void *p_rng,
1213 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001215 unsigned int hashlen,
1216 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001218 int expected_salt_len,
1219 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001220{
Paul Bakker23986e52011-04-24 08:57:21 +00001221 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001222 size_t siglen;
1223 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1225 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001226 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001227 unsigned int hlen;
1228 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 const mbedtls_md_info_t *md_info;
1230 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1233 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001234
Paul Bakker5121ce52009-01-03 21:22:43 +00001235 siglen = ctx->len;
1236
Paul Bakker27fdf462011-06-09 13:55:13 +00001237 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1241 ? mbedtls_rsa_public( ctx, sig, buf )
1242 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001243
1244 if( ret != 0 )
1245 return( ret );
1246
1247 p = buf;
1248
Paul Bakkerb3869132013-02-28 17:21:01 +01001249 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001253 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001254 // Gather length of hash to sign
1255 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001257 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001261 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001264 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001268 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001269
Paul Bakkerb3869132013-02-28 17:21:01 +01001270 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001271
Paul Bakkerb3869132013-02-28 17:21:01 +01001272 // Note: EMSA-PSS verification is over the length of N - 1 bits
1273 //
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001274 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001275
Paul Bakkerb3869132013-02-28 17:21:01 +01001276 // Compensate for boundary condition when applying mask
1277 //
1278 if( msb % 8 == 0 )
1279 {
1280 p++;
1281 siglen -= 1;
1282 }
1283 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_md_init( &md_ctx );
Brian J Murray88c2d222016-06-23 12:57:03 -07001287 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1288 {
1289 mbedtls_md_free( &md_ctx );
1290 return( ret );
1291 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001292
Paul Bakkerb3869132013-02-28 17:21:01 +01001293 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001294
Paul Bakkerb3869132013-02-28 17:21:01 +01001295 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001296
Paul Bakker4de44aa2013-12-31 11:43:01 +01001297 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001298 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001299
Paul Bakkerb3869132013-02-28 17:21:01 +01001300 if( p == buf + siglen ||
1301 *p++ != 0x01 )
1302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 mbedtls_md_free( &md_ctx );
1304 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001305 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001306
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001307 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001308 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001311 slen != (size_t) expected_salt_len )
1312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_md_free( &md_ctx );
1314 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001315 }
1316
Paul Bakkerb3869132013-02-28 17:21:01 +01001317 // Generate H = Hash( M' )
1318 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 mbedtls_md_starts( &md_ctx );
1320 mbedtls_md_update( &md_ctx, zeros, 8 );
1321 mbedtls_md_update( &md_ctx, hash, hashlen );
1322 mbedtls_md_update( &md_ctx, p, slen );
1323 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001326
Paul Bakkerb3869132013-02-28 17:21:01 +01001327 if( memcmp( p + slen, result, hlen ) == 0 )
1328 return( 0 );
1329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001331}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001332
1333/*
1334 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001337 int (*f_rng)(void *, unsigned char *, size_t),
1338 void *p_rng,
1339 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001341 unsigned int hashlen,
1342 const unsigned char *hash,
1343 const unsigned char *sig )
1344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1346 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001347 : md_alg;
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001350 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001352 sig ) );
1353
1354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001358/*
1359 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001362 int (*f_rng)(void *, unsigned char *, size_t),
1363 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001364 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001366 unsigned int hashlen,
1367 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001368 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001369{
1370 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001371 size_t len, siglen, asn1_len;
1372 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
1374 mbedtls_md_type_t msg_md_alg;
1375 const mbedtls_md_info_t *md_info;
1376 mbedtls_asn1_buf oid;
Paul Bakkerb3869132013-02-28 17:21:01 +01001377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1379 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001380
1381 siglen = ctx->len;
1382
1383 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1387 ? mbedtls_rsa_public( ctx, sig, buf )
1388 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001389
1390 if( ret != 0 )
1391 return( ret );
1392
1393 p = buf;
1394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1396 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001397
1398 while( *p != 0 )
1399 {
1400 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001402 p++;
1403 }
1404 p++;
1405
1406 len = siglen - ( p - buf );
1407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001409 {
1410 if( memcmp( p, hash, hashlen ) == 0 )
1411 return( 0 );
1412 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001414 }
1415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001417 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1419 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001420
1421 end = p + len;
1422
1423 // Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1424 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1426 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1427 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001428
1429 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1433 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1434 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001435
1436 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1440 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001441
1442 oid.p = p;
1443 p += oid.len;
1444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1446 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001447
1448 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001450
1451 /*
1452 * assume the algorithm parameters must be NULL
1453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1455 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1458 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001459
1460 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001462
1463 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001465
1466 p += hashlen;
1467
1468 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001470
1471 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001472}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001474
1475/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001476 * Do an RSA operation and check the message digest
1477 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001479 int (*f_rng)(void *, unsigned char *, size_t),
1480 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001481 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001483 unsigned int hashlen,
1484 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001485 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001486{
1487 switch( ctx->padding )
1488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_PKCS1_V15)
1490 case MBEDTLS_RSA_PKCS_V15:
1491 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001492 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001493#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495#if defined(MBEDTLS_PKCS1_V21)
1496 case MBEDTLS_RSA_PKCS_V21:
1497 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001498 hashlen, hash, sig );
1499#endif
1500
1501 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001503 }
1504}
1505
1506/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001507 * Copy the components of an RSA key
1508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001510{
1511 int ret;
1512
1513 dst->ver = src->ver;
1514 dst->len = src->len;
1515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1517 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1520 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1521 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1522 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1523 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1524 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1527 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1528 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1531 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001532
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001533 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001534 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001535
1536cleanup:
1537 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001539
1540 return( ret );
1541}
1542
1543/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001544 * Free the components of an RSA key
1545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001547{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1549 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1550 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1551 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1552 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_THREADING_C)
1555 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001556#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001557}
1558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001560
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001561#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001562
1563/*
1564 * Example RSA-1024 keypair, for test purposes
1565 */
1566#define KEY_LEN 128
1567
1568#define RSA_N "9292758453063D803DD603D5E777D788" \
1569 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1570 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1571 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1572 "93A89813FBF3C4F8066D2D800F7C38A8" \
1573 "1AE31942917403FF4946B0A83D3D3E05" \
1574 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1575 "5E94BB77B07507233A0BC7BAC8F90F79"
1576
1577#define RSA_E "10001"
1578
1579#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1580 "66CA472BC44D253102F8B4A9D3BFA750" \
1581 "91386C0077937FE33FA3252D28855837" \
1582 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1583 "DF79C5CE07EE72C7F123142198164234" \
1584 "CABB724CF78B8173B9F880FC86322407" \
1585 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1586 "071513A1E85B5DFA031F21ECAE91A34D"
1587
1588#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1589 "2C01CAD19EA484A87EA4377637E75500" \
1590 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1591 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1592
1593#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1594 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1595 "910E4168387E3C30AA1E00C339A79508" \
1596 "8452DD96A9A5EA5D9DCA68DA636032AF"
1597
1598#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1599 "3C94D22288ACD763FD8E5600ED4A702D" \
1600 "F84198A5F06C2E72236AE490C93F07F8" \
1601 "3CC559CD27BC2D1CA488811730BB5725"
1602
1603#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1604 "D8AAEA56749EA28623272E4F7D0592AF" \
1605 "7C1F1313CAC9471B5C523BFE592F517B" \
1606 "407A1BD76C164B93DA2D32A383E58357"
1607
1608#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1609 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1610 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1611 "A74206CEC169D74BF5A8C50D6F48EA08"
1612
1613#define PT_LEN 24
1614#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1615 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001618static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001619{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001620#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001621 size_t i;
1622
Paul Bakker545570e2010-07-18 09:00:25 +00001623 if( rng_state != NULL )
1624 rng_state = NULL;
1625
Paul Bakkera3d195c2011-11-27 21:07:34 +00001626 for( i = 0; i < len; ++i )
1627 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001628#else
1629 if( rng_state != NULL )
1630 rng_state = NULL;
1631
1632 arc4random_buf( output, len );
1633#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001634
Paul Bakkera3d195c2011-11-27 21:07:34 +00001635 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001636}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001638
Paul Bakker5121ce52009-01-03 21:22:43 +00001639/*
1640 * Checkup routine
1641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001643{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001644 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001646 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001648 unsigned char rsa_plaintext[PT_LEN];
1649 unsigned char rsa_decrypted[PT_LEN];
1650 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001652 unsigned char sha1sum[20];
1653#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
1657 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1659 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1660 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1661 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1662 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1663 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1664 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1665 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
1667 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1671 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001672 {
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 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001681
1682 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001685 rsa_plaintext, rsa_ciphertext ) != 0 )
1686 {
1687 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001689
1690 return( 1 );
1691 }
1692
1693 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001697 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001698 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001699 {
1700 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001702
1703 return( 1 );
1704 }
1705
1706 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1707 {
1708 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001710
1711 return( 1 );
1712 }
1713
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001714 if( verbose != 0 )
1715 mbedtls_printf( "passed\n" );
1716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001718 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001719 mbedtls_printf( "PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001724 sha1sum, rsa_ciphertext ) != 0 )
1725 {
1726 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001728
1729 return( 1 );
1730 }
1731
1732 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001736 sha1sum, rsa_ciphertext ) != 0 )
1737 {
1738 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001739 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001740
1741 return( 1 );
1742 }
1743
1744 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001745 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001747
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001748 if( verbose != 0 )
1749 mbedtls_printf( "\n" );
1750
Paul Bakker3d8fb632014-04-17 12:42:41 +02001751cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 mbedtls_rsa_free( &rsa );
1753#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001754 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001756 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001757}
1758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761#endif /* MBEDTLS_RSA_C */