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