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