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