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