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