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