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