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