blob: d3feeba88b9a4c8aa36512cff69c2e23a4647673 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * The RSA public-key cryptosystem
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
Simon Butcherbdae02c2016-01-20 00:44:42 +000022 * The following sources were referenced in the design of this implementation
23 * of the RSA algorithm:
Paul Bakker5121ce52009-01-03 21:22:43 +000024 *
Simon Butcherbdae02c2016-01-20 00:44:42 +000025 * [1] A method for obtaining digital signatures and public-key cryptosystems
26 * R Rivest, A Shamir, and L Adleman
27 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
28 *
29 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
30 * Menezes, van Oorschot and Vanstone
31 *
Janos Follathe81102e2017-03-22 13:38:28 +000032 * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks
33 * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and
34 * Stefan Mangard
35 * https://arxiv.org/abs/1702.08719v2
36 *
Paul Bakker5121ce52009-01-03 21:22:43 +000037 */
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020041#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_RSA_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/rsa.h"
48#include "mbedtls/oid.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <string.h>
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS1_V21)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/md.h"
Paul Bakkerbb51f0c2012-08-23 07:46:58 +000054#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Paul Bakker5121ce52009-01-03 21:22:43 +000057#include <stdlib.h>
Rich Evans00ab4702015-02-06 13:43:58 +000058#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/platform.h"
Paul Bakker7dc4c442014-02-01 22:50:26 +010062#else
Rich Evans00ab4702015-02-06 13:43:58 +000063#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_printf printf
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +020065#define mbedtls_calloc calloc
66#define mbedtls_free free
Paul Bakker7dc4c442014-02-01 22:50:26 +010067#endif
68
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010069/* Implementation that should never be optimized out by the compiler */
70static void mbedtls_zeroize( void *v, size_t n ) {
71 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
72}
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/*
75 * Initialize an RSA context
76 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000078 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000079 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if defined(MBEDTLS_THREADING_C)
86 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020087#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000088}
89
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010090/*
91 * Set padding for an existing RSA context
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010094{
95 ctx->padding = padding;
96 ctx->hash_id = hash_id;
97}
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101/*
102 * Generate an RSA keypair
103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000105 int (*f_rng)(void *, unsigned char *, size_t),
106 void *p_rng,
107 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000108{
109 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Paul Bakker21eb2802010-08-16 11:10:02 +0000112 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Janos Follathef441782016-09-21 13:18:12 +0100115 if( nbits % 2 )
116 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
117
118 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath10c575b2016-02-23 14:42:48 +0000119 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 /*
122 * find primes P and Q with Q < P so that:
123 * GCD( E, (P-1)*(Q-1) ) == 1
124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127 do
128 {
Janos Follath10c575b2016-02-23 14:42:48 +0000129 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000130 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Janos Follathef441782016-09-21 13:18:12 +0100132 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000133 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 continue;
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200139 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 continue;
141
Janos Follathef441782016-09-21 13:18:12 +0100142 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
143 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
146 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
147 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
148 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152 /*
153 * D = E^-1 mod ((P-1)*(Q-1))
154 * DP = D mod (P - 1)
155 * DQ = D mod (Q - 1)
156 * QP = Q^-1 mod P
157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
159 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
160 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
161 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200163 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
165cleanup:
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 if( ret != 0 )
170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_rsa_free( ctx );
172 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 }
174
Paul Bakker48377d92013-08-30 12:06:24 +0200175 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176}
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000179
180/*
181 * Check a public RSA key
182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184{
Paul Bakker37940d92009-07-10 22:38:58 +0000185 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000187
Paul Bakker48377d92013-08-30 12:06:24 +0200188 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200192 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
193 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200196 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
198 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
200 return( 0 );
201}
202
203/*
204 * Check a private RSA key
205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000207{
208 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 return( ret );
213
Paul Bakker37940d92009-07-10 22:38:58 +0000214 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
218 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
219 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
220 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
223 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
224 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
225 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
226 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
227 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
230 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
231 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
234 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
235 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000236 /*
237 * Check for a valid PKCS1v2 private key
238 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
240 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
241 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
242 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
243 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
244 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
245 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 }
Paul Bakker48377d92013-08-30 12:06:24 +0200249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
252 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
253 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
254 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000257 return( ret );
258
Paul Bakker6c591fa2011-05-05 11:49:20 +0000259 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000261
262 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263}
264
265/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100266 * Check if contexts holding a public and private key match
267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
271 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100274 }
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
277 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100280 }
281
282 return( 0 );
283}
284
285/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 * Do an RSA public key operation
287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000289 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 unsigned char *output )
291{
Paul Bakker23986e52011-04-24 08:57:21 +0000292 int ret;
293 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200298#if defined(MBEDTLS_THREADING_C)
299 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
300 return( ret );
301#endif
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200307 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
308 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 }
310
311 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
313 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
315cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200317 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
318 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100319#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
323 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000325
326 return( 0 );
327}
328
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200329/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200330 * Generate or update blinding values, see section 10 of:
331 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200332 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200333 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200334 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200335static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200336 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
337{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200338 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200339
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200340 if( ctx->Vf.p != NULL )
341 {
342 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
344 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
345 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
346 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200347
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200348 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200349 }
350
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200351 /* Unblinding value: Vf = random number, invertible mod N */
352 do {
353 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
357 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
358 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200359
360 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
362 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 +0200363
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200364
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200365cleanup:
366 return( ret );
367}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200368
Paul Bakker5121ce52009-01-03 21:22:43 +0000369/*
Janos Follathe81102e2017-03-22 13:38:28 +0000370 * Exponent blinding supposed to prevent side-channel attacks using multiple
371 * traces of measurements to recover the RSA key. The more collisions are there,
372 * the more bits of the key can be recovered. See [3].
373 *
374 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
375 * observations on avarage.
376 *
377 * For example with 28 byte blinding to achieve 2 collisions the adversary has
378 * to make 2^112 observations on avarage.
379 *
380 * (With the currently (as of 2017 April) known best algorithms breaking 2048
381 * bit RSA requires approximately as much time as trying out 2^112 random keys.
382 * Thus in this sense with 28 byte blinding the security is not reduced by
383 * side-channel attacks like the one in [3])
384 *
385 * This countermeasure does not help if the key recovery is possible with a
386 * single trace.
387 */
388#define RSA_EXPONENT_BLINDING 28
389
390/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 * Do an RSA private key operation
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200394 int (*f_rng)(void *, unsigned char *, size_t),
395 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000396 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 unsigned char *output )
398{
Paul Bakker23986e52011-04-24 08:57:21 +0000399 int ret;
400 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100401
402 /* Temporary holding the result */
403 mbedtls_mpi T;
404
405 /* Temporaries holding P-1, Q-1 and the
406 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000407 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100408
409#if !defined(MBEDTLS_RSA_NO_CRT)
410 /* Temporaries holding the results mod p resp. mod q. */
411 mbedtls_mpi TP, TQ;
412
413 /* Temporaries holding the blinded exponents for
414 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000415 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100416
417 /* Pointers to actual exponents to be used - either the unblinded
418 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000419 mbedtls_mpi *DP = &ctx->DP;
420 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100421#else
422 /* Temporary holding the blinded exponent (if used). */
423 mbedtls_mpi D_blind;
424
425 /* Pointer to actual exponent to be used - either the unblinded
426 * or the blinded one, depending on the presence of a PRNG. */
427 mbedtls_mpi *D = &ctx->D;
428#endif
429
430#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
431 /* Temporaries holding input mod p resp. mod q. */
432 mbedtls_mpi IP, IQ;
433
434 /* Temporaries holding double check results mod p resp. mod q;
435 * should in the end have the same values as IP and IQ. */
436 mbedtls_mpi CP, CQ;
437
438 /* Comparison results */
439 int check = 0;
440#endif
441
442#if defined(MBEDTLS_RSA_FORCE_BLINDING)
443 if( f_rng == NULL )
444 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Janos Follathe81102e2017-03-22 13:38:28 +0000445#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000446
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100447 /* Make sure we have private key info, prevent possible misuse */
448 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
449 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
450
Hanno Becker06811ce2017-05-03 15:10:34 +0100451#if defined(MBEDTLS_THREADING_C)
452 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
453 return( ret );
454#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000455
Hanno Becker06811ce2017-05-03 15:10:34 +0100456 /* MPI Initialization */
457
458 mbedtls_mpi_init( &T );
459
460 mbedtls_mpi_init( &P1 );
461 mbedtls_mpi_init( &Q1 );
462 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000463
464 if( f_rng != NULL )
465 {
Janos Follathe81102e2017-03-22 13:38:28 +0000466#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000467 mbedtls_mpi_init( &D_blind );
468#else
469 mbedtls_mpi_init( &DP_blind );
470 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000471#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000472 }
Janos Follathe81102e2017-03-22 13:38:28 +0000473
Hanno Becker06811ce2017-05-03 15:10:34 +0100474#if !defined(MBEDTLS_RSA_NO_CRT)
475 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200476#endif
477
Hanno Becker06811ce2017-05-03 15:10:34 +0100478#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
479 mbedtls_mpi_init( &IP ); mbedtls_mpi_init( &IQ );
480 mbedtls_mpi_init( &CP ); mbedtls_mpi_init( &CQ );
481#endif
482
483 /* End of MPI initialization */
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
486 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200488 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
489 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000490 }
491
Hanno Becker06811ce2017-05-03 15:10:34 +0100492#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
493 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &IP, &T, &ctx->P ) );
494 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &IQ, &T, &ctx->Q ) );
495#endif
496
Paul Bakkerf451bac2013-08-30 15:37:02 +0200497 if( f_rng != NULL )
498 {
499 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200500 * Blinding
501 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200502 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200503 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
504 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000506
Janos Follathe81102e2017-03-22 13:38:28 +0000507 /*
508 * Exponent blinding
509 */
510 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
511 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
512
Janos Follathf9203b42017-03-22 15:13:15 +0000513#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000514 /*
515 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
516 */
517 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
518 f_rng, p_rng ) );
519 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
520 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
521 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
522
523 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000524#else
525 /*
526 * DP_blind = ( P - 1 ) * R + DP
527 */
528 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
529 f_rng, p_rng ) );
530 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
531 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
532 &ctx->DP ) );
533
534 DP = &DP_blind;
535
536 /*
537 * DQ_blind = ( Q - 1 ) * R + DQ
538 */
539 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
540 f_rng, p_rng ) );
541 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
542 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
543 &ctx->DQ ) );
544
545 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000546#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200547 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000550 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100551#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200552 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000553 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000554 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100555 * TP = input ^ dP mod P
556 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100558
559 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
560 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000561
562 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100563 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000564 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100565 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
566 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
567 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100570 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000571 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100572 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
573 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200575
Paul Bakkerf451bac2013-08-30 15:37:02 +0200576 if( f_rng != NULL )
577 {
578 /*
579 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200580 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200581 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200582 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200584 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
Hanno Becker06811ce2017-05-03 15:10:34 +0100586 /* If requested by the config, verify the result to prevent glitching attacks.
587 * For that, check the two prime moduli separately. */
588#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
589 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &CP, &T, &ctx->E, &ctx->P, &ctx->RP ) );
590 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &CQ, &T, &ctx->E, &ctx->Q, &ctx->RQ ) );
591
592 check |= mbedtls_mpi_cmp_mpi( &CP, &IP );
593 check |= mbedtls_mpi_cmp_mpi( &CQ, &IQ );
594
595 if( check != 0 )
596 {
597 /* Verification failed */
598 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
599 goto cleanup;
600 }
601#endif /* MBEDTLS_RSA_REQUIRE_VERIFICATION */
602
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
606cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200608 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
609 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200610#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200611
Hanno Becker06811ce2017-05-03 15:10:34 +0100612 mbedtls_mpi_free( &P1 );
613 mbedtls_mpi_free( &Q1 );
614 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000615
616 if( f_rng != NULL )
617 {
Janos Follathe81102e2017-03-22 13:38:28 +0000618#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000619 mbedtls_mpi_free( &D_blind );
620#else
621 mbedtls_mpi_free( &DP_blind );
622 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000623#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000624 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
Hanno Becker06811ce2017-05-03 15:10:34 +0100626 mbedtls_mpi_free( &T );
627
628#if !defined(MBEDTLS_RSA_NO_CRT)
629 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
630#endif
631
632#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
633 mbedtls_mpi_free( &IP ); mbedtls_mpi_free( &IQ );
634 mbedtls_mpi_free( &CP ); mbedtls_mpi_free( &CQ );
635#endif
636
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640 return( 0 );
641}
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000644/**
645 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
646 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000647 * \param dst buffer to mask
648 * \param dlen length of destination buffer
649 * \param src source of the mask generation
650 * \param slen length of the source buffer
651 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000652 */
Paul Bakker48377d92013-08-30 12:06:24 +0200653static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000655{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000657 unsigned char counter[4];
658 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000659 unsigned int hlen;
660 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000663 memset( counter, 0, 4 );
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000666
Simon Butcher02037452016-03-01 21:19:12 +0000667 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000668 p = dst;
669
670 while( dlen > 0 )
671 {
672 use_len = hlen;
673 if( dlen < hlen )
674 use_len = dlen;
675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_md_starts( md_ctx );
677 mbedtls_md_update( md_ctx, src, slen );
678 mbedtls_md_update( md_ctx, counter, 4 );
679 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000680
681 for( i = 0; i < use_len; ++i )
682 *p++ ^= mask[i];
683
684 counter[3]++;
685
686 dlen -= use_len;
687 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200688
689 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100694/*
695 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100698 int (*f_rng)(void *, unsigned char *, size_t),
699 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100700 int mode,
701 const unsigned char *label, size_t label_len,
702 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100703 const unsigned char *input,
704 unsigned char *output )
705{
706 size_t olen;
707 int ret;
708 unsigned char *p = output;
709 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 const mbedtls_md_info_t *md_info;
711 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
714 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200715
716 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100720 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100722
723 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100725
Simon Butcher02037452016-03-01 21:19:12 +0000726 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000727 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100729
730 memset( output, 0, olen );
731
732 *p++ = 0;
733
Simon Butcher02037452016-03-01 21:19:12 +0000734 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100735 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100737
738 p += hlen;
739
Simon Butcher02037452016-03-01 21:19:12 +0000740 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100742 p += hlen;
743 p += olen - 2 * hlen - 2 - ilen;
744 *p++ = 1;
745 memcpy( p, input, ilen );
746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700748 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
749 {
750 mbedtls_md_free( &md_ctx );
751 return( ret );
752 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100753
Simon Butcher02037452016-03-01 21:19:12 +0000754 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100755 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
756 &md_ctx );
757
Simon Butcher02037452016-03-01 21:19:12 +0000758 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100759 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
760 &md_ctx );
761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 return( ( mode == MBEDTLS_RSA_PUBLIC )
765 ? mbedtls_rsa_public( ctx, output, output )
766 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100767}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100771/*
772 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
773 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100775 int (*f_rng)(void *, unsigned char *, size_t),
776 void *p_rng,
777 int mode, size_t ilen,
778 const unsigned char *input,
779 unsigned char *output )
780{
781 size_t nb_pad, olen;
782 int ret;
783 unsigned char *p = output;
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
786 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200787
Janos Follath1ed9f992016-03-18 11:45:44 +0000788 // We don't check p_rng because it won't be dereferenced here
789 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100791
792 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100793
Simon Butcher02037452016-03-01 21:19:12 +0000794 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000795 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100797
798 nb_pad = olen - 3 - ilen;
799
800 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100804
805 while( nb_pad-- > 0 )
806 {
807 int rng_dl = 100;
808
809 do {
810 ret = f_rng( p_rng, p, 1 );
811 } while( *p == 0 && --rng_dl && ret == 0 );
812
Simon Butcher02037452016-03-01 21:19:12 +0000813 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200814 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100816
817 p++;
818 }
819 }
820 else
821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100823
824 while( nb_pad-- > 0 )
825 *p++ = 0xFF;
826 }
827
828 *p++ = 0;
829 memcpy( p, input, ilen );
830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 return( ( mode == MBEDTLS_RSA_PUBLIC )
832 ? mbedtls_rsa_public( ctx, output, output )
833 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100834}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100836
Paul Bakker5121ce52009-01-03 21:22:43 +0000837/*
838 * Add the message padding, then do an RSA operation
839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000841 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000842 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000843 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000844 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000845 unsigned char *output )
846{
Paul Bakker5121ce52009-01-03 21:22:43 +0000847 switch( ctx->padding )
848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#if defined(MBEDTLS_PKCS1_V15)
850 case MBEDTLS_RSA_PKCS_V15:
851 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100852 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200853#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_PKCS1_V21)
856 case MBEDTLS_RSA_PKCS_V21:
857 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100858 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000859#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000860
861 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000863 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000864}
865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000867/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100868 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000869 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200871 int (*f_rng)(void *, unsigned char *, size_t),
872 void *p_rng,
873 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100874 const unsigned char *label, size_t label_len,
875 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100876 const unsigned char *input,
877 unsigned char *output,
878 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000879{
Paul Bakker23986e52011-04-24 08:57:21 +0000880 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100881 size_t ilen, i, pad_len;
882 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
884 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000885 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 const mbedtls_md_info_t *md_info;
887 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100888
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100889 /*
890 * Parameters sanity checks
891 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
893 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000894
895 ilen = ctx->len;
896
Paul Bakker27fdf462011-06-09 13:55:13 +0000897 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100901 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100903
Janos Follathc17cda12016-02-11 11:08:18 +0000904 hlen = mbedtls_md_get_size( md_info );
905
906 // checking for integer underflow
907 if( 2 * hlen + 2 > ilen )
908 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
909
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100910 /*
911 * RSA operation
912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 ret = ( mode == MBEDTLS_RSA_PUBLIC )
914 ? mbedtls_rsa_public( ctx, input, buf )
915 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000916
917 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100918 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000919
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100920 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100921 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100922 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700924 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
925 {
926 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100927 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700928 }
929
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100930
931 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100933
934 /* seed: Apply seedMask to maskedSeed */
935 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
936 &md_ctx );
937
938 /* DB: Apply dbMask to maskedDB */
939 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
940 &md_ctx );
941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100943
944 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100945 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100946 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000947 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100948 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000949
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100950 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100951
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100952 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100953
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100954 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100955 for( i = 0; i < hlen; i++ )
956 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100957
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100958 /* Get zero-padding len, but always read till end of buffer
959 * (minus one, for the 01 byte) */
960 pad_len = 0;
961 pad_done = 0;
962 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
963 {
964 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100965 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100966 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100967
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100968 p += pad_len;
969 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100970
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100971 /*
972 * The only information "leaked" is whether the padding was correct or not
973 * (eg, no data is copied if it was not correct). This meets the
974 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
975 * the different error conditions.
976 */
977 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100978 {
979 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
980 goto cleanup;
981 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100982
Paul Bakker66d5d072014-06-17 16:39:18 +0200983 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100984 {
985 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
986 goto cleanup;
987 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100988
989 *olen = ilen - (p - buf);
990 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100991 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +0100992
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100993cleanup:
994 mbedtls_zeroize( buf, sizeof( buf ) );
995 mbedtls_zeroize( lhash, sizeof( lhash ) );
996
997 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100998}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001002/*
1003 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001006 int (*f_rng)(void *, unsigned char *, size_t),
1007 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001008 int mode, size_t *olen,
1009 const unsigned char *input,
1010 unsigned char *output,
1011 size_t output_max_len)
1012{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001013 int ret;
1014 size_t ilen, pad_count = 0, i;
1015 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1019 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001020
1021 ilen = ctx->len;
1022
1023 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1027 ? mbedtls_rsa_public( ctx, input, buf )
1028 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001029
1030 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001031 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001032
1033 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001034 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001035
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001036 /*
1037 * Check and get padding len in "constant-time"
1038 */
1039 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001040
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001041 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001045
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001046 /* Get padding len, but always read till end of buffer
1047 * (minus one, for the 00 byte) */
1048 for( i = 0; i < ilen - 3; i++ )
1049 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001050 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1051 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001052 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001053
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001054 p += pad_count;
1055 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001056 }
1057 else
1058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001060
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001061 /* Get padding len, but always read till end of buffer
1062 * (minus one, for the 00 byte) */
1063 for( i = 0; i < ilen - 3; i++ )
1064 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001065 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001066 pad_count += ( pad_done == 0 );
1067 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001068
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001069 p += pad_count;
1070 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001071 }
1072
Janos Follathc69fa502016-02-12 13:30:09 +00001073 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001074
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001075 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001076 {
1077 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1078 goto cleanup;
1079 }
Paul Bakker8804f692013-02-28 18:06:26 +01001080
Paul Bakker66d5d072014-06-17 16:39:18 +02001081 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001082 {
1083 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1084 goto cleanup;
1085 }
Paul Bakker060c5682009-01-12 21:48:39 +00001086
Paul Bakker27fdf462011-06-09 13:55:13 +00001087 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001089 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001090
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001091cleanup:
1092 mbedtls_zeroize( buf, sizeof( buf ) );
1093
1094 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001095}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001097
1098/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001099 * Do an RSA operation, then remove the message padding
1100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001102 int (*f_rng)(void *, unsigned char *, size_t),
1103 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001104 int mode, size_t *olen,
1105 const unsigned char *input,
1106 unsigned char *output,
1107 size_t output_max_len)
1108{
1109 switch( ctx->padding )
1110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#if defined(MBEDTLS_PKCS1_V15)
1112 case MBEDTLS_RSA_PKCS_V15:
1113 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001114 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001115#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#if defined(MBEDTLS_PKCS1_V21)
1118 case MBEDTLS_RSA_PKCS_V21:
1119 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001120 olen, input, output,
1121 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001122#endif
1123
1124 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001126 }
1127}
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001130/*
1131 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001134 int (*f_rng)(void *, unsigned char *, size_t),
1135 void *p_rng,
1136 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001138 unsigned int hashlen,
1139 const unsigned char *hash,
1140 unsigned char *sig )
1141{
1142 size_t olen;
1143 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001145 unsigned int slen, hlen, offset = 0;
1146 int ret;
1147 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 const mbedtls_md_info_t *md_info;
1149 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1152 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001153
1154 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001156
1157 olen = ctx->len;
1158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001160 {
Simon Butcher02037452016-03-01 21:19:12 +00001161 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001163 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001167 }
1168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001170 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001174 slen = hlen;
1175
1176 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001178
1179 memset( sig, 0, olen );
1180
Simon Butcher02037452016-03-01 21:19:12 +00001181 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001182 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001184
Simon Butcher02037452016-03-01 21:19:12 +00001185 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001186 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001187 p += olen - hlen * 2 - 2;
1188 *p++ = 0x01;
1189 memcpy( p, salt, slen );
1190 p += slen;
1191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001193 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1194 {
1195 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001196 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001197 return( ret );
1198 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001199
Simon Butcher02037452016-03-01 21:19:12 +00001200 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_md_starts( &md_ctx );
1202 mbedtls_md_update( &md_ctx, p, 8 );
1203 mbedtls_md_update( &md_ctx, hash, hashlen );
1204 mbedtls_md_update( &md_ctx, salt, slen );
1205 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001206 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001207
Simon Butcher02037452016-03-01 21:19:12 +00001208 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001209 if( msb % 8 == 0 )
1210 offset = 1;
1211
Simon Butcher02037452016-03-01 21:19:12 +00001212 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001213 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001216
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001217 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001218 sig[0] &= 0xFF >> ( olen * 8 - msb );
1219
1220 p += hlen;
1221 *p++ = 0xBC;
1222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 return( ( mode == MBEDTLS_RSA_PUBLIC )
1224 ? mbedtls_rsa_public( ctx, sig, sig )
1225 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001230/*
1231 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1232 */
1233/*
1234 * Do an RSA operation to sign the message digest
1235 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001237 int (*f_rng)(void *, unsigned char *, size_t),
1238 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001239 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001241 unsigned int hashlen,
1242 const unsigned char *hash,
1243 unsigned char *sig )
1244{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001245 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001246 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001247 const char *oid = NULL;
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001248 unsigned char *sig_try = NULL, *verif = NULL;
1249 size_t i;
1250 unsigned char diff;
1251 volatile unsigned char diff_no_optimize;
1252 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1255 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001256
1257 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001258 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001263 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1267 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001268
Paul Bakkerc70b9822013-04-07 22:00:46 +02001269 nb_pad -= 10 + oid_size;
1270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001272 }
1273
Paul Bakkerc70b9822013-04-07 22:00:46 +02001274 nb_pad -= hashlen;
1275
Paul Bakkerb3869132013-02-28 17:21:01 +01001276 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001278
1279 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001281 memset( p, 0xFF, nb_pad );
1282 p += nb_pad;
1283 *p++ = 0;
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001286 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001287 memcpy( p, hash, hashlen );
1288 }
1289 else
1290 {
1291 /*
1292 * DigestInfo ::= SEQUENCE {
1293 * digestAlgorithm DigestAlgorithmIdentifier,
1294 * digest Digest }
1295 *
1296 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1297 *
1298 * Digest ::= OCTET STRING
1299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001301 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001303 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001305 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001306 memcpy( p, oid, oid_size );
1307 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001309 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001311 *p++ = hashlen;
1312 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001313 }
1314
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001315 if( mode == MBEDTLS_RSA_PUBLIC )
1316 return( mbedtls_rsa_public( ctx, sig, sig ) );
1317
1318 /*
1319 * In order to prevent Lenstra's attack, make the signature in a
1320 * temporary buffer and check it before returning it.
1321 */
1322 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Butcher1285ab52016-01-01 21:42:47 +00001323 if( sig_try == NULL )
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001324 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1325
Simon Butcher1285ab52016-01-01 21:42:47 +00001326 verif = mbedtls_calloc( 1, ctx->len );
1327 if( verif == NULL )
1328 {
1329 mbedtls_free( sig_try );
1330 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
1331 }
1332
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001333 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
1334 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
1335
1336 /* Compare in constant time just in case */
1337 for( diff = 0, i = 0; i < ctx->len; i++ )
1338 diff |= verif[i] ^ sig[i];
1339 diff_no_optimize = diff;
1340
1341 if( diff_no_optimize != 0 )
1342 {
1343 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
1344 goto cleanup;
1345 }
1346
1347 memcpy( sig, sig_try, ctx->len );
1348
1349cleanup:
1350 mbedtls_free( sig_try );
1351 mbedtls_free( verif );
1352
1353 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001356
1357/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 * Do an RSA operation to sign the message digest
1359 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001361 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001362 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001363 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001365 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001366 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001367 unsigned char *sig )
1368{
Paul Bakker5121ce52009-01-03 21:22:43 +00001369 switch( ctx->padding )
1370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_PKCS1_V15)
1372 case MBEDTLS_RSA_PKCS_V15:
1373 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001374 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001375#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377#if defined(MBEDTLS_PKCS1_V21)
1378 case MBEDTLS_RSA_PKCS_V21:
1379 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001380 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001381#endif
1382
Paul Bakker5121ce52009-01-03 21:22:43 +00001383 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001386}
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001389/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001390 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001393 int (*f_rng)(void *, unsigned char *, size_t),
1394 void *p_rng,
1395 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001397 unsigned int hashlen,
1398 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001400 int expected_salt_len,
1401 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001402{
Paul Bakker23986e52011-04-24 08:57:21 +00001403 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001404 size_t siglen;
1405 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001407 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001408 unsigned int hlen;
1409 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410 const mbedtls_md_info_t *md_info;
1411 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001412 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1415 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001416
Paul Bakker5121ce52009-01-03 21:22:43 +00001417 siglen = ctx->len;
1418
Paul Bakker27fdf462011-06-09 13:55:13 +00001419 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1423 ? mbedtls_rsa_public( ctx, sig, buf )
1424 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001425
1426 if( ret != 0 )
1427 return( ret );
1428
1429 p = buf;
1430
Paul Bakkerb3869132013-02-28 17:21:01 +01001431 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001435 {
Simon Butcher02037452016-03-01 21:19:12 +00001436 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001438 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001442 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001445 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001449 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001450
Paul Bakkerb3869132013-02-28 17:21:01 +01001451 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001452
Simon Butcher02037452016-03-01 21:19:12 +00001453 /*
1454 * Note: EMSA-PSS verification is over the length of N - 1 bits
1455 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001456 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001457
Simon Butcher02037452016-03-01 21:19:12 +00001458 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001459 if( msb % 8 == 0 )
1460 {
1461 p++;
1462 siglen -= 1;
1463 }
1464 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001468 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1469 {
1470 mbedtls_md_free( &md_ctx );
1471 return( ret );
1472 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001473
Paul Bakkerb3869132013-02-28 17:21:01 +01001474 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001475
Paul Bakkerb3869132013-02-28 17:21:01 +01001476 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001477
Paul Bakker4de44aa2013-12-31 11:43:01 +01001478 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001479 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001480
Paul Bakkerb3869132013-02-28 17:21:01 +01001481 if( p == buf + siglen ||
1482 *p++ != 0x01 )
1483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 mbedtls_md_free( &md_ctx );
1485 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001486 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001487
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001488 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001489 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001492 slen != (size_t) expected_salt_len )
1493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 mbedtls_md_free( &md_ctx );
1495 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001496 }
1497
Simon Butcher02037452016-03-01 21:19:12 +00001498 /*
1499 * Generate H = Hash( M' )
1500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 mbedtls_md_starts( &md_ctx );
1502 mbedtls_md_update( &md_ctx, zeros, 8 );
1503 mbedtls_md_update( &md_ctx, hash, hashlen );
1504 mbedtls_md_update( &md_ctx, p, slen );
1505 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001508
Paul Bakkerb3869132013-02-28 17:21:01 +01001509 if( memcmp( p + slen, result, hlen ) == 0 )
1510 return( 0 );
1511 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001513}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001514
1515/*
1516 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001519 int (*f_rng)(void *, unsigned char *, size_t),
1520 void *p_rng,
1521 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001523 unsigned int hashlen,
1524 const unsigned char *hash,
1525 const unsigned char *sig )
1526{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1528 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001529 : md_alg;
1530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001532 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001534 sig ) );
1535
1536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001540/*
1541 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1542 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001544 int (*f_rng)(void *, unsigned char *, size_t),
1545 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001546 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001548 unsigned int hashlen,
1549 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001550 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001551{
1552 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001553 size_t len, siglen, asn1_len;
1554 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 mbedtls_md_type_t msg_md_alg;
1556 const mbedtls_md_info_t *md_info;
1557 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001558 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1561 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001562
1563 siglen = ctx->len;
1564
1565 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1569 ? mbedtls_rsa_public( ctx, sig, buf )
1570 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001571
1572 if( ret != 0 )
1573 return( ret );
1574
1575 p = buf;
1576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1578 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001579
1580 while( *p != 0 )
1581 {
1582 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001584 p++;
1585 }
1586 p++;
1587
1588 len = siglen - ( p - buf );
1589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001591 {
1592 if( memcmp( p, hash, hashlen ) == 0 )
1593 return( 0 );
1594 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001596 }
1597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001599 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1601 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001602
1603 end = p + len;
1604
Simon Butcher02037452016-03-01 21:19:12 +00001605 /*
1606 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1609 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1610 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001611
1612 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1616 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1617 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001618
1619 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1623 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001624
1625 oid.p = p;
1626 p += oid.len;
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1629 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001630
1631 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001633
1634 /*
1635 * assume the algorithm parameters must be NULL
1636 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1638 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1641 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001642
1643 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001645
1646 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001647 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001648
1649 p += hashlen;
1650
1651 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001653
1654 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001655}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
1658/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001659 * Do an RSA operation and check the message digest
1660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001662 int (*f_rng)(void *, unsigned char *, size_t),
1663 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001664 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001666 unsigned int hashlen,
1667 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001668 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001669{
1670 switch( ctx->padding )
1671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001672#if defined(MBEDTLS_PKCS1_V15)
1673 case MBEDTLS_RSA_PKCS_V15:
1674 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001675 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001676#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#if defined(MBEDTLS_PKCS1_V21)
1679 case MBEDTLS_RSA_PKCS_V21:
1680 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001681 hashlen, hash, sig );
1682#endif
1683
1684 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001686 }
1687}
1688
1689/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001690 * Copy the components of an RSA key
1691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001693{
1694 int ret;
1695
1696 dst->ver = src->ver;
1697 dst->len = src->len;
1698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1700 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1703 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1704 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1705 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1706 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1707 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1710 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1711 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1714 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001715
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001716 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001717 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001718
1719cleanup:
1720 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001722
1723 return( ret );
1724}
1725
1726/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001727 * Free the components of an RSA key
1728 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001729void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001730{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1732 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1733 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1734 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1735 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_THREADING_C)
1738 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001739#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001740}
1741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001742#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001743
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001744#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001745
1746/*
1747 * Example RSA-1024 keypair, for test purposes
1748 */
1749#define KEY_LEN 128
1750
1751#define RSA_N "9292758453063D803DD603D5E777D788" \
1752 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1753 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1754 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1755 "93A89813FBF3C4F8066D2D800F7C38A8" \
1756 "1AE31942917403FF4946B0A83D3D3E05" \
1757 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1758 "5E94BB77B07507233A0BC7BAC8F90F79"
1759
1760#define RSA_E "10001"
1761
1762#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1763 "66CA472BC44D253102F8B4A9D3BFA750" \
1764 "91386C0077937FE33FA3252D28855837" \
1765 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1766 "DF79C5CE07EE72C7F123142198164234" \
1767 "CABB724CF78B8173B9F880FC86322407" \
1768 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1769 "071513A1E85B5DFA031F21ECAE91A34D"
1770
1771#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1772 "2C01CAD19EA484A87EA4377637E75500" \
1773 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1774 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1775
1776#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1777 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1778 "910E4168387E3C30AA1E00C339A79508" \
1779 "8452DD96A9A5EA5D9DCA68DA636032AF"
1780
1781#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1782 "3C94D22288ACD763FD8E5600ED4A702D" \
1783 "F84198A5F06C2E72236AE490C93F07F8" \
1784 "3CC559CD27BC2D1CA488811730BB5725"
1785
1786#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1787 "D8AAEA56749EA28623272E4F7D0592AF" \
1788 "7C1F1313CAC9471B5C523BFE592F517B" \
1789 "407A1BD76C164B93DA2D32A383E58357"
1790
1791#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1792 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1793 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1794 "A74206CEC169D74BF5A8C50D6F48EA08"
1795
1796#define PT_LEN 24
1797#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1798 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001801static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001802{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001803#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001804 size_t i;
1805
Paul Bakker545570e2010-07-18 09:00:25 +00001806 if( rng_state != NULL )
1807 rng_state = NULL;
1808
Paul Bakkera3d195c2011-11-27 21:07:34 +00001809 for( i = 0; i < len; ++i )
1810 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001811#else
1812 if( rng_state != NULL )
1813 rng_state = NULL;
1814
1815 arc4random_buf( output, len );
1816#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001817
Paul Bakkera3d195c2011-11-27 21:07:34 +00001818 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001819}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001821
Paul Bakker5121ce52009-01-03 21:22:43 +00001822/*
1823 * Checkup routine
1824 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001826{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001827 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001829 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001831 unsigned char rsa_plaintext[PT_LEN];
1832 unsigned char rsa_decrypted[PT_LEN];
1833 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001835 unsigned char sha1sum[20];
1836#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001839
1840 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001841 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1842 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1843 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1844 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1845 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1846 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1847 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1848 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001849
1850 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1854 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001855 {
1856 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001857 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001858
Hanno Becker5bc87292017-05-03 15:09:31 +01001859 ret = 1;
1860 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001861 }
1862
1863 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001865
1866 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001868 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001869 rsa_plaintext, rsa_ciphertext ) != 0 )
1870 {
1871 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001873
Hanno Becker5bc87292017-05-03 15:09:31 +01001874 ret = 1;
1875 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001876 }
1877
1878 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001882 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001883 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001884 {
1885 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001887
Hanno Becker5bc87292017-05-03 15:09:31 +01001888 ret = 1;
1889 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001890 }
1891
1892 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1893 {
1894 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001896
Hanno Becker5bc87292017-05-03 15:09:31 +01001897 ret = 1;
1898 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001899 }
1900
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001901 if( verbose != 0 )
1902 mbedtls_printf( "passed\n" );
1903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001905 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001906 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001911 sha1sum, rsa_ciphertext ) != 0 )
1912 {
1913 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001915
Hanno Becker5bc87292017-05-03 15:09:31 +01001916 ret = 1;
1917 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001918 }
1919
1920 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001924 sha1sum, rsa_ciphertext ) != 0 )
1925 {
1926 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001928
Hanno Becker5bc87292017-05-03 15:09:31 +01001929 ret = 1;
1930 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001931 }
1932
1933 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001934 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001936
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001937 if( verbose != 0 )
1938 mbedtls_printf( "\n" );
1939
Paul Bakker3d8fb632014-04-17 12:42:41 +02001940cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 mbedtls_rsa_free( &rsa );
1942#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001943 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001944#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001945 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001946}
1947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001948#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950#endif /* MBEDTLS_RSA_C */