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 | */ |
Hanno Becker | 7471631 | 2017-10-02 10:00:37 +0100 | [diff] [blame] | 21 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 22 | /* |
Simon Butcher | bdae02c | 2016-01-20 00:44:42 +0000 | [diff] [blame] | 23 | * The following sources were referenced in the design of this implementation |
| 24 | * of the RSA algorithm: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 | * |
Simon Butcher | bdae02c | 2016-01-20 00:44:42 +0000 | [diff] [blame] | 26 | * [1] A method for obtaining digital signatures and public-key cryptosystems |
| 27 | * R Rivest, A Shamir, and L Adleman |
| 28 | * http://people.csail.mit.edu/rivest/pubs.html#RSA78 |
| 29 | * |
| 30 | * [2] Handbook of Applied Cryptography - 1997, Chapter 8 |
| 31 | * Menezes, van Oorschot and Vanstone |
| 32 | * |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 33 | * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks |
| 34 | * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and |
| 35 | * Stefan Mangard |
| 36 | * https://arxiv.org/abs/1702.08719v2 |
| 37 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | */ |
| 39 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 41 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 42 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 44 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #if defined(MBEDTLS_RSA_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 48 | #include "mbedtls/rsa.h" |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 49 | #include "mbedtls/rsa_internal.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 50 | #include "mbedtls/oid.h" |
Paul Bakker | bb51f0c | 2012-08-23 07:46:58 +0000 | [diff] [blame] | 51 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 52 | #include <string.h> |
| 53 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #if defined(MBEDTLS_PKCS1_V21) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 55 | #include "mbedtls/md.h" |
Paul Bakker | bb51f0c | 2012-08-23 07:46:58 +0000 | [diff] [blame] | 56 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | #if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | #include <stdlib.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 60 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 62 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 63 | #include "mbedtls/platform.h" |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 64 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 65 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 67 | #define mbedtls_calloc calloc |
| 68 | #define mbedtls_free free |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 69 | #endif |
| 70 | |
Hanno Becker | a565f54 | 2017-10-11 11:00:19 +0100 | [diff] [blame] | 71 | #if !defined(MBEDTLS_RSA_ALT) |
| 72 | |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 73 | /* Implementation that should never be optimized out by the compiler */ |
| 74 | static void mbedtls_zeroize( void *v, size_t n ) { |
| 75 | volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; |
| 76 | } |
| 77 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 78 | int mbedtls_rsa_import( mbedtls_rsa_context *ctx, |
| 79 | const mbedtls_mpi *N, |
| 80 | const mbedtls_mpi *P, const mbedtls_mpi *Q, |
| 81 | const mbedtls_mpi *D, const mbedtls_mpi *E ) |
| 82 | { |
| 83 | int ret; |
| 84 | |
| 85 | if( ( N != NULL && ( ret = mbedtls_mpi_copy( &ctx->N, N ) ) != 0 ) || |
| 86 | ( P != NULL && ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ) || |
| 87 | ( Q != NULL && ( ret = mbedtls_mpi_copy( &ctx->Q, Q ) ) != 0 ) || |
| 88 | ( D != NULL && ( ret = mbedtls_mpi_copy( &ctx->D, D ) ) != 0 ) || |
| 89 | ( E != NULL && ( ret = mbedtls_mpi_copy( &ctx->E, E ) ) != 0 ) ) |
| 90 | { |
| 91 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 92 | } |
| 93 | |
| 94 | if( N != NULL ) |
| 95 | ctx->len = mbedtls_mpi_size( &ctx->N ); |
| 96 | |
| 97 | return( 0 ); |
| 98 | } |
| 99 | |
| 100 | int mbedtls_rsa_import_raw( mbedtls_rsa_context *ctx, |
Hanno Becker | 7471631 | 2017-10-02 10:00:37 +0100 | [diff] [blame] | 101 | unsigned char const *N, size_t N_len, |
| 102 | unsigned char const *P, size_t P_len, |
| 103 | unsigned char const *Q, size_t Q_len, |
| 104 | unsigned char const *D, size_t D_len, |
| 105 | unsigned char const *E, size_t E_len ) |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 106 | { |
| 107 | int ret; |
| 108 | |
| 109 | if( N != NULL ) |
| 110 | { |
| 111 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->N, N, N_len ) ); |
| 112 | ctx->len = mbedtls_mpi_size( &ctx->N ); |
| 113 | } |
| 114 | |
| 115 | if( P != NULL ) |
| 116 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->P, P, P_len ) ); |
| 117 | |
| 118 | if( Q != NULL ) |
| 119 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->Q, Q, Q_len ) ); |
| 120 | |
| 121 | if( D != NULL ) |
| 122 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->D, D, D_len ) ); |
| 123 | |
| 124 | if( E != NULL ) |
| 125 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->E, E, E_len ) ); |
| 126 | |
| 127 | cleanup: |
| 128 | |
| 129 | if( ret != 0 ) |
| 130 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 131 | |
| 132 | return( 0 ); |
| 133 | } |
| 134 | |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 135 | /* |
| 136 | * Checks whether the context fields are set in such a way |
| 137 | * that the RSA primitives will be able to execute without error. |
| 138 | * It does *not* make guarantees for consistency of the parameters. |
| 139 | */ |
Hanno Becker | ebd2c02 | 2017-10-12 10:54:53 +0100 | [diff] [blame^] | 140 | static int rsa_check_context( mbedtls_rsa_context const *ctx, int is_priv, |
| 141 | int blinding_needed ) |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 142 | { |
Hanno Becker | ebd2c02 | 2017-10-12 10:54:53 +0100 | [diff] [blame^] | 143 | #if !defined(MBEDTLS_RSA_NO_CRT) |
| 144 | /* blinding_needed is only used for NO_CRT to decide whether |
| 145 | * P,Q need to be present or not. */ |
| 146 | ((void) blinding_needed); |
| 147 | #endif |
| 148 | |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 149 | if( ctx->len != mbedtls_mpi_size( &ctx->N ) ) |
| 150 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 151 | |
| 152 | /* |
| 153 | * 1. Modular exponentiation needs positive, odd moduli. |
| 154 | */ |
| 155 | |
| 156 | /* Modular exponentiation wrt. N is always used for |
| 157 | * RSA public key operations. */ |
| 158 | if( mbedtls_mpi_cmp_int( &ctx->N, 0 ) <= 0 || |
| 159 | mbedtls_mpi_get_bit( &ctx->N, 0 ) == 0 ) |
| 160 | { |
| 161 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 162 | } |
| 163 | |
| 164 | #if !defined(MBEDTLS_RSA_NO_CRT) |
| 165 | /* Modular exponentiation for P and Q is only |
| 166 | * used for private key operations and if CRT |
| 167 | * is used. */ |
| 168 | if( is_priv && |
| 169 | ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 || |
| 170 | mbedtls_mpi_get_bit( &ctx->P, 0 ) == 0 || |
| 171 | mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 || |
| 172 | mbedtls_mpi_get_bit( &ctx->Q, 0 ) == 0 ) ) |
| 173 | { |
| 174 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 175 | } |
| 176 | #endif /* !MBEDTLS_RSA_NO_CRT */ |
| 177 | |
| 178 | /* |
| 179 | * 2. Exponents must be positive |
| 180 | */ |
| 181 | |
| 182 | /* Always need E for public key operations */ |
| 183 | if( mbedtls_mpi_cmp_int( &ctx->E, 0 ) <= 0 ) |
| 184 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 185 | |
Hanno Becker | b82a5b5 | 2017-10-11 19:10:23 +0100 | [diff] [blame] | 186 | #if defined(MBEDTLS_RSA_NO_CRT) |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 187 | /* For private key operations, use D or DP & DQ |
| 188 | * as (unblinded) exponents. */ |
| 189 | if( is_priv && mbedtls_mpi_cmp_int( &ctx->D, 0 ) <= 0 ) |
| 190 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 191 | #else |
| 192 | if( is_priv && |
| 193 | ( mbedtls_mpi_cmp_int( &ctx->DP, 0 ) <= 0 || |
| 194 | mbedtls_mpi_cmp_int( &ctx->DQ, 0 ) <= 0 ) ) |
| 195 | { |
| 196 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 197 | } |
| 198 | #endif /* MBEDTLS_RSA_NO_CRT */ |
| 199 | |
| 200 | /* Blinding shouldn't make exponents negative either, |
| 201 | * so check that P, Q >= 1 if that hasn't yet been |
| 202 | * done as part of 1. */ |
Hanno Becker | b82a5b5 | 2017-10-11 19:10:23 +0100 | [diff] [blame] | 203 | #if defined(MBEDTLS_RSA_NO_CRT) |
Hanno Becker | ebd2c02 | 2017-10-12 10:54:53 +0100 | [diff] [blame^] | 204 | if( is_priv && blinding_needed && |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 205 | ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) <= 0 || |
| 206 | mbedtls_mpi_cmp_int( &ctx->Q, 0 ) <= 0 ) ) |
| 207 | { |
| 208 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | /* It wouldn't lead to an error if it wasn't satisfied, |
| 213 | * but check for PQ >= 1 nonetheless. */ |
Hanno Becker | b82a5b5 | 2017-10-11 19:10:23 +0100 | [diff] [blame] | 214 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 215 | if( is_priv && |
| 216 | mbedtls_mpi_cmp_int( &ctx->QP, 0 ) <= 0 ) |
| 217 | { |
| 218 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 219 | } |
| 220 | #endif |
| 221 | |
| 222 | return( 0 ); |
| 223 | } |
| 224 | |
Hanno Becker | f9e184b | 2017-10-10 16:49:26 +0100 | [diff] [blame] | 225 | int mbedtls_rsa_complete( mbedtls_rsa_context *ctx ) |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 226 | { |
| 227 | int ret = 0; |
| 228 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 229 | const int have_N = ( mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 ); |
| 230 | const int have_P = ( mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 ); |
| 231 | const int have_Q = ( mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 ); |
| 232 | const int have_D = ( mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 ); |
| 233 | const int have_E = ( mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0 ); |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 234 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 235 | /* |
| 236 | * Check whether provided parameters are enough |
| 237 | * to deduce all others. The following incomplete |
| 238 | * parameter sets for private keys are supported: |
| 239 | * |
| 240 | * (1) P, Q missing. |
| 241 | * (2) D and potentially N missing. |
| 242 | * |
| 243 | */ |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 244 | |
Hanno Becker | 2cca6f3 | 2017-09-29 11:46:40 +0100 | [diff] [blame] | 245 | const int n_missing = have_P && have_Q && have_D && have_E; |
| 246 | const int pq_missing = have_N && !have_P && !have_Q && have_D && have_E; |
| 247 | const int d_missing = have_P && have_Q && !have_D && have_E; |
| 248 | const int is_pub = have_N && !have_P && !have_Q && !have_D && have_E; |
| 249 | |
| 250 | /* These three alternatives are mutually exclusive */ |
| 251 | const int is_priv = n_missing || pq_missing || d_missing; |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 252 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 253 | if( !is_priv && !is_pub ) |
| 254 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 255 | |
| 256 | /* |
Hanno Becker | 2cca6f3 | 2017-09-29 11:46:40 +0100 | [diff] [blame] | 257 | * Step 1: Deduce N if P, Q are provided. |
| 258 | */ |
| 259 | |
| 260 | if( !have_N && have_P && have_Q ) |
| 261 | { |
| 262 | if( ( ret = mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, |
| 263 | &ctx->Q ) ) != 0 ) |
| 264 | { |
| 265 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 266 | } |
| 267 | |
| 268 | ctx->len = mbedtls_mpi_size( &ctx->N ); |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Step 2: Deduce and verify all remaining core parameters. |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 273 | */ |
| 274 | |
| 275 | if( pq_missing ) |
| 276 | { |
Hanno Becker | 0f65e0c | 2017-10-03 14:39:16 +0100 | [diff] [blame] | 277 | ret = mbedtls_rsa_deduce_primes( &ctx->N, &ctx->D, &ctx->E, |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 278 | &ctx->P, &ctx->Q ); |
| 279 | if( ret != 0 ) |
| 280 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 281 | |
| 282 | } |
| 283 | else if( d_missing ) |
| 284 | { |
Hanno Becker | 8ba6ce4 | 2017-10-03 14:36:26 +0100 | [diff] [blame] | 285 | if( ( ret = mbedtls_rsa_deduce_private_exponent( &ctx->P, |
| 286 | &ctx->Q, |
| 287 | &ctx->E, |
| 288 | &ctx->D ) ) != 0 ) |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 289 | { |
| 290 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 291 | } |
| 292 | } |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 293 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 294 | /* |
Hanno Becker | 2cca6f3 | 2017-09-29 11:46:40 +0100 | [diff] [blame] | 295 | * Step 3: Deduce all additional parameters specific |
Hanno Becker | e867489 | 2017-10-10 17:56:14 +0100 | [diff] [blame] | 296 | * to our current RSA implementation. |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 297 | */ |
| 298 | |
Hanno Becker | 23344b5 | 2017-08-23 07:43:27 +0100 | [diff] [blame] | 299 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 300 | if( is_priv ) |
| 301 | { |
| 302 | ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D, |
| 303 | &ctx->DP, &ctx->DQ, &ctx->QP ); |
| 304 | if( ret != 0 ) |
| 305 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 306 | } |
Hanno Becker | 23344b5 | 2017-08-23 07:43:27 +0100 | [diff] [blame] | 307 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 308 | |
| 309 | /* |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 310 | * Step 3: Basic sanity checks |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 311 | */ |
| 312 | |
Hanno Becker | ebd2c02 | 2017-10-12 10:54:53 +0100 | [diff] [blame^] | 313 | return( rsa_check_context( ctx, is_priv, 1 ) ); |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 316 | int mbedtls_rsa_export_raw( const mbedtls_rsa_context *ctx, |
| 317 | unsigned char *N, size_t N_len, |
| 318 | unsigned char *P, size_t P_len, |
| 319 | unsigned char *Q, size_t Q_len, |
| 320 | unsigned char *D, size_t D_len, |
| 321 | unsigned char *E, size_t E_len ) |
| 322 | { |
| 323 | int ret = 0; |
| 324 | |
| 325 | /* Check if key is private or public */ |
| 326 | const int is_priv = |
| 327 | mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && |
| 328 | mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && |
| 329 | mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && |
| 330 | mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 && |
| 331 | mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0; |
| 332 | |
| 333 | if( !is_priv ) |
| 334 | { |
| 335 | /* If we're trying to export private parameters for a public key, |
| 336 | * something must be wrong. */ |
| 337 | if( P != NULL || Q != NULL || D != NULL ) |
| 338 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 339 | |
| 340 | } |
| 341 | |
| 342 | if( N != NULL ) |
| 343 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->N, N, N_len ) ); |
| 344 | |
| 345 | if( P != NULL ) |
| 346 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->P, P, P_len ) ); |
| 347 | |
| 348 | if( Q != NULL ) |
| 349 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->Q, Q, Q_len ) ); |
| 350 | |
| 351 | if( D != NULL ) |
| 352 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->D, D, D_len ) ); |
| 353 | |
| 354 | if( E != NULL ) |
| 355 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->E, E, E_len ) ); |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 356 | |
| 357 | cleanup: |
| 358 | |
| 359 | return( ret ); |
| 360 | } |
| 361 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 362 | int mbedtls_rsa_export( const mbedtls_rsa_context *ctx, |
| 363 | mbedtls_mpi *N, mbedtls_mpi *P, mbedtls_mpi *Q, |
| 364 | mbedtls_mpi *D, mbedtls_mpi *E ) |
| 365 | { |
| 366 | int ret; |
| 367 | |
| 368 | /* Check if key is private or public */ |
| 369 | int is_priv = |
| 370 | mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && |
| 371 | mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && |
| 372 | mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && |
| 373 | mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 && |
| 374 | mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0; |
| 375 | |
| 376 | if( !is_priv ) |
| 377 | { |
| 378 | /* If we're trying to export private parameters for a public key, |
| 379 | * something must be wrong. */ |
| 380 | if( P != NULL || Q != NULL || D != NULL ) |
| 381 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 382 | |
| 383 | } |
| 384 | |
| 385 | /* Export all requested core parameters. */ |
| 386 | |
| 387 | if( ( N != NULL && ( ret = mbedtls_mpi_copy( N, &ctx->N ) ) != 0 ) || |
| 388 | ( P != NULL && ( ret = mbedtls_mpi_copy( P, &ctx->P ) ) != 0 ) || |
| 389 | ( Q != NULL && ( ret = mbedtls_mpi_copy( Q, &ctx->Q ) ) != 0 ) || |
| 390 | ( D != NULL && ( ret = mbedtls_mpi_copy( D, &ctx->D ) ) != 0 ) || |
| 391 | ( E != NULL && ( ret = mbedtls_mpi_copy( E, &ctx->E ) ) != 0 ) ) |
| 392 | { |
| 393 | return( ret ); |
| 394 | } |
| 395 | |
| 396 | return( 0 ); |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Export CRT parameters |
| 401 | * This must also be implemented if CRT is not used, for being able to |
| 402 | * write DER encoded RSA keys. The helper function mbedtls_rsa_deduce_crt |
| 403 | * can be used in this case. |
| 404 | */ |
| 405 | int mbedtls_rsa_export_crt( const mbedtls_rsa_context *ctx, |
| 406 | mbedtls_mpi *DP, mbedtls_mpi *DQ, mbedtls_mpi *QP ) |
| 407 | { |
| 408 | int ret; |
| 409 | |
| 410 | /* Check if key is private or public */ |
| 411 | int is_priv = |
| 412 | mbedtls_mpi_cmp_int( &ctx->N, 0 ) != 0 && |
| 413 | mbedtls_mpi_cmp_int( &ctx->P, 0 ) != 0 && |
| 414 | mbedtls_mpi_cmp_int( &ctx->Q, 0 ) != 0 && |
| 415 | mbedtls_mpi_cmp_int( &ctx->D, 0 ) != 0 && |
| 416 | mbedtls_mpi_cmp_int( &ctx->E, 0 ) != 0; |
| 417 | |
| 418 | if( !is_priv ) |
| 419 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 420 | |
Hanno Becker | dc95c89 | 2017-08-23 06:57:02 +0100 | [diff] [blame] | 421 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 422 | /* Export all requested blinding parameters. */ |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 423 | if( ( DP != NULL && ( ret = mbedtls_mpi_copy( DP, &ctx->DP ) ) != 0 ) || |
| 424 | ( DQ != NULL && ( ret = mbedtls_mpi_copy( DQ, &ctx->DQ ) ) != 0 ) || |
| 425 | ( QP != NULL && ( ret = mbedtls_mpi_copy( QP, &ctx->QP ) ) != 0 ) ) |
| 426 | { |
Hanno Becker | dc95c89 | 2017-08-23 06:57:02 +0100 | [diff] [blame] | 427 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 428 | } |
Hanno Becker | dc95c89 | 2017-08-23 06:57:02 +0100 | [diff] [blame] | 429 | #else |
| 430 | if( ( ret = mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D, |
| 431 | DP, DQ, QP ) ) != 0 ) |
| 432 | { |
| 433 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA + ret ); |
| 434 | } |
| 435 | #endif |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 436 | |
| 437 | return( 0 ); |
| 438 | } |
Hanno Becker | e2e8b8d | 2017-08-23 14:06:45 +0100 | [diff] [blame] | 439 | |
| 440 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 441 | * Initialize an RSA context |
| 442 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 443 | void mbedtls_rsa_init( mbedtls_rsa_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 444 | int padding, |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 445 | int hash_id ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 446 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 447 | memset( ctx, 0, sizeof( mbedtls_rsa_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 449 | mbedtls_rsa_set_padding( ctx, padding, hash_id ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 450 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 451 | #if defined(MBEDTLS_THREADING_C) |
| 452 | mbedtls_mutex_init( &ctx->mutex ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 453 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 454 | } |
| 455 | |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 456 | /* |
| 457 | * Set padding for an existing RSA context |
| 458 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | 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] | 460 | { |
| 461 | ctx->padding = padding; |
| 462 | ctx->hash_id = hash_id; |
| 463 | } |
| 464 | |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 465 | /* |
| 466 | * Get length in bytes of RSA modulus |
| 467 | */ |
| 468 | |
| 469 | size_t mbedtls_rsa_get_len( const mbedtls_rsa_context *ctx ) |
| 470 | { |
Hanno Becker | 2f8f06a | 2017-09-29 11:47:26 +0100 | [diff] [blame] | 471 | return( ctx->len ); |
Hanno Becker | 617c1ae | 2017-08-23 14:11:24 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 475 | #if defined(MBEDTLS_GENPRIME) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 476 | |
| 477 | /* |
| 478 | * Generate an RSA keypair |
| 479 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 480 | int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 481 | int (*f_rng)(void *, unsigned char *, size_t), |
| 482 | void *p_rng, |
| 483 | unsigned int nbits, int exponent ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 484 | { |
| 485 | int ret; |
Hanno Becker | bee3aae | 2017-08-23 06:59:15 +0100 | [diff] [blame] | 486 | mbedtls_mpi H, G; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 487 | |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 488 | if( f_rng == NULL || nbits < 128 || exponent < 3 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 489 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 490 | |
Janos Follath | ef44178 | 2016-09-21 13:18:12 +0100 | [diff] [blame] | 491 | if( nbits % 2 ) |
| 492 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 493 | |
Hanno Becker | bee3aae | 2017-08-23 06:59:15 +0100 | [diff] [blame] | 494 | mbedtls_mpi_init( &H ); |
| 495 | mbedtls_mpi_init( &G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 496 | |
| 497 | /* |
| 498 | * find primes P and Q with Q < P so that: |
| 499 | * GCD( E, (P-1)*(Q-1) ) == 1 |
| 500 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 501 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 502 | |
| 503 | do |
| 504 | { |
Janos Follath | 10c575b | 2016-02-23 14:42:48 +0000 | [diff] [blame] | 505 | MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0, |
Hanno Becker | bee3aae | 2017-08-23 06:59:15 +0100 | [diff] [blame] | 506 | f_rng, p_rng ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 507 | |
Janos Follath | ef44178 | 2016-09-21 13:18:12 +0100 | [diff] [blame] | 508 | MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0, |
Hanno Becker | bee3aae | 2017-08-23 06:59:15 +0100 | [diff] [blame] | 509 | f_rng, p_rng ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 511 | if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 512 | continue; |
| 513 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 514 | 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] | 515 | if( mbedtls_mpi_bitlen( &ctx->N ) != nbits ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 516 | continue; |
| 517 | |
Janos Follath | ef44178 | 2016-09-21 13:18:12 +0100 | [diff] [blame] | 518 | if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 ) |
Hanno Becker | bee3aae | 2017-08-23 06:59:15 +0100 | [diff] [blame] | 519 | mbedtls_mpi_swap( &ctx->P, &ctx->Q ); |
Janos Follath | ef44178 | 2016-09-21 13:18:12 +0100 | [diff] [blame] | 520 | |
Hanno Becker | bee3aae | 2017-08-23 06:59:15 +0100 | [diff] [blame] | 521 | /* Temporarily replace P,Q by P-1, Q-1 */ |
| 522 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->P, &ctx->P, 1 ) ); |
| 523 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &ctx->Q, &ctx->Q, 1 ) ); |
| 524 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &ctx->P, &ctx->Q ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 525 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 526 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 527 | while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 528 | |
Hanno Becker | bee3aae | 2017-08-23 06:59:15 +0100 | [diff] [blame] | 529 | /* Restore P,Q */ |
| 530 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->P, &ctx->P, 1 ) ); |
| 531 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_int( &ctx->Q, &ctx->Q, 1 ) ); |
| 532 | |
| 533 | ctx->len = mbedtls_mpi_size( &ctx->N ); |
| 534 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 535 | /* |
| 536 | * D = E^-1 mod ((P-1)*(Q-1)) |
| 537 | * DP = D mod (P - 1) |
| 538 | * DQ = D mod (Q - 1) |
| 539 | * QP = Q^-1 mod P |
| 540 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 541 | |
Hanno Becker | bee3aae | 2017-08-23 06:59:15 +0100 | [diff] [blame] | 542 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D, &ctx->E, &H ) ); |
| 543 | |
| 544 | #if !defined(MBEDTLS_RSA_NO_CRT) |
| 545 | MBEDTLS_MPI_CHK( mbedtls_rsa_deduce_crt( &ctx->P, &ctx->Q, &ctx->D, |
| 546 | &ctx->DP, &ctx->DQ, &ctx->QP ) ); |
| 547 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 548 | |
Hanno Becker | 83aad1f | 2017-08-23 06:45:10 +0100 | [diff] [blame] | 549 | /* Double-check */ |
| 550 | MBEDTLS_MPI_CHK( mbedtls_rsa_check_privkey( ctx ) ); |
| 551 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 552 | cleanup: |
| 553 | |
Hanno Becker | bee3aae | 2017-08-23 06:59:15 +0100 | [diff] [blame] | 554 | mbedtls_mpi_free( &H ); |
| 555 | mbedtls_mpi_free( &G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 556 | |
| 557 | if( ret != 0 ) |
| 558 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 559 | mbedtls_rsa_free( ctx ); |
| 560 | return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 563 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 566 | #endif /* MBEDTLS_GENPRIME */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 567 | |
| 568 | /* |
| 569 | * Check a public RSA key |
| 570 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 571 | int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 572 | { |
Hanno Becker | ebd2c02 | 2017-10-12 10:54:53 +0100 | [diff] [blame^] | 573 | if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 574 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 575 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 576 | if( mbedtls_mpi_bitlen( &ctx->N ) < 128 || |
| 577 | mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS ) |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 578 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 579 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 580 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 581 | |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 582 | if( mbedtls_mpi_get_bit( &ctx->E, 0 ) == 0 || |
| 583 | mbedtls_mpi_bitlen( &ctx->E ) < 2 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 584 | mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 ) |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 585 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 586 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 587 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 588 | |
| 589 | return( 0 ); |
| 590 | } |
| 591 | |
| 592 | /* |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 593 | * Check for the consistency of all fields in an RSA private key context |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 594 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 595 | int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 596 | { |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 597 | if( mbedtls_rsa_check_pubkey( ctx ) != 0 || |
Hanno Becker | ebd2c02 | 2017-10-12 10:54:53 +0100 | [diff] [blame^] | 598 | rsa_check_context( ctx, 1 /* private */, 1 /* blinding */ ) != 0 ) |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 599 | { |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 600 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 601 | } |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 602 | |
| 603 | if( mbedtls_rsa_validate_params( &ctx->N, &ctx->P, &ctx->Q, |
Hanno Becker | b269a85 | 2017-08-25 08:03:21 +0100 | [diff] [blame] | 604 | &ctx->D, &ctx->E, NULL, NULL ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 605 | { |
Hanno Becker | b269a85 | 2017-08-25 08:03:21 +0100 | [diff] [blame] | 606 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 607 | } |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 608 | |
Hanno Becker | b269a85 | 2017-08-25 08:03:21 +0100 | [diff] [blame] | 609 | #if !defined(MBEDTLS_RSA_NO_CRT) |
| 610 | else if( mbedtls_rsa_validate_crt( &ctx->P, &ctx->Q, &ctx->D, |
| 611 | &ctx->DP, &ctx->DQ, &ctx->QP ) != 0 ) |
| 612 | { |
| 613 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
| 614 | } |
| 615 | #endif |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 616 | |
| 617 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 621 | * Check if contexts holding a public and private key match |
| 622 | */ |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 623 | int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, |
| 624 | const mbedtls_rsa_context *prv ) |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 625 | { |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 626 | if( mbedtls_rsa_check_pubkey( pub ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 627 | mbedtls_rsa_check_privkey( prv ) != 0 ) |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 628 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 629 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 630 | } |
| 631 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 632 | if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 || |
| 633 | mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 ) |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 634 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 635 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | return( 0 ); |
| 639 | } |
| 640 | |
| 641 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 642 | * Do an RSA public key operation |
| 643 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 644 | int mbedtls_rsa_public( mbedtls_rsa_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 645 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 646 | unsigned char *output ) |
| 647 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 648 | int ret; |
| 649 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 650 | mbedtls_mpi T; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 651 | |
Hanno Becker | ebd2c02 | 2017-10-12 10:54:53 +0100 | [diff] [blame^] | 652 | if( rsa_check_context( ctx, 0 /* public */, 0 /* no blinding */ ) ) |
Hanno Becker | 705fc68 | 2017-10-10 17:57:02 +0100 | [diff] [blame] | 653 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 654 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 655 | mbedtls_mpi_init( &T ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 656 | |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 657 | #if defined(MBEDTLS_THREADING_C) |
| 658 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 659 | return( ret ); |
| 660 | #endif |
| 661 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 662 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 664 | if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 665 | { |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 666 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 667 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) ); |
| 672 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 673 | |
| 674 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 675 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 676 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 677 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
Manuel Pégourié-Gonnard | 88fca3e | 2015-03-27 15:06:07 +0100 | [diff] [blame] | 678 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 679 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 680 | mbedtls_mpi_free( &T ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 681 | |
| 682 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 683 | return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 684 | |
| 685 | return( 0 ); |
| 686 | } |
| 687 | |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 688 | /* |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 689 | * Generate or update blinding values, see section 10 of: |
| 690 | * 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] | 691 | * 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] | 692 | * Berlin Heidelberg, 1996. p. 104-113. |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 693 | */ |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 694 | static int rsa_prepare_blinding( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 695 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 696 | { |
Manuel Pégourié-Gonnard | 4d89c7e | 2013-10-04 15:18:38 +0200 | [diff] [blame] | 697 | int ret, count = 0; |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 698 | |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 699 | if( ctx->Vf.p != NULL ) |
| 700 | { |
| 701 | /* We already have blinding values, just update them by squaring */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 702 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) ); |
| 703 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) ); |
| 704 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) ); |
| 705 | 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] | 706 | |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 707 | goto cleanup; |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 708 | } |
| 709 | |
Manuel Pégourié-Gonnard | 4d89c7e | 2013-10-04 15:18:38 +0200 | [diff] [blame] | 710 | /* Unblinding value: Vf = random number, invertible mod N */ |
| 711 | do { |
| 712 | if( count++ > 10 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 713 | return( MBEDTLS_ERR_RSA_RNG_FAILED ); |
Manuel Pégourié-Gonnard | 4d89c7e | 2013-10-04 15:18:38 +0200 | [diff] [blame] | 714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) ); |
| 716 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) ); |
| 717 | } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 ); |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 718 | |
| 719 | /* Blinding value: Vi = Vf^(-e) mod N */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 720 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) ); |
| 721 | 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] | 722 | |
Manuel Pégourié-Gonnard | ae10299 | 2013-10-04 17:07:12 +0200 | [diff] [blame] | 723 | |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 724 | cleanup: |
| 725 | return( ret ); |
| 726 | } |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 727 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 728 | /* |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 729 | * Exponent blinding supposed to prevent side-channel attacks using multiple |
| 730 | * traces of measurements to recover the RSA key. The more collisions are there, |
| 731 | * the more bits of the key can be recovered. See [3]. |
| 732 | * |
| 733 | * Collecting n collisions with m bit long blinding value requires 2^(m-m/n) |
| 734 | * observations on avarage. |
| 735 | * |
| 736 | * For example with 28 byte blinding to achieve 2 collisions the adversary has |
| 737 | * to make 2^112 observations on avarage. |
| 738 | * |
| 739 | * (With the currently (as of 2017 April) known best algorithms breaking 2048 |
| 740 | * bit RSA requires approximately as much time as trying out 2^112 random keys. |
| 741 | * Thus in this sense with 28 byte blinding the security is not reduced by |
| 742 | * side-channel attacks like the one in [3]) |
| 743 | * |
| 744 | * This countermeasure does not help if the key recovery is possible with a |
| 745 | * single trace. |
| 746 | */ |
| 747 | #define RSA_EXPONENT_BLINDING 28 |
| 748 | |
| 749 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 750 | * Do an RSA private key operation |
| 751 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 752 | int mbedtls_rsa_private( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 753 | int (*f_rng)(void *, unsigned char *, size_t), |
| 754 | void *p_rng, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 755 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 756 | unsigned char *output ) |
| 757 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 758 | int ret; |
| 759 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 760 | mbedtls_mpi T, T1, T2; |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 761 | mbedtls_mpi P1, Q1, R; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 762 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 763 | mbedtls_mpi D_blind; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 764 | mbedtls_mpi *D = &ctx->D; |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 765 | #else |
| 766 | mbedtls_mpi DP_blind, DQ_blind; |
| 767 | mbedtls_mpi *DP = &ctx->DP; |
| 768 | mbedtls_mpi *DQ = &ctx->DQ; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 769 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 770 | |
Hanno Becker | ebd2c02 | 2017-10-12 10:54:53 +0100 | [diff] [blame^] | 771 | if( rsa_check_context( ctx, 1 /* private key checks */, |
| 772 | f_rng != NULL /* blinding y/n */ ) != 0 ) |
| 773 | { |
Manuel Pégourié-Gonnard | fb84d38 | 2015-10-30 10:56:25 +0100 | [diff] [blame] | 774 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Hanno Becker | ebd2c02 | 2017-10-12 10:54:53 +0100 | [diff] [blame^] | 775 | } |
Manuel Pégourié-Gonnard | fb84d38 | 2015-10-30 10:56:25 +0100 | [diff] [blame] | 776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 777 | mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 778 | mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R ); |
| 779 | |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 780 | if( f_rng != NULL ) |
| 781 | { |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 782 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 783 | mbedtls_mpi_init( &D_blind ); |
| 784 | #else |
| 785 | mbedtls_mpi_init( &DP_blind ); |
| 786 | mbedtls_mpi_init( &DQ_blind ); |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 787 | #endif |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 788 | } |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 789 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 790 | |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 791 | #if defined(MBEDTLS_THREADING_C) |
| 792 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 793 | return( ret ); |
| 794 | #endif |
| 795 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 796 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) ); |
| 797 | if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 798 | { |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 799 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 800 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 803 | if( f_rng != NULL ) |
| 804 | { |
| 805 | /* |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 806 | * Blinding |
| 807 | * T = T * Vi mod N |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 808 | */ |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 809 | MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) ); |
| 810 | 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] | 811 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) ); |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 812 | |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 813 | /* |
| 814 | * Exponent blinding |
| 815 | */ |
| 816 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) ); |
| 817 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) ); |
| 818 | |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 819 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 820 | /* |
| 821 | * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D |
| 822 | */ |
| 823 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING, |
| 824 | f_rng, p_rng ) ); |
| 825 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) ); |
| 826 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) ); |
| 827 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) ); |
| 828 | |
| 829 | D = &D_blind; |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 830 | #else |
| 831 | /* |
| 832 | * DP_blind = ( P - 1 ) * R + DP |
| 833 | */ |
| 834 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING, |
| 835 | f_rng, p_rng ) ); |
| 836 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) ); |
| 837 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind, |
| 838 | &ctx->DP ) ); |
| 839 | |
| 840 | DP = &DP_blind; |
| 841 | |
| 842 | /* |
| 843 | * DQ_blind = ( Q - 1 ) * R + DQ |
| 844 | */ |
| 845 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING, |
| 846 | f_rng, p_rng ) ); |
| 847 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) ); |
| 848 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind, |
| 849 | &ctx->DQ ) ); |
| 850 | |
| 851 | DQ = &DQ_blind; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 852 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 853 | } |
Paul Bakker | aab30c1 | 2013-08-30 11:00:25 +0200 | [diff] [blame] | 854 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 855 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 856 | 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] | 857 | #else |
Paul Bakker | aab30c1 | 2013-08-30 11:00:25 +0200 | [diff] [blame] | 858 | /* |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 859 | * Faster decryption using the CRT |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 860 | * |
| 861 | * T1 = input ^ dP mod P |
| 862 | * T2 = input ^ dQ mod Q |
| 863 | */ |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 864 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) ); |
| 865 | 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] | 866 | |
| 867 | /* |
| 868 | * T = (T1 - T2) * (Q^-1 mod P) mod P |
| 869 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 870 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) ); |
| 871 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) ); |
| 872 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 873 | |
| 874 | /* |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 875 | * T = T2 + T * Q |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 876 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 877 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) ); |
| 878 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) ); |
| 879 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Paul Bakker | aab30c1 | 2013-08-30 11:00:25 +0200 | [diff] [blame] | 880 | |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 881 | if( f_rng != NULL ) |
| 882 | { |
| 883 | /* |
| 884 | * Unblind |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 885 | * T = T * Vf mod N |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 886 | */ |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 887 | 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] | 888 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) ); |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 889 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 890 | |
| 891 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 892 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 893 | |
| 894 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 895 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 896 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 897 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
Manuel Pégourié-Gonnard | ae10299 | 2013-10-04 17:07:12 +0200 | [diff] [blame] | 898 | #endif |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 899 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 900 | mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 901 | mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R ); |
| 902 | |
| 903 | if( f_rng != NULL ) |
| 904 | { |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 905 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 906 | mbedtls_mpi_free( &D_blind ); |
| 907 | #else |
| 908 | mbedtls_mpi_free( &DP_blind ); |
| 909 | mbedtls_mpi_free( &DQ_blind ); |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 910 | #endif |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 911 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 912 | |
| 913 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 914 | return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 915 | |
| 916 | return( 0 ); |
| 917 | } |
| 918 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 919 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 920 | /** |
| 921 | * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer. |
| 922 | * |
Paul Bakker | b125ed8 | 2011-11-10 13:33:51 +0000 | [diff] [blame] | 923 | * \param dst buffer to mask |
| 924 | * \param dlen length of destination buffer |
| 925 | * \param src source of the mask generation |
| 926 | * \param slen length of the source buffer |
| 927 | * \param md_ctx message digest context to use |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 928 | */ |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 929 | 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] | 930 | size_t slen, mbedtls_md_context_t *md_ctx ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 931 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 932 | unsigned char mask[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 933 | unsigned char counter[4]; |
| 934 | unsigned char *p; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 935 | unsigned int hlen; |
| 936 | size_t i, use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 937 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 938 | memset( mask, 0, MBEDTLS_MD_MAX_SIZE ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 939 | memset( counter, 0, 4 ); |
| 940 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 941 | hlen = mbedtls_md_get_size( md_ctx->md_info ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 942 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 943 | /* Generate and apply dbMask */ |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 944 | p = dst; |
| 945 | |
| 946 | while( dlen > 0 ) |
| 947 | { |
| 948 | use_len = hlen; |
| 949 | if( dlen < hlen ) |
| 950 | use_len = dlen; |
| 951 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 952 | mbedtls_md_starts( md_ctx ); |
| 953 | mbedtls_md_update( md_ctx, src, slen ); |
| 954 | mbedtls_md_update( md_ctx, counter, 4 ); |
| 955 | mbedtls_md_finish( md_ctx, mask ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 956 | |
| 957 | for( i = 0; i < use_len; ++i ) |
| 958 | *p++ ^= mask[i]; |
| 959 | |
| 960 | counter[3]++; |
| 961 | |
| 962 | dlen -= use_len; |
| 963 | } |
Gilles Peskine | 18ac716 | 2017-05-05 19:24:06 +0200 | [diff] [blame] | 964 | |
| 965 | mbedtls_zeroize( mask, sizeof( mask ) ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 966 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 967 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 968 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 969 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 970 | /* |
| 971 | * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function |
| 972 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 973 | int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 974 | int (*f_rng)(void *, unsigned char *, size_t), |
| 975 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 976 | int mode, |
| 977 | const unsigned char *label, size_t label_len, |
| 978 | size_t ilen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 979 | const unsigned char *input, |
| 980 | unsigned char *output ) |
| 981 | { |
| 982 | size_t olen; |
| 983 | int ret; |
| 984 | unsigned char *p = output; |
| 985 | unsigned int hlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 986 | const mbedtls_md_info_t *md_info; |
| 987 | mbedtls_md_context_t md_ctx; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 988 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 989 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 990 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 991 | |
| 992 | if( f_rng == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 993 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 994 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 995 | 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] | 996 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 997 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 998 | |
| 999 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1000 | hlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1001 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1002 | /* first comparison checks for overflow */ |
Janos Follath | eddfe8f | 2016-02-08 14:52:29 +0000 | [diff] [blame] | 1003 | 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] | 1004 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1005 | |
| 1006 | memset( output, 0, olen ); |
| 1007 | |
| 1008 | *p++ = 0; |
| 1009 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1010 | /* Generate a random octet string seed */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1011 | if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1012 | return( MBEDTLS_ERR_RSA_RNG_FAILED + ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1013 | |
| 1014 | p += hlen; |
| 1015 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1016 | /* Construct DB */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1017 | mbedtls_md( md_info, label, label_len, p ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1018 | p += hlen; |
| 1019 | p += olen - 2 * hlen - 2 - ilen; |
| 1020 | *p++ = 1; |
| 1021 | memcpy( p, input, ilen ); |
| 1022 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1023 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1024 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 1025 | { |
| 1026 | mbedtls_md_free( &md_ctx ); |
| 1027 | return( ret ); |
| 1028 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1029 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1030 | /* maskedDB: Apply dbMask to DB */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1031 | mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen, |
| 1032 | &md_ctx ); |
| 1033 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1034 | /* maskedSeed: Apply seedMask to seed */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1035 | mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1, |
| 1036 | &md_ctx ); |
| 1037 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1038 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1040 | return( ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1041 | ? mbedtls_rsa_public( ctx, output, output ) |
| 1042 | : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1043 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1044 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1045 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1046 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1047 | /* |
| 1048 | * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function |
| 1049 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1050 | int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1051 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1052 | void *p_rng, |
| 1053 | int mode, size_t ilen, |
| 1054 | const unsigned char *input, |
| 1055 | unsigned char *output ) |
| 1056 | { |
| 1057 | size_t nb_pad, olen; |
| 1058 | int ret; |
| 1059 | unsigned char *p = output; |
| 1060 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1061 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 1062 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 1063 | |
Janos Follath | 1ed9f99 | 2016-03-18 11:45:44 +0000 | [diff] [blame] | 1064 | // We don't check p_rng because it won't be dereferenced here |
| 1065 | if( f_rng == NULL || input == NULL || output == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1066 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1067 | |
| 1068 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 370717b | 2016-02-11 10:35:13 +0100 | [diff] [blame] | 1069 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1070 | /* first comparison checks for overflow */ |
Janos Follath | eddfe8f | 2016-02-08 14:52:29 +0000 | [diff] [blame] | 1071 | if( ilen + 11 < ilen || olen < ilen + 11 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1072 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1073 | |
| 1074 | nb_pad = olen - 3 - ilen; |
| 1075 | |
| 1076 | *p++ = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1077 | if( mode == MBEDTLS_RSA_PUBLIC ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1078 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1079 | *p++ = MBEDTLS_RSA_CRYPT; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1080 | |
| 1081 | while( nb_pad-- > 0 ) |
| 1082 | { |
| 1083 | int rng_dl = 100; |
| 1084 | |
| 1085 | do { |
| 1086 | ret = f_rng( p_rng, p, 1 ); |
| 1087 | } while( *p == 0 && --rng_dl && ret == 0 ); |
| 1088 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1089 | /* Check if RNG failed to generate data */ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1090 | if( rng_dl == 0 || ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1091 | return( MBEDTLS_ERR_RSA_RNG_FAILED + ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1092 | |
| 1093 | p++; |
| 1094 | } |
| 1095 | } |
| 1096 | else |
| 1097 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1098 | *p++ = MBEDTLS_RSA_SIGN; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1099 | |
| 1100 | while( nb_pad-- > 0 ) |
| 1101 | *p++ = 0xFF; |
| 1102 | } |
| 1103 | |
| 1104 | *p++ = 0; |
| 1105 | memcpy( p, input, ilen ); |
| 1106 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1107 | return( ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1108 | ? mbedtls_rsa_public( ctx, output, output ) |
| 1109 | : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1110 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1111 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1112 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1113 | /* |
| 1114 | * Add the message padding, then do an RSA operation |
| 1115 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1116 | int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1117 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 1118 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1119 | int mode, size_t ilen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1120 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1121 | unsigned char *output ) |
| 1122 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1123 | switch( ctx->padding ) |
| 1124 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1125 | #if defined(MBEDTLS_PKCS1_V15) |
| 1126 | case MBEDTLS_RSA_PKCS_V15: |
| 1127 | 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] | 1128 | input, output ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1129 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1130 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1131 | #if defined(MBEDTLS_PKCS1_V21) |
| 1132 | case MBEDTLS_RSA_PKCS_V21: |
| 1133 | 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] | 1134 | ilen, input, output ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1135 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1136 | |
| 1137 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1138 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1139 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1142 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1143 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1144 | * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1145 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1146 | int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1147 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1148 | void *p_rng, |
| 1149 | int mode, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 1150 | const unsigned char *label, size_t label_len, |
| 1151 | size_t *olen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1152 | const unsigned char *input, |
| 1153 | unsigned char *output, |
| 1154 | size_t output_max_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1155 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1156 | int ret; |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1157 | size_t ilen, i, pad_len; |
| 1158 | unsigned char *p, bad, pad_done; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1159 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
| 1160 | unsigned char lhash[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1161 | unsigned int hlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1162 | const mbedtls_md_info_t *md_info; |
| 1163 | mbedtls_md_context_t md_ctx; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1164 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1165 | /* |
| 1166 | * Parameters sanity checks |
| 1167 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1168 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 1169 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1170 | |
| 1171 | ilen = ctx->len; |
| 1172 | |
Paul Bakker | 27fdf46 | 2011-06-09 13:55:13 +0000 | [diff] [blame] | 1173 | if( ilen < 16 || ilen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1174 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1176 | 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] | 1177 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1178 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1179 | |
Janos Follath | c17cda1 | 2016-02-11 11:08:18 +0000 | [diff] [blame] | 1180 | hlen = mbedtls_md_get_size( md_info ); |
| 1181 | |
| 1182 | // checking for integer underflow |
| 1183 | if( 2 * hlen + 2 > ilen ) |
| 1184 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1185 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1186 | /* |
| 1187 | * RSA operation |
| 1188 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1189 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1190 | ? mbedtls_rsa_public( ctx, input, buf ) |
| 1191 | : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1192 | |
| 1193 | if( ret != 0 ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1194 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1195 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1196 | /* |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1197 | * Unmask data and generate lHash |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1198 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1199 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1200 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 1201 | { |
| 1202 | mbedtls_md_free( &md_ctx ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1203 | goto cleanup; |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1206 | |
| 1207 | /* Generate lHash */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1208 | mbedtls_md( md_info, label, label_len, lhash ); |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1209 | |
| 1210 | /* seed: Apply seedMask to maskedSeed */ |
| 1211 | mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1, |
| 1212 | &md_ctx ); |
| 1213 | |
| 1214 | /* DB: Apply dbMask to maskedDB */ |
| 1215 | mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen, |
| 1216 | &md_ctx ); |
| 1217 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1218 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1219 | |
| 1220 | /* |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1221 | * Check contents, in "constant-time" |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1222 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1223 | p = buf; |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1224 | bad = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1225 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1226 | bad |= *p++; /* First byte must be 0 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1227 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1228 | p += hlen; /* Skip seed */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1229 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1230 | /* Check lHash */ |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1231 | for( i = 0; i < hlen; i++ ) |
| 1232 | bad |= lhash[i] ^ *p++; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1233 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1234 | /* Get zero-padding len, but always read till end of buffer |
| 1235 | * (minus one, for the 01 byte) */ |
| 1236 | pad_len = 0; |
| 1237 | pad_done = 0; |
| 1238 | for( i = 0; i < ilen - 2 * hlen - 2; i++ ) |
| 1239 | { |
| 1240 | pad_done |= p[i]; |
Pascal Junod | b99183d | 2015-03-11 16:49:45 +0100 | [diff] [blame] | 1241 | pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1; |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1242 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1243 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1244 | p += pad_len; |
| 1245 | bad |= *p++ ^ 0x01; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1246 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1247 | /* |
| 1248 | * The only information "leaked" is whether the padding was correct or not |
| 1249 | * (eg, no data is copied if it was not correct). This meets the |
| 1250 | * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between |
| 1251 | * the different error conditions. |
| 1252 | */ |
| 1253 | if( bad != 0 ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1254 | { |
| 1255 | ret = MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 1256 | goto cleanup; |
| 1257 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1258 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1259 | if( ilen - ( p - buf ) > output_max_len ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1260 | { |
| 1261 | ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
| 1262 | goto cleanup; |
| 1263 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1264 | |
| 1265 | *olen = ilen - (p - buf); |
| 1266 | memcpy( output, p, *olen ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1267 | ret = 0; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1268 | |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1269 | cleanup: |
| 1270 | mbedtls_zeroize( buf, sizeof( buf ) ); |
| 1271 | mbedtls_zeroize( lhash, sizeof( lhash ) ); |
| 1272 | |
| 1273 | return( ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1274 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1275 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1276 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1277 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1278 | /* |
| 1279 | * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function |
| 1280 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1281 | int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1282 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1283 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1284 | int mode, size_t *olen, |
| 1285 | const unsigned char *input, |
| 1286 | unsigned char *output, |
| 1287 | size_t output_max_len) |
| 1288 | { |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1289 | int ret; |
| 1290 | size_t ilen, pad_count = 0, i; |
| 1291 | unsigned char *p, bad, pad_done = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1292 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1293 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1294 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 1295 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1296 | |
| 1297 | ilen = ctx->len; |
| 1298 | |
| 1299 | if( ilen < 16 || ilen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1300 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1301 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1302 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1303 | ? mbedtls_rsa_public( ctx, input, buf ) |
| 1304 | : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1305 | |
| 1306 | if( ret != 0 ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1307 | goto cleanup; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1308 | |
| 1309 | p = buf; |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1310 | bad = 0; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1311 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1312 | /* |
| 1313 | * Check and get padding len in "constant-time" |
| 1314 | */ |
| 1315 | bad |= *p++; /* First byte must be 0 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1316 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1317 | /* This test does not depend on secret data */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1318 | if( mode == MBEDTLS_RSA_PRIVATE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1319 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1320 | bad |= *p++ ^ MBEDTLS_RSA_CRYPT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1321 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1322 | /* Get padding len, but always read till end of buffer |
| 1323 | * (minus one, for the 00 byte) */ |
| 1324 | for( i = 0; i < ilen - 3; i++ ) |
| 1325 | { |
Pascal Junod | b99183d | 2015-03-11 16:49:45 +0100 | [diff] [blame] | 1326 | pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1; |
| 1327 | pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1; |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1328 | } |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 1329 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1330 | p += pad_count; |
| 1331 | bad |= *p++; /* Must be zero */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1332 | } |
| 1333 | else |
| 1334 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1335 | bad |= *p++ ^ MBEDTLS_RSA_SIGN; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1336 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1337 | /* Get padding len, but always read till end of buffer |
| 1338 | * (minus one, for the 00 byte) */ |
| 1339 | for( i = 0; i < ilen - 3; i++ ) |
| 1340 | { |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 1341 | pad_done |= ( p[i] != 0xFF ); |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1342 | pad_count += ( pad_done == 0 ); |
| 1343 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1344 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1345 | p += pad_count; |
| 1346 | bad |= *p++; /* Must be zero */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
Janos Follath | c69fa50 | 2016-02-12 13:30:09 +0000 | [diff] [blame] | 1349 | bad |= ( pad_count < 8 ); |
Janos Follath | b6eb1ca | 2016-02-08 13:59:25 +0000 | [diff] [blame] | 1350 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1351 | if( bad ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1352 | { |
| 1353 | ret = MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 1354 | goto cleanup; |
| 1355 | } |
Paul Bakker | 8804f69 | 2013-02-28 18:06:26 +0100 | [diff] [blame] | 1356 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1357 | if( ilen - ( p - buf ) > output_max_len ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1358 | { |
| 1359 | ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
| 1360 | goto cleanup; |
| 1361 | } |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 1362 | |
Paul Bakker | 27fdf46 | 2011-06-09 13:55:13 +0000 | [diff] [blame] | 1363 | *olen = ilen - (p - buf); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1364 | memcpy( output, p, *olen ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1365 | ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1366 | |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1367 | cleanup: |
| 1368 | mbedtls_zeroize( buf, sizeof( buf ) ); |
| 1369 | |
| 1370 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1371 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1372 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1373 | |
| 1374 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1375 | * Do an RSA operation, then remove the message padding |
| 1376 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1377 | int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1378 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1379 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1380 | int mode, size_t *olen, |
| 1381 | const unsigned char *input, |
| 1382 | unsigned char *output, |
| 1383 | size_t output_max_len) |
| 1384 | { |
| 1385 | switch( ctx->padding ) |
| 1386 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1387 | #if defined(MBEDTLS_PKCS1_V15) |
| 1388 | case MBEDTLS_RSA_PKCS_V15: |
| 1389 | 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] | 1390 | input, output, output_max_len ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1391 | #endif |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1392 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1393 | #if defined(MBEDTLS_PKCS1_V21) |
| 1394 | case MBEDTLS_RSA_PKCS_V21: |
| 1395 | 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] | 1396 | olen, input, output, |
| 1397 | output_max_len ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1398 | #endif |
| 1399 | |
| 1400 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1401 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1405 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1406 | /* |
| 1407 | * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function |
| 1408 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1409 | int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1410 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1411 | void *p_rng, |
| 1412 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1413 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1414 | unsigned int hashlen, |
| 1415 | const unsigned char *hash, |
| 1416 | unsigned char *sig ) |
| 1417 | { |
| 1418 | size_t olen; |
| 1419 | unsigned char *p = sig; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1420 | unsigned char salt[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1421 | unsigned int slen, hlen, offset = 0; |
| 1422 | int ret; |
| 1423 | size_t msb; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1424 | const mbedtls_md_info_t *md_info; |
| 1425 | mbedtls_md_context_t md_ctx; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1426 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1427 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 1428 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 1429 | |
| 1430 | if( f_rng == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1431 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1432 | |
| 1433 | olen = ctx->len; |
| 1434 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1435 | if( md_alg != MBEDTLS_MD_NONE ) |
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 | /* Gather length of hash to sign */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1438 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1439 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1440 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1441 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1442 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1443 | } |
| 1444 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1445 | 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] | 1446 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1447 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1448 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1449 | hlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1450 | slen = hlen; |
| 1451 | |
| 1452 | if( olen < hlen + slen + 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1453 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1454 | |
| 1455 | memset( sig, 0, olen ); |
| 1456 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1457 | /* Generate salt of length slen */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1458 | if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1459 | return( MBEDTLS_ERR_RSA_RNG_FAILED + ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1460 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1461 | /* 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] | 1462 | msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1463 | p += olen - hlen * 2 - 2; |
| 1464 | *p++ = 0x01; |
| 1465 | memcpy( p, salt, slen ); |
| 1466 | p += slen; |
| 1467 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1468 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1469 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 1470 | { |
| 1471 | mbedtls_md_free( &md_ctx ); |
Gilles Peskine | 18ac716 | 2017-05-05 19:24:06 +0200 | [diff] [blame] | 1472 | /* No need to zeroize salt: we didn't use it. */ |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1473 | return( ret ); |
| 1474 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1475 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1476 | /* Generate H = Hash( M' ) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1477 | mbedtls_md_starts( &md_ctx ); |
| 1478 | mbedtls_md_update( &md_ctx, p, 8 ); |
| 1479 | mbedtls_md_update( &md_ctx, hash, hashlen ); |
| 1480 | mbedtls_md_update( &md_ctx, salt, slen ); |
| 1481 | mbedtls_md_finish( &md_ctx, p ); |
Gilles Peskine | 18ac716 | 2017-05-05 19:24:06 +0200 | [diff] [blame] | 1482 | mbedtls_zeroize( salt, sizeof( salt ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1483 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1484 | /* Compensate for boundary condition when applying mask */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1485 | if( msb % 8 == 0 ) |
| 1486 | offset = 1; |
| 1487 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1488 | /* maskedDB: Apply dbMask to DB */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1489 | mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx ); |
| 1490 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1491 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1492 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 1493 | msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1494 | sig[0] &= 0xFF >> ( olen * 8 - msb ); |
| 1495 | |
| 1496 | p += hlen; |
| 1497 | *p++ = 0xBC; |
| 1498 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1499 | return( ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1500 | ? mbedtls_rsa_public( ctx, sig, sig ) |
| 1501 | : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1502 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1503 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1504 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1505 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1506 | /* |
| 1507 | * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function |
| 1508 | */ |
| 1509 | /* |
| 1510 | * Do an RSA operation to sign the message digest |
| 1511 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1512 | int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1513 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1514 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1515 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1516 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1517 | unsigned int hashlen, |
| 1518 | const unsigned char *hash, |
| 1519 | unsigned char *sig ) |
| 1520 | { |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1521 | size_t nb_pad, olen, oid_size = 0; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1522 | unsigned char *p = sig; |
Paul Bakker | 21e081b | 2014-07-24 10:38:01 +0200 | [diff] [blame] | 1523 | const char *oid = NULL; |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 1524 | unsigned char *sig_try = NULL, *verif = NULL; |
| 1525 | size_t i; |
| 1526 | unsigned char diff; |
| 1527 | volatile unsigned char diff_no_optimize; |
| 1528 | int ret; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1529 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1530 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 1531 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1532 | |
| 1533 | olen = ctx->len; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1534 | nb_pad = olen - 3; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1535 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1536 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1537 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1538 | 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] | 1539 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1540 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1541 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1542 | if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 ) |
| 1543 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1544 | |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1545 | nb_pad -= 10 + oid_size; |
| 1546 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1547 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1548 | } |
| 1549 | |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1550 | nb_pad -= hashlen; |
| 1551 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1552 | if( ( nb_pad < 8 ) || ( nb_pad > olen ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1553 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1554 | |
| 1555 | *p++ = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1556 | *p++ = MBEDTLS_RSA_SIGN; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1557 | memset( p, 0xFF, nb_pad ); |
| 1558 | p += nb_pad; |
| 1559 | *p++ = 0; |
| 1560 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1561 | if( md_alg == MBEDTLS_MD_NONE ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1562 | { |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1563 | memcpy( p, hash, hashlen ); |
| 1564 | } |
| 1565 | else |
| 1566 | { |
| 1567 | /* |
| 1568 | * DigestInfo ::= SEQUENCE { |
| 1569 | * digestAlgorithm DigestAlgorithmIdentifier, |
| 1570 | * digest Digest } |
| 1571 | * |
| 1572 | * DigestAlgorithmIdentifier ::= AlgorithmIdentifier |
| 1573 | * |
| 1574 | * Digest ::= OCTET STRING |
| 1575 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1576 | *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1577 | *p++ = (unsigned char) ( 0x08 + oid_size + hashlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1578 | *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1579 | *p++ = (unsigned char) ( 0x04 + oid_size ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1580 | *p++ = MBEDTLS_ASN1_OID; |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 1581 | *p++ = oid_size & 0xFF; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1582 | memcpy( p, oid, oid_size ); |
| 1583 | p += oid_size; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1584 | *p++ = MBEDTLS_ASN1_NULL; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1585 | *p++ = 0x00; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1586 | *p++ = MBEDTLS_ASN1_OCTET_STRING; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1587 | *p++ = hashlen; |
| 1588 | memcpy( p, hash, hashlen ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1589 | } |
| 1590 | |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 1591 | if( mode == MBEDTLS_RSA_PUBLIC ) |
| 1592 | return( mbedtls_rsa_public( ctx, sig, sig ) ); |
| 1593 | |
| 1594 | /* |
| 1595 | * In order to prevent Lenstra's attack, make the signature in a |
| 1596 | * temporary buffer and check it before returning it. |
| 1597 | */ |
| 1598 | sig_try = mbedtls_calloc( 1, ctx->len ); |
Simon Butcher | 1285ab5 | 2016-01-01 21:42:47 +0000 | [diff] [blame] | 1599 | if( sig_try == NULL ) |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 1600 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 1601 | |
Simon Butcher | 1285ab5 | 2016-01-01 21:42:47 +0000 | [diff] [blame] | 1602 | verif = mbedtls_calloc( 1, ctx->len ); |
| 1603 | if( verif == NULL ) |
| 1604 | { |
| 1605 | mbedtls_free( sig_try ); |
| 1606 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 1607 | } |
| 1608 | |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 1609 | MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) ); |
| 1610 | MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) ); |
| 1611 | |
| 1612 | /* Compare in constant time just in case */ |
| 1613 | for( diff = 0, i = 0; i < ctx->len; i++ ) |
| 1614 | diff |= verif[i] ^ sig[i]; |
| 1615 | diff_no_optimize = diff; |
| 1616 | |
| 1617 | if( diff_no_optimize != 0 ) |
| 1618 | { |
| 1619 | ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED; |
| 1620 | goto cleanup; |
| 1621 | } |
| 1622 | |
| 1623 | memcpy( sig, sig_try, ctx->len ); |
| 1624 | |
| 1625 | cleanup: |
| 1626 | mbedtls_free( sig_try ); |
| 1627 | mbedtls_free( verif ); |
| 1628 | |
| 1629 | return( ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1630 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1631 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1632 | |
| 1633 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1634 | * Do an RSA operation to sign the message digest |
| 1635 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1637 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1638 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1639 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1640 | mbedtls_md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1641 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1642 | const unsigned char *hash, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1643 | unsigned char *sig ) |
| 1644 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1645 | switch( ctx->padding ) |
| 1646 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1647 | #if defined(MBEDTLS_PKCS1_V15) |
| 1648 | case MBEDTLS_RSA_PKCS_V15: |
| 1649 | 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] | 1650 | hashlen, hash, sig ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1651 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1652 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1653 | #if defined(MBEDTLS_PKCS1_V21) |
| 1654 | case MBEDTLS_RSA_PKCS_V21: |
| 1655 | 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] | 1656 | hashlen, hash, sig ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1657 | #endif |
| 1658 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1659 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1660 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1661 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1664 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1665 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1666 | * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1667 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1668 | 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] | 1669 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1670 | void *p_rng, |
| 1671 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1672 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1673 | unsigned int hashlen, |
| 1674 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1675 | mbedtls_md_type_t mgf1_hash_id, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1676 | int expected_salt_len, |
| 1677 | const unsigned char *sig ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1678 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1679 | int ret; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1680 | size_t siglen; |
| 1681 | unsigned char *p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1682 | unsigned char result[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1683 | unsigned char zeros[8]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1684 | unsigned int hlen; |
| 1685 | size_t slen, msb; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1686 | const mbedtls_md_info_t *md_info; |
| 1687 | mbedtls_md_context_t md_ctx; |
Nicholas Wilson | 409401c | 2016-04-13 11:48:25 +0100 | [diff] [blame] | 1688 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1689 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1690 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 1691 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1692 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1693 | siglen = ctx->len; |
| 1694 | |
Paul Bakker | 27fdf46 | 2011-06-09 13:55:13 +0000 | [diff] [blame] | 1695 | if( siglen < 16 || siglen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1696 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1697 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1698 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1699 | ? mbedtls_rsa_public( ctx, sig, buf ) |
| 1700 | : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1701 | |
| 1702 | if( ret != 0 ) |
| 1703 | return( ret ); |
| 1704 | |
| 1705 | p = buf; |
| 1706 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1707 | if( buf[siglen - 1] != 0xBC ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1708 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
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 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1711 | { |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1712 | /* Gather length of hash to sign */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1713 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1714 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1715 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1716 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1717 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1718 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1719 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1720 | md_info = mbedtls_md_info_from_type( mgf1_hash_id ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1721 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1722 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1724 | hlen = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1725 | slen = siglen - hlen - 1; /* Currently length of salt + padding */ |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1726 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1727 | memset( zeros, 0, 8 ); |
Paul Bakker | 53019ae | 2011-03-25 13:58:48 +0000 | [diff] [blame] | 1728 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1729 | /* |
| 1730 | * Note: EMSA-PSS verification is over the length of N - 1 bits |
| 1731 | */ |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 1732 | msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1733 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1734 | /* Compensate for boundary condition when applying mask */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1735 | if( msb % 8 == 0 ) |
| 1736 | { |
| 1737 | p++; |
| 1738 | siglen -= 1; |
| 1739 | } |
| 1740 | if( buf[0] >> ( 8 - siglen * 8 + msb ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1741 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1742 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1743 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1744 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 1745 | { |
| 1746 | mbedtls_md_free( &md_ctx ); |
| 1747 | return( ret ); |
| 1748 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1749 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1750 | 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] | 1751 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1752 | buf[0] &= 0xFF >> ( siglen * 8 - msb ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1753 | |
Paul Bakker | 4de44aa | 2013-12-31 11:43:01 +0100 | [diff] [blame] | 1754 | while( p < buf + siglen && *p == 0 ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1755 | p++; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1756 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1757 | if( p == buf + siglen || |
| 1758 | *p++ != 0x01 ) |
| 1759 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1760 | mbedtls_md_free( &md_ctx ); |
| 1761 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1762 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1763 | |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1764 | /* Actual salt len */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1765 | slen -= p - buf; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1766 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1767 | if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY && |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1768 | slen != (size_t) expected_salt_len ) |
| 1769 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1770 | mbedtls_md_free( &md_ctx ); |
| 1771 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1772 | } |
| 1773 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1774 | /* |
| 1775 | * Generate H = Hash( M' ) |
| 1776 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1777 | mbedtls_md_starts( &md_ctx ); |
| 1778 | mbedtls_md_update( &md_ctx, zeros, 8 ); |
| 1779 | mbedtls_md_update( &md_ctx, hash, hashlen ); |
| 1780 | mbedtls_md_update( &md_ctx, p, slen ); |
| 1781 | mbedtls_md_finish( &md_ctx, result ); |
Paul Bakker | 53019ae | 2011-03-25 13:58:48 +0000 | [diff] [blame] | 1782 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1783 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1784 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1785 | if( memcmp( p + slen, result, hlen ) == 0 ) |
| 1786 | return( 0 ); |
| 1787 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1788 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1789 | } |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1790 | |
| 1791 | /* |
| 1792 | * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function |
| 1793 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1794 | int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1795 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1796 | void *p_rng, |
| 1797 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1798 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1799 | unsigned int hashlen, |
| 1800 | const unsigned char *hash, |
| 1801 | const unsigned char *sig ) |
| 1802 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1803 | mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE ) |
| 1804 | ? (mbedtls_md_type_t) ctx->hash_id |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1805 | : md_alg; |
| 1806 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1807 | 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] | 1808 | md_alg, hashlen, hash, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1809 | mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1810 | sig ) ); |
| 1811 | |
| 1812 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1813 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | 40628ba | 2013-01-03 10:50:31 +0100 | [diff] [blame] | 1814 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1815 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1816 | /* |
| 1817 | * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function |
| 1818 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1819 | int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1820 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1821 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1822 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1823 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1824 | unsigned int hashlen, |
| 1825 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 1826 | const unsigned char *sig ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1827 | { |
| 1828 | int ret; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1829 | size_t len, siglen, asn1_len; |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 1830 | unsigned char *p, *p0, *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1831 | mbedtls_md_type_t msg_md_alg; |
| 1832 | const mbedtls_md_info_t *md_info; |
| 1833 | mbedtls_asn1_buf oid; |
Nicholas Wilson | 409401c | 2016-04-13 11:48:25 +0100 | [diff] [blame] | 1834 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1835 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1836 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 1837 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1838 | |
| 1839 | siglen = ctx->len; |
| 1840 | |
| 1841 | if( siglen < 16 || siglen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1842 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1843 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1844 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1845 | ? mbedtls_rsa_public( ctx, sig, buf ) |
| 1846 | : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1847 | |
| 1848 | if( ret != 0 ) |
| 1849 | return( ret ); |
| 1850 | |
| 1851 | p = buf; |
| 1852 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1853 | if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN ) |
| 1854 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1855 | |
| 1856 | while( *p != 0 ) |
| 1857 | { |
| 1858 | if( p >= buf + siglen - 1 || *p != 0xFF ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1859 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1860 | p++; |
| 1861 | } |
Manuel Pégourié-Gonnard | c1380de | 2017-05-11 12:49:51 +0200 | [diff] [blame] | 1862 | p++; /* skip 00 byte */ |
| 1863 | |
| 1864 | /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */ |
| 1865 | if( p - buf < 11 ) |
| 1866 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1867 | |
| 1868 | len = siglen - ( p - buf ); |
| 1869 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1870 | if( len == hashlen && md_alg == MBEDTLS_MD_NONE ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1871 | { |
| 1872 | if( memcmp( p, hash, hashlen ) == 0 ) |
| 1873 | return( 0 ); |
| 1874 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1875 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1878 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1879 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1880 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1881 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1882 | |
| 1883 | end = p + len; |
| 1884 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1885 | /* |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1886 | * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure. |
| 1887 | * Insist on 2-byte length tags, to protect against variants of |
| 1888 | * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification. |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1889 | */ |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 1890 | p0 = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1891 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, |
| 1892 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 1893 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 1894 | if( p != p0 + 2 || asn1_len + 2 != len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1895 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1896 | |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1897 | p0 = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1898 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, |
| 1899 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 1900 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1901 | if( p != p0 + 2 || asn1_len + 6 + hashlen != len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1902 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1903 | |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1904 | p0 = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1905 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 ) |
| 1906 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1907 | if( p != p0 + 2 ) |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 1908 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1909 | |
| 1910 | oid.p = p; |
| 1911 | p += oid.len; |
| 1912 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1913 | if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 ) |
| 1914 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1915 | |
| 1916 | if( md_alg != msg_md_alg ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1917 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1918 | |
| 1919 | /* |
| 1920 | * assume the algorithm parameters must be NULL |
| 1921 | */ |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1922 | p0 = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1923 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 ) |
| 1924 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1925 | if( p != p0 + 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1926 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1927 | |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 1928 | p0 = p; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1929 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
| 1930 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 1931 | if( p != p0 + 2 || asn1_len != hashlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1932 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1933 | |
| 1934 | if( memcmp( p, hash, hashlen ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1935 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1936 | |
| 1937 | p += hashlen; |
| 1938 | |
| 1939 | if( p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1940 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1941 | |
| 1942 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1943 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1944 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1945 | |
| 1946 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1947 | * Do an RSA operation and check the message digest |
| 1948 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1949 | int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1950 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1951 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1952 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1953 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1954 | unsigned int hashlen, |
| 1955 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 1956 | const unsigned char *sig ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1957 | { |
| 1958 | switch( ctx->padding ) |
| 1959 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1960 | #if defined(MBEDTLS_PKCS1_V15) |
| 1961 | case MBEDTLS_RSA_PKCS_V15: |
| 1962 | 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] | 1963 | hashlen, hash, sig ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1964 | #endif |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1965 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1966 | #if defined(MBEDTLS_PKCS1_V21) |
| 1967 | case MBEDTLS_RSA_PKCS_V21: |
| 1968 | 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] | 1969 | hashlen, hash, sig ); |
| 1970 | #endif |
| 1971 | |
| 1972 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1973 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | /* |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1978 | * Copy the components of an RSA key |
| 1979 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1980 | 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] | 1981 | { |
| 1982 | int ret; |
| 1983 | |
| 1984 | dst->ver = src->ver; |
| 1985 | dst->len = src->len; |
| 1986 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1987 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) ); |
| 1988 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1989 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1990 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) ); |
| 1991 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) ); |
| 1992 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) ); |
Hanno Becker | 33c30a0 | 2017-08-23 07:00:22 +0100 | [diff] [blame] | 1993 | |
| 1994 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1995 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) ); |
| 1996 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) ); |
| 1997 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1998 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) ); |
| 1999 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) ); |
Hanno Becker | 33c30a0 | 2017-08-23 07:00:22 +0100 | [diff] [blame] | 2000 | #endif |
| 2001 | |
| 2002 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2003 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2004 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) ); |
| 2005 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) ); |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 2006 | |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2007 | dst->padding = src->padding; |
Manuel Pégourié-Gonnard | fdddac9 | 2014-03-25 15:58:35 +0100 | [diff] [blame] | 2008 | dst->hash_id = src->hash_id; |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2009 | |
| 2010 | cleanup: |
| 2011 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2012 | mbedtls_rsa_free( dst ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2013 | |
| 2014 | return( ret ); |
| 2015 | } |
| 2016 | |
| 2017 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2018 | * Free the components of an RSA key |
| 2019 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2020 | void mbedtls_rsa_free( mbedtls_rsa_context *ctx ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2021 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2022 | mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf ); |
Hanno Becker | 33c30a0 | 2017-08-23 07:00:22 +0100 | [diff] [blame] | 2023 | mbedtls_mpi_free( &ctx->RN ); mbedtls_mpi_free( &ctx->D ); |
| 2024 | mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2025 | mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 2026 | |
Hanno Becker | 33c30a0 | 2017-08-23 07:00:22 +0100 | [diff] [blame] | 2027 | #if !defined(MBEDTLS_RSA_NO_CRT) |
| 2028 | mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); |
| 2029 | mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); |
| 2030 | mbedtls_mpi_free( &ctx->DP ); |
| 2031 | #endif /* MBEDTLS_RSA_NO_CRT */ |
| 2032 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2033 | #if defined(MBEDTLS_THREADING_C) |
| 2034 | mbedtls_mutex_free( &ctx->mutex ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 2035 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Hanno Becker | ab37731 | 2017-08-23 16:24:51 +0100 | [diff] [blame] | 2038 | #endif /* !MBEDTLS_RSA_ALT */ |
| 2039 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2040 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2041 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2042 | #include "mbedtls/sha1.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2043 | |
| 2044 | /* |
| 2045 | * Example RSA-1024 keypair, for test purposes |
| 2046 | */ |
| 2047 | #define KEY_LEN 128 |
| 2048 | |
| 2049 | #define RSA_N "9292758453063D803DD603D5E777D788" \ |
| 2050 | "8ED1D5BF35786190FA2F23EBC0848AEA" \ |
| 2051 | "DDA92CA6C3D80B32C4D109BE0F36D6AE" \ |
| 2052 | "7130B9CED7ACDF54CFC7555AC14EEBAB" \ |
| 2053 | "93A89813FBF3C4F8066D2D800F7C38A8" \ |
| 2054 | "1AE31942917403FF4946B0A83D3D3E05" \ |
| 2055 | "EE57C6F5F5606FB5D4BC6CD34EE0801A" \ |
| 2056 | "5E94BB77B07507233A0BC7BAC8F90F79" |
| 2057 | |
| 2058 | #define RSA_E "10001" |
| 2059 | |
| 2060 | #define RSA_D "24BF6185468786FDD303083D25E64EFC" \ |
| 2061 | "66CA472BC44D253102F8B4A9D3BFA750" \ |
| 2062 | "91386C0077937FE33FA3252D28855837" \ |
| 2063 | "AE1B484A8A9A45F7EE8C0C634F99E8CD" \ |
| 2064 | "DF79C5CE07EE72C7F123142198164234" \ |
| 2065 | "CABB724CF78B8173B9F880FC86322407" \ |
| 2066 | "AF1FEDFDDE2BEB674CA15F3E81A1521E" \ |
| 2067 | "071513A1E85B5DFA031F21ECAE91A34D" |
| 2068 | |
| 2069 | #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \ |
| 2070 | "2C01CAD19EA484A87EA4377637E75500" \ |
| 2071 | "FCB2005C5C7DD6EC4AC023CDA285D796" \ |
| 2072 | "C3D9E75E1EFC42488BB4F1D13AC30A57" |
| 2073 | |
| 2074 | #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \ |
| 2075 | "E211C2B9E5DB1ED0BF61D0D9899620F4" \ |
| 2076 | "910E4168387E3C30AA1E00C339A79508" \ |
| 2077 | "8452DD96A9A5EA5D9DCA68DA636032AF" |
| 2078 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2079 | #define PT_LEN 24 |
| 2080 | #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \ |
| 2081 | "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD" |
| 2082 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2083 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2084 | static int myrand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 2085 | { |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 2086 | #if !defined(__OpenBSD__) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2087 | size_t i; |
| 2088 | |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 2089 | if( rng_state != NULL ) |
| 2090 | rng_state = NULL; |
| 2091 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2092 | for( i = 0; i < len; ++i ) |
| 2093 | output[i] = rand(); |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 2094 | #else |
| 2095 | if( rng_state != NULL ) |
| 2096 | rng_state = NULL; |
| 2097 | |
| 2098 | arc4random_buf( output, len ); |
| 2099 | #endif /* !OpenBSD */ |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 2100 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2101 | return( 0 ); |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 2102 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2103 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 2104 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2105 | /* |
| 2106 | * Checkup routine |
| 2107 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2108 | int mbedtls_rsa_self_test( int verbose ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2109 | { |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 2110 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2111 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2112 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2113 | mbedtls_rsa_context rsa; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2114 | unsigned char rsa_plaintext[PT_LEN]; |
| 2115 | unsigned char rsa_decrypted[PT_LEN]; |
| 2116 | unsigned char rsa_ciphertext[KEY_LEN]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2117 | #if defined(MBEDTLS_SHA1_C) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 2118 | unsigned char sha1sum[20]; |
| 2119 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2120 | |
Hanno Becker | 3a70116 | 2017-08-22 13:52:43 +0100 | [diff] [blame] | 2121 | mbedtls_mpi K; |
| 2122 | |
| 2123 | mbedtls_mpi_init( &K ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2124 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2125 | |
Hanno Becker | 3a70116 | 2017-08-22 13:52:43 +0100 | [diff] [blame] | 2126 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) ); |
| 2127 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) ); |
| 2128 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) ); |
| 2129 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) ); |
| 2130 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) ); |
| 2131 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) ); |
| 2132 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) ); |
| 2133 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) ); |
| 2134 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) ); |
| 2135 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) ); |
| 2136 | |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 2137 | MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2138 | |
| 2139 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2140 | mbedtls_printf( " RSA key validation: " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2141 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2142 | if( mbedtls_rsa_check_pubkey( &rsa ) != 0 || |
| 2143 | mbedtls_rsa_check_privkey( &rsa ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2144 | { |
| 2145 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2146 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2147 | |
| 2148 | return( 1 ); |
| 2149 | } |
| 2150 | |
| 2151 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2152 | mbedtls_printf( "passed\n PKCS#1 encryption : " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2153 | |
| 2154 | memcpy( rsa_plaintext, RSA_PT, PT_LEN ); |
| 2155 | |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 2156 | if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, |
| 2157 | PT_LEN, rsa_plaintext, |
| 2158 | rsa_ciphertext ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2159 | { |
| 2160 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2161 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2162 | |
| 2163 | return( 1 ); |
| 2164 | } |
| 2165 | |
| 2166 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2167 | mbedtls_printf( "passed\n PKCS#1 decryption : " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2168 | |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 2169 | if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, |
| 2170 | &len, rsa_ciphertext, rsa_decrypted, |
| 2171 | sizeof(rsa_decrypted) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2172 | { |
| 2173 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2174 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2175 | |
| 2176 | return( 1 ); |
| 2177 | } |
| 2178 | |
| 2179 | if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 ) |
| 2180 | { |
| 2181 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2182 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2183 | |
| 2184 | return( 1 ); |
| 2185 | } |
| 2186 | |
Manuel Pégourié-Gonnard | d1004f0 | 2015-08-07 10:46:54 +0200 | [diff] [blame] | 2187 | if( verbose != 0 ) |
| 2188 | mbedtls_printf( "passed\n" ); |
| 2189 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2190 | #if defined(MBEDTLS_SHA1_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2191 | if( verbose != 0 ) |
Brian Murray | 930a370 | 2016-05-18 14:38:02 -0700 | [diff] [blame] | 2192 | mbedtls_printf( " PKCS#1 data sign : " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2193 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2194 | mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2195 | |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 2196 | if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, |
| 2197 | MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0, |
| 2198 | sha1sum, rsa_ciphertext ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2199 | { |
| 2200 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2201 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2202 | |
| 2203 | return( 1 ); |
| 2204 | } |
| 2205 | |
| 2206 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2207 | mbedtls_printf( "passed\n PKCS#1 sig. verify: " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2208 | |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 2209 | if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, |
| 2210 | MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0, |
| 2211 | sha1sum, rsa_ciphertext ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2212 | { |
| 2213 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2214 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2215 | |
| 2216 | return( 1 ); |
| 2217 | } |
| 2218 | |
| 2219 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | d1004f0 | 2015-08-07 10:46:54 +0200 | [diff] [blame] | 2220 | mbedtls_printf( "passed\n" ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2221 | #endif /* MBEDTLS_SHA1_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2222 | |
Manuel Pégourié-Gonnard | d1004f0 | 2015-08-07 10:46:54 +0200 | [diff] [blame] | 2223 | if( verbose != 0 ) |
| 2224 | mbedtls_printf( "\n" ); |
| 2225 | |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 2226 | cleanup: |
Hanno Becker | 3a70116 | 2017-08-22 13:52:43 +0100 | [diff] [blame] | 2227 | mbedtls_mpi_free( &K ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2228 | mbedtls_rsa_free( &rsa ); |
| 2229 | #else /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 3e41fe8 | 2013-09-15 17:42:50 +0200 | [diff] [blame] | 2230 | ((void) verbose); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2231 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 2232 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2235 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2236 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2237 | #endif /* MBEDTLS_RSA_C */ |