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