blob: 11ba2019abe73c79037e0ebd58a96c1c70efa030 [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
Hanno Beckera988a272017-09-07 11:32:04 +010069#if !defined(MBEDTLS_RSA_FORCE_BLINDING) && \
70 defined(MBEDTLS_DEPRECATED_WARNING)
71#warning Not enforcing blinding checks for RSA private key operations\
72 is deprecated. Please uncomment MBEDTLS_RSA_FORCE_BLINDING\
73 in config.h to enforce blinding checks.
74#endif
75
Gilles Peskine4a7f6a02017-03-23 14:37:37 +010076/* Implementation that should never be optimized out by the compiler */
77static void mbedtls_zeroize( void *v, size_t n ) {
78 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
79}
80
Paul Bakker5121ce52009-01-03 21:22:43 +000081/*
82 * Initialize an RSA context
83 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +000085 int padding,
Paul Bakker21eb2802010-08-16 11:10:02 +000086 int hash_id )
Paul Bakker5121ce52009-01-03 21:22:43 +000087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if defined(MBEDTLS_THREADING_C)
93 mbedtls_mutex_init( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +020094#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000095}
96
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +010097/*
98 * Set padding for an existing RSA context
99 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Manuel Pégourié-Gonnard844a4c02014-03-10 21:55:35 +0100101{
102 ctx->padding = padding;
103 ctx->hash_id = hash_id;
104}
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108/*
109 * Generate an RSA keypair
110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000112 int (*f_rng)(void *, unsigned char *, size_t),
113 void *p_rng,
114 unsigned int nbits, int exponent )
Paul Bakker5121ce52009-01-03 21:22:43 +0000115{
116 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_mpi P1, Q1, H, G;
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Paul Bakker21eb2802010-08-16 11:10:02 +0000119 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
Janos Follathef441782016-09-21 13:18:12 +0100122 if( nbits % 2 )
123 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
124
125 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Janos Follath10c575b2016-02-23 14:42:48 +0000126 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
128 /*
129 * find primes P and Q with Q < P so that:
130 * GCD( E, (P-1)*(Q-1) ) == 1
131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134 do
135 {
Janos Follath10c575b2016-02-23 14:42:48 +0000136 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000137 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Janos Follathef441782016-09-21 13:18:12 +0100139 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Paul Bakker21eb2802010-08-16 11:10:02 +0000140 f_rng, p_rng ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 continue;
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200146 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 continue;
148
Janos Follathef441782016-09-21 13:18:12 +0100149 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
150 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
153 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
154 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
155 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
159 /*
160 * D = E^-1 mod ((P-1)*(Q-1))
161 * DP = D mod (P - 1)
162 * DQ = D mod (Q - 1)
163 * QP = Q^-1 mod P
164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
166 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
167 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
168 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200170 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
172cleanup:
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
176 if( ret != 0 )
177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_rsa_free( ctx );
179 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 }
181
Paul Bakker48377d92013-08-30 12:06:24 +0200182 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183}
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#endif /* MBEDTLS_GENPRIME */
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
187/*
188 * Check a public RSA key
189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000191{
Paul Bakker37940d92009-07-10 22:38:58 +0000192 if( !ctx->N.p || !ctx->E.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000194
Paul Bakker48377d92013-08-30 12:06:24 +0200195 if( ( ctx->N.p[0] & 1 ) == 0 ||
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 ( ctx->E.p[0] & 1 ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200199 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
200 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200203 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
205 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +0000206
207 return( 0 );
208}
209
210/*
211 * Check a private RSA key
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +0000214{
215 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 return( ret );
220
Paul Bakker37940d92009-07-10 22:38:58 +0000221 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Paul Bakker37940d92009-07-10 22:38:58 +0000223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
225 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
226 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
227 mbedtls_mpi_init( &QP );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
230 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
231 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
232 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
233 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
234 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
237 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
238 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
241 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
242 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Paul Bakkerb572adf2010-07-18 08:29:32 +0000243 /*
244 * Check for a valid PKCS1v2 private key
245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
247 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
248 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
249 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
250 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
251 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
252 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 }
Paul Bakker48377d92013-08-30 12:06:24 +0200256
Paul Bakker5121ce52009-01-03 21:22:43 +0000257cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
259 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
260 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
261 mbedtls_mpi_free( &QP );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Paul Bakker9d781402011-05-09 16:17:09 +0000264 return( ret );
265
Paul Bakker6c591fa2011-05-05 11:49:20 +0000266 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Paul Bakker6c591fa2011-05-05 11:49:20 +0000268
269 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000270}
271
272/*
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100273 * Check if contexts holding a public and private key match
274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100276{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
278 mbedtls_rsa_check_privkey( prv ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100281 }
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
284 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Manuel Pégourié-Gonnard2f8d1f92014-11-06 14:02:51 +0100287 }
288
289 return( 0 );
290}
291
292/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 * Do an RSA public key operation
294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000296 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 unsigned char *output )
298{
Paul Bakker23986e52011-04-24 08:57:21 +0000299 int ret;
300 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 mbedtls_mpi T;
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_mpi_init( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200305#if defined(MBEDTLS_THREADING_C)
306 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
307 return( ret );
308#endif
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200314 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
315 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 }
317
318 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
320 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
322cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200324 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
325 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnard88fca3e2015-03-27 15:06:07 +0100326#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_mpi_free( &T );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
333 return( 0 );
334}
335
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200336/*
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200337 * Generate or update blinding values, see section 10 of:
338 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Manuel Pégourié-Gonnard998930a2015-04-03 13:48:06 +0200339 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200340 * Berlin Heidelberg, 1996. p. 104-113.
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200341 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200342static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200343 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
344{
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200345 int ret, count = 0;
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200346
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200347 if( ctx->Vf.p != NULL )
348 {
349 /* We already have blinding values, just update them by squaring */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
351 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
352 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
353 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200354
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200355 goto cleanup;
Manuel Pégourié-Gonnard8a109f12013-09-10 13:37:26 +0200356 }
357
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200358 /* Unblinding value: Vf = random number, invertible mod N */
359 do {
360 if( count++ > 10 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Manuel Pégourié-Gonnard4d89c7e2013-10-04 15:18:38 +0200362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
364 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
365 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200366
367 /* Blinding value: Vi = Vf^(-e) mod N */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
369 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 +0200370
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200371
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200372cleanup:
373 return( ret );
374}
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200375
Paul Bakker5121ce52009-01-03 21:22:43 +0000376/*
Janos Follathe81102e2017-03-22 13:38:28 +0000377 * Exponent blinding supposed to prevent side-channel attacks using multiple
378 * traces of measurements to recover the RSA key. The more collisions are there,
379 * the more bits of the key can be recovered. See [3].
380 *
381 * Collecting n collisions with m bit long blinding value requires 2^(m-m/n)
382 * observations on avarage.
383 *
384 * For example with 28 byte blinding to achieve 2 collisions the adversary has
385 * to make 2^112 observations on avarage.
386 *
387 * (With the currently (as of 2017 April) known best algorithms breaking 2048
388 * bit RSA requires approximately as much time as trying out 2^112 random keys.
389 * Thus in this sense with 28 byte blinding the security is not reduced by
390 * side-channel attacks like the one in [3])
391 *
392 * This countermeasure does not help if the key recovery is possible with a
393 * single trace.
394 */
395#define RSA_EXPONENT_BLINDING 28
396
397/*
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 * Do an RSA private key operation
399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200401 int (*f_rng)(void *, unsigned char *, size_t),
402 void *p_rng,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000403 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000404 unsigned char *output )
405{
Paul Bakker23986e52011-04-24 08:57:21 +0000406 int ret;
407 size_t olen;
Hanno Becker06811ce2017-05-03 15:10:34 +0100408
409 /* Temporary holding the result */
410 mbedtls_mpi T;
411
412 /* Temporaries holding P-1, Q-1 and the
413 * exponent blinding factor, respectively. */
Janos Follathf9203b42017-03-22 15:13:15 +0000414 mbedtls_mpi P1, Q1, R;
Hanno Becker06811ce2017-05-03 15:10:34 +0100415
416#if !defined(MBEDTLS_RSA_NO_CRT)
417 /* Temporaries holding the results mod p resp. mod q. */
418 mbedtls_mpi TP, TQ;
419
420 /* Temporaries holding the blinded exponents for
421 * the mod p resp. mod q computation (if used). */
Janos Follathf9203b42017-03-22 15:13:15 +0000422 mbedtls_mpi DP_blind, DQ_blind;
Hanno Becker06811ce2017-05-03 15:10:34 +0100423
424 /* Pointers to actual exponents to be used - either the unblinded
425 * or the blinded ones, depending on the presence of a PRNG. */
Janos Follathf9203b42017-03-22 15:13:15 +0000426 mbedtls_mpi *DP = &ctx->DP;
427 mbedtls_mpi *DQ = &ctx->DQ;
Hanno Becker06811ce2017-05-03 15:10:34 +0100428#else
429 /* Temporary holding the blinded exponent (if used). */
430 mbedtls_mpi D_blind;
431
432 /* Pointer to actual exponent to be used - either the unblinded
433 * or the blinded one, depending on the presence of a PRNG. */
434 mbedtls_mpi *D = &ctx->D;
Hanno Becker43f94722017-08-25 11:50:00 +0100435#endif /* MBEDTLS_RSA_NO_CRT */
Hanno Becker06811ce2017-05-03 15:10:34 +0100436
437#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100438 /* Temporaries holding the initial input and the double
439 * checked result; should be the same in the end. */
440 mbedtls_mpi I, C;
Hanno Becker06811ce2017-05-03 15:10:34 +0100441#endif
442
443#if defined(MBEDTLS_RSA_FORCE_BLINDING)
444 if( f_rng == NULL )
445 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Janos Follathe81102e2017-03-22 13:38:28 +0000446#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000447
Hanno Becker43f94722017-08-25 11:50:00 +0100448 /* Sanity-check that all relevant fields are at least set,
449 * but don't perform a full keycheck. */
450 if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) == 0 ||
Hanno Becker43f94722017-08-25 11:50:00 +0100451 mbedtls_mpi_cmp_int( &ctx->D, 0 ) == 0 ||
452 mbedtls_mpi_cmp_int( &ctx->E, 0 ) == 0 )
453 {
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100454 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Hanno Becker43f94722017-08-25 11:50:00 +0100455 }
456#if !defined(MBEDTLS_RSA_NO_CRT)
Hanno Becker2c9f0272017-09-28 11:04:13 +0100457 if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 ||
458 mbedtls_mpi_cmp_int( &ctx->Q, 0 ) == 0 ||
459 mbedtls_mpi_cmp_int( &ctx->DP, 0 ) == 0 ||
Hanno Becker43f94722017-08-25 11:50:00 +0100460 mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) == 0 ||
461 mbedtls_mpi_cmp_int( &ctx->QP, 0 ) == 0 )
462 {
463 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
464 }
465#endif /* MBEDTLS_RSA_NO_CRT */
Manuel Pégourié-Gonnardfb84d382015-10-30 10:56:25 +0100466
Hanno Becker06811ce2017-05-03 15:10:34 +0100467#if defined(MBEDTLS_THREADING_C)
468 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
469 return( ret );
470#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000471
Hanno Becker06811ce2017-05-03 15:10:34 +0100472 /* MPI Initialization */
473
474 mbedtls_mpi_init( &T );
475
476 mbedtls_mpi_init( &P1 );
477 mbedtls_mpi_init( &Q1 );
478 mbedtls_mpi_init( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000479
480 if( f_rng != NULL )
481 {
Janos Follathe81102e2017-03-22 13:38:28 +0000482#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000483 mbedtls_mpi_init( &D_blind );
484#else
485 mbedtls_mpi_init( &DP_blind );
486 mbedtls_mpi_init( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000487#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000488 }
Janos Follathe81102e2017-03-22 13:38:28 +0000489
Hanno Becker06811ce2017-05-03 15:10:34 +0100490#if !defined(MBEDTLS_RSA_NO_CRT)
491 mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ );
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200492#endif
493
Hanno Becker06811ce2017-05-03 15:10:34 +0100494#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100495 mbedtls_mpi_init( &I );
496 mbedtls_mpi_init( &C );
Hanno Becker06811ce2017-05-03 15:10:34 +0100497#endif
498
499 /* End of MPI initialization */
500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
502 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000503 {
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200504 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
505 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 }
507
Hanno Becker06811ce2017-05-03 15:10:34 +0100508#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100509 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) );
Hanno Becker06811ce2017-05-03 15:10:34 +0100510#endif
511
Paul Bakkerf451bac2013-08-30 15:37:02 +0200512 if( f_rng != NULL )
513 {
514 /*
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200515 * Blinding
516 * T = T * Vi mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200517 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200518 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
519 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Janos Follathe81102e2017-03-22 13:38:28 +0000521
Janos Follathe81102e2017-03-22 13:38:28 +0000522 /*
523 * Exponent blinding
524 */
525 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
526 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
527
Janos Follathf9203b42017-03-22 15:13:15 +0000528#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000529 /*
530 * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D
531 */
532 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
533 f_rng, p_rng ) );
534 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) );
535 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) );
536 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) );
537
538 D = &D_blind;
Janos Follathf9203b42017-03-22 15:13:15 +0000539#else
540 /*
541 * DP_blind = ( P - 1 ) * R + DP
542 */
543 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
544 f_rng, p_rng ) );
545 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) );
546 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind,
547 &ctx->DP ) );
548
549 DP = &DP_blind;
550
551 /*
552 * DQ_blind = ( Q - 1 ) * R + DQ
553 */
554 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING,
555 f_rng, p_rng ) );
556 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) );
557 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind,
558 &ctx->DQ ) );
559
560 DQ = &DQ_blind;
Janos Follathe81102e2017-03-22 13:38:28 +0000561#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkerf451bac2013-08-30 15:37:02 +0200562 }
Paul Bakkeraab30c12013-08-30 11:00:25 +0200563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathe81102e2017-03-22 13:38:28 +0000565 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) );
Manuel Pégourié-Gonnarde10e06d2014-11-06 18:15:12 +0100566#else
Paul Bakkeraab30c12013-08-30 11:00:25 +0200567 /*
Janos Follathe81102e2017-03-22 13:38:28 +0000568 * Faster decryption using the CRT
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 *
Hanno Becker06811ce2017-05-03 15:10:34 +0100570 * TP = input ^ dP mod P
571 * TQ = input ^ dQ mod Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100573
574 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) );
575 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TQ, &T, DQ, &ctx->Q, &ctx->RQ ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100578 * T = (TP - TQ) * (Q^-1 mod P) mod P
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100580 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) );
581 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) );
582 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000583
584 /*
Hanno Becker06811ce2017-05-03 15:10:34 +0100585 * T = TQ + T * Q
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 */
Hanno Becker06811ce2017-05-03 15:10:34 +0100587 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) );
588 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589#endif /* MBEDTLS_RSA_NO_CRT */
Paul Bakkeraab30c12013-08-30 11:00:25 +0200590
Paul Bakkerf451bac2013-08-30 15:37:02 +0200591 if( f_rng != NULL )
592 {
593 /*
594 * Unblind
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +0200595 * T = T * Vf mod N
Paul Bakkerf451bac2013-08-30 15:37:02 +0200596 */
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200597 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Paul Bakkerf451bac2013-08-30 15:37:02 +0200599 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100601 /* If requested by the config, verify the result to prevent glitching attacks. */
Hanno Becker06811ce2017-05-03 15:10:34 +0100602#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100603 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E, &ctx->N, &ctx->RN ) );
604 if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 )
Hanno Becker06811ce2017-05-03 15:10:34 +0100605 {
Hanno Becker06811ce2017-05-03 15:10:34 +0100606 ret = MBEDTLS_ERR_RSA_VERIFY_FAILED;
607 goto cleanup;
608 }
609#endif /* MBEDTLS_RSA_REQUIRE_VERIFICATION */
610
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard4d04cdc2015-08-28 10:32:21 +0200616 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
617 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Manuel Pégourié-Gonnardae102992013-10-04 17:07:12 +0200618#endif
Manuel Pégourié-Gonnard1385a282015-08-27 11:30:58 +0200619
Hanno Becker06811ce2017-05-03 15:10:34 +0100620 mbedtls_mpi_free( &P1 );
621 mbedtls_mpi_free( &Q1 );
622 mbedtls_mpi_free( &R );
Janos Follathf9203b42017-03-22 15:13:15 +0000623
624 if( f_rng != NULL )
625 {
Janos Follathe81102e2017-03-22 13:38:28 +0000626#if defined(MBEDTLS_RSA_NO_CRT)
Janos Follathf9203b42017-03-22 15:13:15 +0000627 mbedtls_mpi_free( &D_blind );
628#else
629 mbedtls_mpi_free( &DP_blind );
630 mbedtls_mpi_free( &DQ_blind );
Janos Follathe81102e2017-03-22 13:38:28 +0000631#endif
Janos Follathf9203b42017-03-22 15:13:15 +0000632 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Hanno Becker06811ce2017-05-03 15:10:34 +0100634 mbedtls_mpi_free( &T );
635
636#if !defined(MBEDTLS_RSA_NO_CRT)
637 mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ );
638#endif
639
640#if defined(MBEDTLS_RSA_REQUIRE_VERIFICATION)
Hanno Beckerc6075cc2017-08-25 11:45:35 +0100641 mbedtls_mpi_free( &C );
642 mbedtls_mpi_free( &I );
Hanno Becker06811ce2017-05-03 15:10:34 +0100643#endif
644
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000647
648 return( 0 );
649}
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker9dcc3222011-03-08 14:16:06 +0000652/**
653 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
654 *
Paul Bakkerb125ed82011-11-10 13:33:51 +0000655 * \param dst buffer to mask
656 * \param dlen length of destination buffer
657 * \param src source of the mask generation
658 * \param slen length of the source buffer
659 * \param md_ctx message digest context to use
Paul Bakker9dcc3222011-03-08 14:16:06 +0000660 */
Paul Bakker48377d92013-08-30 12:06:24 +0200661static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 size_t slen, mbedtls_md_context_t *md_ctx )
Paul Bakker9dcc3222011-03-08 14:16:06 +0000663{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +0000665 unsigned char counter[4];
666 unsigned char *p;
Paul Bakker23986e52011-04-24 08:57:21 +0000667 unsigned int hlen;
668 size_t i, use_len;
Paul Bakker9dcc3222011-03-08 14:16:06 +0000669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000671 memset( counter, 0, 4 );
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 hlen = mbedtls_md_get_size( md_ctx->md_info );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000674
Simon Butcher02037452016-03-01 21:19:12 +0000675 /* Generate and apply dbMask */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000676 p = dst;
677
678 while( dlen > 0 )
679 {
680 use_len = hlen;
681 if( dlen < hlen )
682 use_len = dlen;
683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_md_starts( md_ctx );
685 mbedtls_md_update( md_ctx, src, slen );
686 mbedtls_md_update( md_ctx, counter, 4 );
687 mbedtls_md_finish( md_ctx, mask );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000688
689 for( i = 0; i < use_len; ++i )
690 *p++ ^= mask[i];
691
692 counter[3]++;
693
694 dlen -= use_len;
695 }
Gilles Peskine18ac7162017-05-05 19:24:06 +0200696
697 mbedtls_zeroize( mask, sizeof( mask ) );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000698}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker9dcc3222011-03-08 14:16:06 +0000700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +0100702/*
703 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100706 int (*f_rng)(void *, unsigned char *, size_t),
707 void *p_rng,
Paul Bakkera43231c2013-02-28 17:33:49 +0100708 int mode,
709 const unsigned char *label, size_t label_len,
710 size_t ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100711 const unsigned char *input,
712 unsigned char *output )
713{
714 size_t olen;
715 int ret;
716 unsigned char *p = output;
717 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 const mbedtls_md_info_t *md_info;
719 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
722 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200723
724 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +0100728 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100730
731 olen = ctx->len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +0100733
Simon Butcher02037452016-03-01 21:19:12 +0000734 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000735 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100737
738 memset( output, 0, olen );
739
740 *p++ = 0;
741
Simon Butcher02037452016-03-01 21:19:12 +0000742 /* Generate a random octet string seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100743 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100745
746 p += hlen;
747
Simon Butcher02037452016-03-01 21:19:12 +0000748 /* Construct DB */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_md( md_info, label, label_len, p );
Paul Bakkerb3869132013-02-28 17:21:01 +0100750 p += hlen;
751 p += olen - 2 * hlen - 2 - ilen;
752 *p++ = 1;
753 memcpy( p, input, ilen );
754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700756 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
757 {
758 mbedtls_md_free( &md_ctx );
759 return( ret );
760 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100761
Simon Butcher02037452016-03-01 21:19:12 +0000762 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +0100763 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
764 &md_ctx );
765
Simon Butcher02037452016-03-01 21:19:12 +0000766 /* maskedSeed: Apply seedMask to seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100767 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
768 &md_ctx );
769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +0100771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 return( ( mode == MBEDTLS_RSA_PUBLIC )
773 ? mbedtls_rsa_public( ctx, output, output )
774 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100775}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +0100779/*
780 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +0100783 int (*f_rng)(void *, unsigned char *, size_t),
784 void *p_rng,
785 int mode, size_t ilen,
786 const unsigned char *input,
787 unsigned char *output )
788{
789 size_t nb_pad, olen;
790 int ret;
791 unsigned char *p = output;
792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
794 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +0200795
Janos Follath1ed9f992016-03-18 11:45:44 +0000796 // We don't check p_rng because it won't be dereferenced here
797 if( f_rng == NULL || input == NULL || output == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100799
800 olen = ctx->len;
Manuel Pégourié-Gonnard370717b2016-02-11 10:35:13 +0100801
Simon Butcher02037452016-03-01 21:19:12 +0000802 /* first comparison checks for overflow */
Janos Follatheddfe8f2016-02-08 14:52:29 +0000803 if( ilen + 11 < ilen || olen < ilen + 11 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +0100805
806 nb_pad = olen - 3 - ilen;
807
808 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 if( mode == MBEDTLS_RSA_PUBLIC )
Paul Bakkerb3869132013-02-28 17:21:01 +0100810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 *p++ = MBEDTLS_RSA_CRYPT;
Paul Bakkerb3869132013-02-28 17:21:01 +0100812
813 while( nb_pad-- > 0 )
814 {
815 int rng_dl = 100;
816
817 do {
818 ret = f_rng( p_rng, p, 1 );
819 } while( *p == 0 && --rng_dl && ret == 0 );
820
Simon Butcher02037452016-03-01 21:19:12 +0000821 /* Check if RNG failed to generate data */
Paul Bakker66d5d072014-06-17 16:39:18 +0200822 if( rng_dl == 0 || ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +0100824
825 p++;
826 }
827 }
828 else
829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +0100831
832 while( nb_pad-- > 0 )
833 *p++ = 0xFF;
834 }
835
836 *p++ = 0;
837 memcpy( p, input, ilen );
838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 return( ( mode == MBEDTLS_RSA_PUBLIC )
840 ? mbedtls_rsa_public( ctx, output, output )
841 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Paul Bakkerb3869132013-02-28 17:21:01 +0100842}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100844
Paul Bakker5121ce52009-01-03 21:22:43 +0000845/*
846 * Add the message padding, then do an RSA operation
847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000849 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker21eb2802010-08-16 11:10:02 +0000850 void *p_rng,
Paul Bakker23986e52011-04-24 08:57:21 +0000851 int mode, size_t ilen,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000852 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000853 unsigned char *output )
854{
Paul Bakker5121ce52009-01-03 21:22:43 +0000855 switch( ctx->padding )
856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_PKCS1_V15)
858 case MBEDTLS_RSA_PKCS_V15:
859 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100860 input, output );
Paul Bakker48377d92013-08-30 12:06:24 +0200861#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#if defined(MBEDTLS_PKCS1_V21)
864 case MBEDTLS_RSA_PKCS_V21:
865 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakkerb3869132013-02-28 17:21:01 +0100866 ilen, input, output );
Paul Bakker9dcc3222011-03-08 14:16:06 +0000867#endif
Paul Bakker5121ce52009-01-03 21:22:43 +0000868
869 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +0000871 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000872}
873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +0000875/*
Paul Bakkerb3869132013-02-28 17:21:01 +0100876 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Paul Bakker5121ce52009-01-03 21:22:43 +0000877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +0200879 int (*f_rng)(void *, unsigned char *, size_t),
880 void *p_rng,
881 int mode,
Paul Bakkera43231c2013-02-28 17:33:49 +0100882 const unsigned char *label, size_t label_len,
883 size_t *olen,
Paul Bakkerb3869132013-02-28 17:21:01 +0100884 const unsigned char *input,
885 unsigned char *output,
886 size_t output_max_len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000887{
Paul Bakker23986e52011-04-24 08:57:21 +0000888 int ret;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100889 size_t ilen, i, pad_len;
890 unsigned char *p, bad, pad_done;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
892 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Paul Bakker23986e52011-04-24 08:57:21 +0000893 unsigned int hlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 const mbedtls_md_info_t *md_info;
895 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +0100896
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100897 /*
898 * Parameters sanity checks
899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
901 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000902
903 ilen = ctx->len;
904
Paul Bakker27fdf462011-06-09 13:55:13 +0000905 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +0000907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100909 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100911
Janos Follathc17cda12016-02-11 11:08:18 +0000912 hlen = mbedtls_md_get_size( md_info );
913
914 // checking for integer underflow
915 if( 2 * hlen + 2 > ilen )
916 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
917
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100918 /*
919 * RSA operation
920 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 ret = ( mode == MBEDTLS_RSA_PUBLIC )
922 ? mbedtls_rsa_public( ctx, input, buf )
923 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000924
925 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100926 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +0000927
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100928 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100929 * Unmask data and generate lHash
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100930 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700932 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
933 {
934 mbedtls_md_free( &md_ctx );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100935 goto cleanup;
Brian J Murraye7be5bd2016-06-23 12:57:03 -0700936 }
937
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100938
939 /* Generate lHash */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 mbedtls_md( md_info, label, label_len, lhash );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100941
942 /* seed: Apply seedMask to maskedSeed */
943 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
944 &md_ctx );
945
946 /* DB: Apply dbMask to maskedDB */
947 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
948 &md_ctx );
949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100951
952 /*
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100953 * Check contents, in "constant-time"
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100954 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000955 p = buf;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100956 bad = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000957
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100958 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +0100959
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100960 p += hlen; /* Skip seed */
Paul Bakkerb3869132013-02-28 17:21:01 +0100961
Manuel Pégourié-Gonnarda5cfc352013-11-28 15:57:52 +0100962 /* Check lHash */
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100963 for( i = 0; i < hlen; i++ )
964 bad |= lhash[i] ^ *p++;
Paul Bakkerb3869132013-02-28 17:21:01 +0100965
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100966 /* Get zero-padding len, but always read till end of buffer
967 * (minus one, for the 01 byte) */
968 pad_len = 0;
969 pad_done = 0;
970 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
971 {
972 pad_done |= p[i];
Pascal Junodb99183d2015-03-11 16:49:45 +0100973 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100974 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100975
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100976 p += pad_len;
977 bad |= *p++ ^ 0x01;
Paul Bakkerb3869132013-02-28 17:21:01 +0100978
Manuel Pégourié-Gonnardab44d7e2013-11-29 12:49:44 +0100979 /*
980 * The only information "leaked" is whether the padding was correct or not
981 * (eg, no data is copied if it was not correct). This meets the
982 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
983 * the different error conditions.
984 */
985 if( bad != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100986 {
987 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
988 goto cleanup;
989 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100990
Paul Bakker66d5d072014-06-17 16:39:18 +0200991 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100992 {
993 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
994 goto cleanup;
995 }
Paul Bakkerb3869132013-02-28 17:21:01 +0100996
997 *olen = ilen - (p - buf);
998 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +0100999 ret = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001000
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001001cleanup:
1002 mbedtls_zeroize( buf, sizeof( buf ) );
1003 mbedtls_zeroize( lhash, sizeof( lhash ) );
1004
1005 return( ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001006}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001010/*
1011 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
1012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001014 int (*f_rng)(void *, unsigned char *, size_t),
1015 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001016 int mode, size_t *olen,
1017 const unsigned char *input,
1018 unsigned char *output,
1019 size_t output_max_len)
1020{
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001021 int ret;
1022 size_t ilen, pad_count = 0, i;
1023 unsigned char *p, bad, pad_done = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1027 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001028
1029 ilen = ctx->len;
1030
1031 if( ilen < 16 || ilen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1035 ? mbedtls_rsa_public( ctx, input, buf )
1036 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001037
1038 if( ret != 0 )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001039 goto cleanup;
Paul Bakkerb3869132013-02-28 17:21:01 +01001040
1041 p = buf;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001042 bad = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001043
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001044 /*
1045 * Check and get padding len in "constant-time"
1046 */
1047 bad |= *p++; /* First byte must be 0 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001048
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001049 /* This test does not depend on secret data */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 if( mode == MBEDTLS_RSA_PRIVATE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Paul Bakker5121ce52009-01-03 21:22:43 +00001053
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001054 /* Get padding len, but always read till end of buffer
1055 * (minus one, for the 00 byte) */
1056 for( i = 0; i < ilen - 3; i++ )
1057 {
Pascal Junodb99183d2015-03-11 16:49:45 +01001058 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
1059 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001060 }
Paul Bakkere6ee41f2012-05-19 08:43:48 +00001061
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001062 p += pad_count;
1063 bad |= *p++; /* Must be zero */
Paul Bakkerb3869132013-02-28 17:21:01 +01001064 }
1065 else
1066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001068
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001069 /* Get padding len, but always read till end of buffer
1070 * (minus one, for the 00 byte) */
1071 for( i = 0; i < ilen - 3; i++ )
1072 {
Manuel Pégourié-Gonnardfbf09152014-02-03 11:58:55 +01001073 pad_done |= ( p[i] != 0xFF );
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001074 pad_count += ( pad_done == 0 );
1075 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001076
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001077 p += pad_count;
1078 bad |= *p++; /* Must be zero */
Paul Bakker5121ce52009-01-03 21:22:43 +00001079 }
1080
Janos Follathc69fa502016-02-12 13:30:09 +00001081 bad |= ( pad_count < 8 );
Janos Follathb6eb1ca2016-02-08 13:59:25 +00001082
Manuel Pégourié-Gonnard27290da2013-11-30 13:36:53 +01001083 if( bad )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001084 {
1085 ret = MBEDTLS_ERR_RSA_INVALID_PADDING;
1086 goto cleanup;
1087 }
Paul Bakker8804f692013-02-28 18:06:26 +01001088
Paul Bakker66d5d072014-06-17 16:39:18 +02001089 if( ilen - ( p - buf ) > output_max_len )
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001090 {
1091 ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
1092 goto cleanup;
1093 }
Paul Bakker060c5682009-01-12 21:48:39 +00001094
Paul Bakker27fdf462011-06-09 13:55:13 +00001095 *olen = ilen - (p - buf);
Paul Bakker5121ce52009-01-03 21:22:43 +00001096 memcpy( output, p, *olen );
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001097 ret = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
Gilles Peskine4a7f6a02017-03-23 14:37:37 +01001099cleanup:
1100 mbedtls_zeroize( buf, sizeof( buf ) );
1101
1102 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001105
1106/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001107 * Do an RSA operation, then remove the message padding
1108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001110 int (*f_rng)(void *, unsigned char *, size_t),
1111 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001112 int mode, size_t *olen,
1113 const unsigned char *input,
1114 unsigned char *output,
1115 size_t output_max_len)
1116{
1117 switch( ctx->padding )
1118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119#if defined(MBEDTLS_PKCS1_V15)
1120 case MBEDTLS_RSA_PKCS_V15:
1121 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Paul Bakker548957d2013-08-30 10:30:02 +02001122 input, output, output_max_len );
Paul Bakker48377d92013-08-30 12:06:24 +02001123#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125#if defined(MBEDTLS_PKCS1_V21)
1126 case MBEDTLS_RSA_PKCS_V21:
1127 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Paul Bakker548957d2013-08-30 10:30:02 +02001128 olen, input, output,
1129 output_max_len );
Paul Bakkerb3869132013-02-28 17:21:01 +01001130#endif
1131
1132 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001134 }
1135}
1136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137#if defined(MBEDTLS_PKCS1_V21)
Paul Bakkerb3869132013-02-28 17:21:01 +01001138/*
1139 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
1140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Paul Bakkerb3869132013-02-28 17:21:01 +01001142 int (*f_rng)(void *, unsigned char *, size_t),
1143 void *p_rng,
1144 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001146 unsigned int hashlen,
1147 const unsigned char *hash,
1148 unsigned char *sig )
1149{
1150 size_t olen;
1151 unsigned char *p = sig;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001153 unsigned int slen, hlen, offset = 0;
1154 int ret;
1155 size_t msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 const mbedtls_md_info_t *md_info;
1157 mbedtls_md_context_t md_ctx;
Paul Bakkerb3869132013-02-28 17:21:01 +01001158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1160 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6d1d822014-06-02 16:47:02 +02001161
1162 if( f_rng == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001164
1165 olen = ctx->len;
1166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001168 {
Simon Butcher02037452016-03-01 21:19:12 +00001169 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001171 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001175 }
1176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001178 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 hlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001182 slen = hlen;
1183
1184 if( olen < hlen + slen + 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001186
1187 memset( sig, 0, olen );
1188
Simon Butcher02037452016-03-01 21:19:12 +00001189 /* Generate salt of length slen */
Paul Bakkerb3869132013-02-28 17:21:01 +01001190 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Paul Bakkerb3869132013-02-28 17:21:01 +01001192
Simon Butcher02037452016-03-01 21:19:12 +00001193 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001194 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001195 p += olen - hlen * 2 - 2;
1196 *p++ = 0x01;
1197 memcpy( p, salt, slen );
1198 p += slen;
1199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001201 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1202 {
1203 mbedtls_md_free( &md_ctx );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001204 /* No need to zeroize salt: we didn't use it. */
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001205 return( ret );
1206 }
Paul Bakkerb3869132013-02-28 17:21:01 +01001207
Simon Butcher02037452016-03-01 21:19:12 +00001208 /* Generate H = Hash( M' ) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_md_starts( &md_ctx );
1210 mbedtls_md_update( &md_ctx, p, 8 );
1211 mbedtls_md_update( &md_ctx, hash, hashlen );
1212 mbedtls_md_update( &md_ctx, salt, slen );
1213 mbedtls_md_finish( &md_ctx, p );
Gilles Peskine18ac7162017-05-05 19:24:06 +02001214 mbedtls_zeroize( salt, sizeof( salt ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001215
Simon Butcher02037452016-03-01 21:19:12 +00001216 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001217 if( msb % 8 == 0 )
1218 offset = 1;
1219
Simon Butcher02037452016-03-01 21:19:12 +00001220 /* maskedDB: Apply dbMask to DB */
Paul Bakkerb3869132013-02-28 17:21:01 +01001221 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
1222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 mbedtls_md_free( &md_ctx );
Paul Bakkerb3869132013-02-28 17:21:01 +01001224
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001225 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakkerb3869132013-02-28 17:21:01 +01001226 sig[0] &= 0xFF >> ( olen * 8 - msb );
1227
1228 p += hlen;
1229 *p++ = 0xBC;
1230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 return( ( mode == MBEDTLS_RSA_PUBLIC )
1232 ? mbedtls_rsa_public( ctx, sig, sig )
1233 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001238/*
1239 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
1240 */
1241/*
1242 * Do an RSA operation to sign the message digest
1243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001245 int (*f_rng)(void *, unsigned char *, size_t),
1246 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001247 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001249 unsigned int hashlen,
1250 const unsigned char *hash,
1251 unsigned char *sig )
1252{
Paul Bakkerc70b9822013-04-07 22:00:46 +02001253 size_t nb_pad, olen, oid_size = 0;
Paul Bakkerb3869132013-02-28 17:21:01 +01001254 unsigned char *p = sig;
Paul Bakker21e081b2014-07-24 10:38:01 +02001255 const char *oid = NULL;
Paul Bakkerb3869132013-02-28 17:21:01 +01001256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1258 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001259
1260 olen = ctx->len;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001261 nb_pad = olen - 3;
Paul Bakkerb3869132013-02-28 17:21:01 +01001262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001266 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
1270 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001271
Paul Bakkerc70b9822013-04-07 22:00:46 +02001272 nb_pad -= 10 + oid_size;
1273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001275 }
1276
Paul Bakkerc70b9822013-04-07 22:00:46 +02001277 nb_pad -= hashlen;
1278
Paul Bakkerb3869132013-02-28 17:21:01 +01001279 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001281
1282 *p++ = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 *p++ = MBEDTLS_RSA_SIGN;
Paul Bakkerb3869132013-02-28 17:21:01 +01001284 memset( p, 0xFF, nb_pad );
1285 p += nb_pad;
1286 *p++ = 0;
1287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 if( md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001289 {
Paul Bakkerc70b9822013-04-07 22:00:46 +02001290 memcpy( p, hash, hashlen );
1291 }
1292 else
1293 {
1294 /*
1295 * DigestInfo ::= SEQUENCE {
1296 * digestAlgorithm DigestAlgorithmIdentifier,
1297 * digest Digest }
1298 *
1299 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1300 *
1301 * Digest ::= OCTET STRING
1302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001304 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001306 *p++ = (unsigned char) ( 0x04 + oid_size );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 *p++ = MBEDTLS_ASN1_OID;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001308 *p++ = oid_size & 0xFF;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001309 memcpy( p, oid, oid_size );
1310 p += oid_size;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 *p++ = MBEDTLS_ASN1_NULL;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001312 *p++ = 0x00;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001314 *p++ = hashlen;
1315 memcpy( p, hash, hashlen );
Paul Bakkerb3869132013-02-28 17:21:01 +01001316 }
1317
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001318 if( mode == MBEDTLS_RSA_PUBLIC )
Hanno Becker43f94722017-08-25 11:50:00 +01001319 return( mbedtls_rsa_public( ctx, sig, sig ) );
Manuel Pégourié-Gonnard5f501042015-09-03 20:03:15 +02001320
Hanno Beckercc209ca2017-08-25 11:51:03 +01001321 return( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Paul Bakkerb3869132013-02-28 17:21:01 +01001322}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakkerb3869132013-02-28 17:21:01 +01001324
1325/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 * Do an RSA operation to sign the message digest
1327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Paul Bakkera3d195c2011-11-27 21:07:34 +00001329 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker9dcc3222011-03-08 14:16:06 +00001330 void *p_rng,
Paul Bakker5121ce52009-01-03 21:22:43 +00001331 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 mbedtls_md_type_t md_alg,
Paul Bakker23986e52011-04-24 08:57:21 +00001333 unsigned int hashlen,
Paul Bakkerff60ee62010-03-16 21:09:09 +00001334 const unsigned char *hash,
Paul Bakker5121ce52009-01-03 21:22:43 +00001335 unsigned char *sig )
1336{
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 switch( ctx->padding )
1338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#if defined(MBEDTLS_PKCS1_V15)
1340 case MBEDTLS_RSA_PKCS_V15:
1341 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001342 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001343#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#if defined(MBEDTLS_PKCS1_V21)
1346 case MBEDTLS_RSA_PKCS_V21:
1347 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001348 hashlen, hash, sig );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001349#endif
1350
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakker5121ce52009-01-03 21:22:43 +00001353 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001354}
1355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#if defined(MBEDTLS_PKCS1_V21)
Paul Bakker5121ce52009-01-03 21:22:43 +00001357/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001358 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001361 int (*f_rng)(void *, unsigned char *, size_t),
1362 void *p_rng,
1363 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001365 unsigned int hashlen,
1366 const unsigned char *hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 mbedtls_md_type_t mgf1_hash_id,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001368 int expected_salt_len,
1369 const unsigned char *sig )
Paul Bakker5121ce52009-01-03 21:22:43 +00001370{
Paul Bakker23986e52011-04-24 08:57:21 +00001371 int ret;
Paul Bakkerb3869132013-02-28 17:21:01 +01001372 size_t siglen;
1373 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Paul Bakker9dcc3222011-03-08 14:16:06 +00001375 unsigned char zeros[8];
Paul Bakker23986e52011-04-24 08:57:21 +00001376 unsigned int hlen;
1377 size_t slen, msb;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 const mbedtls_md_info_t *md_info;
1379 mbedtls_md_context_t md_ctx;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001380 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
1383 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001384
Paul Bakker5121ce52009-01-03 21:22:43 +00001385 siglen = ctx->len;
1386
Paul Bakker27fdf462011-06-09 13:55:13 +00001387 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1391 ? mbedtls_rsa_public( ctx, sig, buf )
1392 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393
1394 if( ret != 0 )
1395 return( ret );
1396
1397 p = buf;
1398
Paul Bakkerb3869132013-02-28 17:21:01 +01001399 if( buf[siglen - 1] != 0xBC )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 if( md_alg != MBEDTLS_MD_NONE )
Paul Bakker5121ce52009-01-03 21:22:43 +00001403 {
Simon Butcher02037452016-03-01 21:19:12 +00001404 /* Gather length of hash to sign */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001406 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerb3869132013-02-28 17:21:01 +01001410 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Paul Bakkerb3869132013-02-28 17:21:01 +01001413 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 hlen = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001417 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Paul Bakker9dcc3222011-03-08 14:16:06 +00001418
Paul Bakkerb3869132013-02-28 17:21:01 +01001419 memset( zeros, 0, 8 );
Paul Bakker53019ae2011-03-25 13:58:48 +00001420
Simon Butcher02037452016-03-01 21:19:12 +00001421 /*
1422 * Note: EMSA-PSS verification is over the length of N - 1 bits
1423 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +02001424 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001425
Simon Butcher02037452016-03-01 21:19:12 +00001426 /* Compensate for boundary condition when applying mask */
Paul Bakkerb3869132013-02-28 17:21:01 +01001427 if( msb % 8 == 0 )
1428 {
1429 p++;
1430 siglen -= 1;
1431 }
1432 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 mbedtls_md_init( &md_ctx );
Brian J Murraye7be5bd2016-06-23 12:57:03 -07001436 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
1437 {
1438 mbedtls_md_free( &md_ctx );
1439 return( ret );
1440 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001441
Paul Bakkerb3869132013-02-28 17:21:01 +01001442 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Paul Bakker02303e82013-01-03 11:08:31 +01001443
Paul Bakkerb3869132013-02-28 17:21:01 +01001444 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001445
Paul Bakker4de44aa2013-12-31 11:43:01 +01001446 while( p < buf + siglen && *p == 0 )
Paul Bakkerb3869132013-02-28 17:21:01 +01001447 p++;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001448
Paul Bakkerb3869132013-02-28 17:21:01 +01001449 if( p == buf + siglen ||
1450 *p++ != 0x01 )
1451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 mbedtls_md_free( &md_ctx );
1453 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001454 }
Paul Bakker9dcc3222011-03-08 14:16:06 +00001455
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001456 /* Actual salt len */
Paul Bakkerb3869132013-02-28 17:21:01 +01001457 slen -= p - buf;
Paul Bakker9dcc3222011-03-08 14:16:06 +00001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001460 slen != (size_t) expected_salt_len )
1461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 mbedtls_md_free( &md_ctx );
1463 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001464 }
1465
Simon Butcher02037452016-03-01 21:19:12 +00001466 /*
1467 * Generate H = Hash( M' )
1468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 mbedtls_md_starts( &md_ctx );
1470 mbedtls_md_update( &md_ctx, zeros, 8 );
1471 mbedtls_md_update( &md_ctx, hash, hashlen );
1472 mbedtls_md_update( &md_ctx, p, slen );
1473 mbedtls_md_finish( &md_ctx, result );
Paul Bakker53019ae2011-03-25 13:58:48 +00001474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 mbedtls_md_free( &md_ctx );
Paul Bakker9dcc3222011-03-08 14:16:06 +00001476
Paul Bakkerb3869132013-02-28 17:21:01 +01001477 if( memcmp( p + slen, result, hlen ) == 0 )
1478 return( 0 );
1479 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerb3869132013-02-28 17:21:01 +01001481}
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001482
1483/*
1484 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
1485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001487 int (*f_rng)(void *, unsigned char *, size_t),
1488 void *p_rng,
1489 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490 mbedtls_md_type_t md_alg,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001491 unsigned int hashlen,
1492 const unsigned char *hash,
1493 const unsigned char *sig )
1494{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
1496 ? (mbedtls_md_type_t) ctx->hash_id
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001497 : md_alg;
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001500 md_alg, hashlen, hash,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Manuel Pégourié-Gonnard5ec628a2014-06-03 11:44:06 +02001502 sig ) );
1503
1504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505#endif /* MBEDTLS_PKCS1_V21 */
Paul Bakker40628ba2013-01-03 10:50:31 +01001506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkerb3869132013-02-28 17:21:01 +01001508/*
1509 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
1510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001511int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001512 int (*f_rng)(void *, unsigned char *, size_t),
1513 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001514 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001515 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001516 unsigned int hashlen,
1517 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001518 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001519{
1520 int ret;
Paul Bakkerc70b9822013-04-07 22:00:46 +02001521 size_t len, siglen, asn1_len;
1522 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523 mbedtls_md_type_t msg_md_alg;
1524 const mbedtls_md_info_t *md_info;
1525 mbedtls_asn1_buf oid;
Nicholas Wilson409401c2016-04-13 11:48:25 +01001526 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Paul Bakkerb3869132013-02-28 17:21:01 +01001527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
1529 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001530
1531 siglen = ctx->len;
1532
1533 if( siglen < 16 || siglen > sizeof( buf ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Paul Bakkerb3869132013-02-28 17:21:01 +01001535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 ret = ( mode == MBEDTLS_RSA_PUBLIC )
1537 ? mbedtls_rsa_public( ctx, sig, buf )
1538 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Paul Bakkerb3869132013-02-28 17:21:01 +01001539
1540 if( ret != 0 )
1541 return( ret );
1542
1543 p = buf;
1544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001545 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
1546 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001547
1548 while( *p != 0 )
1549 {
1550 if( p >= buf + siglen - 1 || *p != 0xFF )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001552 p++;
1553 }
1554 p++;
1555
1556 len = siglen - ( p - buf );
1557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Paul Bakkerb3869132013-02-28 17:21:01 +01001559 {
1560 if( memcmp( p, hash, hashlen ) == 0 )
1561 return( 0 );
1562 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakker5121ce52009-01-03 21:22:43 +00001564 }
1565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 md_info = mbedtls_md_info_from_type( md_alg );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001567 if( md_info == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
1569 hashlen = mbedtls_md_get_size( md_info );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001570
1571 end = p + len;
1572
Simon Butcher02037452016-03-01 21:19:12 +00001573 /*
1574 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
1575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1577 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1578 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001579
1580 if( asn1_len + 2 != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
1584 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1585 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001586
1587 if( asn1_len + 6 + hashlen != len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
1591 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001592
1593 oid.p = p;
1594 p += oid.len;
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
1597 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001598
1599 if( md_alg != msg_md_alg )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001601
1602 /*
1603 * assume the algorithm parameters must be NULL
1604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
1606 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1609 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001610
1611 if( asn1_len != hashlen )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001613
1614 if( memcmp( p, hash, hashlen ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001615 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001616
1617 p += hashlen;
1618
1619 if( p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Paul Bakkerc70b9822013-04-07 22:00:46 +02001621
1622 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001623}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001625
1626/*
Paul Bakkerb3869132013-02-28 17:21:01 +01001627 * Do an RSA operation and check the message digest
1628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Paul Bakker548957d2013-08-30 10:30:02 +02001630 int (*f_rng)(void *, unsigned char *, size_t),
1631 void *p_rng,
Paul Bakkerb3869132013-02-28 17:21:01 +01001632 int mode,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 mbedtls_md_type_t md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001634 unsigned int hashlen,
1635 const unsigned char *hash,
Manuel Pégourié-Gonnardcc0a9d02013-08-12 11:34:35 +02001636 const unsigned char *sig )
Paul Bakkerb3869132013-02-28 17:21:01 +01001637{
1638 switch( ctx->padding )
1639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#if defined(MBEDTLS_PKCS1_V15)
1641 case MBEDTLS_RSA_PKCS_V15:
1642 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001643 hashlen, hash, sig );
Paul Bakker48377d92013-08-30 12:06:24 +02001644#endif
Paul Bakkerb3869132013-02-28 17:21:01 +01001645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001646#if defined(MBEDTLS_PKCS1_V21)
1647 case MBEDTLS_RSA_PKCS_V21:
1648 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Paul Bakkerb3869132013-02-28 17:21:01 +01001649 hashlen, hash, sig );
1650#endif
1651
1652 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Paul Bakkerb3869132013-02-28 17:21:01 +01001654 }
1655}
1656
1657/*
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001658 * Copy the components of an RSA key
1659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001661{
1662 int ret;
1663
1664 dst->ver = src->ver;
1665 dst->len = src->len;
1666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
1668 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
1671 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
1672 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
1673 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
1674 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
1675 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
1678 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
1679 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
1682 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Manuel Pégourié-Gonnardea53a552013-09-10 13:29:30 +02001683
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001684 dst->padding = src->padding;
Manuel Pégourié-Gonnardfdddac92014-03-25 15:58:35 +01001685 dst->hash_id = src->hash_id;
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001686
1687cleanup:
1688 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 mbedtls_rsa_free( dst );
Manuel Pégourié-Gonnard3053f5b2013-08-14 13:39:57 +02001690
1691 return( ret );
1692}
1693
1694/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001695 * Free the components of an RSA key
1696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Paul Bakker5121ce52009-01-03 21:22:43 +00001698{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001699 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
1700 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
1701 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
1702 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
1703 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#if defined(MBEDTLS_THREADING_C)
1706 mbedtls_mutex_free( &ctx->mutex );
Paul Bakkerc9965dc2013-09-29 14:58:17 +02001707#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001708}
1709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +00001711
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00001712#include "mbedtls/sha1.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00001713
1714/*
1715 * Example RSA-1024 keypair, for test purposes
1716 */
1717#define KEY_LEN 128
1718
1719#define RSA_N "9292758453063D803DD603D5E777D788" \
1720 "8ED1D5BF35786190FA2F23EBC0848AEA" \
1721 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
1722 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
1723 "93A89813FBF3C4F8066D2D800F7C38A8" \
1724 "1AE31942917403FF4946B0A83D3D3E05" \
1725 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
1726 "5E94BB77B07507233A0BC7BAC8F90F79"
1727
1728#define RSA_E "10001"
1729
1730#define RSA_D "24BF6185468786FDD303083D25E64EFC" \
1731 "66CA472BC44D253102F8B4A9D3BFA750" \
1732 "91386C0077937FE33FA3252D28855837" \
1733 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
1734 "DF79C5CE07EE72C7F123142198164234" \
1735 "CABB724CF78B8173B9F880FC86322407" \
1736 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
1737 "071513A1E85B5DFA031F21ECAE91A34D"
1738
1739#define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
1740 "2C01CAD19EA484A87EA4377637E75500" \
1741 "FCB2005C5C7DD6EC4AC023CDA285D796" \
1742 "C3D9E75E1EFC42488BB4F1D13AC30A57"
1743
1744#define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
1745 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
1746 "910E4168387E3C30AA1E00C339A79508" \
1747 "8452DD96A9A5EA5D9DCA68DA636032AF"
1748
1749#define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
1750 "3C94D22288ACD763FD8E5600ED4A702D" \
1751 "F84198A5F06C2E72236AE490C93F07F8" \
1752 "3CC559CD27BC2D1CA488811730BB5725"
1753
1754#define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
1755 "D8AAEA56749EA28623272E4F7D0592AF" \
1756 "7C1F1313CAC9471B5C523BFE592F517B" \
1757 "407A1BD76C164B93DA2D32A383E58357"
1758
1759#define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
1760 "F38D18D2B2F0E2DD275AA977E2BF4411" \
1761 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
1762 "A74206CEC169D74BF5A8C50D6F48EA08"
1763
1764#define PT_LEN 24
1765#define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
1766 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
1767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768#if defined(MBEDTLS_PKCS1_V15)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001769static int myrand( void *rng_state, unsigned char *output, size_t len )
Paul Bakker545570e2010-07-18 09:00:25 +00001770{
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001771#if !defined(__OpenBSD__)
Paul Bakkera3d195c2011-11-27 21:07:34 +00001772 size_t i;
1773
Paul Bakker545570e2010-07-18 09:00:25 +00001774 if( rng_state != NULL )
1775 rng_state = NULL;
1776
Paul Bakkera3d195c2011-11-27 21:07:34 +00001777 for( i = 0; i < len; ++i )
1778 output[i] = rand();
Paul Bakkerf96f7b62014-04-30 16:02:38 +02001779#else
1780 if( rng_state != NULL )
1781 rng_state = NULL;
1782
1783 arc4random_buf( output, len );
1784#endif /* !OpenBSD */
Paul Bakker48377d92013-08-30 12:06:24 +02001785
Paul Bakkera3d195c2011-11-27 21:07:34 +00001786 return( 0 );
Paul Bakker545570e2010-07-18 09:00:25 +00001787}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker545570e2010-07-18 09:00:25 +00001789
Paul Bakker5121ce52009-01-03 21:22:43 +00001790/*
1791 * Checkup routine
1792 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793int mbedtls_rsa_self_test( int verbose )
Paul Bakker5121ce52009-01-03 21:22:43 +00001794{
Paul Bakker3d8fb632014-04-17 12:42:41 +02001795 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#if defined(MBEDTLS_PKCS1_V15)
Paul Bakker23986e52011-04-24 08:57:21 +00001797 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 mbedtls_rsa_context rsa;
Paul Bakker5121ce52009-01-03 21:22:43 +00001799 unsigned char rsa_plaintext[PT_LEN];
1800 unsigned char rsa_decrypted[PT_LEN];
1801 unsigned char rsa_ciphertext[KEY_LEN];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802#if defined(MBEDTLS_SHA1_C)
Paul Bakker5690efc2011-05-26 13:16:06 +00001803 unsigned char sha1sum[20];
1804#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00001805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001807
1808 rsa.len = KEY_LEN;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
1810 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
1811 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
1812 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
1813 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
1814 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
1815 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
1816 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001817
1818 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 mbedtls_printf( " RSA key validation: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
1822 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001823 {
1824 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001826
Hanno Becker5bc87292017-05-03 15:09:31 +01001827 ret = 1;
1828 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001829 }
1830
1831 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
1834 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
1835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Paul Bakker5121ce52009-01-03 21:22:43 +00001837 rsa_plaintext, rsa_ciphertext ) != 0 )
1838 {
1839 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001841
Hanno Becker5bc87292017-05-03 15:09:31 +01001842 ret = 1;
1843 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001844 }
1845
1846 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Paul Bakker060c5682009-01-12 21:48:39 +00001850 rsa_ciphertext, rsa_decrypted,
Paul Bakker23986e52011-04-24 08:57:21 +00001851 sizeof(rsa_decrypted) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001852 {
1853 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Hanno Becker5bc87292017-05-03 15:09:31 +01001856 ret = 1;
1857 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001858 }
1859
1860 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
1861 {
1862 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001864
Hanno Becker5bc87292017-05-03 15:09:31 +01001865 ret = 1;
1866 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001867 }
1868
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001869 if( verbose != 0 )
1870 mbedtls_printf( "passed\n" );
1871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#if defined(MBEDTLS_SHA1_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00001873 if( verbose != 0 )
Brian Murray930a3702016-05-18 14:38:02 -07001874 mbedtls_printf( " PKCS#1 data sign : " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00001877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001879 sha1sum, rsa_ciphertext ) != 0 )
1880 {
1881 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 mbedtls_printf( "failed\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +00001883
Hanno Becker5bc87292017-05-03 15:09:31 +01001884 ret = 1;
1885 goto cleanup;
Paul Bakker5121ce52009-01-03 21:22:43 +00001886 }
1887
1888 if( verbose != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Paul Bakker5121ce52009-01-03 21:22:43 +00001890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Paul Bakker5121ce52009-01-03 21:22:43 +00001892 sha1sum, rsa_ciphertext ) != 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
1901 if( verbose != 0 )
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001902 mbedtls_printf( "passed\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001903#endif /* MBEDTLS_SHA1_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001904
Manuel Pégourié-Gonnardd1004f02015-08-07 10:46:54 +02001905 if( verbose != 0 )
1906 mbedtls_printf( "\n" );
1907
Paul Bakker3d8fb632014-04-17 12:42:41 +02001908cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909 mbedtls_rsa_free( &rsa );
1910#else /* MBEDTLS_PKCS1_V15 */
Paul Bakker3e41fe82013-09-15 17:42:50 +02001911 ((void) verbose);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912#endif /* MBEDTLS_PKCS1_V15 */
Paul Bakker3d8fb632014-04-17 12:42:41 +02001913 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001914}
1915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001916#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +00001917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918#endif /* MBEDTLS_RSA_C */