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 | 49e94e3 | 2020-06-26 11:19:12 +0200 | [diff] [blame] | 811 | mbedtls_mpi R; |
| 812 | |
| 813 | mbedtls_mpi_init( &R ); |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 814 | |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 815 | if( ctx->Vf.p != NULL ) |
| 816 | { |
| 817 | /* We already have blinding values, just update them by squaring */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 818 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) ); |
| 819 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) ); |
| 820 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) ); |
| 821 | 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] | 822 | |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 823 | goto cleanup; |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 824 | } |
| 825 | |
Manuel Pégourié-Gonnard | 4d89c7e | 2013-10-04 15:18:38 +0200 | [diff] [blame] | 826 | /* Unblinding value: Vf = random number, invertible mod N */ |
| 827 | do { |
| 828 | if( count++ > 10 ) |
Manuel Pégourié-Gonnard | cadcf4c | 2020-07-16 09:23:30 +0200 | [diff] [blame] | 829 | { |
| 830 | ret = MBEDTLS_ERR_RSA_RNG_FAILED; |
| 831 | goto cleanup; |
| 832 | } |
Manuel Pégourié-Gonnard | 4d89c7e | 2013-10-04 15:18:38 +0200 | [diff] [blame] | 833 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 834 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) ); |
Manuel Pégourié-Gonnard | 86ad5be | 2020-06-26 11:03:19 +0200 | [diff] [blame] | 835 | |
Manuel Pégourié-Gonnard | 87a602d | 2020-07-16 09:48:54 +0200 | [diff] [blame^] | 836 | /* Compute Vf^-1 as R * (R Vf)^-1 to avoid leaks from inv_mod. */ |
Manuel Pégourié-Gonnard | 49e94e3 | 2020-06-26 11:19:12 +0200 | [diff] [blame] | 837 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, ctx->len - 1, f_rng, p_rng ) ); |
| 838 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vf, &R ) ); |
| 839 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) ); |
| 840 | |
Manuel Pégourié-Gonnard | 87a602d | 2020-07-16 09:48:54 +0200 | [diff] [blame^] | 841 | /* At this point, Vi is invertible mod N if and only if both Vf and R |
| 842 | * are invertible mod N. If one of them isn't, we don't need to know |
| 843 | * which one, we just loop and choose new values for both of them. |
| 844 | * (Each iteration succeeds with overwhelming probability.) */ |
Manuel Pégourié-Gonnard | 49e94e3 | 2020-06-26 11:19:12 +0200 | [diff] [blame] | 845 | ret = mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vi, &ctx->N ); |
| 846 | if( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE ) |
| 847 | continue; |
| 848 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 86ad5be | 2020-06-26 11:03:19 +0200 | [diff] [blame] | 849 | goto cleanup; |
| 850 | |
Manuel Pégourié-Gonnard | 87a602d | 2020-07-16 09:48:54 +0200 | [diff] [blame^] | 851 | /* Finish the computation of Vf^-1 = R * (R Vf)^-1 */ |
Manuel Pégourié-Gonnard | 49e94e3 | 2020-06-26 11:19:12 +0200 | [diff] [blame] | 852 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &R ) ); |
| 853 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) ); |
| 854 | } while( 0 ); |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 855 | |
Manuel Pégourié-Gonnard | 87a602d | 2020-07-16 09:48:54 +0200 | [diff] [blame^] | 856 | /* Blinding value: Vi = Vf^(-e) mod N |
Manuel Pégourié-Gonnard | 49e94e3 | 2020-06-26 11:19:12 +0200 | [diff] [blame] | 857 | * (Vi already contains Vf^-1 at this point) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 858 | 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] | 859 | |
Manuel Pégourié-Gonnard | ae10299 | 2013-10-04 17:07:12 +0200 | [diff] [blame] | 860 | |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 861 | cleanup: |
Manuel Pégourié-Gonnard | 49e94e3 | 2020-06-26 11:19:12 +0200 | [diff] [blame] | 862 | mbedtls_mpi_free( &R ); |
| 863 | |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 864 | return( ret ); |
| 865 | } |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 866 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 867 | /* |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 868 | * Exponent blinding supposed to prevent side-channel attacks using multiple |
| 869 | * traces of measurements to recover the RSA key. The more collisions are there, |
| 870 | * the more bits of the key can be recovered. See [3]. |
| 871 | * |
| 872 | * Collecting n collisions with m bit long blinding value requires 2^(m-m/n) |
| 873 | * observations on avarage. |
| 874 | * |
| 875 | * For example with 28 byte blinding to achieve 2 collisions the adversary has |
| 876 | * to make 2^112 observations on avarage. |
| 877 | * |
| 878 | * (With the currently (as of 2017 April) known best algorithms breaking 2048 |
| 879 | * bit RSA requires approximately as much time as trying out 2^112 random keys. |
| 880 | * Thus in this sense with 28 byte blinding the security is not reduced by |
| 881 | * side-channel attacks like the one in [3]) |
| 882 | * |
| 883 | * This countermeasure does not help if the key recovery is possible with a |
| 884 | * single trace. |
| 885 | */ |
| 886 | #define RSA_EXPONENT_BLINDING 28 |
| 887 | |
| 888 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 889 | * Do an RSA private key operation |
| 890 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 891 | int mbedtls_rsa_private( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 892 | int (*f_rng)(void *, unsigned char *, size_t), |
| 893 | void *p_rng, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 894 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 895 | unsigned char *output ) |
| 896 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 897 | int ret; |
| 898 | size_t olen; |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 899 | |
| 900 | /* Temporary holding the result */ |
| 901 | mbedtls_mpi T; |
| 902 | |
| 903 | /* Temporaries holding P-1, Q-1 and the |
| 904 | * exponent blinding factor, respectively. */ |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 905 | mbedtls_mpi P1, Q1, R; |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 906 | |
| 907 | #if !defined(MBEDTLS_RSA_NO_CRT) |
| 908 | /* Temporaries holding the results mod p resp. mod q. */ |
| 909 | mbedtls_mpi TP, TQ; |
| 910 | |
| 911 | /* Temporaries holding the blinded exponents for |
| 912 | * the mod p resp. mod q computation (if used). */ |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 913 | mbedtls_mpi DP_blind, DQ_blind; |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 914 | |
| 915 | /* Pointers to actual exponents to be used - either the unblinded |
| 916 | * or the blinded ones, depending on the presence of a PRNG. */ |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 917 | mbedtls_mpi *DP = &ctx->DP; |
| 918 | mbedtls_mpi *DQ = &ctx->DQ; |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 919 | #else |
| 920 | /* Temporary holding the blinded exponent (if used). */ |
| 921 | mbedtls_mpi D_blind; |
| 922 | |
| 923 | /* Pointer to actual exponent to be used - either the unblinded |
| 924 | * or the blinded one, depending on the presence of a PRNG. */ |
| 925 | mbedtls_mpi *D = &ctx->D; |
Hanno Becker | 43f9472 | 2017-08-25 11:50:00 +0100 | [diff] [blame] | 926 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 927 | |
Hanno Becker | c6075cc | 2017-08-25 11:45:35 +0100 | [diff] [blame] | 928 | /* Temporaries holding the initial input and the double |
| 929 | * checked result; should be the same in the end. */ |
| 930 | mbedtls_mpi I, C; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 931 | |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 932 | RSA_VALIDATE_RET( ctx != NULL ); |
| 933 | RSA_VALIDATE_RET( input != NULL ); |
| 934 | RSA_VALIDATE_RET( output != NULL ); |
| 935 | |
Hanno Becker | ebd2c02 | 2017-10-12 10:54:53 +0100 | [diff] [blame] | 936 | if( rsa_check_context( ctx, 1 /* private key checks */, |
| 937 | f_rng != NULL /* blinding y/n */ ) != 0 ) |
| 938 | { |
Manuel Pégourié-Gonnard | fb84d38 | 2015-10-30 10:56:25 +0100 | [diff] [blame] | 939 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Hanno Becker | ebd2c02 | 2017-10-12 10:54:53 +0100 | [diff] [blame] | 940 | } |
Manuel Pégourié-Gonnard | fb84d38 | 2015-10-30 10:56:25 +0100 | [diff] [blame] | 941 | |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 942 | #if defined(MBEDTLS_THREADING_C) |
| 943 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 944 | return( ret ); |
| 945 | #endif |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 946 | |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 947 | /* MPI Initialization */ |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 948 | mbedtls_mpi_init( &T ); |
| 949 | |
| 950 | mbedtls_mpi_init( &P1 ); |
| 951 | mbedtls_mpi_init( &Q1 ); |
| 952 | mbedtls_mpi_init( &R ); |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 953 | |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 954 | if( f_rng != NULL ) |
| 955 | { |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 956 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 957 | mbedtls_mpi_init( &D_blind ); |
| 958 | #else |
| 959 | mbedtls_mpi_init( &DP_blind ); |
| 960 | mbedtls_mpi_init( &DQ_blind ); |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 961 | #endif |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 962 | } |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 963 | |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 964 | #if !defined(MBEDTLS_RSA_NO_CRT) |
| 965 | mbedtls_mpi_init( &TP ); mbedtls_mpi_init( &TQ ); |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 966 | #endif |
| 967 | |
Hanno Becker | c6075cc | 2017-08-25 11:45:35 +0100 | [diff] [blame] | 968 | mbedtls_mpi_init( &I ); |
| 969 | mbedtls_mpi_init( &C ); |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 970 | |
| 971 | /* End of MPI initialization */ |
| 972 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 973 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) ); |
| 974 | if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 975 | { |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 976 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 977 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Hanno Becker | c6075cc | 2017-08-25 11:45:35 +0100 | [diff] [blame] | 980 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &I, &T ) ); |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 981 | |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 982 | if( f_rng != NULL ) |
| 983 | { |
| 984 | /* |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 985 | * Blinding |
| 986 | * T = T * Vi mod N |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 987 | */ |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 988 | MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) ); |
| 989 | 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] | 990 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) ); |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 991 | |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 992 | /* |
| 993 | * Exponent blinding |
| 994 | */ |
| 995 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) ); |
| 996 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) ); |
| 997 | |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 998 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 999 | /* |
| 1000 | * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D |
| 1001 | */ |
| 1002 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING, |
| 1003 | f_rng, p_rng ) ); |
| 1004 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) ); |
| 1005 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) ); |
| 1006 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) ); |
| 1007 | |
| 1008 | D = &D_blind; |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1009 | #else |
| 1010 | /* |
| 1011 | * DP_blind = ( P - 1 ) * R + DP |
| 1012 | */ |
| 1013 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING, |
| 1014 | f_rng, p_rng ) ); |
| 1015 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) ); |
| 1016 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind, |
| 1017 | &ctx->DP ) ); |
| 1018 | |
| 1019 | DP = &DP_blind; |
| 1020 | |
| 1021 | /* |
| 1022 | * DQ_blind = ( Q - 1 ) * R + DQ |
| 1023 | */ |
| 1024 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING, |
| 1025 | f_rng, p_rng ) ); |
| 1026 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) ); |
| 1027 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind, |
| 1028 | &ctx->DQ ) ); |
| 1029 | |
| 1030 | DQ = &DQ_blind; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1031 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 1032 | } |
Paul Bakker | aab30c1 | 2013-08-30 11:00:25 +0200 | [diff] [blame] | 1033 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1034 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1035 | 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] | 1036 | #else |
Paul Bakker | aab30c1 | 2013-08-30 11:00:25 +0200 | [diff] [blame] | 1037 | /* |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1038 | * Faster decryption using the CRT |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1039 | * |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 1040 | * TP = input ^ dP mod P |
| 1041 | * TQ = input ^ dQ mod Q |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1042 | */ |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 1043 | |
| 1044 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &TP, &T, DP, &ctx->P, &ctx->RP ) ); |
| 1045 | 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] | 1046 | |
| 1047 | /* |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 1048 | * T = (TP - TQ) * (Q^-1 mod P) mod P |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1049 | */ |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 1050 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &TP, &TQ ) ); |
| 1051 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->QP ) ); |
| 1052 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &TP, &ctx->P ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1053 | |
| 1054 | /* |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 1055 | * T = TQ + T * Q |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1056 | */ |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 1057 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &TP, &T, &ctx->Q ) ); |
| 1058 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &TQ, &TP ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1059 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Paul Bakker | aab30c1 | 2013-08-30 11:00:25 +0200 | [diff] [blame] | 1060 | |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 1061 | if( f_rng != NULL ) |
| 1062 | { |
| 1063 | /* |
| 1064 | * Unblind |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 1065 | * T = T * Vf mod N |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 1066 | */ |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 1067 | 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] | 1068 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) ); |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 1069 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1070 | |
Hanno Becker | 2dec5e8 | 2017-10-03 07:49:52 +0100 | [diff] [blame] | 1071 | /* Verify the result to prevent glitching attacks. */ |
| 1072 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &C, &T, &ctx->E, |
| 1073 | &ctx->N, &ctx->RN ) ); |
Hanno Becker | c6075cc | 2017-08-25 11:45:35 +0100 | [diff] [blame] | 1074 | if( mbedtls_mpi_cmp_mpi( &C, &I ) != 0 ) |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 1075 | { |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 1076 | ret = MBEDTLS_ERR_RSA_VERIFY_FAILED; |
| 1077 | goto cleanup; |
| 1078 | } |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 1079 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1080 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1081 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1082 | |
| 1083 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1084 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 1085 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 1086 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
Manuel Pégourié-Gonnard | ae10299 | 2013-10-04 17:07:12 +0200 | [diff] [blame] | 1087 | #endif |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 1088 | |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 1089 | mbedtls_mpi_free( &P1 ); |
| 1090 | mbedtls_mpi_free( &Q1 ); |
| 1091 | mbedtls_mpi_free( &R ); |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1092 | |
| 1093 | if( f_rng != NULL ) |
| 1094 | { |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1095 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1096 | mbedtls_mpi_free( &D_blind ); |
| 1097 | #else |
| 1098 | mbedtls_mpi_free( &DP_blind ); |
| 1099 | mbedtls_mpi_free( &DQ_blind ); |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 1100 | #endif |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 1101 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1102 | |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 1103 | mbedtls_mpi_free( &T ); |
| 1104 | |
| 1105 | #if !defined(MBEDTLS_RSA_NO_CRT) |
| 1106 | mbedtls_mpi_free( &TP ); mbedtls_mpi_free( &TQ ); |
| 1107 | #endif |
| 1108 | |
Hanno Becker | c6075cc | 2017-08-25 11:45:35 +0100 | [diff] [blame] | 1109 | mbedtls_mpi_free( &C ); |
| 1110 | mbedtls_mpi_free( &I ); |
Hanno Becker | 06811ce | 2017-05-03 15:10:34 +0100 | [diff] [blame] | 1111 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1112 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1113 | return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1114 | |
| 1115 | return( 0 ); |
| 1116 | } |
| 1117 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1118 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1119 | /** |
| 1120 | * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer. |
| 1121 | * |
Paul Bakker | b125ed8 | 2011-11-10 13:33:51 +0000 | [diff] [blame] | 1122 | * \param dst buffer to mask |
| 1123 | * \param dlen length of destination buffer |
| 1124 | * \param src source of the mask generation |
| 1125 | * \param slen length of the source buffer |
| 1126 | * \param md_ctx message digest context to use |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1127 | */ |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1128 | 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] | 1129 | size_t slen, mbedtls_md_context_t *md_ctx ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1130 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1131 | unsigned char mask[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1132 | unsigned char counter[4]; |
| 1133 | unsigned char *p; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1134 | unsigned int hlen; |
| 1135 | size_t i, use_len; |
Andres Amaya Garcia | 94682d1 | 2017-07-20 14:26:37 +0100 | [diff] [blame] | 1136 | int ret = 0; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1137 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1138 | memset( mask, 0, MBEDTLS_MD_MAX_SIZE ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1139 | memset( counter, 0, 4 ); |
| 1140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1141 | hlen = mbedtls_md_get_size( md_ctx->md_info ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1142 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1143 | /* Generate and apply dbMask */ |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1144 | p = dst; |
| 1145 | |
| 1146 | while( dlen > 0 ) |
| 1147 | { |
| 1148 | use_len = hlen; |
| 1149 | if( dlen < hlen ) |
| 1150 | use_len = dlen; |
| 1151 | |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1152 | if( ( ret = mbedtls_md_starts( md_ctx ) ) != 0 ) |
| 1153 | goto exit; |
| 1154 | if( ( ret = mbedtls_md_update( md_ctx, src, slen ) ) != 0 ) |
| 1155 | goto exit; |
| 1156 | if( ( ret = mbedtls_md_update( md_ctx, counter, 4 ) ) != 0 ) |
| 1157 | goto exit; |
| 1158 | if( ( ret = mbedtls_md_finish( md_ctx, mask ) ) != 0 ) |
| 1159 | goto exit; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1160 | |
| 1161 | for( i = 0; i < use_len; ++i ) |
| 1162 | *p++ ^= mask[i]; |
| 1163 | |
| 1164 | counter[3]++; |
| 1165 | |
| 1166 | dlen -= use_len; |
| 1167 | } |
Gilles Peskine | 18ac716 | 2017-05-05 19:24:06 +0200 | [diff] [blame] | 1168 | |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1169 | exit: |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1170 | mbedtls_platform_zeroize( mask, sizeof( mask ) ); |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1171 | |
| 1172 | return( ret ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1173 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1174 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1175 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1176 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1177 | /* |
| 1178 | * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function |
| 1179 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1180 | int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1181 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1182 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 1183 | int mode, |
| 1184 | const unsigned char *label, size_t label_len, |
| 1185 | size_t ilen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1186 | const unsigned char *input, |
| 1187 | unsigned char *output ) |
| 1188 | { |
| 1189 | size_t olen; |
| 1190 | int ret; |
| 1191 | unsigned char *p = output; |
| 1192 | unsigned int hlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1193 | const mbedtls_md_info_t *md_info; |
| 1194 | mbedtls_md_context_t md_ctx; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1195 | |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 1196 | RSA_VALIDATE_RET( ctx != NULL ); |
| 1197 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 1198 | mode == MBEDTLS_RSA_PUBLIC ); |
| 1199 | RSA_VALIDATE_RET( output != NULL ); |
Hanno Becker | 2f660d0 | 2018-12-18 17:04:59 +0000 | [diff] [blame] | 1200 | RSA_VALIDATE_RET( input != NULL ); |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 1201 | RSA_VALIDATE_RET( label_len == 0 || label != NULL ); |
| 1202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1203 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 1204 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 1205 | |
| 1206 | if( f_rng == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1207 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1208 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1209 | 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] | 1210 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1211 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1212 | |
| 1213 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1214 | hlen = mbedtls_md_get_size( md_info ); |
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 | /* first comparison checks for overflow */ |
Janos Follath | eddfe8f | 2016-02-08 14:52:29 +0000 | [diff] [blame] | 1217 | 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] | 1218 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1219 | |
| 1220 | memset( output, 0, olen ); |
| 1221 | |
| 1222 | *p++ = 0; |
| 1223 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1224 | /* Generate a random octet string seed */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1225 | if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1226 | return( MBEDTLS_ERR_RSA_RNG_FAILED + ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1227 | |
| 1228 | p += hlen; |
| 1229 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1230 | /* Construct DB */ |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1231 | if( ( ret = mbedtls_md( md_info, label, label_len, p ) ) != 0 ) |
| 1232 | return( ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1233 | p += hlen; |
| 1234 | p += olen - 2 * hlen - 2 - ilen; |
| 1235 | *p++ = 1; |
| 1236 | memcpy( p, input, ilen ); |
| 1237 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1238 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1239 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1240 | goto exit; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1241 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1242 | /* maskedDB: Apply dbMask to DB */ |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1243 | if( ( ret = mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen, |
| 1244 | &md_ctx ) ) != 0 ) |
| 1245 | goto exit; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1246 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1247 | /* maskedSeed: Apply seedMask to seed */ |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1248 | if( ( ret = mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1, |
| 1249 | &md_ctx ) ) != 0 ) |
| 1250 | goto exit; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1251 | |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1252 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1253 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1254 | |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1255 | if( ret != 0 ) |
| 1256 | return( ret ); |
| 1257 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1258 | return( ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1259 | ? mbedtls_rsa_public( ctx, output, output ) |
| 1260 | : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1261 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1262 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1263 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1264 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1265 | /* |
| 1266 | * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function |
| 1267 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1268 | int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1269 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1270 | void *p_rng, |
| 1271 | int mode, size_t ilen, |
| 1272 | const unsigned char *input, |
| 1273 | unsigned char *output ) |
| 1274 | { |
| 1275 | size_t nb_pad, olen; |
| 1276 | int ret; |
| 1277 | unsigned char *p = output; |
| 1278 | |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 1279 | RSA_VALIDATE_RET( ctx != NULL ); |
| 1280 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 1281 | mode == MBEDTLS_RSA_PUBLIC ); |
| 1282 | RSA_VALIDATE_RET( output != NULL ); |
Hanno Becker | 2f660d0 | 2018-12-18 17:04:59 +0000 | [diff] [blame] | 1283 | RSA_VALIDATE_RET( input != NULL ); |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 1284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1285 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 1286 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 1287 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1288 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 370717b | 2016-02-11 10:35:13 +0100 | [diff] [blame] | 1289 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1290 | /* first comparison checks for overflow */ |
Janos Follath | eddfe8f | 2016-02-08 14:52:29 +0000 | [diff] [blame] | 1291 | if( ilen + 11 < ilen || olen < ilen + 11 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1292 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1293 | |
| 1294 | nb_pad = olen - 3 - ilen; |
| 1295 | |
| 1296 | *p++ = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1297 | if( mode == MBEDTLS_RSA_PUBLIC ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1298 | { |
Hanno Becker | b86e684 | 2018-12-18 14:46:04 +0000 | [diff] [blame] | 1299 | if( f_rng == NULL ) |
| 1300 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1301 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1302 | *p++ = MBEDTLS_RSA_CRYPT; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1303 | |
| 1304 | while( nb_pad-- > 0 ) |
| 1305 | { |
| 1306 | int rng_dl = 100; |
| 1307 | |
| 1308 | do { |
| 1309 | ret = f_rng( p_rng, p, 1 ); |
| 1310 | } while( *p == 0 && --rng_dl && ret == 0 ); |
| 1311 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1312 | /* Check if RNG failed to generate data */ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1313 | if( rng_dl == 0 || ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1314 | return( MBEDTLS_ERR_RSA_RNG_FAILED + ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1315 | |
| 1316 | p++; |
| 1317 | } |
| 1318 | } |
| 1319 | else |
| 1320 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1321 | *p++ = MBEDTLS_RSA_SIGN; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1322 | |
| 1323 | while( nb_pad-- > 0 ) |
| 1324 | *p++ = 0xFF; |
| 1325 | } |
| 1326 | |
| 1327 | *p++ = 0; |
| 1328 | memcpy( p, input, ilen ); |
| 1329 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1330 | return( ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1331 | ? mbedtls_rsa_public( ctx, output, output ) |
| 1332 | : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1333 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1334 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1335 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1336 | /* |
| 1337 | * Add the message padding, then do an RSA operation |
| 1338 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1339 | int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1340 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 1341 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1342 | int mode, size_t ilen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1343 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1344 | unsigned char *output ) |
| 1345 | { |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 1346 | RSA_VALIDATE_RET( ctx != NULL ); |
| 1347 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 1348 | mode == MBEDTLS_RSA_PUBLIC ); |
| 1349 | RSA_VALIDATE_RET( output != NULL ); |
Hanno Becker | 2f660d0 | 2018-12-18 17:04:59 +0000 | [diff] [blame] | 1350 | RSA_VALIDATE_RET( input != NULL ); |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 1351 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1352 | switch( ctx->padding ) |
| 1353 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1354 | #if defined(MBEDTLS_PKCS1_V15) |
| 1355 | case MBEDTLS_RSA_PKCS_V15: |
| 1356 | 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] | 1357 | input, output ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1358 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1359 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1360 | #if defined(MBEDTLS_PKCS1_V21) |
| 1361 | case MBEDTLS_RSA_PKCS_V21: |
| 1362 | 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] | 1363 | ilen, input, output ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1364 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1365 | |
| 1366 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1367 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1368 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1371 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1372 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1373 | * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1374 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1375 | int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1376 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1377 | void *p_rng, |
| 1378 | int mode, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 1379 | const unsigned char *label, size_t label_len, |
| 1380 | size_t *olen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1381 | const unsigned char *input, |
| 1382 | unsigned char *output, |
| 1383 | size_t output_max_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1384 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1385 | int ret; |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1386 | size_t ilen, i, pad_len; |
| 1387 | unsigned char *p, bad, pad_done; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1388 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
| 1389 | unsigned char lhash[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1390 | unsigned int hlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1391 | const mbedtls_md_info_t *md_info; |
| 1392 | mbedtls_md_context_t md_ctx; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1393 | |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 1394 | RSA_VALIDATE_RET( ctx != NULL ); |
| 1395 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 1396 | mode == MBEDTLS_RSA_PUBLIC ); |
| 1397 | RSA_VALIDATE_RET( output_max_len == 0 || output != NULL ); |
| 1398 | RSA_VALIDATE_RET( label_len == 0 || label != NULL ); |
| 1399 | RSA_VALIDATE_RET( input != NULL ); |
| 1400 | RSA_VALIDATE_RET( olen != NULL ); |
| 1401 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1402 | /* |
| 1403 | * Parameters sanity checks |
| 1404 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1405 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 1406 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1407 | |
| 1408 | ilen = ctx->len; |
| 1409 | |
Paul Bakker | 27fdf46 | 2011-06-09 13:55:13 +0000 | [diff] [blame] | 1410 | if( ilen < 16 || ilen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1411 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1412 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1413 | 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] | 1414 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1415 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1416 | |
Janos Follath | c17cda1 | 2016-02-11 11:08:18 +0000 | [diff] [blame] | 1417 | hlen = mbedtls_md_get_size( md_info ); |
| 1418 | |
| 1419 | // checking for integer underflow |
| 1420 | if( 2 * hlen + 2 > ilen ) |
| 1421 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1422 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1423 | /* |
| 1424 | * RSA operation |
| 1425 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1426 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1427 | ? mbedtls_rsa_public( ctx, input, buf ) |
| 1428 | : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1429 | |
| 1430 | if( ret != 0 ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1431 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1432 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1433 | /* |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1434 | * Unmask data and generate lHash |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1435 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1436 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1437 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 1438 | { |
| 1439 | mbedtls_md_free( &md_ctx ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1440 | goto cleanup; |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1443 | /* seed: Apply seedMask to maskedSeed */ |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1444 | if( ( ret = mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1, |
| 1445 | &md_ctx ) ) != 0 || |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1446 | /* DB: Apply dbMask to maskedDB */ |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1447 | ( ret = mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen, |
| 1448 | &md_ctx ) ) != 0 ) |
| 1449 | { |
| 1450 | mbedtls_md_free( &md_ctx ); |
| 1451 | goto cleanup; |
| 1452 | } |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1453 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1454 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1455 | |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1456 | /* Generate lHash */ |
| 1457 | if( ( ret = mbedtls_md( md_info, label, label_len, lhash ) ) != 0 ) |
| 1458 | goto cleanup; |
| 1459 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1460 | /* |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1461 | * Check contents, in "constant-time" |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1462 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1463 | p = buf; |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1464 | bad = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1465 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1466 | bad |= *p++; /* First byte must be 0 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1467 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1468 | p += hlen; /* Skip seed */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1469 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 1470 | /* Check lHash */ |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1471 | for( i = 0; i < hlen; i++ ) |
| 1472 | bad |= lhash[i] ^ *p++; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1473 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1474 | /* Get zero-padding len, but always read till end of buffer |
| 1475 | * (minus one, for the 01 byte) */ |
| 1476 | pad_len = 0; |
| 1477 | pad_done = 0; |
| 1478 | for( i = 0; i < ilen - 2 * hlen - 2; i++ ) |
| 1479 | { |
| 1480 | pad_done |= p[i]; |
Pascal Junod | b99183d | 2015-03-11 16:49:45 +0100 | [diff] [blame] | 1481 | pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1; |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1482 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1483 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1484 | p += pad_len; |
| 1485 | bad |= *p++ ^ 0x01; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1486 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 1487 | /* |
| 1488 | * The only information "leaked" is whether the padding was correct or not |
| 1489 | * (eg, no data is copied if it was not correct). This meets the |
| 1490 | * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between |
| 1491 | * the different error conditions. |
| 1492 | */ |
| 1493 | if( bad != 0 ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1494 | { |
| 1495 | ret = MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 1496 | goto cleanup; |
| 1497 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1498 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1499 | if( ilen - ( p - buf ) > output_max_len ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1500 | { |
| 1501 | ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
| 1502 | goto cleanup; |
| 1503 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1504 | |
| 1505 | *olen = ilen - (p - buf); |
| 1506 | memcpy( output, p, *olen ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1507 | ret = 0; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1508 | |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1509 | cleanup: |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1510 | mbedtls_platform_zeroize( buf, sizeof( buf ) ); |
| 1511 | mbedtls_platform_zeroize( lhash, sizeof( lhash ) ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1512 | |
| 1513 | return( ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1514 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1515 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1516 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1517 | #if defined(MBEDTLS_PKCS1_V15) |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1518 | /** Turn zero-or-nonzero into zero-or-all-bits-one, without branches. |
| 1519 | * |
| 1520 | * \param value The value to analyze. |
Gilles Peskine | 331d80e | 2018-10-04 18:32:29 +0200 | [diff] [blame] | 1521 | * \return Zero if \p value is zero, otherwise all-bits-one. |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1522 | */ |
Gilles Peskine | 331d80e | 2018-10-04 18:32:29 +0200 | [diff] [blame] | 1523 | static unsigned all_or_nothing_int( unsigned value ) |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1524 | { |
| 1525 | /* MSVC has a warning about unary minus on unsigned, but this is |
| 1526 | * well-defined and precisely what we want to do here */ |
| 1527 | #if defined(_MSC_VER) |
| 1528 | #pragma warning( push ) |
| 1529 | #pragma warning( disable : 4146 ) |
| 1530 | #endif |
| 1531 | return( - ( ( value | - value ) >> ( sizeof( value ) * 8 - 1 ) ) ); |
| 1532 | #if defined(_MSC_VER) |
| 1533 | #pragma warning( pop ) |
| 1534 | #endif |
| 1535 | } |
| 1536 | |
Gilles Peskine | a1af5c8 | 2018-10-04 21:18:30 +0200 | [diff] [blame] | 1537 | /** Check whether a size is out of bounds, without branches. |
| 1538 | * |
| 1539 | * This is equivalent to `size > max`, but is likely to be compiled to |
| 1540 | * to code using bitwise operation rather than a branch. |
| 1541 | * |
| 1542 | * \param size Size to check. |
| 1543 | * \param max Maximum desired value for \p size. |
| 1544 | * \return \c 0 if `size <= max`. |
| 1545 | * \return \c 1 if `size > max`. |
| 1546 | */ |
| 1547 | static unsigned size_greater_than( size_t size, size_t max ) |
| 1548 | { |
| 1549 | /* Return the sign bit (1 for negative) of (max - size). */ |
| 1550 | return( ( max - size ) >> ( sizeof( size_t ) * 8 - 1 ) ); |
| 1551 | } |
| 1552 | |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1553 | /** Choose between two integer values, without branches. |
| 1554 | * |
Gilles Peskine | 331d80e | 2018-10-04 18:32:29 +0200 | [diff] [blame] | 1555 | * This is equivalent to `cond ? if1 : if0`, but is likely to be compiled |
| 1556 | * to code using bitwise operation rather than a branch. |
| 1557 | * |
| 1558 | * \param cond Condition to test. |
| 1559 | * \param if1 Value to use if \p cond is nonzero. |
| 1560 | * \param if0 Value to use if \p cond is zero. |
| 1561 | * \return \c if1 if \p cond is nonzero, otherwise \c if0. |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1562 | */ |
Gilles Peskine | 331d80e | 2018-10-04 18:32:29 +0200 | [diff] [blame] | 1563 | static unsigned if_int( unsigned cond, unsigned if1, unsigned if0 ) |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1564 | { |
Gilles Peskine | 331d80e | 2018-10-04 18:32:29 +0200 | [diff] [blame] | 1565 | unsigned mask = all_or_nothing_int( cond ); |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1566 | return( ( mask & if1 ) | (~mask & if0 ) ); |
| 1567 | } |
| 1568 | |
Gilles Peskine | a1af5c8 | 2018-10-04 21:18:30 +0200 | [diff] [blame] | 1569 | /** Shift some data towards the left inside a buffer without leaking |
| 1570 | * the length of the data through side channels. |
| 1571 | * |
| 1572 | * `mem_move_to_left(start, total, offset)` is functionally equivalent to |
| 1573 | * ``` |
| 1574 | * memmove(start, start + offset, total - offset); |
| 1575 | * memset(start + offset, 0, total - offset); |
| 1576 | * ``` |
| 1577 | * but it strives to use a memory access pattern (and thus total timing) |
| 1578 | * that does not depend on \p offset. This timing independence comes at |
| 1579 | * the expense of performance. |
| 1580 | * |
| 1581 | * \param start Pointer to the start of the buffer. |
| 1582 | * \param total Total size of the buffer. |
| 1583 | * \param offset Offset from which to copy \p total - \p offset bytes. |
| 1584 | */ |
| 1585 | static void mem_move_to_left( void *start, |
| 1586 | size_t total, |
| 1587 | size_t offset ) |
| 1588 | { |
| 1589 | volatile unsigned char *buf = start; |
| 1590 | size_t i, n; |
| 1591 | if( total == 0 ) |
| 1592 | return; |
| 1593 | for( i = 0; i < total; i++ ) |
| 1594 | { |
| 1595 | unsigned no_op = size_greater_than( total - offset, i ); |
| 1596 | /* The first `total - offset` passes are a no-op. The last |
| 1597 | * `offset` passes shift the data one byte to the left and |
| 1598 | * zero out the last byte. */ |
| 1599 | for( n = 0; n < total - 1; n++ ) |
Gilles Peskine | 9b43070 | 2018-10-12 19:15:34 +0200 | [diff] [blame] | 1600 | { |
| 1601 | unsigned char current = buf[n]; |
| 1602 | unsigned char next = buf[n+1]; |
| 1603 | buf[n] = if_int( no_op, current, next ); |
| 1604 | } |
Gilles Peskine | a1af5c8 | 2018-10-04 21:18:30 +0200 | [diff] [blame] | 1605 | buf[total-1] = if_int( no_op, buf[total-1], 0 ); |
| 1606 | } |
| 1607 | } |
| 1608 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1609 | /* |
| 1610 | * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function |
| 1611 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1612 | int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1613 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1614 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1615 | int mode, size_t *olen, |
| 1616 | const unsigned char *input, |
| 1617 | unsigned char *output, |
Gilles Peskine | 5908dd4 | 2018-10-02 22:43:06 +0200 | [diff] [blame] | 1618 | size_t output_max_len ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1619 | { |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1620 | int ret; |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 1621 | size_t ilen, i, plaintext_max_size; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1622 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
Gilles Peskine | 85a7442 | 2018-10-05 14:50:21 +0200 | [diff] [blame] | 1623 | /* The following variables take sensitive values: their value must |
| 1624 | * not leak into the observable behavior of the function other than |
| 1625 | * the designated outputs (output, olen, return value). Otherwise |
| 1626 | * this would open the execution of the function to |
| 1627 | * side-channel-based variants of the Bleichenbacher padding oracle |
| 1628 | * attack. Potential side channels include overall timing, memory |
| 1629 | * access patterns (especially visible to an adversary who has access |
| 1630 | * to a shared memory cache), and branches (especially visible to |
| 1631 | * an adversary who has access to a shared code cache or to a shared |
| 1632 | * branch predictor). */ |
| 1633 | size_t pad_count = 0; |
| 1634 | unsigned bad = 0; |
| 1635 | unsigned char pad_done = 0; |
| 1636 | size_t plaintext_size = 0; |
| 1637 | unsigned output_too_large; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1638 | |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 1639 | RSA_VALIDATE_RET( ctx != NULL ); |
| 1640 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 1641 | mode == MBEDTLS_RSA_PUBLIC ); |
| 1642 | RSA_VALIDATE_RET( output_max_len == 0 || output != NULL ); |
| 1643 | RSA_VALIDATE_RET( input != NULL ); |
| 1644 | RSA_VALIDATE_RET( olen != NULL ); |
| 1645 | |
| 1646 | ilen = ctx->len; |
| 1647 | plaintext_max_size = ( output_max_len > ilen - 11 ? |
| 1648 | ilen - 11 : |
| 1649 | output_max_len ); |
| 1650 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1651 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 1652 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1653 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1654 | if( ilen < 16 || ilen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1655 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1657 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1658 | ? mbedtls_rsa_public( ctx, input, buf ) |
| 1659 | : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1660 | |
| 1661 | if( ret != 0 ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1662 | goto cleanup; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1663 | |
Gilles Peskine | 40b57f4 | 2018-10-05 15:06:12 +0200 | [diff] [blame] | 1664 | /* Check and get padding length in constant time and constant |
| 1665 | * memory trace. The first byte must be 0. */ |
| 1666 | bad |= buf[0]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1667 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1668 | if( mode == MBEDTLS_RSA_PRIVATE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1669 | { |
Gilles Peskine | 40b57f4 | 2018-10-05 15:06:12 +0200 | [diff] [blame] | 1670 | /* Decode EME-PKCS1-v1_5 padding: 0x00 || 0x02 || PS || 0x00 |
| 1671 | * where PS must be at least 8 nonzero bytes. */ |
Gilles Peskine | 85a7442 | 2018-10-05 14:50:21 +0200 | [diff] [blame] | 1672 | bad |= buf[1] ^ MBEDTLS_RSA_CRYPT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1673 | |
Gilles Peskine | ec2a5fd | 2018-10-05 18:11:27 +0200 | [diff] [blame] | 1674 | /* Read the whole buffer. Set pad_done to nonzero if we find |
| 1675 | * the 0x00 byte and remember the padding length in pad_count. */ |
| 1676 | for( i = 2; i < ilen; i++ ) |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1677 | { |
Gilles Peskine | 85a7442 | 2018-10-05 14:50:21 +0200 | [diff] [blame] | 1678 | pad_done |= ((buf[i] | (unsigned char)-buf[i]) >> 7) ^ 1; |
Pascal Junod | b99183d | 2015-03-11 16:49:45 +0100 | [diff] [blame] | 1679 | pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1; |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1680 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1681 | } |
| 1682 | else |
| 1683 | { |
Gilles Peskine | 40b57f4 | 2018-10-05 15:06:12 +0200 | [diff] [blame] | 1684 | /* Decode EMSA-PKCS1-v1_5 padding: 0x00 || 0x01 || PS || 0x00 |
| 1685 | * where PS must be at least 8 bytes with the value 0xFF. */ |
Gilles Peskine | 85a7442 | 2018-10-05 14:50:21 +0200 | [diff] [blame] | 1686 | bad |= buf[1] ^ MBEDTLS_RSA_SIGN; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1687 | |
Gilles Peskine | ec2a5fd | 2018-10-05 18:11:27 +0200 | [diff] [blame] | 1688 | /* Read the whole buffer. Set pad_done to nonzero if we find |
| 1689 | * the 0x00 byte and remember the padding length in pad_count. |
| 1690 | * If there's a non-0xff byte in the padding, the padding is bad. */ |
| 1691 | for( i = 2; i < ilen; i++ ) |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1692 | { |
Gilles Peskine | 40b57f4 | 2018-10-05 15:06:12 +0200 | [diff] [blame] | 1693 | pad_done |= if_int( buf[i], 0, 1 ); |
| 1694 | pad_count += if_int( pad_done, 0, 1 ); |
| 1695 | bad |= if_int( pad_done, 0, buf[i] ^ 0xFF ); |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1696 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Gilles Peskine | 40b57f4 | 2018-10-05 15:06:12 +0200 | [diff] [blame] | 1699 | /* If pad_done is still zero, there's no data, only unfinished padding. */ |
| 1700 | bad |= if_int( pad_done, 0, 1 ); |
Janos Follath | b6eb1ca | 2016-02-08 13:59:25 +0000 | [diff] [blame] | 1701 | |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1702 | /* There must be at least 8 bytes of padding. */ |
Gilles Peskine | 8c9440a | 2018-10-04 21:24:21 +0200 | [diff] [blame] | 1703 | bad |= size_greater_than( 8, pad_count ); |
Paul Bakker | 8804f69 | 2013-02-28 18:06:26 +0100 | [diff] [blame] | 1704 | |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1705 | /* If the padding is valid, set plaintext_size to the number of |
| 1706 | * remaining bytes after stripping the padding. If the padding |
| 1707 | * is invalid, avoid leaking this fact through the size of the |
| 1708 | * output: use the maximum message size that fits in the output |
| 1709 | * buffer. Do it without branches to avoid leaking the padding |
| 1710 | * validity through timing. RSA keys are small enough that all the |
| 1711 | * size_t values involved fit in unsigned int. */ |
Gilles Peskine | 331d80e | 2018-10-04 18:32:29 +0200 | [diff] [blame] | 1712 | plaintext_size = if_int( bad, |
| 1713 | (unsigned) plaintext_max_size, |
Gilles Peskine | 85a7442 | 2018-10-05 14:50:21 +0200 | [diff] [blame] | 1714 | (unsigned) ( ilen - pad_count - 3 ) ); |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 1715 | |
Gilles Peskine | 9265ff4 | 2018-10-04 19:13:43 +0200 | [diff] [blame] | 1716 | /* 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] | 1717 | * buffer and to 1 otherwise. */ |
| 1718 | output_too_large = size_greater_than( plaintext_size, |
| 1719 | plaintext_max_size ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1720 | |
Gilles Peskine | 9265ff4 | 2018-10-04 19:13:43 +0200 | [diff] [blame] | 1721 | /* Set ret without branches to avoid timing attacks. Return: |
| 1722 | * - INVALID_PADDING if the padding is bad (bad != 0). |
| 1723 | * - OUTPUT_TOO_LARGE if the padding is good but the decrypted |
| 1724 | * plaintext does not fit in the output buffer. |
| 1725 | * - 0 if the padding is correct. */ |
Gilles Peskine | 4899247 | 2018-10-12 19:19:12 +0200 | [diff] [blame] | 1726 | ret = - (int) if_int( bad, - MBEDTLS_ERR_RSA_INVALID_PADDING, |
| 1727 | if_int( output_too_large, - MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE, |
| 1728 | 0 ) ); |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1729 | |
Gilles Peskine | 9265ff4 | 2018-10-04 19:13:43 +0200 | [diff] [blame] | 1730 | /* If the padding is bad or the plaintext is too large, zero the |
| 1731 | * data that we're about to copy to the output buffer. |
| 1732 | * We need to copy the same amount of data |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1733 | * from the same buffer whether the padding is good or not to |
| 1734 | * avoid leaking the padding validity through overall timing or |
| 1735 | * through memory or cache access patterns. */ |
Gilles Peskine | 40b57f4 | 2018-10-05 15:06:12 +0200 | [diff] [blame] | 1736 | bad = all_or_nothing_int( bad | output_too_large ); |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1737 | for( i = 11; i < ilen; i++ ) |
Gilles Peskine | 40b57f4 | 2018-10-05 15:06:12 +0200 | [diff] [blame] | 1738 | buf[i] &= ~bad; |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1739 | |
Gilles Peskine | 9265ff4 | 2018-10-04 19:13:43 +0200 | [diff] [blame] | 1740 | /* If the plaintext is too large, truncate it to the buffer size. |
| 1741 | * Copy anyway to avoid revealing the length through timing, because |
| 1742 | * revealing the length is as bad as revealing the padding validity |
| 1743 | * for a Bleichenbacher attack. */ |
| 1744 | plaintext_size = if_int( output_too_large, |
| 1745 | (unsigned) plaintext_max_size, |
| 1746 | (unsigned) plaintext_size ); |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1747 | |
Gilles Peskine | eeedabe | 2018-10-04 22:45:13 +0200 | [diff] [blame] | 1748 | /* Move the plaintext to the leftmost position where it can start in |
| 1749 | * the working buffer, i.e. make it start plaintext_max_size from |
| 1750 | * the end of the buffer. Do this with a memory access trace that |
| 1751 | * does not depend on the plaintext size. After this move, the |
| 1752 | * starting location of the plaintext is no longer sensitive |
| 1753 | * information. */ |
Gilles Peskine | 85a7442 | 2018-10-05 14:50:21 +0200 | [diff] [blame] | 1754 | mem_move_to_left( buf + ilen - plaintext_max_size, |
| 1755 | plaintext_max_size, |
Gilles Peskine | eeedabe | 2018-10-04 22:45:13 +0200 | [diff] [blame] | 1756 | plaintext_max_size - plaintext_size ); |
Gilles Peskine | 9265ff4 | 2018-10-04 19:13:43 +0200 | [diff] [blame] | 1757 | |
Gilles Peskine | eeedabe | 2018-10-04 22:45:13 +0200 | [diff] [blame] | 1758 | /* Finally copy the decrypted plaintext plus trailing zeros |
Gilles Peskine | 9265ff4 | 2018-10-04 19:13:43 +0200 | [diff] [blame] | 1759 | * into the output buffer. */ |
Gilles Peskine | 85a7442 | 2018-10-05 14:50:21 +0200 | [diff] [blame] | 1760 | memcpy( output, buf + ilen - plaintext_max_size, plaintext_max_size ); |
Gilles Peskine | 9265ff4 | 2018-10-04 19:13:43 +0200 | [diff] [blame] | 1761 | |
| 1762 | /* Report the amount of data we copied to the output buffer. In case |
| 1763 | * of errors (bad padding or output too large), the value of *olen |
| 1764 | * when this function returns is not specified. Making it equivalent |
| 1765 | * to the good case limits the risks of leaking the padding validity. */ |
Gilles Peskine | e2a10de | 2018-10-02 22:44:41 +0200 | [diff] [blame] | 1766 | *olen = plaintext_size; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1767 | |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1768 | cleanup: |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1769 | mbedtls_platform_zeroize( buf, sizeof( buf ) ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1770 | |
| 1771 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1772 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1773 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1774 | |
| 1775 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1776 | * Do an RSA operation, then remove the message padding |
| 1777 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1778 | int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1779 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1780 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1781 | int mode, size_t *olen, |
| 1782 | const unsigned char *input, |
| 1783 | unsigned char *output, |
| 1784 | size_t output_max_len) |
| 1785 | { |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 1786 | RSA_VALIDATE_RET( ctx != NULL ); |
| 1787 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 1788 | mode == MBEDTLS_RSA_PUBLIC ); |
| 1789 | RSA_VALIDATE_RET( output_max_len == 0 || output != NULL ); |
| 1790 | RSA_VALIDATE_RET( input != NULL ); |
| 1791 | RSA_VALIDATE_RET( olen != NULL ); |
| 1792 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1793 | switch( ctx->padding ) |
| 1794 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1795 | #if defined(MBEDTLS_PKCS1_V15) |
| 1796 | case MBEDTLS_RSA_PKCS_V15: |
| 1797 | 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] | 1798 | input, output, output_max_len ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1799 | #endif |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1800 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1801 | #if defined(MBEDTLS_PKCS1_V21) |
| 1802 | case MBEDTLS_RSA_PKCS_V21: |
| 1803 | 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] | 1804 | olen, input, output, |
| 1805 | output_max_len ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1806 | #endif |
| 1807 | |
| 1808 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1809 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1810 | } |
| 1811 | } |
| 1812 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1813 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1814 | /* |
| 1815 | * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function |
| 1816 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1817 | int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1818 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1819 | void *p_rng, |
| 1820 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1821 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1822 | unsigned int hashlen, |
| 1823 | const unsigned char *hash, |
| 1824 | unsigned char *sig ) |
| 1825 | { |
| 1826 | size_t olen; |
| 1827 | unsigned char *p = sig; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1828 | unsigned char salt[MBEDTLS_MD_MAX_SIZE]; |
Jaeden Amero | 3725bb2 | 2018-09-07 19:12:36 +0100 | [diff] [blame] | 1829 | size_t slen, min_slen, hlen, offset = 0; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1830 | int ret; |
| 1831 | size_t msb; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1832 | const mbedtls_md_info_t *md_info; |
| 1833 | mbedtls_md_context_t md_ctx; |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 1834 | RSA_VALIDATE_RET( ctx != NULL ); |
| 1835 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 1836 | mode == MBEDTLS_RSA_PUBLIC ); |
| 1837 | RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && |
| 1838 | hashlen == 0 ) || |
| 1839 | hash != NULL ); |
| 1840 | RSA_VALIDATE_RET( sig != NULL ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1841 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1842 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 1843 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 1844 | |
| 1845 | if( f_rng == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1846 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1847 | |
| 1848 | olen = ctx->len; |
| 1849 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1850 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1851 | { |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1852 | /* Gather length of hash to sign */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1853 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1854 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1855 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1856 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1857 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1858 | } |
| 1859 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1860 | 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] | 1861 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1862 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1863 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1864 | hlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1865 | |
Jaeden Amero | 3725bb2 | 2018-09-07 19:12:36 +0100 | [diff] [blame] | 1866 | /* Calculate the largest possible salt length. Normally this is the hash |
| 1867 | * length, which is the maximum length the salt can have. If there is not |
| 1868 | * enough room, use the maximum salt length that fits. The constraint is |
| 1869 | * that the hash length plus the salt length plus 2 bytes must be at most |
| 1870 | * the key length. This complies with FIPS 186-4 §5.5 (e) and RFC 8017 |
| 1871 | * (PKCS#1 v2.2) §9.1.1 step 3. */ |
| 1872 | min_slen = hlen - 2; |
| 1873 | if( olen < hlen + min_slen + 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1874 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Jaeden Amero | 3725bb2 | 2018-09-07 19:12:36 +0100 | [diff] [blame] | 1875 | else if( olen >= hlen + hlen + 2 ) |
| 1876 | slen = hlen; |
| 1877 | else |
| 1878 | slen = olen - hlen - 2; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1879 | |
| 1880 | memset( sig, 0, olen ); |
| 1881 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1882 | /* Generate salt of length slen */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1883 | if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1884 | return( MBEDTLS_ERR_RSA_RNG_FAILED + ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1885 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1886 | /* 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] | 1887 | msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; |
Jaeden Amero | 3725bb2 | 2018-09-07 19:12:36 +0100 | [diff] [blame] | 1888 | p += olen - hlen - slen - 2; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1889 | *p++ = 0x01; |
| 1890 | memcpy( p, salt, slen ); |
| 1891 | p += slen; |
| 1892 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1893 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1894 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1895 | goto exit; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1896 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1897 | /* Generate H = Hash( M' ) */ |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1898 | if( ( ret = mbedtls_md_starts( &md_ctx ) ) != 0 ) |
| 1899 | goto exit; |
| 1900 | if( ( ret = mbedtls_md_update( &md_ctx, p, 8 ) ) != 0 ) |
| 1901 | goto exit; |
| 1902 | if( ( ret = mbedtls_md_update( &md_ctx, hash, hashlen ) ) != 0 ) |
| 1903 | goto exit; |
| 1904 | if( ( ret = mbedtls_md_update( &md_ctx, salt, slen ) ) != 0 ) |
| 1905 | goto exit; |
| 1906 | if( ( ret = mbedtls_md_finish( &md_ctx, p ) ) != 0 ) |
| 1907 | goto exit; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1908 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1909 | /* Compensate for boundary condition when applying mask */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1910 | if( msb % 8 == 0 ) |
| 1911 | offset = 1; |
| 1912 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1913 | /* maskedDB: Apply dbMask to DB */ |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1914 | if( ( ret = mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, |
| 1915 | &md_ctx ) ) != 0 ) |
| 1916 | goto exit; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1917 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 1918 | msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1919 | sig[0] &= 0xFF >> ( olen * 8 - msb ); |
| 1920 | |
| 1921 | p += hlen; |
| 1922 | *p++ = 0xBC; |
| 1923 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1924 | mbedtls_platform_zeroize( salt, sizeof( salt ) ); |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 1925 | |
| 1926 | exit: |
| 1927 | mbedtls_md_free( &md_ctx ); |
| 1928 | |
| 1929 | if( ret != 0 ) |
| 1930 | return( ret ); |
| 1931 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1932 | return( ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1933 | ? mbedtls_rsa_public( ctx, sig, sig ) |
| 1934 | : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1935 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1936 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1937 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1938 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1939 | /* |
| 1940 | * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function |
| 1941 | */ |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 1942 | |
| 1943 | /* Construct a PKCS v1.5 encoding of a hashed message |
| 1944 | * |
| 1945 | * This is used both for signature generation and verification. |
| 1946 | * |
| 1947 | * Parameters: |
| 1948 | * - md_alg: Identifies the hash algorithm used to generate the given hash; |
Hanno Becker | e58d38c | 2017-09-27 17:09:00 +0100 | [diff] [blame] | 1949 | * MBEDTLS_MD_NONE if raw data is signed. |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 1950 | * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE. |
Hanno Becker | e58d38c | 2017-09-27 17:09:00 +0100 | [diff] [blame] | 1951 | * - hash: Buffer containing the hashed message or the raw data. |
| 1952 | * - dst_len: Length of the encoded message. |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 1953 | * - dst: Buffer to hold the encoded message. |
| 1954 | * |
| 1955 | * Assumptions: |
| 1956 | * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE. |
| 1957 | * - 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] | 1958 | * - dst points to a buffer of size at least dst_len. |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 1959 | * |
| 1960 | */ |
| 1961 | static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg, |
| 1962 | unsigned int hashlen, |
| 1963 | const unsigned char *hash, |
Hanno Becker | e58d38c | 2017-09-27 17:09:00 +0100 | [diff] [blame] | 1964 | size_t dst_len, |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 1965 | unsigned char *dst ) |
| 1966 | { |
| 1967 | size_t oid_size = 0; |
Hanno Becker | e58d38c | 2017-09-27 17:09:00 +0100 | [diff] [blame] | 1968 | size_t nb_pad = dst_len; |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 1969 | unsigned char *p = dst; |
| 1970 | const char *oid = NULL; |
| 1971 | |
| 1972 | /* Are we signing hashed or raw data? */ |
| 1973 | if( md_alg != MBEDTLS_MD_NONE ) |
| 1974 | { |
| 1975 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 1976 | if( md_info == NULL ) |
| 1977 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1978 | |
| 1979 | if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 ) |
| 1980 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1981 | |
| 1982 | hashlen = mbedtls_md_get_size( md_info ); |
| 1983 | |
| 1984 | /* Double-check that 8 + hashlen + oid_size can be used as a |
| 1985 | * 1-byte ASN.1 length encoding and that there's no overflow. */ |
| 1986 | if( 8 + hashlen + oid_size >= 0x80 || |
| 1987 | 10 + hashlen < hashlen || |
| 1988 | 10 + hashlen + oid_size < 10 + hashlen ) |
| 1989 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1990 | |
| 1991 | /* |
| 1992 | * Static bounds check: |
| 1993 | * - Need 10 bytes for five tag-length pairs. |
| 1994 | * (Insist on 1-byte length encodings to protect against variants of |
| 1995 | * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification) |
| 1996 | * - Need hashlen bytes for hash |
| 1997 | * - Need oid_size bytes for hash alg OID. |
| 1998 | */ |
| 1999 | if( nb_pad < 10 + hashlen + oid_size ) |
| 2000 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 2001 | nb_pad -= 10 + hashlen + oid_size; |
| 2002 | } |
| 2003 | else |
| 2004 | { |
| 2005 | if( nb_pad < hashlen ) |
| 2006 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 2007 | |
| 2008 | nb_pad -= hashlen; |
| 2009 | } |
| 2010 | |
Hanno Becker | 2b2f898 | 2017-09-27 17:10:03 +0100 | [diff] [blame] | 2011 | /* Need space for signature header and padding delimiter (3 bytes), |
| 2012 | * and 8 bytes for the minimal padding */ |
| 2013 | if( nb_pad < 3 + 8 ) |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2014 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 2015 | nb_pad -= 3; |
| 2016 | |
| 2017 | /* Now nb_pad is the amount of memory to be filled |
Hanno Becker | 2b2f898 | 2017-09-27 17:10:03 +0100 | [diff] [blame] | 2018 | * with padding, and at least 8 bytes long. */ |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2019 | |
| 2020 | /* Write signature header and padding */ |
| 2021 | *p++ = 0; |
| 2022 | *p++ = MBEDTLS_RSA_SIGN; |
| 2023 | memset( p, 0xFF, nb_pad ); |
| 2024 | p += nb_pad; |
| 2025 | *p++ = 0; |
| 2026 | |
| 2027 | /* Are we signing raw data? */ |
| 2028 | if( md_alg == MBEDTLS_MD_NONE ) |
| 2029 | { |
| 2030 | memcpy( p, hash, hashlen ); |
| 2031 | return( 0 ); |
| 2032 | } |
| 2033 | |
| 2034 | /* Signing hashed data, add corresponding ASN.1 structure |
| 2035 | * |
| 2036 | * DigestInfo ::= SEQUENCE { |
| 2037 | * digestAlgorithm DigestAlgorithmIdentifier, |
| 2038 | * digest Digest } |
| 2039 | * DigestAlgorithmIdentifier ::= AlgorithmIdentifier |
| 2040 | * Digest ::= OCTET STRING |
| 2041 | * |
| 2042 | * Schematic: |
| 2043 | * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ] |
| 2044 | * TAG-NULL + LEN [ NULL ] ] |
| 2045 | * TAG-OCTET + LEN [ HASH ] ] |
| 2046 | */ |
| 2047 | *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED; |
Hanno Becker | 87ae197 | 2018-01-15 15:27:56 +0000 | [diff] [blame] | 2048 | *p++ = (unsigned char)( 0x08 + oid_size + hashlen ); |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2049 | *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED; |
Hanno Becker | 87ae197 | 2018-01-15 15:27:56 +0000 | [diff] [blame] | 2050 | *p++ = (unsigned char)( 0x04 + oid_size ); |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2051 | *p++ = MBEDTLS_ASN1_OID; |
Hanno Becker | 87ae197 | 2018-01-15 15:27:56 +0000 | [diff] [blame] | 2052 | *p++ = (unsigned char) oid_size; |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2053 | memcpy( p, oid, oid_size ); |
| 2054 | p += oid_size; |
| 2055 | *p++ = MBEDTLS_ASN1_NULL; |
| 2056 | *p++ = 0x00; |
| 2057 | *p++ = MBEDTLS_ASN1_OCTET_STRING; |
Hanno Becker | 87ae197 | 2018-01-15 15:27:56 +0000 | [diff] [blame] | 2058 | *p++ = (unsigned char) hashlen; |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2059 | memcpy( p, hash, hashlen ); |
| 2060 | p += hashlen; |
| 2061 | |
| 2062 | /* Just a sanity-check, should be automatic |
| 2063 | * after the initial bounds check. */ |
Hanno Becker | e58d38c | 2017-09-27 17:09:00 +0100 | [diff] [blame] | 2064 | if( p != dst + dst_len ) |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2065 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2066 | mbedtls_platform_zeroize( dst, dst_len ); |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2067 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 2068 | } |
| 2069 | |
| 2070 | return( 0 ); |
| 2071 | } |
| 2072 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2073 | /* |
| 2074 | * Do an RSA operation to sign the message digest |
| 2075 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2076 | int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 2077 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2078 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2079 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2080 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2081 | unsigned int hashlen, |
| 2082 | const unsigned char *hash, |
| 2083 | unsigned char *sig ) |
| 2084 | { |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 2085 | int ret; |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2086 | unsigned char *sig_try = NULL, *verif = NULL; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2087 | |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 2088 | RSA_VALIDATE_RET( ctx != NULL ); |
| 2089 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 2090 | mode == MBEDTLS_RSA_PUBLIC ); |
| 2091 | RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && |
| 2092 | hashlen == 0 ) || |
| 2093 | hash != NULL ); |
| 2094 | RSA_VALIDATE_RET( sig != NULL ); |
| 2095 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2096 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 2097 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2098 | |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2099 | /* |
| 2100 | * Prepare PKCS1-v1.5 encoding (padding and hash identifier) |
| 2101 | */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2102 | |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2103 | if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, |
| 2104 | ctx->len, sig ) ) != 0 ) |
| 2105 | return( ret ); |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 2106 | |
| 2107 | /* |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2108 | * Call respective RSA primitive |
| 2109 | */ |
| 2110 | |
| 2111 | if( mode == MBEDTLS_RSA_PUBLIC ) |
| 2112 | { |
| 2113 | /* Skip verification on a public key operation */ |
| 2114 | return( mbedtls_rsa_public( ctx, sig, sig ) ); |
| 2115 | } |
| 2116 | |
| 2117 | /* Private key operation |
| 2118 | * |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 2119 | * In order to prevent Lenstra's attack, make the signature in a |
| 2120 | * temporary buffer and check it before returning it. |
| 2121 | */ |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2122 | |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 2123 | sig_try = mbedtls_calloc( 1, ctx->len ); |
Simon Butcher | 1285ab5 | 2016-01-01 21:42:47 +0000 | [diff] [blame] | 2124 | if( sig_try == NULL ) |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 2125 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 2126 | |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame] | 2127 | verif = mbedtls_calloc( 1, ctx->len ); |
Simon Butcher | 1285ab5 | 2016-01-01 21:42:47 +0000 | [diff] [blame] | 2128 | if( verif == NULL ) |
| 2129 | { |
| 2130 | mbedtls_free( sig_try ); |
| 2131 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 2132 | } |
| 2133 | |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 2134 | MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) ); |
| 2135 | MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) ); |
| 2136 | |
Hanno Becker | 171a8f1 | 2017-09-06 12:32:16 +0100 | [diff] [blame] | 2137 | if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 ) |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 2138 | { |
| 2139 | ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED; |
| 2140 | goto cleanup; |
| 2141 | } |
| 2142 | |
| 2143 | memcpy( sig, sig_try, ctx->len ); |
| 2144 | |
| 2145 | cleanup: |
| 2146 | mbedtls_free( sig_try ); |
| 2147 | mbedtls_free( verif ); |
| 2148 | |
| 2149 | return( ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2150 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2151 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2152 | |
| 2153 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2154 | * Do an RSA operation to sign the message digest |
| 2155 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2156 | int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2157 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2158 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2159 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2160 | mbedtls_md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2161 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 2162 | const unsigned char *hash, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2163 | unsigned char *sig ) |
| 2164 | { |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 2165 | RSA_VALIDATE_RET( ctx != NULL ); |
| 2166 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 2167 | mode == MBEDTLS_RSA_PUBLIC ); |
| 2168 | RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && |
| 2169 | hashlen == 0 ) || |
| 2170 | hash != NULL ); |
| 2171 | RSA_VALIDATE_RET( sig != NULL ); |
| 2172 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2173 | switch( ctx->padding ) |
| 2174 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2175 | #if defined(MBEDTLS_PKCS1_V15) |
| 2176 | case MBEDTLS_RSA_PKCS_V15: |
| 2177 | 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] | 2178 | hashlen, hash, sig ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 2179 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2181 | #if defined(MBEDTLS_PKCS1_V21) |
| 2182 | case MBEDTLS_RSA_PKCS_V21: |
| 2183 | 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] | 2184 | hashlen, hash, sig ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2185 | #endif |
| 2186 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2187 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2188 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2189 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2192 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2193 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2194 | * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2195 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2196 | 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] | 2197 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2198 | void *p_rng, |
| 2199 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2200 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2201 | unsigned int hashlen, |
| 2202 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2203 | mbedtls_md_type_t mgf1_hash_id, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2204 | int expected_salt_len, |
| 2205 | const unsigned char *sig ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2206 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2207 | int ret; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2208 | size_t siglen; |
| 2209 | unsigned char *p; |
Gilles Peskine | 6a54b02 | 2017-10-17 19:02:13 +0200 | [diff] [blame] | 2210 | unsigned char *hash_start; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2211 | unsigned char result[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2212 | unsigned char zeros[8]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2213 | unsigned int hlen; |
Gilles Peskine | 6a54b02 | 2017-10-17 19:02:13 +0200 | [diff] [blame] | 2214 | size_t observed_salt_len, msb; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2215 | const mbedtls_md_info_t *md_info; |
| 2216 | mbedtls_md_context_t md_ctx; |
Nicholas Wilson | 409401c | 2016-04-13 11:48:25 +0100 | [diff] [blame] | 2217 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2218 | |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 2219 | RSA_VALIDATE_RET( ctx != NULL ); |
| 2220 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 2221 | mode == MBEDTLS_RSA_PUBLIC ); |
| 2222 | RSA_VALIDATE_RET( sig != NULL ); |
| 2223 | RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && |
| 2224 | hashlen == 0 ) || |
| 2225 | hash != NULL ); |
| 2226 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2227 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 2228 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2229 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2230 | siglen = ctx->len; |
| 2231 | |
Paul Bakker | 27fdf46 | 2011-06-09 13:55:13 +0000 | [diff] [blame] | 2232 | if( siglen < 16 || siglen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2233 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2234 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2235 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 2236 | ? mbedtls_rsa_public( ctx, sig, buf ) |
| 2237 | : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2238 | |
| 2239 | if( ret != 0 ) |
| 2240 | return( ret ); |
| 2241 | |
| 2242 | p = buf; |
| 2243 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2244 | if( buf[siglen - 1] != 0xBC ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2245 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2246 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2247 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2248 | { |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 2249 | /* Gather length of hash to sign */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2250 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2251 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2252 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2253 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2254 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2255 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2256 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2257 | md_info = mbedtls_md_info_from_type( mgf1_hash_id ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2258 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2259 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2260 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2261 | hlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2262 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2263 | memset( zeros, 0, 8 ); |
Paul Bakker | 53019ae | 2011-03-25 13:58:48 +0000 | [diff] [blame] | 2264 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 2265 | /* |
| 2266 | * Note: EMSA-PSS verification is over the length of N - 1 bits |
| 2267 | */ |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 2268 | msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2269 | |
Gilles Peskine | b00b0da | 2017-10-19 15:23:49 +0200 | [diff] [blame] | 2270 | if( buf[0] >> ( 8 - siglen * 8 + msb ) ) |
| 2271 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 2272 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 2273 | /* Compensate for boundary condition when applying mask */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2274 | if( msb % 8 == 0 ) |
| 2275 | { |
| 2276 | p++; |
| 2277 | siglen -= 1; |
| 2278 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2279 | |
Gilles Peskine | 139108a | 2017-10-18 19:03:42 +0200 | [diff] [blame] | 2280 | if( siglen < hlen + 2 ) |
| 2281 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 2282 | hash_start = p + siglen - hlen - 1; |
| 2283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2284 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 2285 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2286 | goto exit; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2287 | |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 2288 | ret = mgf_mask( p, siglen - hlen - 1, hash_start, hlen, &md_ctx ); |
| 2289 | if( ret != 0 ) |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2290 | goto exit; |
Paul Bakker | 02303e8 | 2013-01-03 11:08:31 +0100 | [diff] [blame] | 2291 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2292 | buf[0] &= 0xFF >> ( siglen * 8 - msb ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2293 | |
Gilles Peskine | 6a54b02 | 2017-10-17 19:02:13 +0200 | [diff] [blame] | 2294 | while( p < hash_start - 1 && *p == 0 ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2295 | p++; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2296 | |
Gilles Peskine | 91048a3 | 2017-10-19 17:46:14 +0200 | [diff] [blame] | 2297 | if( *p++ != 0x01 ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2298 | { |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2299 | ret = MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 2300 | goto exit; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2301 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2302 | |
Gilles Peskine | 6a54b02 | 2017-10-17 19:02:13 +0200 | [diff] [blame] | 2303 | observed_salt_len = hash_start - p; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2304 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2305 | if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY && |
Gilles Peskine | 6a54b02 | 2017-10-17 19:02:13 +0200 | [diff] [blame] | 2306 | observed_salt_len != (size_t) expected_salt_len ) |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2307 | { |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2308 | ret = MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 2309 | goto exit; |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2310 | } |
| 2311 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 2312 | /* |
| 2313 | * Generate H = Hash( M' ) |
| 2314 | */ |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 2315 | ret = mbedtls_md_starts( &md_ctx ); |
| 2316 | if ( ret != 0 ) |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2317 | goto exit; |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 2318 | ret = mbedtls_md_update( &md_ctx, zeros, 8 ); |
| 2319 | if ( ret != 0 ) |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2320 | goto exit; |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 2321 | ret = mbedtls_md_update( &md_ctx, hash, hashlen ); |
| 2322 | if ( ret != 0 ) |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2323 | goto exit; |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 2324 | ret = mbedtls_md_update( &md_ctx, p, observed_salt_len ); |
| 2325 | if ( ret != 0 ) |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2326 | goto exit; |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 2327 | ret = mbedtls_md_finish( &md_ctx, result ); |
| 2328 | if ( ret != 0 ) |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2329 | goto exit; |
Paul Bakker | 53019ae | 2011-03-25 13:58:48 +0000 | [diff] [blame] | 2330 | |
Jaeden Amero | 66954e1 | 2018-01-25 16:05:54 +0000 | [diff] [blame] | 2331 | if( memcmp( hash_start, result, hlen ) != 0 ) |
Andres Amaya Garcia | c5c7d76 | 2017-07-20 14:42:16 +0100 | [diff] [blame] | 2332 | { |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2333 | ret = MBEDTLS_ERR_RSA_VERIFY_FAILED; |
Andres Amaya Garcia | c5c7d76 | 2017-07-20 14:42:16 +0100 | [diff] [blame] | 2334 | goto exit; |
| 2335 | } |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2336 | |
| 2337 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2338 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 2339 | |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2340 | return( ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2341 | } |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2342 | |
| 2343 | /* |
| 2344 | * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function |
| 2345 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2346 | int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2347 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2348 | void *p_rng, |
| 2349 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2350 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2351 | unsigned int hashlen, |
| 2352 | const unsigned char *hash, |
| 2353 | const unsigned char *sig ) |
| 2354 | { |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 2355 | mbedtls_md_type_t mgf1_hash_id; |
| 2356 | RSA_VALIDATE_RET( ctx != NULL ); |
| 2357 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 2358 | mode == MBEDTLS_RSA_PUBLIC ); |
| 2359 | RSA_VALIDATE_RET( sig != NULL ); |
| 2360 | RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && |
| 2361 | hashlen == 0 ) || |
| 2362 | hash != NULL ); |
| 2363 | |
| 2364 | mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2365 | ? (mbedtls_md_type_t) ctx->hash_id |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2366 | : md_alg; |
| 2367 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2368 | 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] | 2369 | md_alg, hashlen, hash, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2370 | mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 2371 | sig ) ); |
| 2372 | |
| 2373 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2374 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | 40628ba | 2013-01-03 10:50:31 +0100 | [diff] [blame] | 2375 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2376 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2377 | /* |
| 2378 | * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function |
| 2379 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2380 | int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 2381 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2382 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2383 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2384 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2385 | unsigned int hashlen, |
| 2386 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 2387 | const unsigned char *sig ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2388 | { |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2389 | int ret = 0; |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 2390 | size_t sig_len; |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2391 | unsigned char *encoded = NULL, *encoded_expected = NULL; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2392 | |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 2393 | RSA_VALIDATE_RET( ctx != NULL ); |
| 2394 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 2395 | mode == MBEDTLS_RSA_PUBLIC ); |
| 2396 | RSA_VALIDATE_RET( sig != NULL ); |
| 2397 | RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && |
| 2398 | hashlen == 0 ) || |
| 2399 | hash != NULL ); |
| 2400 | |
| 2401 | sig_len = ctx->len; |
| 2402 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2403 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 2404 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2405 | |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2406 | /* |
| 2407 | * Prepare expected PKCS1 v1.5 encoding of hash. |
| 2408 | */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2409 | |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2410 | if( ( encoded = mbedtls_calloc( 1, sig_len ) ) == NULL || |
| 2411 | ( encoded_expected = mbedtls_calloc( 1, sig_len ) ) == NULL ) |
| 2412 | { |
| 2413 | ret = MBEDTLS_ERR_MPI_ALLOC_FAILED; |
| 2414 | goto cleanup; |
| 2415 | } |
| 2416 | |
| 2417 | if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, sig_len, |
| 2418 | encoded_expected ) ) != 0 ) |
| 2419 | goto cleanup; |
| 2420 | |
| 2421 | /* |
| 2422 | * Apply RSA primitive to get what should be PKCS1 encoded hash. |
| 2423 | */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2424 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2425 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2426 | ? mbedtls_rsa_public( ctx, sig, encoded ) |
| 2427 | : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, encoded ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2428 | if( ret != 0 ) |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2429 | goto cleanup; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2430 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 2431 | /* |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2432 | * Compare |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 2433 | */ |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2434 | |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2435 | if( ( ret = mbedtls_safer_memcmp( encoded, encoded_expected, |
| 2436 | sig_len ) ) != 0 ) |
| 2437 | { |
| 2438 | ret = MBEDTLS_ERR_RSA_VERIFY_FAILED; |
| 2439 | goto cleanup; |
| 2440 | } |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2441 | |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2442 | cleanup: |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2443 | |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2444 | if( encoded != NULL ) |
| 2445 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2446 | mbedtls_platform_zeroize( encoded, sig_len ); |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2447 | mbedtls_free( encoded ); |
| 2448 | } |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2449 | |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2450 | if( encoded_expected != NULL ) |
| 2451 | { |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 2452 | mbedtls_platform_zeroize( encoded_expected, sig_len ); |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2453 | mbedtls_free( encoded_expected ); |
| 2454 | } |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 2455 | |
Hanno Becker | 64a8c0a | 2017-09-06 12:39:49 +0100 | [diff] [blame] | 2456 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2457 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2458 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2459 | |
| 2460 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2461 | * Do an RSA operation and check the message digest |
| 2462 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2463 | int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 2464 | int (*f_rng)(void *, unsigned char *, size_t), |
| 2465 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2466 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2467 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2468 | unsigned int hashlen, |
| 2469 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 2470 | const unsigned char *sig ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2471 | { |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 2472 | RSA_VALIDATE_RET( ctx != NULL ); |
| 2473 | RSA_VALIDATE_RET( mode == MBEDTLS_RSA_PRIVATE || |
| 2474 | mode == MBEDTLS_RSA_PUBLIC ); |
| 2475 | RSA_VALIDATE_RET( sig != NULL ); |
| 2476 | RSA_VALIDATE_RET( ( md_alg == MBEDTLS_MD_NONE && |
| 2477 | hashlen == 0 ) || |
| 2478 | hash != NULL ); |
| 2479 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2480 | switch( ctx->padding ) |
| 2481 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2482 | #if defined(MBEDTLS_PKCS1_V15) |
| 2483 | case MBEDTLS_RSA_PKCS_V15: |
| 2484 | 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] | 2485 | hashlen, hash, sig ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 2486 | #endif |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2487 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2488 | #if defined(MBEDTLS_PKCS1_V21) |
| 2489 | case MBEDTLS_RSA_PKCS_V21: |
| 2490 | 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] | 2491 | hashlen, hash, sig ); |
| 2492 | #endif |
| 2493 | |
| 2494 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2495 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | /* |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2500 | * Copy the components of an RSA key |
| 2501 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2502 | 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] | 2503 | { |
| 2504 | int ret; |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 2505 | RSA_VALIDATE_RET( dst != NULL ); |
| 2506 | RSA_VALIDATE_RET( src != NULL ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2507 | |
| 2508 | dst->ver = src->ver; |
| 2509 | dst->len = src->len; |
| 2510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2511 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) ); |
| 2512 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2513 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2514 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) ); |
| 2515 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) ); |
| 2516 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) ); |
Hanno Becker | 33c30a0 | 2017-08-23 07:00:22 +0100 | [diff] [blame] | 2517 | |
| 2518 | #if !defined(MBEDTLS_RSA_NO_CRT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2519 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) ); |
| 2520 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) ); |
| 2521 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2522 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) ); |
| 2523 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) ); |
Hanno Becker | 33c30a0 | 2017-08-23 07:00:22 +0100 | [diff] [blame] | 2524 | #endif |
| 2525 | |
| 2526 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2527 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2528 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) ); |
| 2529 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) ); |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 2530 | |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2531 | dst->padding = src->padding; |
Manuel Pégourié-Gonnard | fdddac9 | 2014-03-25 15:58:35 +0100 | [diff] [blame] | 2532 | dst->hash_id = src->hash_id; |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2533 | |
| 2534 | cleanup: |
| 2535 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2536 | mbedtls_rsa_free( dst ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 2537 | |
| 2538 | return( ret ); |
| 2539 | } |
| 2540 | |
| 2541 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2542 | * Free the components of an RSA key |
| 2543 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2544 | void mbedtls_rsa_free( mbedtls_rsa_context *ctx ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2545 | { |
Hanno Becker | ddeeed7 | 2018-12-13 18:07:00 +0000 | [diff] [blame] | 2546 | if( ctx == NULL ) |
| 2547 | return; |
| 2548 | |
irwir | 2239a86 | 2018-06-12 18:25:09 +0300 | [diff] [blame] | 2549 | mbedtls_mpi_free( &ctx->Vi ); |
| 2550 | mbedtls_mpi_free( &ctx->Vf ); |
| 2551 | mbedtls_mpi_free( &ctx->RN ); |
| 2552 | mbedtls_mpi_free( &ctx->D ); |
| 2553 | mbedtls_mpi_free( &ctx->Q ); |
| 2554 | mbedtls_mpi_free( &ctx->P ); |
| 2555 | mbedtls_mpi_free( &ctx->E ); |
| 2556 | mbedtls_mpi_free( &ctx->N ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 2557 | |
Hanno Becker | 33c30a0 | 2017-08-23 07:00:22 +0100 | [diff] [blame] | 2558 | #if !defined(MBEDTLS_RSA_NO_CRT) |
irwir | 2239a86 | 2018-06-12 18:25:09 +0300 | [diff] [blame] | 2559 | mbedtls_mpi_free( &ctx->RQ ); |
| 2560 | mbedtls_mpi_free( &ctx->RP ); |
| 2561 | mbedtls_mpi_free( &ctx->QP ); |
| 2562 | mbedtls_mpi_free( &ctx->DQ ); |
Hanno Becker | 33c30a0 | 2017-08-23 07:00:22 +0100 | [diff] [blame] | 2563 | mbedtls_mpi_free( &ctx->DP ); |
| 2564 | #endif /* MBEDTLS_RSA_NO_CRT */ |
| 2565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2566 | #if defined(MBEDTLS_THREADING_C) |
| 2567 | mbedtls_mutex_free( &ctx->mutex ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 2568 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2569 | } |
| 2570 | |
Hanno Becker | ab37731 | 2017-08-23 16:24:51 +0100 | [diff] [blame] | 2571 | #endif /* !MBEDTLS_RSA_ALT */ |
| 2572 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2573 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2574 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2575 | #include "mbedtls/sha1.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2576 | |
| 2577 | /* |
| 2578 | * Example RSA-1024 keypair, for test purposes |
| 2579 | */ |
| 2580 | #define KEY_LEN 128 |
| 2581 | |
| 2582 | #define RSA_N "9292758453063D803DD603D5E777D788" \ |
| 2583 | "8ED1D5BF35786190FA2F23EBC0848AEA" \ |
| 2584 | "DDA92CA6C3D80B32C4D109BE0F36D6AE" \ |
| 2585 | "7130B9CED7ACDF54CFC7555AC14EEBAB" \ |
| 2586 | "93A89813FBF3C4F8066D2D800F7C38A8" \ |
| 2587 | "1AE31942917403FF4946B0A83D3D3E05" \ |
| 2588 | "EE57C6F5F5606FB5D4BC6CD34EE0801A" \ |
| 2589 | "5E94BB77B07507233A0BC7BAC8F90F79" |
| 2590 | |
| 2591 | #define RSA_E "10001" |
| 2592 | |
| 2593 | #define RSA_D "24BF6185468786FDD303083D25E64EFC" \ |
| 2594 | "66CA472BC44D253102F8B4A9D3BFA750" \ |
| 2595 | "91386C0077937FE33FA3252D28855837" \ |
| 2596 | "AE1B484A8A9A45F7EE8C0C634F99E8CD" \ |
| 2597 | "DF79C5CE07EE72C7F123142198164234" \ |
| 2598 | "CABB724CF78B8173B9F880FC86322407" \ |
| 2599 | "AF1FEDFDDE2BEB674CA15F3E81A1521E" \ |
| 2600 | "071513A1E85B5DFA031F21ECAE91A34D" |
| 2601 | |
| 2602 | #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \ |
| 2603 | "2C01CAD19EA484A87EA4377637E75500" \ |
| 2604 | "FCB2005C5C7DD6EC4AC023CDA285D796" \ |
| 2605 | "C3D9E75E1EFC42488BB4F1D13AC30A57" |
| 2606 | |
| 2607 | #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \ |
| 2608 | "E211C2B9E5DB1ED0BF61D0D9899620F4" \ |
| 2609 | "910E4168387E3C30AA1E00C339A79508" \ |
| 2610 | "8452DD96A9A5EA5D9DCA68DA636032AF" |
| 2611 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2612 | #define PT_LEN 24 |
| 2613 | #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \ |
| 2614 | "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD" |
| 2615 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2616 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2617 | static int myrand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 2618 | { |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 2619 | #if !defined(__OpenBSD__) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2620 | size_t i; |
| 2621 | |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 2622 | if( rng_state != NULL ) |
| 2623 | rng_state = NULL; |
| 2624 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2625 | for( i = 0; i < len; ++i ) |
| 2626 | output[i] = rand(); |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 2627 | #else |
| 2628 | if( rng_state != NULL ) |
| 2629 | rng_state = NULL; |
| 2630 | |
| 2631 | arc4random_buf( output, len ); |
| 2632 | #endif /* !OpenBSD */ |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 2633 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 2634 | return( 0 ); |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 2635 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2636 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 2637 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2638 | /* |
| 2639 | * Checkup routine |
| 2640 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2641 | int mbedtls_rsa_self_test( int verbose ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2642 | { |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 2643 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2644 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 2645 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2646 | mbedtls_rsa_context rsa; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2647 | unsigned char rsa_plaintext[PT_LEN]; |
| 2648 | unsigned char rsa_decrypted[PT_LEN]; |
| 2649 | unsigned char rsa_ciphertext[KEY_LEN]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2650 | #if defined(MBEDTLS_SHA1_C) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 2651 | unsigned char sha1sum[20]; |
| 2652 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2653 | |
Hanno Becker | 3a70116 | 2017-08-22 13:52:43 +0100 | [diff] [blame] | 2654 | mbedtls_mpi K; |
| 2655 | |
| 2656 | mbedtls_mpi_init( &K ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2657 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2658 | |
Hanno Becker | 3a70116 | 2017-08-22 13:52:43 +0100 | [diff] [blame] | 2659 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_N ) ); |
| 2660 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, &K, NULL, NULL, NULL, NULL ) ); |
| 2661 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_P ) ); |
| 2662 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, &K, NULL, NULL, NULL ) ); |
| 2663 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_Q ) ); |
| 2664 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, &K, NULL, NULL ) ); |
| 2665 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_D ) ); |
| 2666 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, &K, NULL ) ); |
| 2667 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &K, 16, RSA_E ) ); |
| 2668 | MBEDTLS_MPI_CHK( mbedtls_rsa_import( &rsa, NULL, NULL, NULL, NULL, &K ) ); |
| 2669 | |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 2670 | MBEDTLS_MPI_CHK( mbedtls_rsa_complete( &rsa ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2671 | |
| 2672 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2673 | mbedtls_printf( " RSA key validation: " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2675 | if( mbedtls_rsa_check_pubkey( &rsa ) != 0 || |
| 2676 | mbedtls_rsa_check_privkey( &rsa ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2677 | { |
| 2678 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2679 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2680 | |
Hanno Becker | 5bc8729 | 2017-05-03 15:09:31 +0100 | [diff] [blame] | 2681 | ret = 1; |
| 2682 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
| 2685 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2686 | mbedtls_printf( "passed\n PKCS#1 encryption : " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2687 | |
| 2688 | memcpy( rsa_plaintext, RSA_PT, PT_LEN ); |
| 2689 | |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 2690 | if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, |
| 2691 | PT_LEN, rsa_plaintext, |
| 2692 | rsa_ciphertext ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2693 | { |
| 2694 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2695 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2696 | |
Hanno Becker | 5bc8729 | 2017-05-03 15:09:31 +0100 | [diff] [blame] | 2697 | ret = 1; |
| 2698 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2702 | mbedtls_printf( "passed\n PKCS#1 decryption : " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2703 | |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 2704 | if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, |
| 2705 | &len, rsa_ciphertext, rsa_decrypted, |
| 2706 | sizeof(rsa_decrypted) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2707 | { |
| 2708 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2709 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2710 | |
Hanno Becker | 5bc8729 | 2017-05-03 15:09:31 +0100 | [diff] [blame] | 2711 | ret = 1; |
| 2712 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 ) |
| 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 | |
Manuel Pégourié-Gonnard | d1004f0 | 2015-08-07 10:46:54 +0200 | [diff] [blame] | 2724 | if( verbose != 0 ) |
| 2725 | mbedtls_printf( "passed\n" ); |
| 2726 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2727 | #if defined(MBEDTLS_SHA1_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2728 | if( verbose != 0 ) |
Brian Murray | 930a370 | 2016-05-18 14:38:02 -0700 | [diff] [blame] | 2729 | mbedtls_printf( " PKCS#1 data sign : " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2730 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 2731 | if( mbedtls_sha1_ret( rsa_plaintext, PT_LEN, sha1sum ) != 0 ) |
Andres Amaya Garcia | 698089e | 2017-06-28 11:46:46 +0100 | [diff] [blame] | 2732 | { |
| 2733 | if( verbose != 0 ) |
| 2734 | mbedtls_printf( "failed\n" ); |
| 2735 | |
| 2736 | return( 1 ); |
| 2737 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2738 | |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 2739 | if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, |
| 2740 | MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0, |
| 2741 | sha1sum, rsa_ciphertext ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2742 | { |
| 2743 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2744 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2745 | |
Hanno Becker | 5bc8729 | 2017-05-03 15:09:31 +0100 | [diff] [blame] | 2746 | ret = 1; |
| 2747 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
| 2750 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2751 | mbedtls_printf( "passed\n PKCS#1 sig. verify: " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2752 | |
Hanno Becker | 98838b0 | 2017-10-02 13:16:10 +0100 | [diff] [blame] | 2753 | if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, |
| 2754 | MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0, |
| 2755 | sha1sum, rsa_ciphertext ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2756 | { |
| 2757 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2758 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2759 | |
Hanno Becker | 5bc8729 | 2017-05-03 15:09:31 +0100 | [diff] [blame] | 2760 | ret = 1; |
| 2761 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
| 2764 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | d1004f0 | 2015-08-07 10:46:54 +0200 | [diff] [blame] | 2765 | mbedtls_printf( "passed\n" ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2766 | #endif /* MBEDTLS_SHA1_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2767 | |
Manuel Pégourié-Gonnard | d1004f0 | 2015-08-07 10:46:54 +0200 | [diff] [blame] | 2768 | if( verbose != 0 ) |
| 2769 | mbedtls_printf( "\n" ); |
| 2770 | |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 2771 | cleanup: |
Hanno Becker | 3a70116 | 2017-08-22 13:52:43 +0100 | [diff] [blame] | 2772 | mbedtls_mpi_free( &K ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2773 | mbedtls_rsa_free( &rsa ); |
| 2774 | #else /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 3e41fe8 | 2013-09-15 17:42:50 +0200 | [diff] [blame] | 2775 | ((void) verbose); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2776 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 2777 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2780 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 2781 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 2782 | #endif /* MBEDTLS_RSA_C */ |