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