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