Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * The RSA public-key cryptosystem |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | /* |
Simon Butcher | bdae02c | 2016-01-20 00:44:42 +0000 | [diff] [blame] | 22 | * The following sources were referenced in the design of this implementation |
| 23 | * of the RSA algorithm: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 24 | * |
Simon Butcher | bdae02c | 2016-01-20 00:44:42 +0000 | [diff] [blame] | 25 | * [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 Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 32 | * [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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 | */ |
| 38 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 41 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 43 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #if defined(MBEDTLS_RSA_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 46 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 47 | #include "mbedtls/rsa.h" |
| 48 | #include "mbedtls/oid.h" |
Paul Bakker | bb51f0c | 2012-08-23 07:46:58 +0000 | [diff] [blame] | 49 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 50 | #include <string.h> |
| 51 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #if defined(MBEDTLS_PKCS1_V21) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 53 | #include "mbedtls/md.h" |
Paul Bakker | bb51f0c | 2012-08-23 07:46:58 +0000 | [diff] [blame] | 54 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | #include <stdlib.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 58 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 61 | #include "mbedtls/platform.h" |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 62 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 63 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 65 | #define mbedtls_calloc calloc |
| 66 | #define mbedtls_free free |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 67 | #endif |
| 68 | |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 69 | /* Implementation that should never be optimized out by the compiler */ |
| 70 | static void mbedtls_zeroize( void *v, size_t n ) { |
| 71 | volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; |
| 72 | } |
| 73 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | /* |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 75 | * Context-independent RSA helper functions. |
| 76 | * |
| 77 | * The following three functions |
| 78 | * - mbedtls_rsa_deduce_moduli |
| 79 | * - mbedtls_rsa_deduce_private |
| 80 | * - mbedtls_rsa_check_params |
| 81 | * are helper functions operating on the core RSA parameters |
| 82 | * (represented as MPI's). They do not use the RSA context structure |
| 83 | * and therefore need not be replaced when providing an alternative |
| 84 | * RSA implementation. |
| 85 | * |
| 86 | * Their purpose is to provide common MPI operations in the context |
| 87 | * of RSA that can be easily shared across multiple implementations. |
| 88 | */ |
| 89 | |
| 90 | /* |
| 91 | * mbedtls_rsa_deduce_moduli |
| 92 | * |
| 93 | * Given the modulus N=PQ and a pair of public and private |
| 94 | * exponents E and D, respectively, factor N. |
| 95 | * |
| 96 | * Setting F := lcm(P-1,Q-1), the idea is as follows: |
| 97 | * |
| 98 | * (a) For any 1 <= X < N with gcd(X,N)=1, we have X^F = 1 modulo N, so X^(F/2) |
| 99 | * is a square root of 1 in Z/NZ. Since Z/NZ ~= Z/PZ x Z/QZ by CRT and the |
| 100 | * square roots of 1 in Z/PZ and Z/QZ are +1 and -1, this leaves the four |
| 101 | * possibilities X^(F/2) = (+-1, +-1). If it happens that X^(F/2) = (-1,+1) |
| 102 | * or (+1,-1), then gcd(X^(F/2) + 1, N) will be equal to one of the prime |
| 103 | * factors of N. |
| 104 | * |
| 105 | * (b) If we don't know F/2 but (F/2) * K for some odd (!) K, then the same |
| 106 | * construction still applies since (-)^K is the identity on the set of |
| 107 | * roots of 1 in Z/NZ. |
| 108 | * |
| 109 | * The public and private key primitives (-)^E and (-)^D are mutually inverse |
| 110 | * bijections on Z/NZ if and only if (-)^(DE) is the identity on Z/NZ, i.e. |
| 111 | * if and only if DE - 1 is a multiple of F, say DE - 1 = F * L. |
| 112 | * Splitting L = 2^t * K with K odd, we have |
| 113 | * |
| 114 | * DE - 1 = FL = (F/2) * (2^(t+1)) * K, |
| 115 | * |
| 116 | * so (F / 2) * K is among the numbers |
| 117 | * |
| 118 | * (DE - 1) >> 1, (DE - 1) >> 2, ..., (DE - 1) >> ord |
| 119 | * |
| 120 | * where ord is the order of 2 in (DE - 1). |
| 121 | * We can therefore iterate through these numbers apply the construction |
| 122 | * of (a) and (b) above to attempt to factor N. |
| 123 | * |
| 124 | */ |
| 125 | int mbedtls_rsa_deduce_moduli( mbedtls_mpi *N, mbedtls_mpi *D, mbedtls_mpi *E, |
| 126 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
| 127 | mbedtls_mpi *P, mbedtls_mpi *Q ) |
| 128 | { |
| 129 | /* Implementation note: |
| 130 | * |
| 131 | * Space-efficiency is given preference over time-efficiency here: |
| 132 | * several calculations are done in place and temporarily change |
| 133 | * the values of D and E. |
| 134 | * |
| 135 | * Specifically, D is replaced the largest odd divisor of DE - 1 |
| 136 | * throughout the calculations. |
| 137 | */ |
| 138 | |
| 139 | int ret = 0; |
| 140 | |
| 141 | uint16_t attempt; /* Number of current attempt */ |
| 142 | uint16_t iter; /* Number of squares computed in the current attempt */ |
| 143 | |
| 144 | uint16_t bitlen_half; /* Half the bitsize of the modulus N */ |
| 145 | uint16_t order; /* Order of 2 in DE - 1 */ |
| 146 | |
| 147 | mbedtls_mpi K; /* Temporary used for two purposes: |
| 148 | * - During factorization attempts, stores a andom integer |
| 149 | * in the range of [0,..,N] |
| 150 | * - During verification, holding intermediate results. |
| 151 | */ |
| 152 | |
| 153 | if( P == NULL || Q == NULL || P->p != NULL || Q->p != NULL ) |
| 154 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 155 | |
| 156 | if( mbedtls_mpi_cmp_int( N, 0 ) <= 0 || |
| 157 | mbedtls_mpi_cmp_int( D, 1 ) <= 0 || |
| 158 | mbedtls_mpi_cmp_mpi( D, N ) >= 0 || |
| 159 | mbedtls_mpi_cmp_int( E, 1 ) <= 0 || |
| 160 | mbedtls_mpi_cmp_mpi( E, N ) >= 0 ) |
| 161 | { |
| 162 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Initializations and temporary changes |
| 167 | */ |
| 168 | |
| 169 | mbedtls_mpi_init( &K ); |
| 170 | mbedtls_mpi_init( P ); |
| 171 | mbedtls_mpi_init( Q ); |
| 172 | |
| 173 | /* Replace D by DE - 1 */ |
| 174 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( D, D, E ) ); |
| 175 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( D, D, 1 ) ); |
| 176 | |
| 177 | if( ( order = mbedtls_mpi_lsb( D ) ) == 0 ) |
| 178 | { |
| 179 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 180 | goto cleanup; |
| 181 | } |
| 182 | |
| 183 | /* After this operation, D holds the largest odd divisor |
| 184 | * of DE - 1 for the original values of D and E. */ |
| 185 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( D, order ) ); |
| 186 | |
| 187 | /* This is used to generate a few numbers around N / 2 |
| 188 | * if no PRNG is provided. */ |
| 189 | if( f_rng == NULL ) |
| 190 | bitlen_half = mbedtls_mpi_bitlen( N ) / 2; |
| 191 | |
| 192 | /* |
| 193 | * Actual work |
| 194 | */ |
| 195 | |
| 196 | for( attempt = 0; attempt < 30; ++attempt ) |
| 197 | { |
| 198 | /* Generate some number in [0,N], either randomly |
| 199 | * if a PRNG is given, or try numbers around N/2 */ |
| 200 | if( f_rng != NULL ) |
| 201 | { |
| 202 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &K, |
| 203 | mbedtls_mpi_size( N ), |
| 204 | f_rng, p_rng ) ); |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &K, 1 ) ) ; |
| 209 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( &K, bitlen_half ) ) ; |
| 210 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, attempt + 1 ) ); |
| 211 | } |
| 212 | |
| 213 | /* Check if gcd(K,N) = 1 */ |
| 214 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) ); |
| 215 | if( mbedtls_mpi_cmp_int( P, 1 ) != 0 ) |
| 216 | continue; |
| 217 | |
| 218 | /* Go through K^X + 1, K^(2X) + 1, K^(4X) + 1, ... |
| 219 | * and check whether they have nontrivial GCD with N. */ |
| 220 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &K, &K, D, N, |
| 221 | Q /* temporarily use Q for storing Montgomery |
| 222 | * multiplication helper values */ ) ); |
| 223 | |
| 224 | for( iter = 1; iter < order; ++iter ) |
| 225 | { |
| 226 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &K, &K, 1 ) ); |
| 227 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( P, &K, N ) ); |
| 228 | |
| 229 | if( mbedtls_mpi_cmp_int( P, 1 ) == 1 && |
| 230 | mbedtls_mpi_cmp_mpi( P, N ) == -1 ) |
| 231 | { |
| 232 | /* |
| 233 | * Have found a nontrivial divisor P of N. |
| 234 | * Set Q := N / P and verify D, E. |
| 235 | */ |
| 236 | |
| 237 | MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( Q, &K, N, P ) ); |
| 238 | |
| 239 | /* |
| 240 | * Verify that DE - 1 is indeed a multiple of |
| 241 | * lcm(P-1, Q-1), i.e. that it's a multiple of both |
| 242 | * P-1 and Q-1. |
| 243 | */ |
| 244 | |
| 245 | /* Restore DE - 1 and temporarily replace P, Q by P-1, Q-1. */ |
| 246 | MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l( D, order ) ); |
| 247 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) ); |
| 248 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) ); |
| 249 | |
| 250 | /* Compute DE-1 mod P-1 */ |
| 251 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, D, P ) ); |
| 252 | if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 ) |
| 253 | { |
| 254 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 255 | goto cleanup; |
| 256 | } |
| 257 | |
| 258 | /* Compute DE-1 mod Q-1 */ |
| 259 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, D, Q ) ); |
| 260 | if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 ) |
| 261 | { |
| 262 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 263 | goto cleanup; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * All good, restore P, Q and D and return. |
| 268 | */ |
| 269 | |
| 270 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) ); |
| 271 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) ); |
| 272 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( D, D, 1 ) ); |
| 273 | MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( D, NULL, D, E ) ); |
| 274 | |
| 275 | goto cleanup; |
| 276 | } |
| 277 | |
| 278 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) ); |
| 279 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, &K, &K ) ); |
| 280 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, N ) ); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 285 | |
| 286 | cleanup: |
| 287 | |
| 288 | mbedtls_mpi_free( &K ); |
| 289 | return( ret ); |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Given P, Q and the public exponent E, deduce D. |
| 294 | * This is essentially a modular inversion. |
| 295 | */ |
| 296 | |
| 297 | int mbedtls_rsa_deduce_private( mbedtls_mpi *P, mbedtls_mpi *Q, |
| 298 | mbedtls_mpi *D, mbedtls_mpi *E ) |
| 299 | { |
| 300 | int ret = 0; |
| 301 | mbedtls_mpi K; |
| 302 | |
| 303 | if( D == NULL || mbedtls_mpi_cmp_int( D, 0 ) != 0 ) |
| 304 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 305 | |
| 306 | if( mbedtls_mpi_cmp_int( P, 1 ) <= 0 || |
| 307 | mbedtls_mpi_cmp_int( Q, 1 ) <= 0 || |
| 308 | mbedtls_mpi_cmp_int( E, 0 ) == 0 ) |
| 309 | { |
| 310 | return( MBEDTLS_ERR_MPI_BAD_INPUT_DATA ); |
| 311 | } |
| 312 | |
| 313 | mbedtls_mpi_init( &K ); |
| 314 | |
| 315 | /* Temporarily replace P and Q by P-1 and Q-1, respectively. */ |
| 316 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) ); |
| 317 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) ); |
| 318 | |
| 319 | /* Temporarily compute the gcd(P-1, Q-1) in D. */ |
| 320 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( D, P, Q ) ); |
| 321 | |
| 322 | /* Compute LCM(P-1, Q-1) in K */ |
| 323 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) ); |
| 324 | MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &K, NULL, &K, D ) ); |
| 325 | |
| 326 | /* Compute modular inverse of E in LCM(P-1, Q-1) */ |
| 327 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( D, E, &K ) ); |
| 328 | |
| 329 | /* Restore P and Q. */ |
| 330 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) ); |
| 331 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) ); |
| 332 | |
| 333 | /* Double-check result */ |
| 334 | MBEDTLS_MPI_CHK( mbedtls_rsa_check_params( NULL, P, Q, D, E, NULL, NULL ) ); |
| 335 | |
| 336 | cleanup: |
| 337 | |
| 338 | mbedtls_mpi_free( &K ); |
| 339 | |
| 340 | return( ret ); |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Check that core RSA parameters are sane. |
| 345 | * |
| 346 | * Note that the inputs are not declared const and may be |
| 347 | * altered on an unsuccessful run. |
| 348 | */ |
| 349 | |
| 350 | int mbedtls_rsa_check_params( mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q, |
| 351 | mbedtls_mpi *D, mbedtls_mpi *E, |
| 352 | int (*f_rng)(void *, unsigned char *, size_t), |
| 353 | void *p_rng ) |
| 354 | { |
| 355 | int ret = 0; |
| 356 | mbedtls_mpi K; |
| 357 | |
| 358 | mbedtls_mpi_init( &K ); |
| 359 | |
| 360 | /* |
| 361 | * Step 1: If PRNG provided, check that P and Q are prime |
| 362 | */ |
| 363 | |
| 364 | if( f_rng != NULL && P != NULL && |
| 365 | ( ret = mbedtls_mpi_is_prime( P, f_rng, p_rng ) ) != 0 ) |
| 366 | { |
| 367 | goto cleanup; |
| 368 | } |
| 369 | |
| 370 | if( f_rng != NULL && Q != NULL && |
| 371 | ( ret = mbedtls_mpi_is_prime( Q, f_rng, p_rng ) ) != 0 ) |
| 372 | { |
| 373 | goto cleanup; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * Step 2: Check that N = PQ |
| 378 | */ |
| 379 | |
| 380 | if( P != NULL && Q != NULL && N != NULL ) |
| 381 | { |
| 382 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, P, Q ) ); |
| 383 | if( mbedtls_mpi_cmp_mpi( &K, N ) != 0 ) |
| 384 | { |
| 385 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 386 | goto cleanup; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Step 3: Check that D, E are inverse modulo P-1 and Q-1 |
| 392 | */ |
| 393 | |
| 394 | if( P != NULL && Q != NULL && D != NULL && E != NULL ) |
| 395 | { |
| 396 | /* Temporarily replace P, Q by P-1, Q-1. */ |
| 397 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( P, P, 1 ) ); |
| 398 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( Q, Q, 1 ) ); |
| 399 | |
| 400 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &K, D, E ) ); |
| 401 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, &K, 1 ) ); |
| 402 | |
| 403 | /* Compute DE-1 mod P-1 */ |
| 404 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, P ) ); |
| 405 | if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 ) |
| 406 | { |
| 407 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 408 | goto cleanup; |
| 409 | } |
| 410 | |
| 411 | /* Compute DE-1 mod Q-1 */ |
| 412 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &K, &K, Q ) ); |
| 413 | if( mbedtls_mpi_cmp_int( &K, 0 ) != 0 ) |
| 414 | { |
| 415 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 416 | goto cleanup; |
| 417 | } |
| 418 | |
| 419 | /* Restore P, Q. */ |
| 420 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( P, P, 1 ) ); |
| 421 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( Q, Q, 1 ) ); |
| 422 | } |
| 423 | |
| 424 | cleanup: |
| 425 | |
| 426 | mbedtls_mpi_free( &K ); |
| 427 | |
| 428 | return( ret ); |
| 429 | } |
| 430 | |
| 431 | int mbedtls_rsa_deduce_crt( const mbedtls_mpi *P, const mbedtls_mpi *Q, |
| 432 | const mbedtls_mpi *D, mbedtls_mpi *DP, |
| 433 | mbedtls_mpi *DQ, mbedtls_mpi *QP ) |
| 434 | { |
| 435 | int ret = 0; |
| 436 | mbedtls_mpi K; |
| 437 | mbedtls_mpi_init( &K ); |
| 438 | |
| 439 | if( DP != NULL ) |
| 440 | { |
| 441 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, P, 1 ) ); |
| 442 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, D, &K ) ); |
| 443 | } |
| 444 | |
| 445 | if( DQ != NULL ) |
| 446 | { |
| 447 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &K, Q, 1 ) ); |
| 448 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, D, &K ) ); |
| 449 | } |
| 450 | |
| 451 | if( QP != NULL ) |
| 452 | { |
| 453 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( QP, Q, P ) ); |
| 454 | } |
| 455 | |
| 456 | cleanup: |
| 457 | mbedtls_mpi_free( &K ); |
| 458 | |
| 459 | return( ret ); |
| 460 | } |
| 461 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 462 | |
| 463 | /* |
| 464 | * Default RSA interface implementation |
| 465 | */ |
| 466 | |
| 467 | |
| 468 | int mbedtls_rsa_import( mbedtls_rsa_context *ctx, |
| 469 | const mbedtls_mpi *N, |
| 470 | const mbedtls_mpi *P, const mbedtls_mpi *Q, |
| 471 | const mbedtls_mpi *D, const mbedtls_mpi *E ) |
| 472 | { |
| 473 | int ret; |
| 474 | |
| 475 | if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) || |
| 476 | ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) || |
| 477 | ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) || |
| 478 | ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) || |
| 479 | ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) ) |
| 480 | { |
| 481 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 482 | } |
| 483 | |
| 484 | if( N != NULL ) |
| 485 | ctx->len = mbedtls_mpi_size( &ctx->N ); |
| 486 | |
| 487 | return( 0 ); |
| 488 | } |
| 489 | |
| 490 | int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx, |
| 491 | unsigned char *N, size_t N_len, |
| 492 | unsigned char *P, size_t P_len, |
| 493 | unsigned char *Q, size_t Q_len, |
| 494 | unsigned char *D, size_t D_len, |
| 495 | unsigned char *E, size_t E_len ) |
| 496 | { |
| 497 | int ret; |
| 498 | |
| 499 | if( N != NULL ) |
| 500 | { |
| 501 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) ); |
| 502 | ctx->len = mbedtls_mpi_size( &ctx->N ); |
| 503 | } |
| 504 | |
| 505 | if( P != NULL ) |
| 506 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) ); |
| 507 | |
| 508 | if( Q != NULL ) |
| 509 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) ); |
| 510 | |
| 511 | if( D != NULL ) |
| 512 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) ); |
| 513 | |
| 514 | if( E != NULL ) |
| 515 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) ); |
| 516 | |
| 517 | cleanup: |
| 518 | |
| 519 | if( ret != 0 ) |
| 520 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 521 | |
| 522 | return( 0 ); |
| 523 | } |
| 524 | |
| 525 | int mbedtls_rsa_complete( mbedtls_rsa_context *ctx, |
| 526 | int (*f_rng)(void *, unsigned char *, size_t), |
| 527 | void *p_rng ) |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 528 | { |
| 529 | int ret = 0; |
| 530 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 531 | const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 ); |
| 532 | const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 ); |
| 533 | const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 ); |
| 534 | const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 ); |
| 535 | const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 ); |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 536 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 537 | /* |
| 538 | * Check whether provided parameters are enough |
| 539 | * to deduce all others. The following incomplete |
| 540 | * parameter sets for private keys are supported: |
| 541 | * |
| 542 | * (1) P, Q missing. |
| 543 | * (2) D and potentially N missing. |
| 544 | * |
| 545 | */ |
| 546 | const int complete = have_N && have_P && have_Q && have_D && have_E; |
| 547 | const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E; |
| 548 | const int d_missing = have_P && have_Q && !have_D && have_E; |
| 549 | const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E; |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 550 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 551 | const int is_priv = complete || pq_missing || d_missing; |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 552 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 553 | if( !is_priv && !is_pub ) |
| 554 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 555 | |
| 556 | /* |
| 557 | * Step 1: Deduce and verify all core parameters. |
| 558 | */ |
| 559 | |
| 560 | if( pq_missing ) |
| 561 | { |
| 562 | /* This includes sanity checking of core parameters, |
| 563 | * so no further checks necessary. */ |
| 564 | ret = mbedtls_rsa_deduce_moduli( &ctx->N, &ctx->D, &ctx->E, |
| 565 | f_rng, p_rng, |
| 566 | &ctx->P, &ctx->Q ); |
| 567 | if( ret != 0 ) |
| 568 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 569 | |
| 570 | } |
| 571 | else if( d_missing ) |
| 572 | { |
| 573 | /* If a PRNG is provided, check if P, Q are prime. */ |
| 574 | if( f_rng != NULL && |
| 575 | ( ( ret = mbedtls_mpi_is_prime( &ctx->P, f_rng, p_rng ) ) != 0 || |
| 576 | ( ret = mbedtls_mpi_is_prime( &ctx->Q, f_rng, p_rng ) ) != 0 ) ) |
| 577 | { |
| 578 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 579 | } |
| 580 | |
| 581 | /* Compute N if missing. */ |
| 582 | if( !have_N && |
| 583 | ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) ) != 0 ) |
| 584 | { |
| 585 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 586 | } |
| 587 | |
| 588 | /* Deduce private exponent. This includes double-checking of the result, |
| 589 | * so together with the primality test above all core parameters are |
| 590 | * guaranteed to be sane if this call succeeds. */ |
| 591 | if( ( ret = mbedtls_rsa_deduce_private( &ctx->P, &ctx->Q, |
| 592 | &ctx->D, &ctx->E ) ) != 0 ) |
| 593 | { |
| 594 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 595 | } |
| 596 | } |
| 597 | else if( complete ) |
| 598 | { |
| 599 | /* Check complete set of imported core parameters. */ |
| 600 | if( ( ret = mbedtls_rsa_check_params( &ctx->N, &ctx->P, &ctx->Q, |
| 601 | &ctx->D, &ctx->E, |
| 602 | f_rng, p_rng ) ) != 0 ) |
| 603 | { |
| 604 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /* In the remaining case of a public key, there's nothing to check for. */ |
| 609 | |
| 610 | /* |
| 611 | * Step 2: Deduce all additional parameters specific |
| 612 | * to our current RSA implementaiton. |
| 613 | */ |
| 614 | |
Hanno Becker | 23344b5 | 2017-08-23 07:43:27 +0100 | [diff] [blame] | 615 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 616 | if( is_priv ) |
| 617 | { |
| 618 | ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D, |
| 619 | &ctx->DP, &ctx->DQ, &ctx->QP ); |
| 620 | if( ret != 0 ) |
| 621 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 622 | } |
Hanno Becker | 23344b5 | 2017-08-23 07:43:27 +0100 | [diff] [blame] | 623 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 624 | |
| 625 | /* |
| 626 | * Step 3: Double check |
| 627 | */ |
| 628 | |
| 629 | if( is_priv ) |
| 630 | { |
| 631 | if( ( ret = mbedtls_rsa_check_privkey( ctx ) ) != 0 ) |
| 632 | return( ret ); |
| 633 | } |
| 634 | else |
| 635 | { |
| 636 | if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 ) |
| 637 | return( ret ); |
| 638 | } |
| 639 | |
| 640 | return( 0 ); |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Check if CRT parameters match RSA context. |
| 645 | * This has to be implemented even if CRT is not used, |
| 646 | * in order to be able to validate DER encoded RSA keys, |
| 647 | * which always contain CRT parameters. |
| 648 | */ |
| 649 | int mbedtls_rsa_check_crt( mbedtls_rsa_context *ctx, mbedtls_mpi *DP, |
| 650 | mbedtls_mpi *DQ, mbedtls_mpi *QP ) |
| 651 | { |
Hanno Becker | 23344b5 | 2017-08-23 07:43:27 +0100 | [diff] [blame] | 652 | int ret = 0; |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 653 | |
Hanno Becker | 23344b5 | 2017-08-23 07:43:27 +0100 | [diff] [blame] | 654 | /* Check if key is private or public */ |
| 655 | const int is_priv = |
| 656 | mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && |
| 657 | mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && |
| 658 | mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && |
| 659 | mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 && |
| 660 | mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0; |
| 661 | |
| 662 | if( !is_priv ) |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 663 | { |
| 664 | /* Checking optional parameters only makes sense for private keys. */ |
| 665 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 666 | } |
| 667 | |
Hanno Becker | 23344b5 | 2017-08-23 07:43:27 +0100 | [diff] [blame] | 668 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 669 | if( ( DP != NULL && mbedtls_mpi_cmp_mpi( DP, &ctx->DP ) != 0 ) || |
| 670 | ( DQ != NULL && mbedtls_mpi_cmp_mpi( DQ, &ctx->DQ ) != 0 ) || |
| 671 | ( QP != NULL && mbedtls_mpi_cmp_mpi( QP, &ctx->QP ) != 0 ) ) |
| 672 | { |
| 673 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 674 | } |
Hanno Becker | 23344b5 | 2017-08-23 07:43:27 +0100 | [diff] [blame] | 675 | #else /* MBEDTLS_RSA_NO_CRT */ |
| 676 | |
| 677 | /* |
| 678 | * Check that DP, DQ and QP are in accordance with core parameters. |
| 679 | * (1) Check that DP - P == 0 mod P - 1 |
| 680 | * (2) Check that DQ - Q == 0 mod Q - 1 |
| 681 | * (3) Check that QP * P - 1 == 0 mod P |
| 682 | |
| 683 | * Alternative implementation also not using DP, DQ and QP |
| 684 | * should be able to reuse this codepath. |
| 685 | */ |
| 686 | |
| 687 | /* Check (1) */ |
| 688 | if( DP != NULL ) |
| 689 | { |
| 690 | /* Temporarily replace P by P-1 and compute DP - D mod P-1 */ |
| 691 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) ); |
| 692 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( DP, DP, &ctx->D ) ); |
| 693 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DP, DP, &ctx->P ) ); |
| 694 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) ); |
| 695 | |
| 696 | if( mbedtls_mpi_cmp_int( DP, 0 ) != 0 ) |
| 697 | { |
| 698 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | /* Check (1) */ |
| 703 | if( DQ != NULL ) |
| 704 | { |
| 705 | /* Temporarily replace Q by Q-1 and compute DQ - D mod Q-1 */ |
| 706 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) ); |
| 707 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( DQ, DQ, &ctx->D ) ); |
| 708 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( DQ, DQ, &ctx->Q ) ); |
| 709 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) ); |
| 710 | |
| 711 | if( mbedtls_mpi_cmp_int( DQ, 0 ) != 0 ) |
| 712 | { |
| 713 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | /* Check (3) */ |
| 718 | if( QP != NULL ) |
| 719 | { |
| 720 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( QP, QP, &ctx->Q ) ); |
| 721 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( QP, QP, 1 ) ); |
| 722 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( QP, QP, &ctx->P ) ); |
| 723 | if( mbedtls_mpi_cmp_int( QP, 0 ) != 0 ) |
| 724 | { |
| 725 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | cleanup: |
| 730 | |
| 731 | #endif |
| 732 | |
| 733 | if( ret != 0 ) |
| 734 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 735 | |
| 736 | return( 0 ); |
| 737 | } |
| 738 | |
| 739 | int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx, |
| 740 | unsigned char *N, size_t N_len, |
| 741 | unsigned char *P, size_t P_len, |
| 742 | unsigned char *Q, size_t Q_len, |
| 743 | unsigned char *D, size_t D_len, |
| 744 | unsigned char *E, size_t E_len ) |
| 745 | { |
| 746 | int ret = 0; |
| 747 | |
| 748 | /* Check if key is private or public */ |
| 749 | const int is_priv = |
| 750 | mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && |
| 751 | mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && |
| 752 | mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && |
| 753 | mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 && |
| 754 | mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0; |
| 755 | |
| 756 | if( !is_priv ) |
| 757 | { |
| 758 | /* If we're trying to export private parameters for a public key, |
| 759 | * something must be wrong. */ |
| 760 | if( P != NULL || Q != NULL || D != NULL ) |
| 761 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 762 | |
| 763 | } |
| 764 | |
| 765 | if( N != NULL ) |
| 766 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) ); |
| 767 | |
| 768 | if( P != NULL ) |
| 769 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) ); |
| 770 | |
| 771 | if( Q != NULL ) |
| 772 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) ); |
| 773 | |
| 774 | if( D != NULL ) |
| 775 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) ); |
| 776 | |
| 777 | if( E != NULL ) |
| 778 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) ); |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 779 | |
| 780 | cleanup: |
| 781 | |
| 782 | return( ret ); |
| 783 | } |
| 784 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 785 | int mbedtls_rsa_export( const mbedtls_rsa_context *ctx, |
| 786 | mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q, |
| 787 | mbedtls_mpi *D, mbedtls_mpi *E ) |
| 788 | { |
| 789 | int ret; |
| 790 | |
| 791 | /* Check if key is private or public */ |
| 792 | int is_priv = |
| 793 | mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && |
| 794 | mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && |
| 795 | mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && |
| 796 | mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 && |
| 797 | mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0; |
| 798 | |
| 799 | if( !is_priv ) |
| 800 | { |
| 801 | /* If we're trying to export private parameters for a public key, |
| 802 | * something must be wrong. */ |
| 803 | if( P != NULL || Q != NULL || D != NULL ) |
| 804 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 805 | |
| 806 | } |
| 807 | |
| 808 | /* Export all requested core parameters. */ |
| 809 | |
| 810 | if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) || |
| 811 | ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) || |
| 812 | ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) || |
| 813 | ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) || |
| 814 | ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) ) |
| 815 | { |
| 816 | return( ret ); |
| 817 | } |
| 818 | |
| 819 | return( 0 ); |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | * Export CRT parameters |
| 824 | * This must also be implemented if CRT is not used, for being able to |
| 825 | * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt |
| 826 | * can be used in this case. |
| 827 | */ |
| 828 | int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx, |
| 829 | mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP ) |
| 830 | { |
| 831 | int ret; |
| 832 | |
| 833 | /* Check if key is private or public */ |
| 834 | int is_priv = |
| 835 | mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && |
| 836 | mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && |
| 837 | mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && |
| 838 | mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 && |
| 839 | mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0; |
| 840 | |
| 841 | if( !is_priv ) |
| 842 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 843 | |
| 844 | /* Export all requested blinding parameters. */ |
| 845 | |
| 846 | if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) || |
| 847 | ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) || |
| 848 | ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) ) |
| 849 | { |
| 850 | return( ret ); |
| 851 | } |
| 852 | |
| 853 | return( 0 ); |
| 854 | } |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 855 | |
| 856 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 857 | * Initialize an RSA context |
| 858 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 859 | void mbedtls_rsa_init( mbedtls_rsa_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 860 | int padding, |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 861 | int hash_id ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 862 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 863 | memset( ctx, 0, sizeof( mbedtls_rsa_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 864 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 865 | mbedtls_rsa_set_padding( ctx, padding, hash_id ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 866 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 867 | #if defined(MBEDTLS_THREADING_C) |
| 868 | mbedtls_mutex_init( &ctx->mutex ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 869 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 872 | /* |
| 873 | * Set padding for an existing RSA context |
| 874 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 875 | void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id ) |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 876 | { |
| 877 | ctx->padding = padding; |
| 878 | ctx->hash_id = hash_id; |
| 879 | } |
| 880 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 881 | /* |
| 882 | * Get length in bytes of RSA modulus |
| 883 | */ |
| 884 | |
| 885 | size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx ) |
| 886 | { |
| 887 | return( mbedtls_mpi_size( &ctx->N ) ); |
| 888 | } |
| 889 | |
| 890 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 891 | #if defined(MBEDTLS_GENPRIME) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 892 | |
| 893 | /* |
| 894 | * Generate an RSA keypair |
| 895 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 896 | int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 897 | int (*f_rng)(void *, unsigned char *, size_t), |
| 898 | void *p_rng, |
| 899 | unsigned int nbits, int exponent ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 900 | { |
| 901 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 902 | mbedtls_mpi P1, Q1, H, G; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 903 | |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 904 | if( f_rng == NULL || nbits < 128 || exponent < 3 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 905 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 906 | |
Janos Follath | ef44178 | 2016-09-21 13:18:12 +0100 | [diff] [blame] | 907 | if( nbits % 2 ) |
| 908 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 909 | |
| 910 | mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); |
Janos Follath | 10c575b | 2016-02-23 14:42:48 +0000 | [diff] [blame] | 911 | mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 912 | |
| 913 | /* |
| 914 | * find primes P and Q with Q < P so that: |
| 915 | * GCD( E, (P-1)*(Q-1) ) == 1 |
| 916 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 917 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 918 | |
| 919 | do |
| 920 | { |
Janos Follath | 10c575b | 2016-02-23 14:42:48 +0000 | [diff] [blame] | 921 | MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0, |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 922 | f_rng, p_rng ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 923 | |
Janos Follath | ef44178 | 2016-09-21 13:18:12 +0100 | [diff] [blame] | 924 | MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0, |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 925 | f_rng, p_rng ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 926 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 927 | if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 928 | continue; |
| 929 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 930 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) ); |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 931 | if( mbedtls_mpi_bitlen( &ctx->N ) != nbits ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 932 | continue; |
| 933 | |
Janos Follath | ef44178 | 2016-09-21 13:18:12 +0100 | [diff] [blame] | 934 | if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 ) |
| 935 | mbedtls_mpi_swap( &ctx->P, &ctx->Q ); |
| 936 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 937 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) ); |
| 938 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) ); |
| 939 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) ); |
| 940 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 941 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 942 | while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 943 | |
| 944 | /* |
| 945 | * D = E^-1 mod ((P-1)*(Q-1)) |
| 946 | * DP = D mod (P - 1) |
| 947 | * DQ = D mod (Q - 1) |
| 948 | * QP = Q^-1 mod P |
| 949 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 950 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) ); |
| 951 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) ); |
| 952 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) ); |
| 953 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 954 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 955 | ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 956 | |
Hanno Becker | 83aad1f | 2017-08-23 06:45:10 +0100 | [diff] [blame] | 957 | /* Double-check */ |
| 958 | MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) ); |
| 959 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 960 | cleanup: |
| 961 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 962 | mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 963 | |
| 964 | if( ret != 0 ) |
| 965 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 966 | mbedtls_rsa_free( ctx ); |
| 967 | return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 970 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 973 | #endif /* MBEDTLS_GENPRIME */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 974 | |
| 975 | /* |
| 976 | * Check a public RSA key |
| 977 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 978 | int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 979 | { |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 980 | if( !ctx->N.p || !ctx->E.p ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 981 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 982 | |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 983 | if( ( ctx->N.p[0] & 1 ) == 0 || |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 984 | ( ctx->E.p[0] & 1 ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 985 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 986 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 987 | if( mbedtls_mpi_bitlen( &ctx->N ) < 128 || |
| 988 | mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 989 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 990 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 991 | if( mbedtls_mpi_bitlen( &ctx->E ) < 2 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 992 | mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 ) |
| 993 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 994 | |
| 995 | return( 0 ); |
| 996 | } |
| 997 | |
| 998 | /* |
| 999 | * Check a private RSA key |
| 1000 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1001 | int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1002 | { |
| 1003 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1004 | mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1005 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1006 | if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1007 | return( ret ); |
| 1008 | |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 1009 | if( !ctx->P.p || !ctx->Q.p || !ctx->D.p ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1010 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 1011 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1012 | mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); |
| 1013 | mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 ); |
| 1014 | mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ ); |
| 1015 | mbedtls_mpi_init( &QP ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1016 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1017 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) ); |
| 1018 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) ); |
| 1019 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) ); |
| 1020 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) ); |
| 1021 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) ); |
| 1022 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1023 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1024 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) ); |
| 1025 | MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) ); |
| 1026 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) ); |
Paul Bakker | b572adf | 2010-07-18 08:29:32 +0000 | [diff] [blame] | 1027 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1028 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) ); |
| 1029 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) ); |
| 1030 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) ); |
Paul Bakker | b572adf | 2010-07-18 08:29:32 +0000 | [diff] [blame] | 1031 | /* |
| 1032 | * Check for a valid PKCS1v2 private key |
| 1033 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1034 | if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 || |
| 1035 | mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 || |
| 1036 | mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 || |
| 1037 | mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 || |
| 1038 | mbedtls_mpi_cmp_int( &L2, 0 ) != 0 || |
| 1039 | mbedtls_mpi_cmp_int( &I, 1 ) != 0 || |
| 1040 | mbedtls_mpi_cmp_int( &G, 1 ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1041 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1042 | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1043 | } |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1044 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1045 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1046 | mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); |
| 1047 | mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 ); |
| 1048 | mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ ); |
| 1049 | mbedtls_mpi_free( &QP ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1050 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1051 | if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 1052 | return( ret ); |
| 1053 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1054 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1055 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 1056 | |
| 1057 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | /* |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1061 | * Check if contexts holding a public and private key match |
| 1062 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1063 | int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv ) |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1064 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1065 | if( mbedtls_rsa_check_pubkey( pub ) != 0 || |
| 1066 | mbedtls_rsa_check_privkey( prv ) != 0 ) |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1067 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1068 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1069 | } |
| 1070 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1071 | if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 || |
| 1072 | mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 ) |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1073 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1074 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | return( 0 ); |
| 1078 | } |
| 1079 | |
| 1080 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1081 | * Do an RSA public key operation |
| 1082 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1083 | int mbedtls_rsa_public( mbedtls_rsa_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1084 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1085 | unsigned char *output ) |
| 1086 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1087 | int ret; |
| 1088 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | mbedtls_mpi T; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1090 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | mbedtls_mpi_init( &T ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1092 | |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 1093 | #if defined(MBEDTLS_THREADING_C) |
| 1094 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 1095 | return( ret ); |
| 1096 | #endif |
| 1097 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1098 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1099 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1100 | if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1101 | { |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 1102 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 1103 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1107 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) ); |
| 1108 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1109 | |
| 1110 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1111 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 1112 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 1113 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
Manuel Pégourié-Gonnard | 88fca3e | 2015-03-27 15:06:07 +0100 | [diff] [blame] | 1114 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1115 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1116 | mbedtls_mpi_free( &T ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1117 | |
| 1118 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1119 | return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1120 | |
| 1121 | return( 0 ); |
| 1122 | } |
| 1123 | |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 1124 | /* |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 1125 | * Generate or update blinding values, see section 10 of: |
| 1126 | * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA, |
Manuel Pégourié-Gonnard | 998930a | 2015-04-03 13:48:06 +0200 | [diff] [blame] | 1127 | * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 1128 | * Berlin Heidelberg, 1996. p. 104-113. |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 1129 | */ |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 1130 | static int rsa_prepare_blinding( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 1131 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 1132 | { |
Manuel Pégourié-Gonnard | 4d89c7e | 2013-10-04 15:18:38 +0200 | [diff] [blame] | 1133 | int ret, count = 0; |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 1134 | |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 1135 | if( ctx->Vf.p != NULL ) |
| 1136 | { |
| 1137 | /* We already have blinding values, just update them by squaring */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1138 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) ); |
| 1139 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) ); |
| 1140 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) ); |
| 1141 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) ); |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 1142 | |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 1143 | goto cleanup; |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 1144 | } |
| 1145 | |
Manuel Pégourié-Gonnard | 4d89c7e | 2013-10-04 15:18:38 +0200 | [diff] [blame] | 1146 | /* Unblinding value: Vf = random number, invertible mod N */ |
| 1147 | do { |
| 1148 | if( count++ > 10 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1149 | return( MBEDTLS_ERR_RSA_RNG_FAILED ); |
Manuel Pégourié-Gonnard | 4d89c7e | 2013-10-04 15:18:38 +0200 | [diff] [blame] | 1150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1151 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) ); |
| 1152 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) ); |
| 1153 | } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 ); |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 1154 | |
| 1155 | /* Blinding value: Vi = Vf^(-e) mod N */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1156 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) ); |
| 1157 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) ); |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 1158 | |
Manuel Pégourié-Gonnard | ae10299 | 2013-10-04 17:07:12 +0200 | [diff] [blame] | 1159 | |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 1160 | cleanup: |
| 1161 | return( ret ); |
| 1162 | } |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 1163 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1164 | /* |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1165 | * Exponent blinding supposed to prevent side-channel attacks using multiple |
| 1166 | * traces of measurements to recover the RSA key. The more collisions are there, |
| 1167 | * the more bits of the key can be recovered. See [3]. |
| 1168 | * |
| 1169 | * Collecting n collisions with m bit long blinding value requires 2^(m-m/n) |
| 1170 | * observations on avarage. |
| 1171 | * |
| 1172 | * For example with 28 byte blinding to achieve 2 collisions the adversary has |
| 1173 | * to make 2^112 observations on avarage. |
| 1174 | * |
| 1175 | * (With the currently (as of 2017 April) known best algorithms breaking 2048 |
| 1176 | * bit RSA requires approximately as much time as trying out 2^112 random keys. |
| 1177 | * Thus in this sense with 28 byte blinding the security is not reduced by |
| 1178 | * side-channel attacks like the one in [3]) |
| 1179 | * |
| 1180 | * This countermeasure does not help if the key recovery is possible with a |
| 1181 | * single trace. |
| 1182 | */ |
| 1183 | #define RSA_EXPONENT_BLINDING 28 |
| 1184 | |
| 1185 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1186 | * Do an RSA private key operation |
| 1187 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1188 | int mbedtls_rsa_private( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1189 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1190 | void *p_rng, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1191 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1192 | unsigned char *output ) |
| 1193 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1194 | int ret; |
| 1195 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1196 | mbedtls_mpi T, T1, T2; |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1197 | mbedtls_mpi P1, Q1, R; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1198 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1199 | mbedtls_mpi D_blind; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1200 | mbedtls_mpi *D = &ctx->D; |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1201 | #else |
| 1202 | mbedtls_mpi DP_blind, DQ_blind; |
| 1203 | mbedtls_mpi *DP = &ctx->DP; |
| 1204 | mbedtls_mpi *DQ = &ctx->DQ; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1205 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1206 | |
Manuel Pégourié-Gonnard | fb84d38 | 2015-10-30 10:56:25 +0100 | [diff] [blame] | 1207 | /* Make sure we have private key info, prevent possible misuse */ |
| 1208 | if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL ) |
| 1209 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1210 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1211 | mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1212 | mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R ); |
| 1213 | |
| 1214 | |
| 1215 | if( f_rng != NULL ) |
| 1216 | { |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1217 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1218 | mbedtls_mpi_init( &D_blind ); |
| 1219 | #else |
| 1220 | mbedtls_mpi_init( &DP_blind ); |
| 1221 | mbedtls_mpi_init( &DQ_blind ); |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1222 | #endif |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1223 | } |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1224 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1225 | |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 1226 | #if defined(MBEDTLS_THREADING_C) |
| 1227 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 1228 | return( ret ); |
| 1229 | #endif |
| 1230 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1231 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) ); |
| 1232 | if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1233 | { |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 1234 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 1235 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 1238 | if( f_rng != NULL ) |
| 1239 | { |
| 1240 | /* |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 1241 | * Blinding |
| 1242 | * T = T * Vi mod N |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 1243 | */ |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 1244 | MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) ); |
| 1245 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1246 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) ); |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1247 | |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1248 | /* |
| 1249 | * Exponent blinding |
| 1250 | */ |
| 1251 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) ); |
| 1252 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) ); |
| 1253 | |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1254 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1255 | /* |
| 1256 | * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D |
| 1257 | */ |
| 1258 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING, |
| 1259 | f_rng, p_rng ) ); |
| 1260 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) ); |
| 1261 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) ); |
| 1262 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) ); |
| 1263 | |
| 1264 | D = &D_blind; |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1265 | #else |
| 1266 | /* |
| 1267 | * DP_blind = ( P - 1 ) * R + DP |
| 1268 | */ |
| 1269 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING, |
| 1270 | f_rng, p_rng ) ); |
| 1271 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) ); |
| 1272 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind, |
| 1273 | &ctx->DP ) ); |
| 1274 | |
| 1275 | DP = &DP_blind; |
| 1276 | |
| 1277 | /* |
| 1278 | * DQ_blind = ( Q - 1 ) * R + DQ |
| 1279 | */ |
| 1280 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING, |
| 1281 | f_rng, p_rng ) ); |
| 1282 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) ); |
| 1283 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind, |
| 1284 | &ctx->DQ ) ); |
| 1285 | |
| 1286 | DQ = &DQ_blind; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1287 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 1288 | } |
Paul Bakker | aab30c1 | 2013-08-30 11:00:25 +0200 | [diff] [blame] | 1289 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1290 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1291 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, D, &ctx->N, &ctx->RN ) ); |
Manuel Pégourié-Gonnard | e10e06d | 2014-11-06 18:15:12 +0100 | [diff] [blame] | 1292 | #else |
Paul Bakker | aab30c1 | 2013-08-30 11:00:25 +0200 | [diff] [blame] | 1293 | /* |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1294 | * Faster decryption using the CRT |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1295 | * |
| 1296 | * T1 = input ^ dP mod P |
| 1297 | * T2 = input ^ dQ mod Q |
| 1298 | */ |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1299 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) ); |
| 1300 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1301 | |
| 1302 | /* |
| 1303 | * T = (T1 - T2) * (Q^-1 mod P) mod P |
| 1304 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1305 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) ); |
| 1306 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) ); |
| 1307 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1308 | |
| 1309 | /* |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 1310 | * T = T2 + T * Q |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1311 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1312 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) ); |
| 1313 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) ); |
| 1314 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Paul Bakker | aab30c1 | 2013-08-30 11:00:25 +0200 | [diff] [blame] | 1315 | |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 1316 | if( f_rng != NULL ) |
| 1317 | { |
| 1318 | /* |
| 1319 | * Unblind |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 1320 | * T = T * Vf mod N |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 1321 | */ |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 1322 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1323 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) ); |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 1324 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1325 | |
| 1326 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1327 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1328 | |
| 1329 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1330 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 1331 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 1332 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
Manuel Pégourié-Gonnard | ae10299 | 2013-10-04 17:07:12 +0200 | [diff] [blame] | 1333 | #endif |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 1334 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1335 | mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1336 | mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R ); |
| 1337 | |
| 1338 | if( f_rng != NULL ) |
| 1339 | { |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1340 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1341 | mbedtls_mpi_free( &D_blind ); |
| 1342 | #else |
| 1343 | mbedtls_mpi_free( &DP_blind ); |
| 1344 | mbedtls_mpi_free( &DQ_blind ); |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1345 | #endif |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1346 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1347 | |
| 1348 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1349 | return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1350 | |
| 1351 | return( 0 ); |
| 1352 | } |
| 1353 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1354 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1355 | /** |
| 1356 | * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer. |
| 1357 | * |
Paul Bakker | b125ed8 | 2011-11-10 13:33:51 +0000 | [diff] [blame] | 1358 | * \param dst buffer to mask |
| 1359 | * \param dlen length of destination buffer |
| 1360 | * \param src source of the mask generation |
| 1361 | * \param slen length of the source buffer |
| 1362 | * \param md_ctx message digest context to use |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1363 | */ |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1364 | static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1365 | size_t slen, mbedtls_md_context_t *md_ctx ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1366 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1367 | unsigned char mask[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1368 | unsigned char counter[4]; |
| 1369 | unsigned char *p; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1370 | unsigned int hlen; |
| 1371 | size_t i, use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1372 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1373 | memset( mask, 0, MBEDTLS_MD_MAX_SIZE ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1374 | memset( counter, 0, 4 ); |
| 1375 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1376 | hlen = mbedtls_md_get_size( md_ctx->md_info ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1377 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1378 | /* Generate and apply dbMask */ |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1379 | p = dst; |
| 1380 | |
| 1381 | while( dlen > 0 ) |
| 1382 | { |
| 1383 | use_len = hlen; |
| 1384 | if( dlen < hlen ) |
| 1385 | use_len = dlen; |
| 1386 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1387 | mbedtls_md_starts( md_ctx ); |
| 1388 | mbedtls_md_update( md_ctx, src, slen ); |
| 1389 | mbedtls_md_update( md_ctx, counter, 4 ); |
| 1390 | mbedtls_md_finish( md_ctx, mask ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1391 | |
| 1392 | for( i = 0; i < use_len; ++i ) |
| 1393 | *p++ ^= mask[i]; |
| 1394 | |
| 1395 | counter[3]++; |
| 1396 | |
| 1397 | dlen -= use_len; |
| 1398 | } |
Gilles Peskine | 18ac716 | 2017-05-05 19:24:06 +0200 | [diff] [blame] | 1399 | |
| 1400 | mbedtls_zeroize( mask, sizeof( mask ) ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1401 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1402 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1403 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1404 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1405 | /* |
| 1406 | * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function |
| 1407 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1408 | int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1409 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1410 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 1411 | int mode, |
| 1412 | const unsigned char *label, size_t label_len, |
| 1413 | size_t ilen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1414 | const unsigned char *input, |
| 1415 | unsigned char *output ) |
| 1416 | { |
| 1417 | size_t olen; |
| 1418 | int ret; |
| 1419 | unsigned char *p = output; |
| 1420 | unsigned int hlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1421 | const mbedtls_md_info_t *md_info; |
| 1422 | mbedtls_md_context_t md_ctx; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1424 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 1425 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 1426 | |
| 1427 | if( f_rng == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1428 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1429 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1430 | md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1431 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1432 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1433 | |
| 1434 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1435 | hlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1436 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1437 | /* first comparison checks for overflow */ |
Janos Follath | eddfe8f | 2016-02-08 14:52:29 +0000 | [diff] [blame] | 1438 | if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1439 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1440 | |
| 1441 | memset( output, 0, olen ); |
| 1442 | |
| 1443 | *p++ = 0; |
| 1444 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1445 | /* Generate a random octet string seed */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1446 | if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1447 | return( MBEDTLS_ERR_RSA_RNG_FAILED + ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1448 | |
| 1449 | p += hlen; |
| 1450 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1451 | /* Construct DB */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1452 | mbedtls_md( md_info, label, label_len, p ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1453 | p += hlen; |
| 1454 | p += olen - 2 * hlen - 2 - ilen; |
| 1455 | *p++ = 1; |
| 1456 | memcpy( p, input, ilen ); |
| 1457 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1458 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1459 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 1460 | { |
| 1461 | mbedtls_md_free( &md_ctx ); |
| 1462 | return( ret ); |
| 1463 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1464 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1465 | /* maskedDB: Apply dbMask to DB */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1466 | mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen, |
| 1467 | &md_ctx ); |
| 1468 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1469 | /* maskedSeed: Apply seedMask to seed */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1470 | mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1, |
| 1471 | &md_ctx ); |
| 1472 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1473 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1474 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1475 | return( ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1476 | ? mbedtls_rsa_public( ctx, output, output ) |
| 1477 | : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1478 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1479 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1480 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1481 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1482 | /* |
| 1483 | * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function |
| 1484 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1485 | int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1486 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1487 | void *p_rng, |
| 1488 | int mode, size_t ilen, |
| 1489 | const unsigned char *input, |
| 1490 | unsigned char *output ) |
| 1491 | { |
| 1492 | size_t nb_pad, olen; |
| 1493 | int ret; |
| 1494 | unsigned char *p = output; |
| 1495 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1496 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 1497 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 1498 | |
Janos Follath | 1ed9f99 | 2016-03-18 11:45:44 +0000 | [diff] [blame] | 1499 | // We don't check p_rng because it won't be dereferenced here |
| 1500 | if( f_rng == NULL || input == NULL || output == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1501 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1502 | |
| 1503 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 370717b | 2016-02-11 10:35:13 +0100 | [diff] [blame] | 1504 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1505 | /* first comparison checks for overflow */ |
Janos Follath | eddfe8f | 2016-02-08 14:52:29 +0000 | [diff] [blame] | 1506 | if( ilen + 11 < ilen || olen < ilen + 11 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1507 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1508 | |
| 1509 | nb_pad = olen - 3 - ilen; |
| 1510 | |
| 1511 | *p++ = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1512 | if( mode == MBEDTLS_RSA_PUBLIC ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1513 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1514 | *p++ = MBEDTLS_RSA_CRYPT; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1515 | |
| 1516 | while( nb_pad-- > 0 ) |
| 1517 | { |
| 1518 | int rng_dl = 100; |
| 1519 | |
| 1520 | do { |
| 1521 | ret = f_rng( p_rng, p, 1 ); |
| 1522 | } while( *p == 0 && --rng_dl && ret == 0 ); |
| 1523 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1524 | /* Check if RNG failed to generate data */ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1525 | if( rng_dl == 0 || ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1526 | return( MBEDTLS_ERR_RSA_RNG_FAILED + ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1527 | |
| 1528 | p++; |
| 1529 | } |
| 1530 | } |
| 1531 | else |
| 1532 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1533 | *p++ = MBEDTLS_RSA_SIGN; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1534 | |
| 1535 | while( nb_pad-- > 0 ) |
| 1536 | *p++ = 0xFF; |
| 1537 | } |
| 1538 | |
| 1539 | *p++ = 0; |
| 1540 | memcpy( p, input, ilen ); |
| 1541 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1542 | return( ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1543 | ? mbedtls_rsa_public( ctx, output, output ) |
| 1544 | : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1545 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1546 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1547 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1548 | /* |
| 1549 | * Add the message padding, then do an RSA operation |
| 1550 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1551 | int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1552 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 1553 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1554 | int mode, size_t ilen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1555 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1556 | unsigned char *output ) |
| 1557 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1558 | switch( ctx->padding ) |
| 1559 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1560 | #if defined(MBEDTLS_PKCS1_V15) |
| 1561 | case MBEDTLS_RSA_PKCS_V15: |
| 1562 | return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1563 | input, output ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1564 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1566 | #if defined(MBEDTLS_PKCS1_V21) |
| 1567 | case MBEDTLS_RSA_PKCS_V21: |
| 1568 | return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1569 | ilen, input, output ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1570 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1571 | |
| 1572 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1573 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1574 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1577 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1578 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1579 | * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1580 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1581 | int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1582 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1583 | void *p_rng, |
| 1584 | int mode, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 1585 | const unsigned char *label, size_t label_len, |
| 1586 | size_t *olen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1587 | const unsigned char *input, |
| 1588 | unsigned char *output, |
| 1589 | size_t output_max_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1590 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1591 | int ret; |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1592 | size_t ilen, i, pad_len; |
| 1593 | unsigned char *p, bad, pad_done; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1594 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
| 1595 | unsigned char lhash[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1596 | unsigned int hlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1597 | const mbedtls_md_info_t *md_info; |
| 1598 | mbedtls_md_context_t md_ctx; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1599 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1600 | /* |
| 1601 | * Parameters sanity checks |
| 1602 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1603 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 1604 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1605 | |
| 1606 | ilen = ctx->len; |
| 1607 | |
Paul Bakker | 27fdf46 | 2011-06-09 13:55:13 +0000 | [diff] [blame] | 1608 | if( ilen < 16 || ilen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1609 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1610 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1611 | md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id ); |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1612 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1613 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1614 | |
Janos Follath | c17cda1 | 2016-02-11 11:08:18 +0000 | [diff] [blame] | 1615 | hlen = mbedtls_md_get_size( md_info ); |
| 1616 | |
| 1617 | // checking for integer underflow |
| 1618 | if( 2 * hlen + 2 > ilen ) |
| 1619 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1620 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1621 | /* |
| 1622 | * RSA operation |
| 1623 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1624 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1625 | ? mbedtls_rsa_public( ctx, input, buf ) |
| 1626 | : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1627 | |
| 1628 | if( ret != 0 ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1629 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1630 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1631 | /* |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1632 | * Unmask data and generate lHash |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1633 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1634 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1635 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 1636 | { |
| 1637 | mbedtls_md_free( &md_ctx ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1638 | goto cleanup; |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1639 | } |
| 1640 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1641 | |
| 1642 | /* Generate lHash */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1643 | mbedtls_md( md_info, label, label_len, lhash ); |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1644 | |
| 1645 | /* seed: Apply seedMask to maskedSeed */ |
| 1646 | mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1, |
| 1647 | &md_ctx ); |
| 1648 | |
| 1649 | /* DB: Apply dbMask to maskedDB */ |
| 1650 | mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen, |
| 1651 | &md_ctx ); |
| 1652 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1653 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1654 | |
| 1655 | /* |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1656 | * Check contents, in "constant-time" |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1657 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1658 | p = buf; |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1659 | bad = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1660 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1661 | bad |= *p++; /* First byte must be 0 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1662 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1663 | p += hlen; /* Skip seed */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1664 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1665 | /* Check lHash */ |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1666 | for( i = 0; i < hlen; i++ ) |
| 1667 | bad |= lhash[i] ^ *p++; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1668 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1669 | /* Get zero-padding len, but always read till end of buffer |
| 1670 | * (minus one, for the 01 byte) */ |
| 1671 | pad_len = 0; |
| 1672 | pad_done = 0; |
| 1673 | for( i = 0; i < ilen - 2 * hlen - 2; i++ ) |
| 1674 | { |
| 1675 | pad_done |= p[i]; |
Pascal Junod | b99183d | 2015-03-11 16:49:45 +0100 | [diff] [blame] | 1676 | pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1; |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1677 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1678 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1679 | p += pad_len; |
| 1680 | bad |= *p++ ^ 0x01; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1681 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1682 | /* |
| 1683 | * The only information "leaked" is whether the padding was correct or not |
| 1684 | * (eg, no data is copied if it was not correct). This meets the |
| 1685 | * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between |
| 1686 | * the different error conditions. |
| 1687 | */ |
| 1688 | if( bad != 0 ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1689 | { |
| 1690 | ret = MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 1691 | goto cleanup; |
| 1692 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1693 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1694 | if( ilen - ( p - buf ) > output_max_len ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1695 | { |
| 1696 | ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
| 1697 | goto cleanup; |
| 1698 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1699 | |
| 1700 | *olen = ilen - (p - buf); |
| 1701 | memcpy( output, p, *olen ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1702 | ret = 0; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1703 | |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1704 | cleanup: |
| 1705 | mbedtls_zeroize( buf, sizeof( buf ) ); |
| 1706 | mbedtls_zeroize( lhash, sizeof( lhash ) ); |
| 1707 | |
| 1708 | return( ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1709 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1710 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1711 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1712 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1713 | /* |
| 1714 | * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function |
| 1715 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1716 | int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1717 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1718 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1719 | int mode, size_t *olen, |
| 1720 | const unsigned char *input, |
| 1721 | unsigned char *output, |
| 1722 | size_t output_max_len) |
| 1723 | { |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1724 | int ret; |
| 1725 | size_t ilen, pad_count = 0, i; |
| 1726 | unsigned char *p, bad, pad_done = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1727 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1728 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1729 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 1730 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1731 | |
| 1732 | ilen = ctx->len; |
| 1733 | |
| 1734 | if( ilen < 16 || ilen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1735 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1736 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1737 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1738 | ? mbedtls_rsa_public( ctx, input, buf ) |
| 1739 | : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1740 | |
| 1741 | if( ret != 0 ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1742 | goto cleanup; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1743 | |
| 1744 | p = buf; |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1745 | bad = 0; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1746 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1747 | /* |
| 1748 | * Check and get padding len in "constant-time" |
| 1749 | */ |
| 1750 | bad |= *p++; /* First byte must be 0 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1751 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1752 | /* This test does not depend on secret data */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1753 | if( mode == MBEDTLS_RSA_PRIVATE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1754 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1755 | bad |= *p++ ^ MBEDTLS_RSA_CRYPT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1756 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1757 | /* Get padding len, but always read till end of buffer |
| 1758 | * (minus one, for the 00 byte) */ |
| 1759 | for( i = 0; i < ilen - 3; i++ ) |
| 1760 | { |
Pascal Junod | b99183d | 2015-03-11 16:49:45 +0100 | [diff] [blame] | 1761 | pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1; |
| 1762 | pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1; |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1763 | } |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 1764 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1765 | p += pad_count; |
| 1766 | bad |= *p++; /* Must be zero */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1767 | } |
| 1768 | else |
| 1769 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1770 | bad |= *p++ ^ MBEDTLS_RSA_SIGN; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1771 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1772 | /* Get padding len, but always read till end of buffer |
| 1773 | * (minus one, for the 00 byte) */ |
| 1774 | for( i = 0; i < ilen - 3; i++ ) |
| 1775 | { |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 1776 | pad_done |= ( p[i] != 0xFF ); |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1777 | pad_count += ( pad_done == 0 ); |
| 1778 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1779 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1780 | p += pad_count; |
| 1781 | bad |= *p++; /* Must be zero */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
Janos Follath | c69fa50 | 2016-02-12 13:30:09 +0000 | [diff] [blame] | 1784 | bad |= ( pad_count < 8 ); |
Janos Follath | b6eb1ca | 2016-02-08 13:59:25 +0000 | [diff] [blame] | 1785 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1786 | if( bad ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1787 | { |
| 1788 | ret = MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 1789 | goto cleanup; |
| 1790 | } |
Paul Bakker | 8804f69 | 2013-02-28 18:06:26 +0100 | [diff] [blame] | 1791 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1792 | if( ilen - ( p - buf ) > output_max_len ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1793 | { |
| 1794 | ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
| 1795 | goto cleanup; |
| 1796 | } |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 1797 | |
Paul Bakker | 27fdf46 | 2011-06-09 13:55:13 +0000 | [diff] [blame] | 1798 | *olen = ilen - (p - buf); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1799 | memcpy( output, p, *olen ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1800 | ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1801 | |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1802 | cleanup: |
| 1803 | mbedtls_zeroize( buf, sizeof( buf ) ); |
| 1804 | |
| 1805 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1806 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1807 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1808 | |
| 1809 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1810 | * Do an RSA operation, then remove the message padding |
| 1811 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1812 | int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1813 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1814 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1815 | int mode, size_t *olen, |
| 1816 | const unsigned char *input, |
| 1817 | unsigned char *output, |
| 1818 | size_t output_max_len) |
| 1819 | { |
| 1820 | switch( ctx->padding ) |
| 1821 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1822 | #if defined(MBEDTLS_PKCS1_V15) |
| 1823 | case MBEDTLS_RSA_PKCS_V15: |
| 1824 | return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1825 | input, output, output_max_len ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1826 | #endif |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1827 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1828 | #if defined(MBEDTLS_PKCS1_V21) |
| 1829 | case MBEDTLS_RSA_PKCS_V21: |
| 1830 | return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1831 | olen, input, output, |
| 1832 | output_max_len ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1833 | #endif |
| 1834 | |
| 1835 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1836 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1837 | } |
| 1838 | } |
| 1839 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1840 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1841 | /* |
| 1842 | * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function |
| 1843 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1844 | int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1845 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1846 | void *p_rng, |
| 1847 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1848 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1849 | unsigned int hashlen, |
| 1850 | const unsigned char *hash, |
| 1851 | unsigned char *sig ) |
| 1852 | { |
| 1853 | size_t olen; |
| 1854 | unsigned char *p = sig; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1855 | unsigned char salt[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1856 | unsigned int slen, hlen, offset = 0; |
| 1857 | int ret; |
| 1858 | size_t msb; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1859 | const mbedtls_md_info_t *md_info; |
| 1860 | mbedtls_md_context_t md_ctx; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1861 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1862 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 1863 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 1864 | |
| 1865 | if( f_rng == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1866 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1867 | |
| 1868 | olen = ctx->len; |
| 1869 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1870 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1871 | { |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1872 | /* Gather length of hash to sign */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1873 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1874 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1875 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1876 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1877 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1878 | } |
| 1879 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1880 | md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1881 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1882 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1883 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1884 | hlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1885 | slen = hlen; |
| 1886 | |
| 1887 | if( olen < hlen + slen + 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1888 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1889 | |
| 1890 | memset( sig, 0, olen ); |
| 1891 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1892 | /* Generate salt of length slen */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1893 | if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1894 | return( MBEDTLS_ERR_RSA_RNG_FAILED + ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1895 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1896 | /* Note: EMSA-PSS encoding is over the length of N - 1 bits */ |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 1897 | msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1898 | p += olen - hlen * 2 - 2; |
| 1899 | *p++ = 0x01; |
| 1900 | memcpy( p, salt, slen ); |
| 1901 | p += slen; |
| 1902 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1903 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1904 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 1905 | { |
| 1906 | mbedtls_md_free( &md_ctx ); |
Gilles Peskine | 18ac716 | 2017-05-05 19:24:06 +0200 | [diff] [blame] | 1907 | /* No need to zeroize salt: we didn't use it. */ |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1908 | return( ret ); |
| 1909 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1910 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1911 | /* Generate H = Hash( M' ) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1912 | mbedtls_md_starts( &md_ctx ); |
| 1913 | mbedtls_md_update( &md_ctx, p, 8 ); |
| 1914 | mbedtls_md_update( &md_ctx, hash, hashlen ); |
| 1915 | mbedtls_md_update( &md_ctx, salt, slen ); |
| 1916 | mbedtls_md_finish( &md_ctx, p ); |
Gilles Peskine | 18ac716 | 2017-05-05 19:24:06 +0200 | [diff] [blame] | 1917 | mbedtls_zeroize( salt, sizeof( salt ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1918 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1919 | /* Compensate for boundary condition when applying mask */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1920 | if( msb % 8 == 0 ) |
| 1921 | offset = 1; |
| 1922 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1923 | /* maskedDB: Apply dbMask to DB */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1924 | mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx ); |
| 1925 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1926 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1927 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 1928 | msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1929 | sig[0] &= 0xFF >> ( olen * 8 - msb ); |
| 1930 | |
| 1931 | p += hlen; |
| 1932 | *p++ = 0xBC; |
| 1933 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1934 | return( ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1935 | ? mbedtls_rsa_public( ctx, sig, sig ) |
| 1936 | : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1937 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1938 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1939 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1940 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1941 | /* |
| 1942 | * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function |
| 1943 | */ |
| 1944 | /* |
| 1945 | * Do an RSA operation to sign the message digest |
| 1946 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1947 | int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1948 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1949 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1950 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1951 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1952 | unsigned int hashlen, |
| 1953 | const unsigned char *hash, |
| 1954 | unsigned char *sig ) |
| 1955 | { |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1956 | size_t nb_pad, olen, oid_size = 0; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1957 | unsigned char *p = sig; |
Paul Bakker | 21e081b | 2014-07-24 10:38:01 +0200 | [diff] [blame] | 1958 | const char *oid = NULL; |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 1959 | unsigned char *sig_try = NULL, *verif = NULL; |
| 1960 | size_t i; |
| 1961 | unsigned char diff; |
| 1962 | volatile unsigned char diff_no_optimize; |
| 1963 | int ret; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1964 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1965 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 1966 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1967 | |
| 1968 | olen = ctx->len; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1969 | nb_pad = olen - 3; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1970 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1971 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1972 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1973 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1974 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1975 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1976 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1977 | if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 ) |
| 1978 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1979 | |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1980 | nb_pad -= 10 + oid_size; |
| 1981 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1982 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1983 | } |
| 1984 | |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1985 | nb_pad -= hashlen; |
| 1986 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1987 | if( ( nb_pad < 8 ) || ( nb_pad > olen ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1988 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1989 | |
| 1990 | *p++ = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1991 | *p++ = MBEDTLS_RSA_SIGN; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1992 | memset( p, 0xFF, nb_pad ); |
| 1993 | p += nb_pad; |
| 1994 | *p++ = 0; |
| 1995 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1996 | if( md_alg == MBEDTLS_MD_NONE ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1997 | { |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1998 | memcpy( p, hash, hashlen ); |
| 1999 | } |
| 2000 | else |
| 2001 | { |
| 2002 | /* |
| 2003 | * DigestInfo ::= SEQUENCE { |
| 2004 | * digestAlgorithm DigestAlgorithmIdentifier, |
| 2005 | * digest Digest } |
| 2006 | * |
| 2007 | * DigestAlgorithmIdentifier ::= AlgorithmIdentifier |
| 2008 | * |
| 2009 | * Digest ::= OCTET STRING |
| 2010 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2011 | *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2012 | *p++ = (unsigned char) ( 0x08 + oid_size + hashlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2013 | *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2014 | *p++ = (unsigned char) ( 0x04 + oid_size ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2015 | *p++ = MBEDTLS_ASN1_OID; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 2016 | *p++ = oid_size & 0xFF; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2017 | memcpy( p, oid, oid_size ); |
| 2018 | p += oid_size; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2019 | *p++ = MBEDTLS_ASN1_NULL; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2020 | *p++ = 0x00; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2021 | *p++ = MBEDTLS_ASN1_OCTET_STRING; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2022 | *p++ = hashlen; |
| 2023 | memcpy( p, hash, hashlen ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2024 | } |
| 2025 | |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 2026 | if( mode == MBEDTLS_RSA_PUBLIC ) |
| 2027 | return( mbedtls_rsa_public( ctx, sig, sig ) ); |
| 2028 | |
| 2029 | /* |
| 2030 | * In order to prevent Lenstra's attack, make the signature in a |
| 2031 | * temporary buffer and check it before returning it. |
| 2032 | */ |
| 2033 | sig_try = mbedtls_calloc( 1, ctx->len ); |
Simon Butcher | 1285ab5 | 2016-01-01 21:42:47 +0000 | [diff] [blame] | 2034 | if( sig_try == NULL ) |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 2035 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 2036 | |
Simon Butcher | 1285ab5 | 2016-01-01 21:42:47 +0000 | [diff] [blame] | 2037 | verif = mbedtls_calloc( 1, ctx->len ); |
| 2038 | if( verif == NULL ) |
| 2039 | { |
| 2040 | mbedtls_free( sig_try ); |
| 2041 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 2042 | } |
| 2043 | |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 2044 | MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) ); |
| 2045 | MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) ); |
| 2046 | |
| 2047 | /* Compare in constant time just in case */ |
| 2048 | for( diff = 0, i = 0; i < ctx->len; i++ ) |
| 2049 | diff |= verif[i] ^ sig[i]; |
| 2050 | diff_no_optimize = diff; |
| 2051 | |
| 2052 | if( diff_no_optimize != 0 ) |
| 2053 | { |
| 2054 | ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED; |
| 2055 | goto cleanup; |
| 2056 | } |
| 2057 | |
| 2058 | memcpy( sig, sig_try, ctx->len ); |
| 2059 | |
| 2060 | cleanup: |
| 2061 | mbedtls_free( sig_try ); |
| 2062 | mbedtls_free( verif ); |
| 2063 | |
| 2064 | return( ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2065 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2066 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2067 | |
| 2068 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2069 | * Do an RSA operation to sign the message digest |
| 2070 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2071 | int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2072 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2073 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2074 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2075 | mbedtls_md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2076 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2077 | const unsigned char *hash, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2078 | unsigned char *sig ) |
| 2079 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2080 | switch( ctx->padding ) |
| 2081 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2082 | #if defined(MBEDTLS_PKCS1_V15) |
| 2083 | case MBEDTLS_RSA_PKCS_V15: |
| 2084 | return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2085 | hashlen, hash, sig ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 2086 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2087 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2088 | #if defined(MBEDTLS_PKCS1_V21) |
| 2089 | case MBEDTLS_RSA_PKCS_V21: |
| 2090 | return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2091 | hashlen, hash, sig ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2092 | #endif |
| 2093 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2094 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2095 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2096 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2097 | } |
| 2098 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2099 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2100 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2101 | * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2102 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2103 | int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2104 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2105 | void *p_rng, |
| 2106 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2107 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2108 | unsigned int hashlen, |
| 2109 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2110 | mbedtls_md_type_t mgf1_hash_id, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2111 | int expected_salt_len, |
| 2112 | const unsigned char *sig ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2113 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2114 | int ret; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2115 | size_t siglen; |
| 2116 | unsigned char *p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2117 | unsigned char result[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2118 | unsigned char zeros[8]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2119 | unsigned int hlen; |
| 2120 | size_t slen, msb; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2121 | const mbedtls_md_info_t *md_info; |
| 2122 | mbedtls_md_context_t md_ctx; |
Nicholas Wilson | 409401c | 2016-04-13 11:48:25 +0100 | [diff] [blame] | 2123 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2124 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2125 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 2126 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2127 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2128 | siglen = ctx->len; |
| 2129 | |
Paul Bakker | 27fdf46 | 2011-06-09 13:55:13 +0000 | [diff] [blame] | 2130 | if( siglen < 16 || siglen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2131 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2132 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2133 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 2134 | ? mbedtls_rsa_public( ctx, sig, buf ) |
| 2135 | : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2136 | |
| 2137 | if( ret != 0 ) |
| 2138 | return( ret ); |
| 2139 | |
| 2140 | p = buf; |
| 2141 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2142 | if( buf[siglen - 1] != 0xBC ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2143 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2144 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2145 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2146 | { |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 2147 | /* Gather length of hash to sign */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2148 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2149 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2150 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2151 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2152 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2153 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2154 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2155 | md_info = mbedtls_md_info_from_type( mgf1_hash_id ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2156 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2157 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2159 | hlen = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2160 | slen = siglen - hlen - 1; /* Currently length of salt + padding */ |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2161 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2162 | memset( zeros, 0, 8 ); |
Paul Bakker | 53019ae | 2011-03-25 13:58:48 +0000 | [diff] [blame] | 2163 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 2164 | /* |
| 2165 | * Note: EMSA-PSS verification is over the length of N - 1 bits |
| 2166 | */ |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 2167 | msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2168 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 2169 | /* Compensate for boundary condition when applying mask */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2170 | if( msb % 8 == 0 ) |
| 2171 | { |
| 2172 | p++; |
| 2173 | siglen -= 1; |
| 2174 | } |
| 2175 | if( buf[0] >> ( 8 - siglen * 8 + msb ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2176 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2177 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2178 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 2179 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 2180 | { |
| 2181 | mbedtls_md_free( &md_ctx ); |
| 2182 | return( ret ); |
| 2183 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2184 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2185 | mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx ); |
Paul Bakker | 02303e8 | 2013-01-03 11:08:31 +0100 | [diff] [blame] | 2186 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2187 | buf[0] &= 0xFF >> ( siglen * 8 - msb ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2188 | |
Paul Bakker | 4de44aa | 2013-12-31 11:43:01 +0100 | [diff] [blame] | 2189 | while( p < buf + siglen && *p == 0 ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2190 | p++; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2191 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2192 | if( p == buf + siglen || |
| 2193 | *p++ != 0x01 ) |
| 2194 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2195 | mbedtls_md_free( &md_ctx ); |
| 2196 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2197 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2198 | |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2199 | /* Actual salt len */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2200 | slen -= p - buf; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2201 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2202 | if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY && |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2203 | slen != (size_t) expected_salt_len ) |
| 2204 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2205 | mbedtls_md_free( &md_ctx ); |
| 2206 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2207 | } |
| 2208 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 2209 | /* |
| 2210 | * Generate H = Hash( M' ) |
| 2211 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2212 | mbedtls_md_starts( &md_ctx ); |
| 2213 | mbedtls_md_update( &md_ctx, zeros, 8 ); |
| 2214 | mbedtls_md_update( &md_ctx, hash, hashlen ); |
| 2215 | mbedtls_md_update( &md_ctx, p, slen ); |
| 2216 | mbedtls_md_finish( &md_ctx, result ); |
Paul Bakker | 53019ae | 2011-03-25 13:58:48 +0000 | [diff] [blame] | 2217 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2218 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2219 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2220 | if( memcmp( p + slen, result, hlen ) == 0 ) |
| 2221 | return( 0 ); |
| 2222 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2223 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2224 | } |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2225 | |
| 2226 | /* |
| 2227 | * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function |
| 2228 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2229 | int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2230 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2231 | void *p_rng, |
| 2232 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2233 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2234 | unsigned int hashlen, |
| 2235 | const unsigned char *hash, |
| 2236 | const unsigned char *sig ) |
| 2237 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2238 | mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE ) |
| 2239 | ? (mbedtls_md_type_t) ctx->hash_id |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2240 | : md_alg; |
| 2241 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2242 | return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2243 | md_alg, hashlen, hash, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2244 | mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2245 | sig ) ); |
| 2246 | |
| 2247 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2248 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | 40628ba | 2013-01-03 10:50:31 +0100 | [diff] [blame] | 2249 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2250 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2251 | /* |
| 2252 | * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function |
| 2253 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2254 | int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 2255 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2256 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2257 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2258 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2259 | unsigned int hashlen, |
| 2260 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 2261 | const unsigned char *sig ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2262 | { |
| 2263 | int ret; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2264 | size_t len, siglen, asn1_len; |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 2265 | unsigned char *p, *p0, *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2266 | mbedtls_md_type_t msg_md_alg; |
| 2267 | const mbedtls_md_info_t *md_info; |
| 2268 | mbedtls_asn1_buf oid; |
Nicholas Wilson | 409401c | 2016-04-13 11:48:25 +0100 | [diff] [blame] | 2269 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2270 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2271 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 2272 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2273 | |
| 2274 | siglen = ctx->len; |
| 2275 | |
| 2276 | if( siglen < 16 || siglen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2277 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2278 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2279 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 2280 | ? mbedtls_rsa_public( ctx, sig, buf ) |
| 2281 | : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2282 | |
| 2283 | if( ret != 0 ) |
| 2284 | return( ret ); |
| 2285 | |
| 2286 | p = buf; |
| 2287 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2288 | if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN ) |
| 2289 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2290 | |
| 2291 | while( *p != 0 ) |
| 2292 | { |
| 2293 | if( p >= buf + siglen - 1 || *p != 0xFF ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2294 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2295 | p++; |
| 2296 | } |
Manuel Pégourié-Gonnard | c1380de | 2017-05-11 12:49:51 +0200 | [diff] [blame] | 2297 | p++; /* skip 00 byte */ |
| 2298 | |
| 2299 | /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */ |
| 2300 | if( p - buf < 11 ) |
| 2301 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2302 | |
| 2303 | len = siglen - ( p - buf ); |
| 2304 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2305 | if( len == hashlen && md_alg == MBEDTLS_MD_NONE ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2306 | { |
| 2307 | if( memcmp( p, hash, hashlen ) == 0 ) |
| 2308 | return( 0 ); |
| 2309 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2310 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2311 | } |
| 2312 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2313 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2314 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2315 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 2316 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2317 | |
| 2318 | end = p + len; |
| 2319 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 2320 | /* |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 2321 | * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure. |
| 2322 | * Insist on 2-byte length tags, to protect against variants of |
| 2323 | * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification. |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 2324 | */ |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 2325 | p0 = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2326 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, |
| 2327 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 2328 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 2329 | if( p != p0 + 2 || asn1_len + 2 != len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2330 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2331 | |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 2332 | p0 = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2333 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, |
| 2334 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 2335 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 2336 | if( p != p0 + 2 || asn1_len + 6 + hashlen != len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2337 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2338 | |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 2339 | p0 = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2340 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 ) |
| 2341 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 2342 | if( p != p0 + 2 ) |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 2343 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2344 | |
| 2345 | oid.p = p; |
| 2346 | p += oid.len; |
| 2347 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2348 | if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 ) |
| 2349 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2350 | |
| 2351 | if( md_alg != msg_md_alg ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2352 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2353 | |
| 2354 | /* |
| 2355 | * assume the algorithm parameters must be NULL |
| 2356 | */ |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 2357 | p0 = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2358 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 ) |
| 2359 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 2360 | if( p != p0 + 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2361 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2362 | |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 2363 | p0 = p; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2364 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
| 2365 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 2366 | if( p != p0 + 2 || asn1_len != hashlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2367 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2368 | |
| 2369 | if( memcmp( p, hash, hashlen ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2370 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2371 | |
| 2372 | p += hashlen; |
| 2373 | |
| 2374 | if( p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2375 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2376 | |
| 2377 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2378 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2379 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2380 | |
| 2381 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2382 | * Do an RSA operation and check the message digest |
| 2383 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2384 | int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 2385 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2386 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2387 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2388 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2389 | unsigned int hashlen, |
| 2390 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 2391 | const unsigned char *sig ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2392 | { |
| 2393 | switch( ctx->padding ) |
| 2394 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2395 | #if defined(MBEDTLS_PKCS1_V15) |
| 2396 | case MBEDTLS_RSA_PKCS_V15: |
| 2397 | return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2398 | hashlen, hash, sig ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 2399 | #endif |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2400 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2401 | #if defined(MBEDTLS_PKCS1_V21) |
| 2402 | case MBEDTLS_RSA_PKCS_V21: |
| 2403 | return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2404 | hashlen, hash, sig ); |
| 2405 | #endif |
| 2406 | |
| 2407 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2408 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2409 | } |
| 2410 | } |
| 2411 | |
| 2412 | /* |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2413 | * Copy the components of an RSA key |
| 2414 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2415 | int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src ) |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2416 | { |
| 2417 | int ret; |
| 2418 | |
| 2419 | dst->ver = src->ver; |
| 2420 | dst->len = src->len; |
| 2421 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2422 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) ); |
| 2423 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2424 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2425 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) ); |
| 2426 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) ); |
| 2427 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) ); |
Hanno Becker | 33c30a0 | 2017-08-23 07:00:22 +0100 | [diff] [blame^] | 2428 | |
| 2429 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2430 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) ); |
| 2431 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) ); |
| 2432 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2433 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) ); |
| 2434 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) ); |
Hanno Becker | 33c30a0 | 2017-08-23 07:00:22 +0100 | [diff] [blame^] | 2435 | #endif |
| 2436 | |
| 2437 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2438 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2439 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) ); |
| 2440 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) ); |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 2441 | |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2442 | dst->padding = src->padding; |
Manuel Pégourié-Gonnard | fdddac9 | 2014-03-25 15:58:35 +0100 | [diff] [blame] | 2443 | dst->hash_id = src->hash_id; |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2444 | |
| 2445 | cleanup: |
| 2446 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2447 | mbedtls_rsa_free( dst ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2448 | |
| 2449 | return( ret ); |
| 2450 | } |
| 2451 | |
| 2452 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2453 | * Free the components of an RSA key |
| 2454 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2455 | void mbedtls_rsa_free( mbedtls_rsa_context *ctx ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2456 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2457 | mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf ); |
Hanno Becker | 33c30a0 | 2017-08-23 07:00:22 +0100 | [diff] [blame^] | 2458 | mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D ); |
| 2459 | mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2460 | mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 2461 | |
Hanno Becker | 33c30a0 | 2017-08-23 07:00:22 +0100 | [diff] [blame^] | 2462 | #if !defined(MBEDTLS_RSA_NO_CRT) |
| 2463 | mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); |
| 2464 | mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); |
| 2465 | mbedtls_mpi_free( &ctx->DP ); |
| 2466 | #endif /* MBEDTLS_RSA_NO_CRT */ |
| 2467 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2468 | #if defined(MBEDTLS_THREADING_C) |
| 2469 | mbedtls_mutex_free( &ctx->mutex ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 2470 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2473 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2474 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2475 | #include "mbedtls/sha1.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2476 | |
| 2477 | /* |
| 2478 | * Example RSA-1024 keypair, for test purposes |
| 2479 | */ |
| 2480 | #define KEY_LEN 128 |
| 2481 | |
| 2482 | #define RSA_N "9292758453063D803DD603D5E777D788" \ |
| 2483 | "8ED1D5BF35786190FA2F23EBC0848AEA" \ |
| 2484 | "DDA92CA6C3D80B32C4D109BE0F36D6AE" \ |
| 2485 | "7130B9CED7ACDF54CFC7555AC14EEBAB" \ |
| 2486 | "93A89813FBF3C4F8066D2D800F7C38A8" \ |
| 2487 | "1AE31942917403FF4946B0A83D3D3E05" \ |
| 2488 | "EE57C6F5F5606FB5D4BC6CD34EE0801A" \ |
| 2489 | "5E94BB77B07507233A0BC7BAC8F90F79" |
| 2490 | |
| 2491 | #define RSA_E "10001" |
| 2492 | |
| 2493 | #define RSA_D "24BF6185468786FDD303083D25E64EFC" \ |
| 2494 | "66CA472BC44D253102F8B4A9D3BFA750" \ |
| 2495 | "91386C0077937FE33FA3252D28855837" \ |
| 2496 | "AE1B484A8A9A45F7EE8C0C634F99E8CD" \ |
| 2497 | "DF79C5CE07EE72C7F123142198164234" \ |
| 2498 | "CABB724CF78B8173B9F880FC86322407" \ |
| 2499 | "AF1FEDFDDE2BEB674CA15F3E81A1521E" \ |
| 2500 | "071513A1E85B5DFA031F21ECAE91A34D" |
| 2501 | |
| 2502 | #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \ |
| 2503 | "2C01CAD19EA484A87EA4377637E75500" \ |
| 2504 | "FCB2005C5C7DD6EC4AC023CDA285D796" \ |
| 2505 | "C3D9E75E1EFC42488BB4F1D13AC30A57" |
| 2506 | |
| 2507 | #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \ |
| 2508 | "E211C2B9E5DB1ED0BF61D0D9899620F4" \ |
| 2509 | "910E4168387E3C30AA1E00C339A79508" \ |
| 2510 | "8452DD96A9A5EA5D9DCA68DA636032AF" |
| 2511 | |
| 2512 | #define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \ |
| 2513 | "3C94D22288ACD763FD8E5600ED4A702D" \ |
| 2514 | "F84198A5F06C2E72236AE490C93F07F8" \ |
| 2515 | "3CC559CD27BC2D1CA488811730BB5725" |
| 2516 | |
| 2517 | #define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \ |
| 2518 | "D8AAEA56749EA28623272E4F7D0592AF" \ |
| 2519 | "7C1F1313CAC9471B5C523BFE592F517B" \ |
| 2520 | "407A1BD76C164B93DA2D32A383E58357" |
| 2521 | |
| 2522 | #define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \ |
| 2523 | "F38D18D2B2F0E2DD275AA977E2BF4411" \ |
| 2524 | "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \ |
| 2525 | "A74206CEC169D74BF5A8C50D6F48EA08" |
| 2526 | |
| 2527 | #define PT_LEN 24 |
| 2528 | #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \ |
| 2529 | "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD" |
| 2530 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2531 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2532 | static int myrand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 2533 | { |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 2534 | #if !defined(__OpenBSD__) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2535 | size_t i; |
| 2536 | |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 2537 | if( rng_state != NULL ) |
| 2538 | rng_state = NULL; |
| 2539 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2540 | for( i = 0; i < len; ++i ) |
| 2541 | output[i] = rand(); |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 2542 | #else |
| 2543 | if( rng_state != NULL ) |
| 2544 | rng_state = NULL; |
| 2545 | |
| 2546 | arc4random_buf( output, len ); |
| 2547 | #endif /* !OpenBSD */ |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 2548 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2549 | return( 0 ); |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 2550 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2551 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 2552 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2553 | /* |
| 2554 | * Checkup routine |
| 2555 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2556 | int mbedtls_rsa_self_test( int verbose ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2557 | { |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 2558 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2559 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2560 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2561 | mbedtls_rsa_context rsa; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2562 | unsigned char rsa_plaintext[PT_LEN]; |
| 2563 | unsigned char rsa_decrypted[PT_LEN]; |
| 2564 | unsigned char rsa_ciphertext[KEY_LEN]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2565 | #if defined(MBEDTLS_SHA1_C) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 2566 | unsigned char sha1sum[20]; |
| 2567 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2568 | |
Hanno Becker | 3a70116 | 2017-08-22 13:52:43 +0100 | [diff] [blame] | 2569 | mbedtls_mpi K; |
| 2570 | |
| 2571 | mbedtls_mpi_init( &K ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2572 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2573 | |
Hanno Becker | 3a70116 | 2017-08-22 13:52:43 +0100 | [diff] [blame] | 2574 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) ); |
| 2575 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) ); |
| 2576 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) ); |
| 2577 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) ); |
| 2578 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) ); |
| 2579 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) ); |
| 2580 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) ); |
| 2581 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) ); |
| 2582 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) ); |
| 2583 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) ); |
| 2584 | |
| 2585 | MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa, NULL, NULL ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2586 | |
| 2587 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2588 | mbedtls_printf( " RSA key validation: " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2589 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2590 | if( mbedtls_rsa_check_pubkey( &rsa ) != 0 || |
| 2591 | mbedtls_rsa_check_privkey( &rsa ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2592 | { |
| 2593 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2594 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2595 | |
| 2596 | return( 1 ); |
| 2597 | } |
| 2598 | |
Hanno Becker | 3a70116 | 2017-08-22 13:52:43 +0100 | [diff] [blame] | 2599 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DP ) ); |
| 2600 | MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, &K, NULL, NULL ) ); |
| 2601 | |
| 2602 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_DQ ) ); |
| 2603 | MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, &K, NULL ) ); |
| 2604 | |
| 2605 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_QP ) ); |
| 2606 | MBEDTLS_MPI_CHK( mbedtls_rsa_check_crt( &rsa, NULL, NULL, &K ) ); |
| 2607 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2608 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2609 | mbedtls_printf( "passed\n PKCS#1 encryption : " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2610 | |
| 2611 | memcpy( rsa_plaintext, RSA_PT, PT_LEN ); |
| 2612 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2613 | if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2614 | rsa_plaintext, rsa_ciphertext ) != 0 ) |
| 2615 | { |
| 2616 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2617 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2618 | |
| 2619 | return( 1 ); |
| 2620 | } |
| 2621 | |
| 2622 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2623 | mbedtls_printf( "passed\n PKCS#1 decryption : " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2624 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2625 | if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len, |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 2626 | rsa_ciphertext, rsa_decrypted, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2627 | sizeof(rsa_decrypted) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2628 | { |
| 2629 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2630 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2631 | |
| 2632 | return( 1 ); |
| 2633 | } |
| 2634 | |
| 2635 | if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 ) |
| 2636 | { |
| 2637 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2638 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2639 | |
| 2640 | return( 1 ); |
| 2641 | } |
| 2642 | |
Manuel Pégourié-Gonnard | d1004f0 | 2015-08-07 10:46:54 +0200 | [diff] [blame] | 2643 | if( verbose != 0 ) |
| 2644 | mbedtls_printf( "passed\n" ); |
| 2645 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2646 | #if defined(MBEDTLS_SHA1_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2647 | if( verbose != 0 ) |
Brian Murray | 930a370 | 2016-05-18 14:38:02 -0700 | [diff] [blame] | 2648 | mbedtls_printf( " PKCS#1 data sign : " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2649 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2650 | mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2651 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2652 | if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2653 | sha1sum, rsa_ciphertext ) != 0 ) |
| 2654 | { |
| 2655 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2656 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2657 | |
| 2658 | return( 1 ); |
| 2659 | } |
| 2660 | |
| 2661 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2662 | mbedtls_printf( "passed\n PKCS#1 sig. verify: " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2664 | if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2665 | sha1sum, rsa_ciphertext ) != 0 ) |
| 2666 | { |
| 2667 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2668 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2669 | |
| 2670 | return( 1 ); |
| 2671 | } |
| 2672 | |
| 2673 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | d1004f0 | 2015-08-07 10:46:54 +0200 | [diff] [blame] | 2674 | mbedtls_printf( "passed\n" ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2675 | #endif /* MBEDTLS_SHA1_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2676 | |
Manuel Pégourié-Gonnard | d1004f0 | 2015-08-07 10:46:54 +0200 | [diff] [blame] | 2677 | if( verbose != 0 ) |
| 2678 | mbedtls_printf( "\n" ); |
| 2679 | |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 2680 | cleanup: |
Hanno Becker | 3a70116 | 2017-08-22 13:52:43 +0100 | [diff] [blame] | 2681 | mbedtls_mpi_free( &K ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2682 | mbedtls_rsa_free( &rsa ); |
| 2683 | #else /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 3e41fe8 | 2013-09-15 17:42:50 +0200 | [diff] [blame] | 2684 | ((void) verbose); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2685 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 2686 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2687 | } |
| 2688 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2689 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2690 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2691 | #endif /* MBEDTLS_RSA_C */ |