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 |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | /* |
Simon Butcher | bdae02c | 2016-01-20 00:44:42 +0000 | [diff] [blame] | 22 | * The following sources were referenced in the design of this implementation |
| 23 | * of the RSA algorithm: |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 24 | * |
Simon Butcher | bdae02c | 2016-01-20 00:44:42 +0000 | [diff] [blame] | 25 | * [1] A method for obtaining digital signatures and public-key cryptosystems |
| 26 | * R Rivest, A Shamir, and L Adleman |
| 27 | * http://people.csail.mit.edu/rivest/pubs.html#RSA78 |
| 28 | * |
| 29 | * [2] Handbook of Applied Cryptography - 1997, Chapter 8 |
| 30 | * Menezes, van Oorschot and Vanstone |
| 31 | * |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 32 | * [3] Malware Guard Extension: Using SGX to Conceal Cache Attacks |
| 33 | * Michael Schwarz, Samuel Weiser, Daniel Gruss, Clémentine Maurice and |
| 34 | * Stefan Mangard |
| 35 | * https://arxiv.org/abs/1702.08719v2 |
| 36 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 | */ |
| 38 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 41 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 43 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 45 | #if defined(MBEDTLS_RSA_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 46 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 47 | #include "mbedtls/rsa.h" |
| 48 | #include "mbedtls/oid.h" |
Paul Bakker | bb51f0c | 2012-08-23 07:46:58 +0000 | [diff] [blame] | 49 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 50 | #include <string.h> |
| 51 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #if defined(MBEDTLS_PKCS1_V21) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 53 | #include "mbedtls/md.h" |
Paul Bakker | bb51f0c | 2012-08-23 07:46:58 +0000 | [diff] [blame] | 54 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | #include <stdlib.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 58 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 61 | #include "mbedtls/platform.h" |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 62 | #else |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 63 | #include <stdio.h> |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 65 | #define mbedtls_calloc calloc |
| 66 | #define mbedtls_free free |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 67 | #endif |
| 68 | |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 69 | /* Implementation that should never be optimized out by the compiler */ |
| 70 | static void mbedtls_zeroize( void *v, size_t n ) { |
| 71 | volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0; |
| 72 | } |
| 73 | |
Hanno Becker | 171a8f1 | 2017-09-06 12:32:16 +0100 | [diff] [blame] | 74 | /* constant-time buffer comparison */ |
| 75 | static inline int mbedtls_safer_memcmp( const void *a, const void *b, size_t n ) |
| 76 | { |
| 77 | size_t i; |
| 78 | const unsigned char *A = (const unsigned char *) a; |
| 79 | const unsigned char *B = (const unsigned char *) b; |
| 80 | unsigned char diff = 0; |
| 81 | |
| 82 | for( i = 0; i < n; i++ ) |
| 83 | diff |= A[i] ^ B[i]; |
| 84 | |
| 85 | return( diff ); |
| 86 | } |
| 87 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | /* |
| 89 | * Initialize an RSA context |
| 90 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 91 | void mbedtls_rsa_init( mbedtls_rsa_context *ctx, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 92 | int padding, |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 93 | int hash_id ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 94 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 95 | memset( ctx, 0, sizeof( mbedtls_rsa_context ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | mbedtls_rsa_set_padding( ctx, padding, hash_id ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 98 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | #if defined(MBEDTLS_THREADING_C) |
| 100 | mbedtls_mutex_init( &ctx->mutex ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 101 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 104 | /* |
| 105 | * Set padding for an existing RSA context |
| 106 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id ) |
Manuel Pégourié-Gonnard | 844a4c0 | 2014-03-10 21:55:35 +0100 | [diff] [blame] | 108 | { |
| 109 | ctx->padding = padding; |
| 110 | ctx->hash_id = hash_id; |
| 111 | } |
| 112 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | #if defined(MBEDTLS_GENPRIME) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * Generate an RSA keypair |
| 117 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 119 | int (*f_rng)(void *, unsigned char *, size_t), |
| 120 | void *p_rng, |
| 121 | unsigned int nbits, int exponent ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | { |
| 123 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | mbedtls_mpi P1, Q1, H, G; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 126 | if( f_rng == NULL || nbits < 128 || exponent < 3 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | |
Janos Follath | ef44178 | 2016-09-21 13:18:12 +0100 | [diff] [blame] | 129 | if( nbits % 2 ) |
| 130 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 131 | |
| 132 | mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); |
Janos Follath | 10c575b | 2016-02-23 14:42:48 +0000 | [diff] [blame] | 133 | mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * find primes P and Q with Q < P so that: |
| 137 | * GCD( E, (P-1)*(Q-1) ) == 1 |
| 138 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 140 | |
| 141 | do |
| 142 | { |
Janos Follath | 10c575b | 2016-02-23 14:42:48 +0000 | [diff] [blame] | 143 | MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0, |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 144 | f_rng, p_rng ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | |
Janos Follath | ef44178 | 2016-09-21 13:18:12 +0100 | [diff] [blame] | 146 | MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0, |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 147 | f_rng, p_rng ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 150 | continue; |
| 151 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) ); |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 153 | if( mbedtls_mpi_bitlen( &ctx->N ) != nbits ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | continue; |
| 155 | |
Janos Follath | ef44178 | 2016-09-21 13:18:12 +0100 | [diff] [blame] | 156 | if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 ) |
| 157 | mbedtls_mpi_swap( &ctx->P, &ctx->Q ); |
| 158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) ); |
| 160 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) ); |
| 161 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) ); |
| 162 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | |
| 166 | /* |
| 167 | * D = E^-1 mod ((P-1)*(Q-1)) |
| 168 | * DP = D mod (P - 1) |
| 169 | * DQ = D mod (Q - 1) |
| 170 | * QP = Q^-1 mod P |
| 171 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) ); |
| 173 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) ); |
| 174 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) ); |
| 175 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 177 | ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 178 | |
| 179 | cleanup: |
| 180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 182 | |
| 183 | if( ret != 0 ) |
| 184 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | mbedtls_rsa_free( ctx ); |
| 186 | return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 189 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | #endif /* MBEDTLS_GENPRIME */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 | |
| 194 | /* |
| 195 | * Check a public RSA key |
| 196 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 198 | { |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 199 | if( !ctx->N.p || !ctx->E.p ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 201 | |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 202 | if( ( ctx->N.p[0] & 1 ) == 0 || |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 203 | ( ctx->E.p[0] & 1 ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 205 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 206 | if( mbedtls_mpi_bitlen( &ctx->N ) < 128 || |
| 207 | mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 209 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 210 | if( mbedtls_mpi_bitlen( &ctx->E ) < 2 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 ) |
| 212 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 213 | |
| 214 | return( 0 ); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Check a private RSA key |
| 219 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 221 | { |
| 222 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 224 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 225 | if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 226 | return( ret ); |
| 227 | |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 228 | if( !ctx->P.p || !ctx->Q.p || !ctx->D.p ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Paul Bakker | 37940d9f | 2009-07-10 22:38:58 +0000 | [diff] [blame] | 230 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); |
| 232 | mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 ); |
| 233 | mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ ); |
| 234 | mbedtls_mpi_init( &QP ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) ); |
| 237 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) ); |
| 238 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) ); |
| 239 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) ); |
| 240 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) ); |
| 241 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 242 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) ); |
| 244 | MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) ); |
| 245 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) ); |
Paul Bakker | b572adf | 2010-07-18 08:29:32 +0000 | [diff] [blame] | 246 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) ); |
| 248 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) ); |
| 249 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) ); |
Paul Bakker | b572adf | 2010-07-18 08:29:32 +0000 | [diff] [blame] | 250 | /* |
| 251 | * Check for a valid PKCS1v2 private key |
| 252 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 || |
| 254 | mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 || |
| 255 | mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 || |
| 256 | mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 || |
| 257 | mbedtls_mpi_cmp_int( &L2, 0 ) != 0 || |
| 258 | mbedtls_mpi_cmp_int( &I, 1 ) != 0 || |
| 259 | mbedtls_mpi_cmp_int( &G, 1 ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 260 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 261 | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 262 | } |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 263 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 264 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); |
| 266 | mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 ); |
| 267 | mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ ); |
| 268 | mbedtls_mpi_free( &QP ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 269 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ) |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 271 | return( ret ); |
| 272 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 273 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret ); |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 275 | |
| 276 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 280 | * Check if contexts holding a public and private key match |
| 281 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv ) |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 283 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 284 | if( mbedtls_rsa_check_pubkey( pub ) != 0 || |
| 285 | mbedtls_rsa_check_privkey( prv ) != 0 ) |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 286 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 288 | } |
| 289 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 || |
| 291 | mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 ) |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 292 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED ); |
Manuel Pégourié-Gonnard | 2f8d1f9 | 2014-11-06 14:02:51 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | return( 0 ); |
| 297 | } |
| 298 | |
| 299 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 300 | * Do an RSA public key operation |
| 301 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | int mbedtls_rsa_public( mbedtls_rsa_context *ctx, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 303 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 304 | unsigned char *output ) |
| 305 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 306 | int ret; |
| 307 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | mbedtls_mpi T; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 309 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 310 | mbedtls_mpi_init( &T ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 311 | |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 312 | #if defined(MBEDTLS_THREADING_C) |
| 313 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 314 | return( ret ); |
| 315 | #endif |
| 316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 318 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 319 | if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 320 | { |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 321 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 322 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) ); |
| 327 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 328 | |
| 329 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 330 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 331 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 332 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
Manuel Pégourié-Gonnard | 88fca3e | 2015-03-27 15:06:07 +0100 | [diff] [blame] | 333 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 334 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 335 | mbedtls_mpi_free( &T ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 336 | |
| 337 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 338 | return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 339 | |
| 340 | return( 0 ); |
| 341 | } |
| 342 | |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 343 | /* |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 344 | * Generate or update blinding values, see section 10 of: |
| 345 | * 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] | 346 | * 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] | 347 | * Berlin Heidelberg, 1996. p. 104-113. |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 348 | */ |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 349 | static int rsa_prepare_blinding( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 350 | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng ) |
| 351 | { |
Manuel Pégourié-Gonnard | 4d89c7e | 2013-10-04 15:18:38 +0200 | [diff] [blame] | 352 | int ret, count = 0; |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 353 | |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 354 | if( ctx->Vf.p != NULL ) |
| 355 | { |
| 356 | /* We already have blinding values, just update them by squaring */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 357 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) ); |
| 358 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) ); |
| 359 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) ); |
| 360 | 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] | 361 | |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 362 | goto cleanup; |
Manuel Pégourié-Gonnard | 8a109f1 | 2013-09-10 13:37:26 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Manuel Pégourié-Gonnard | 4d89c7e | 2013-10-04 15:18:38 +0200 | [diff] [blame] | 365 | /* Unblinding value: Vf = random number, invertible mod N */ |
| 366 | do { |
| 367 | if( count++ > 10 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 368 | return( MBEDTLS_ERR_RSA_RNG_FAILED ); |
Manuel Pégourié-Gonnard | 4d89c7e | 2013-10-04 15:18:38 +0200 | [diff] [blame] | 369 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 370 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) ); |
| 371 | MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) ); |
| 372 | } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 ); |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 373 | |
| 374 | /* Blinding value: Vi = Vf^(-e) mod N */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 375 | MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) ); |
| 376 | 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] | 377 | |
Manuel Pégourié-Gonnard | ae10299 | 2013-10-04 17:07:12 +0200 | [diff] [blame] | 378 | |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 379 | cleanup: |
| 380 | return( ret ); |
| 381 | } |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 382 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 383 | /* |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 384 | * Exponent blinding supposed to prevent side-channel attacks using multiple |
| 385 | * traces of measurements to recover the RSA key. The more collisions are there, |
| 386 | * the more bits of the key can be recovered. See [3]. |
| 387 | * |
| 388 | * Collecting n collisions with m bit long blinding value requires 2^(m-m/n) |
| 389 | * observations on avarage. |
| 390 | * |
| 391 | * For example with 28 byte blinding to achieve 2 collisions the adversary has |
| 392 | * to make 2^112 observations on avarage. |
| 393 | * |
| 394 | * (With the currently (as of 2017 April) known best algorithms breaking 2048 |
| 395 | * bit RSA requires approximately as much time as trying out 2^112 random keys. |
| 396 | * Thus in this sense with 28 byte blinding the security is not reduced by |
| 397 | * side-channel attacks like the one in [3]) |
| 398 | * |
| 399 | * This countermeasure does not help if the key recovery is possible with a |
| 400 | * single trace. |
| 401 | */ |
| 402 | #define RSA_EXPONENT_BLINDING 28 |
| 403 | |
| 404 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 405 | * Do an RSA private key operation |
| 406 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 407 | int mbedtls_rsa_private( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 408 | int (*f_rng)(void *, unsigned char *, size_t), |
| 409 | void *p_rng, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 410 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 411 | unsigned char *output ) |
| 412 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 413 | int ret; |
| 414 | size_t olen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | mbedtls_mpi T, T1, T2; |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 416 | mbedtls_mpi P1, Q1, R; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 417 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 418 | mbedtls_mpi D_blind; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 419 | mbedtls_mpi *D = &ctx->D; |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 420 | #else |
| 421 | mbedtls_mpi DP_blind, DQ_blind; |
| 422 | mbedtls_mpi *DP = &ctx->DP; |
| 423 | mbedtls_mpi *DQ = &ctx->DQ; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 424 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 425 | |
Manuel Pégourié-Gonnard | fb84d38 | 2015-10-30 10:56:25 +0100 | [diff] [blame] | 426 | /* Make sure we have private key info, prevent possible misuse */ |
| 427 | if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL ) |
| 428 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 429 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 430 | mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 ); |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 431 | mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 ); mbedtls_mpi_init( &R ); |
| 432 | |
| 433 | |
| 434 | if( f_rng != NULL ) |
| 435 | { |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 436 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 437 | mbedtls_mpi_init( &D_blind ); |
| 438 | #else |
| 439 | mbedtls_mpi_init( &DP_blind ); |
| 440 | mbedtls_mpi_init( &DQ_blind ); |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 441 | #endif |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 442 | } |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 443 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 444 | |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 445 | #if defined(MBEDTLS_THREADING_C) |
| 446 | if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) |
| 447 | return( ret ); |
| 448 | #endif |
| 449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 450 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) ); |
| 451 | if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 452 | { |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 453 | ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA; |
| 454 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 457 | if( f_rng != NULL ) |
| 458 | { |
| 459 | /* |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 460 | * Blinding |
| 461 | * T = T * Vi mod N |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 462 | */ |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 463 | MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) ); |
| 464 | 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] | 465 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) ); |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 466 | |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 467 | /* |
| 468 | * Exponent blinding |
| 469 | */ |
| 470 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) ); |
| 471 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) ); |
| 472 | |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 473 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 474 | /* |
| 475 | * D_blind = ( P - 1 ) * ( Q - 1 ) * R + D |
| 476 | */ |
| 477 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING, |
| 478 | f_rng, p_rng ) ); |
| 479 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &P1, &Q1 ) ); |
| 480 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &D_blind, &D_blind, &R ) ); |
| 481 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &D_blind, &D_blind, &ctx->D ) ); |
| 482 | |
| 483 | D = &D_blind; |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 484 | #else |
| 485 | /* |
| 486 | * DP_blind = ( P - 1 ) * R + DP |
| 487 | */ |
| 488 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING, |
| 489 | f_rng, p_rng ) ); |
| 490 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DP_blind, &P1, &R ) ); |
| 491 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DP_blind, &DP_blind, |
| 492 | &ctx->DP ) ); |
| 493 | |
| 494 | DP = &DP_blind; |
| 495 | |
| 496 | /* |
| 497 | * DQ_blind = ( Q - 1 ) * R + DQ |
| 498 | */ |
| 499 | MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &R, RSA_EXPONENT_BLINDING, |
| 500 | f_rng, p_rng ) ); |
| 501 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DQ_blind, &Q1, &R ) ); |
| 502 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &DQ_blind, &DQ_blind, |
| 503 | &ctx->DQ ) ); |
| 504 | |
| 505 | DQ = &DQ_blind; |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 506 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 507 | } |
Paul Bakker | aab30c1 | 2013-08-30 11:00:25 +0200 | [diff] [blame] | 508 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 509 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 510 | 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] | 511 | #else |
Paul Bakker | aab30c1 | 2013-08-30 11:00:25 +0200 | [diff] [blame] | 512 | /* |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 513 | * Faster decryption using the CRT |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 514 | * |
| 515 | * T1 = input ^ dP mod P |
| 516 | * T2 = input ^ dQ mod Q |
| 517 | */ |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 518 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, DP, &ctx->P, &ctx->RP ) ); |
| 519 | MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, DQ, &ctx->Q, &ctx->RQ ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 520 | |
| 521 | /* |
| 522 | * T = (T1 - T2) * (Q^-1 mod P) mod P |
| 523 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 524 | MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) ); |
| 525 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) ); |
| 526 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 527 | |
| 528 | /* |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 529 | * T = T2 + T * Q |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 530 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 531 | MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) ); |
| 532 | MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) ); |
| 533 | #endif /* MBEDTLS_RSA_NO_CRT */ |
Paul Bakker | aab30c1 | 2013-08-30 11:00:25 +0200 | [diff] [blame] | 534 | |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 535 | if( f_rng != NULL ) |
| 536 | { |
| 537 | /* |
| 538 | * Unblind |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 539 | * T = T * Vf mod N |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 540 | */ |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 541 | 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] | 542 | MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) ); |
Paul Bakker | f451bac | 2013-08-30 15:37:02 +0200 | [diff] [blame] | 543 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 544 | |
| 545 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 546 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 547 | |
| 548 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 549 | #if defined(MBEDTLS_THREADING_C) |
Manuel Pégourié-Gonnard | 4d04cdc | 2015-08-28 10:32:21 +0200 | [diff] [blame] | 550 | if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) |
| 551 | return( MBEDTLS_ERR_THREADING_MUTEX_ERROR ); |
Manuel Pégourié-Gonnard | ae10299 | 2013-10-04 17:07:12 +0200 | [diff] [blame] | 552 | #endif |
Manuel Pégourié-Gonnard | 1385a28 | 2015-08-27 11:30:58 +0200 | [diff] [blame] | 553 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 554 | mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 ); |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 555 | mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &R ); |
| 556 | |
| 557 | if( f_rng != NULL ) |
| 558 | { |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 559 | #if defined(MBEDTLS_RSA_NO_CRT) |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 560 | mbedtls_mpi_free( &D_blind ); |
| 561 | #else |
| 562 | mbedtls_mpi_free( &DP_blind ); |
| 563 | mbedtls_mpi_free( &DQ_blind ); |
Janos Follath | e81102e | 2017-03-22 13:38:28 +0000 | [diff] [blame] | 564 | #endif |
Janos Follath | f9203b4 | 2017-03-22 15:13:15 +0000 | [diff] [blame] | 565 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 566 | |
| 567 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 568 | return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 569 | |
| 570 | return( 0 ); |
| 571 | } |
| 572 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 573 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 574 | /** |
| 575 | * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer. |
| 576 | * |
Paul Bakker | b125ed8 | 2011-11-10 13:33:51 +0000 | [diff] [blame] | 577 | * \param dst buffer to mask |
| 578 | * \param dlen length of destination buffer |
| 579 | * \param src source of the mask generation |
| 580 | * \param slen length of the source buffer |
| 581 | * \param md_ctx message digest context to use |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 582 | */ |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 583 | static void 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] | 584 | size_t slen, mbedtls_md_context_t *md_ctx ) |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 585 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 586 | unsigned char mask[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 587 | unsigned char counter[4]; |
| 588 | unsigned char *p; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 589 | unsigned int hlen; |
| 590 | size_t i, use_len; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 591 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 592 | memset( mask, 0, MBEDTLS_MD_MAX_SIZE ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 593 | memset( counter, 0, 4 ); |
| 594 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 595 | hlen = mbedtls_md_get_size( md_ctx->md_info ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 596 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 597 | /* Generate and apply dbMask */ |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 598 | p = dst; |
| 599 | |
| 600 | while( dlen > 0 ) |
| 601 | { |
| 602 | use_len = hlen; |
| 603 | if( dlen < hlen ) |
| 604 | use_len = dlen; |
| 605 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 606 | mbedtls_md_starts( md_ctx ); |
| 607 | mbedtls_md_update( md_ctx, src, slen ); |
| 608 | mbedtls_md_update( md_ctx, counter, 4 ); |
| 609 | mbedtls_md_finish( md_ctx, mask ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 610 | |
| 611 | for( i = 0; i < use_len; ++i ) |
| 612 | *p++ ^= mask[i]; |
| 613 | |
| 614 | counter[3]++; |
| 615 | |
| 616 | dlen -= use_len; |
| 617 | } |
Gilles Peskine | 18ac716 | 2017-05-05 19:24:06 +0200 | [diff] [blame] | 618 | |
| 619 | mbedtls_zeroize( mask, sizeof( mask ) ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 620 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 622 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 624 | /* |
| 625 | * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function |
| 626 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 627 | int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 628 | int (*f_rng)(void *, unsigned char *, size_t), |
| 629 | void *p_rng, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 630 | int mode, |
| 631 | const unsigned char *label, size_t label_len, |
| 632 | size_t ilen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 633 | const unsigned char *input, |
| 634 | unsigned char *output ) |
| 635 | { |
| 636 | size_t olen; |
| 637 | int ret; |
| 638 | unsigned char *p = output; |
| 639 | unsigned int hlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 640 | const mbedtls_md_info_t *md_info; |
| 641 | mbedtls_md_context_t md_ctx; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 642 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 643 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 644 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 645 | |
| 646 | if( f_rng == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 647 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 648 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 649 | 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] | 650 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 651 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 652 | |
| 653 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | hlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 655 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 656 | /* first comparison checks for overflow */ |
Janos Follath | eddfe8f | 2016-02-08 14:52:29 +0000 | [diff] [blame] | 657 | 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] | 658 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 659 | |
| 660 | memset( output, 0, olen ); |
| 661 | |
| 662 | *p++ = 0; |
| 663 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 664 | /* Generate a random octet string seed */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 665 | if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 666 | return( MBEDTLS_ERR_RSA_RNG_FAILED + ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 667 | |
| 668 | p += hlen; |
| 669 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 670 | /* Construct DB */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | mbedtls_md( md_info, label, label_len, p ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 672 | p += hlen; |
| 673 | p += olen - 2 * hlen - 2 - ilen; |
| 674 | *p++ = 1; |
| 675 | memcpy( p, input, ilen ); |
| 676 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 678 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 679 | { |
| 680 | mbedtls_md_free( &md_ctx ); |
| 681 | return( ret ); |
| 682 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 683 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 684 | /* maskedDB: Apply dbMask to DB */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 685 | mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen, |
| 686 | &md_ctx ); |
| 687 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 688 | /* maskedSeed: Apply seedMask to seed */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 689 | mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1, |
| 690 | &md_ctx ); |
| 691 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 692 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 693 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 694 | return( ( mode == MBEDTLS_RSA_PUBLIC ) |
| 695 | ? mbedtls_rsa_public( ctx, output, output ) |
| 696 | : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 697 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 698 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 699 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 700 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 701 | /* |
| 702 | * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function |
| 703 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 704 | int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 705 | int (*f_rng)(void *, unsigned char *, size_t), |
| 706 | void *p_rng, |
| 707 | int mode, size_t ilen, |
| 708 | const unsigned char *input, |
| 709 | unsigned char *output ) |
| 710 | { |
| 711 | size_t nb_pad, olen; |
| 712 | int ret; |
| 713 | unsigned char *p = output; |
| 714 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 716 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 717 | |
Janos Follath | 1ed9f99 | 2016-03-18 11:45:44 +0000 | [diff] [blame] | 718 | // We don't check p_rng because it won't be dereferenced here |
| 719 | if( f_rng == NULL || input == NULL || output == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 720 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 721 | |
| 722 | olen = ctx->len; |
Manuel Pégourié-Gonnard | 370717b | 2016-02-11 10:35:13 +0100 | [diff] [blame] | 723 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 724 | /* first comparison checks for overflow */ |
Janos Follath | eddfe8f | 2016-02-08 14:52:29 +0000 | [diff] [blame] | 725 | if( ilen + 11 < ilen || olen < ilen + 11 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 726 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 727 | |
| 728 | nb_pad = olen - 3 - ilen; |
| 729 | |
| 730 | *p++ = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 731 | if( mode == MBEDTLS_RSA_PUBLIC ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 732 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 733 | *p++ = MBEDTLS_RSA_CRYPT; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 734 | |
| 735 | while( nb_pad-- > 0 ) |
| 736 | { |
| 737 | int rng_dl = 100; |
| 738 | |
| 739 | do { |
| 740 | ret = f_rng( p_rng, p, 1 ); |
| 741 | } while( *p == 0 && --rng_dl && ret == 0 ); |
| 742 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 743 | /* Check if RNG failed to generate data */ |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 744 | if( rng_dl == 0 || ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 745 | return( MBEDTLS_ERR_RSA_RNG_FAILED + ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 746 | |
| 747 | p++; |
| 748 | } |
| 749 | } |
| 750 | else |
| 751 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 752 | *p++ = MBEDTLS_RSA_SIGN; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 753 | |
| 754 | while( nb_pad-- > 0 ) |
| 755 | *p++ = 0xFF; |
| 756 | } |
| 757 | |
| 758 | *p++ = 0; |
| 759 | memcpy( p, input, ilen ); |
| 760 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 761 | return( ( mode == MBEDTLS_RSA_PUBLIC ) |
| 762 | ? mbedtls_rsa_public( ctx, output, output ) |
| 763 | : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 764 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 765 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 766 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 767 | /* |
| 768 | * Add the message padding, then do an RSA operation |
| 769 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 770 | int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 771 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 21eb280 | 2010-08-16 11:10:02 +0000 | [diff] [blame] | 772 | void *p_rng, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 773 | int mode, size_t ilen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 774 | const unsigned char *input, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 775 | unsigned char *output ) |
| 776 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 777 | switch( ctx->padding ) |
| 778 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 779 | #if defined(MBEDTLS_PKCS1_V15) |
| 780 | case MBEDTLS_RSA_PKCS_V15: |
| 781 | 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] | 782 | input, output ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 783 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 784 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 785 | #if defined(MBEDTLS_PKCS1_V21) |
| 786 | case MBEDTLS_RSA_PKCS_V21: |
| 787 | 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] | 788 | ilen, input, output ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 789 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 790 | |
| 791 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 792 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 793 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 796 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 797 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 798 | * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 799 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 800 | int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 801 | int (*f_rng)(void *, unsigned char *, size_t), |
| 802 | void *p_rng, |
| 803 | int mode, |
Paul Bakker | a43231c | 2013-02-28 17:33:49 +0100 | [diff] [blame] | 804 | const unsigned char *label, size_t label_len, |
| 805 | size_t *olen, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 806 | const unsigned char *input, |
| 807 | unsigned char *output, |
| 808 | size_t output_max_len ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 809 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 810 | int ret; |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 811 | size_t ilen, i, pad_len; |
| 812 | unsigned char *p, bad, pad_done; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 813 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
| 814 | unsigned char lhash[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 815 | unsigned int hlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 816 | const mbedtls_md_info_t *md_info; |
| 817 | mbedtls_md_context_t md_ctx; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 818 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 819 | /* |
| 820 | * Parameters sanity checks |
| 821 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 822 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 823 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 824 | |
| 825 | ilen = ctx->len; |
| 826 | |
Paul Bakker | 27fdf46 | 2011-06-09 13:55:13 +0000 | [diff] [blame] | 827 | if( ilen < 16 || ilen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 828 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 829 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 830 | 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] | 831 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 832 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 833 | |
Janos Follath | c17cda1 | 2016-02-11 11:08:18 +0000 | [diff] [blame] | 834 | hlen = mbedtls_md_get_size( md_info ); |
| 835 | |
| 836 | // checking for integer underflow |
| 837 | if( 2 * hlen + 2 > ilen ) |
| 838 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 839 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 840 | /* |
| 841 | * RSA operation |
| 842 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 843 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 844 | ? mbedtls_rsa_public( ctx, input, buf ) |
| 845 | : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 846 | |
| 847 | if( ret != 0 ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 848 | goto cleanup; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 849 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 850 | /* |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 851 | * Unmask data and generate lHash |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 852 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 853 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 854 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 855 | { |
| 856 | mbedtls_md_free( &md_ctx ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 857 | goto cleanup; |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 860 | |
| 861 | /* Generate lHash */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 862 | mbedtls_md( md_info, label, label_len, lhash ); |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 863 | |
| 864 | /* seed: Apply seedMask to maskedSeed */ |
| 865 | mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1, |
| 866 | &md_ctx ); |
| 867 | |
| 868 | /* DB: Apply dbMask to maskedDB */ |
| 869 | mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen, |
| 870 | &md_ctx ); |
| 871 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 872 | mbedtls_md_free( &md_ctx ); |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 873 | |
| 874 | /* |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 875 | * Check contents, in "constant-time" |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 876 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 877 | p = buf; |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 878 | bad = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 879 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 880 | bad |= *p++; /* First byte must be 0 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 881 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 882 | p += hlen; /* Skip seed */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 883 | |
Manuel Pégourié-Gonnard | a5cfc35 | 2013-11-28 15:57:52 +0100 | [diff] [blame] | 884 | /* Check lHash */ |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 885 | for( i = 0; i < hlen; i++ ) |
| 886 | bad |= lhash[i] ^ *p++; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 887 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 888 | /* Get zero-padding len, but always read till end of buffer |
| 889 | * (minus one, for the 01 byte) */ |
| 890 | pad_len = 0; |
| 891 | pad_done = 0; |
| 892 | for( i = 0; i < ilen - 2 * hlen - 2; i++ ) |
| 893 | { |
| 894 | pad_done |= p[i]; |
Pascal Junod | b99183d | 2015-03-11 16:49:45 +0100 | [diff] [blame] | 895 | pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1; |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 896 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 897 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 898 | p += pad_len; |
| 899 | bad |= *p++ ^ 0x01; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 900 | |
Manuel Pégourié-Gonnard | ab44d7e | 2013-11-29 12:49:44 +0100 | [diff] [blame] | 901 | /* |
| 902 | * The only information "leaked" is whether the padding was correct or not |
| 903 | * (eg, no data is copied if it was not correct). This meets the |
| 904 | * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between |
| 905 | * the different error conditions. |
| 906 | */ |
| 907 | if( bad != 0 ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 908 | { |
| 909 | ret = MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 910 | goto cleanup; |
| 911 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 912 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 913 | if( ilen - ( p - buf ) > output_max_len ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 914 | { |
| 915 | ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
| 916 | goto cleanup; |
| 917 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 918 | |
| 919 | *olen = ilen - (p - buf); |
| 920 | memcpy( output, p, *olen ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 921 | ret = 0; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 922 | |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 923 | cleanup: |
| 924 | mbedtls_zeroize( buf, sizeof( buf ) ); |
| 925 | mbedtls_zeroize( lhash, sizeof( lhash ) ); |
| 926 | |
| 927 | return( ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 928 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 929 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 930 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 931 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 932 | /* |
| 933 | * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function |
| 934 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 935 | int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 936 | int (*f_rng)(void *, unsigned char *, size_t), |
| 937 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 938 | int mode, size_t *olen, |
| 939 | const unsigned char *input, |
| 940 | unsigned char *output, |
| 941 | size_t output_max_len) |
| 942 | { |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 943 | int ret; |
| 944 | size_t ilen, pad_count = 0, i; |
| 945 | unsigned char *p, bad, pad_done = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 946 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 947 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 948 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 949 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 950 | |
| 951 | ilen = ctx->len; |
| 952 | |
| 953 | if( ilen < 16 || ilen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 954 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 955 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 956 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 957 | ? mbedtls_rsa_public( ctx, input, buf ) |
| 958 | : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 959 | |
| 960 | if( ret != 0 ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 961 | goto cleanup; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 962 | |
| 963 | p = buf; |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 964 | bad = 0; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 965 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 966 | /* |
| 967 | * Check and get padding len in "constant-time" |
| 968 | */ |
| 969 | bad |= *p++; /* First byte must be 0 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 970 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 971 | /* This test does not depend on secret data */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 972 | if( mode == MBEDTLS_RSA_PRIVATE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 973 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 974 | bad |= *p++ ^ MBEDTLS_RSA_CRYPT; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 975 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 976 | /* Get padding len, but always read till end of buffer |
| 977 | * (minus one, for the 00 byte) */ |
| 978 | for( i = 0; i < ilen - 3; i++ ) |
| 979 | { |
Pascal Junod | b99183d | 2015-03-11 16:49:45 +0100 | [diff] [blame] | 980 | pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1; |
| 981 | pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1; |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 982 | } |
Paul Bakker | e6ee41f | 2012-05-19 08:43:48 +0000 | [diff] [blame] | 983 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 984 | p += pad_count; |
| 985 | bad |= *p++; /* Must be zero */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 986 | } |
| 987 | else |
| 988 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 989 | bad |= *p++ ^ MBEDTLS_RSA_SIGN; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 990 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 991 | /* Get padding len, but always read till end of buffer |
| 992 | * (minus one, for the 00 byte) */ |
| 993 | for( i = 0; i < ilen - 3; i++ ) |
| 994 | { |
Manuel Pégourié-Gonnard | fbf0915 | 2014-02-03 11:58:55 +0100 | [diff] [blame] | 995 | pad_done |= ( p[i] != 0xFF ); |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 996 | pad_count += ( pad_done == 0 ); |
| 997 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 998 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 999 | p += pad_count; |
| 1000 | bad |= *p++; /* Must be zero */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Janos Follath | c69fa50 | 2016-02-12 13:30:09 +0000 | [diff] [blame] | 1003 | bad |= ( pad_count < 8 ); |
Janos Follath | b6eb1ca | 2016-02-08 13:59:25 +0000 | [diff] [blame] | 1004 | |
Manuel Pégourié-Gonnard | 27290da | 2013-11-30 13:36:53 +0100 | [diff] [blame] | 1005 | if( bad ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1006 | { |
| 1007 | ret = MBEDTLS_ERR_RSA_INVALID_PADDING; |
| 1008 | goto cleanup; |
| 1009 | } |
Paul Bakker | 8804f69 | 2013-02-28 18:06:26 +0100 | [diff] [blame] | 1010 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 1011 | if( ilen - ( p - buf ) > output_max_len ) |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1012 | { |
| 1013 | ret = MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
| 1014 | goto cleanup; |
| 1015 | } |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 1016 | |
Paul Bakker | 27fdf46 | 2011-06-09 13:55:13 +0000 | [diff] [blame] | 1017 | *olen = ilen - (p - buf); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1018 | memcpy( output, p, *olen ); |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1019 | ret = 0; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1020 | |
Gilles Peskine | 4a7f6a0 | 2017-03-23 14:37:37 +0100 | [diff] [blame] | 1021 | cleanup: |
| 1022 | mbedtls_zeroize( buf, sizeof( buf ) ); |
| 1023 | |
| 1024 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1025 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1026 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1027 | |
| 1028 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1029 | * Do an RSA operation, then remove the message padding |
| 1030 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1031 | int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1032 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1033 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1034 | int mode, size_t *olen, |
| 1035 | const unsigned char *input, |
| 1036 | unsigned char *output, |
| 1037 | size_t output_max_len) |
| 1038 | { |
| 1039 | switch( ctx->padding ) |
| 1040 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1041 | #if defined(MBEDTLS_PKCS1_V15) |
| 1042 | case MBEDTLS_RSA_PKCS_V15: |
| 1043 | 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] | 1044 | input, output, output_max_len ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1045 | #endif |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1046 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1047 | #if defined(MBEDTLS_PKCS1_V21) |
| 1048 | case MBEDTLS_RSA_PKCS_V21: |
| 1049 | 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] | 1050 | olen, input, output, |
| 1051 | output_max_len ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1052 | #endif |
| 1053 | |
| 1054 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1055 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1059 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1060 | /* |
| 1061 | * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function |
| 1062 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1063 | int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1064 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1065 | void *p_rng, |
| 1066 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1067 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1068 | unsigned int hashlen, |
| 1069 | const unsigned char *hash, |
| 1070 | unsigned char *sig ) |
| 1071 | { |
| 1072 | size_t olen; |
| 1073 | unsigned char *p = sig; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1074 | unsigned char salt[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1075 | unsigned int slen, hlen, offset = 0; |
| 1076 | int ret; |
| 1077 | size_t msb; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1078 | const mbedtls_md_info_t *md_info; |
| 1079 | mbedtls_md_context_t md_ctx; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1080 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1081 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 1082 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Manuel Pégourié-Gonnard | e6d1d82 | 2014-06-02 16:47:02 +0200 | [diff] [blame] | 1083 | |
| 1084 | if( f_rng == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1085 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1086 | |
| 1087 | olen = ctx->len; |
| 1088 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1089 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1090 | { |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1091 | /* Gather length of hash to sign */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1092 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1093 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1094 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1095 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1096 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1097 | } |
| 1098 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1099 | 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] | 1100 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1101 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1102 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1103 | hlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1104 | slen = hlen; |
| 1105 | |
| 1106 | if( olen < hlen + slen + 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1107 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1108 | |
| 1109 | memset( sig, 0, olen ); |
| 1110 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1111 | /* Generate salt of length slen */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1112 | if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1113 | return( MBEDTLS_ERR_RSA_RNG_FAILED + ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1114 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1115 | /* 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] | 1116 | msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1117 | p += olen - hlen * 2 - 2; |
| 1118 | *p++ = 0x01; |
| 1119 | memcpy( p, salt, slen ); |
| 1120 | p += slen; |
| 1121 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1122 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1123 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 1124 | { |
| 1125 | mbedtls_md_free( &md_ctx ); |
Gilles Peskine | 18ac716 | 2017-05-05 19:24:06 +0200 | [diff] [blame] | 1126 | /* No need to zeroize salt: we didn't use it. */ |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1127 | return( ret ); |
| 1128 | } |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1129 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1130 | /* Generate H = Hash( M' ) */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1131 | mbedtls_md_starts( &md_ctx ); |
| 1132 | mbedtls_md_update( &md_ctx, p, 8 ); |
| 1133 | mbedtls_md_update( &md_ctx, hash, hashlen ); |
| 1134 | mbedtls_md_update( &md_ctx, salt, slen ); |
| 1135 | mbedtls_md_finish( &md_ctx, p ); |
Gilles Peskine | 18ac716 | 2017-05-05 19:24:06 +0200 | [diff] [blame] | 1136 | mbedtls_zeroize( salt, sizeof( salt ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1137 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1138 | /* Compensate for boundary condition when applying mask */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1139 | if( msb % 8 == 0 ) |
| 1140 | offset = 1; |
| 1141 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1142 | /* maskedDB: Apply dbMask to DB */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1143 | mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx ); |
| 1144 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1145 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1146 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 1147 | msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1148 | sig[0] &= 0xFF >> ( olen * 8 - msb ); |
| 1149 | |
| 1150 | p += hlen; |
| 1151 | *p++ = 0xBC; |
| 1152 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1153 | return( ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1154 | ? mbedtls_rsa_public( ctx, sig, sig ) |
| 1155 | : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1156 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1157 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1159 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1160 | /* |
| 1161 | * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function |
| 1162 | */ |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame^] | 1163 | |
| 1164 | /* Construct a PKCS v1.5 encoding of a hashed message |
| 1165 | * |
| 1166 | * This is used both for signature generation and verification. |
| 1167 | * |
| 1168 | * Parameters: |
| 1169 | * - md_alg: Identifies the hash algorithm used to generate the given hash; |
| 1170 | * MBEDTLS_MD_NONE if raw data are signed. |
| 1171 | * - hashlen: Length of hash in case hashlen is MBEDTLS_MD_NONE. |
| 1172 | * - hash: Buffer containing the hashed message. |
| 1173 | * - sig_len: Length of the encoded message. |
| 1174 | * - dst: Buffer to hold the encoded message. |
| 1175 | * |
| 1176 | * Assumptions: |
| 1177 | * - hash has size hashlen if md_alg == MBEDTLS_MD_NONE. |
| 1178 | * - hash has size corresponding to md_alg if md_alg != MBEDTLS_MD_NONE. |
| 1179 | * - dst points to a buffer of size at least sig_len. |
| 1180 | * |
| 1181 | */ |
| 1182 | static int rsa_rsassa_pkcs1_v15_encode( mbedtls_md_type_t md_alg, |
| 1183 | unsigned int hashlen, |
| 1184 | const unsigned char *hash, |
| 1185 | size_t sig_len, |
| 1186 | unsigned char *dst ) |
| 1187 | { |
| 1188 | size_t oid_size = 0; |
| 1189 | size_t nb_pad = sig_len; |
| 1190 | unsigned char *p = dst; |
| 1191 | const char *oid = NULL; |
| 1192 | |
| 1193 | /* Are we signing hashed or raw data? */ |
| 1194 | if( md_alg != MBEDTLS_MD_NONE ) |
| 1195 | { |
| 1196 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); |
| 1197 | if( md_info == NULL ) |
| 1198 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1199 | |
| 1200 | if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 ) |
| 1201 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1202 | |
| 1203 | hashlen = mbedtls_md_get_size( md_info ); |
| 1204 | |
| 1205 | /* Double-check that 8 + hashlen + oid_size can be used as a |
| 1206 | * 1-byte ASN.1 length encoding and that there's no overflow. */ |
| 1207 | if( 8 + hashlen + oid_size >= 0x80 || |
| 1208 | 10 + hashlen < hashlen || |
| 1209 | 10 + hashlen + oid_size < 10 + hashlen ) |
| 1210 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1211 | |
| 1212 | /* |
| 1213 | * Static bounds check: |
| 1214 | * - Need 10 bytes for five tag-length pairs. |
| 1215 | * (Insist on 1-byte length encodings to protect against variants of |
| 1216 | * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification) |
| 1217 | * - Need hashlen bytes for hash |
| 1218 | * - Need oid_size bytes for hash alg OID. |
| 1219 | */ |
| 1220 | if( nb_pad < 10 + hashlen + oid_size ) |
| 1221 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1222 | nb_pad -= 10 + hashlen + oid_size; |
| 1223 | } |
| 1224 | else |
| 1225 | { |
| 1226 | if( nb_pad < hashlen ) |
| 1227 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1228 | |
| 1229 | nb_pad -= hashlen; |
| 1230 | } |
| 1231 | |
| 1232 | /* Signature header and padding delimiter */ |
| 1233 | if( nb_pad < 3 ) |
| 1234 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1235 | nb_pad -= 3; |
| 1236 | |
| 1237 | /* Now nb_pad is the amount of memory to be filled |
| 1238 | * with padding; must be at least 8 bytes. */ |
| 1239 | if( nb_pad < 8 ) |
| 1240 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1241 | |
| 1242 | /* Write signature header and padding */ |
| 1243 | *p++ = 0; |
| 1244 | *p++ = MBEDTLS_RSA_SIGN; |
| 1245 | memset( p, 0xFF, nb_pad ); |
| 1246 | p += nb_pad; |
| 1247 | *p++ = 0; |
| 1248 | |
| 1249 | /* Are we signing raw data? */ |
| 1250 | if( md_alg == MBEDTLS_MD_NONE ) |
| 1251 | { |
| 1252 | memcpy( p, hash, hashlen ); |
| 1253 | return( 0 ); |
| 1254 | } |
| 1255 | |
| 1256 | /* Signing hashed data, add corresponding ASN.1 structure |
| 1257 | * |
| 1258 | * DigestInfo ::= SEQUENCE { |
| 1259 | * digestAlgorithm DigestAlgorithmIdentifier, |
| 1260 | * digest Digest } |
| 1261 | * DigestAlgorithmIdentifier ::= AlgorithmIdentifier |
| 1262 | * Digest ::= OCTET STRING |
| 1263 | * |
| 1264 | * Schematic: |
| 1265 | * TAG-SEQ + LEN [ TAG-SEQ + LEN [ TAG-OID + LEN [ OID ] |
| 1266 | * TAG-NULL + LEN [ NULL ] ] |
| 1267 | * TAG-OCTET + LEN [ HASH ] ] |
| 1268 | */ |
| 1269 | *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED; |
| 1270 | *p++ = 0x08 + oid_size + hashlen; |
| 1271 | *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED; |
| 1272 | *p++ = 0x04 + oid_size; |
| 1273 | *p++ = MBEDTLS_ASN1_OID; |
| 1274 | *p++ = oid_size; |
| 1275 | memcpy( p, oid, oid_size ); |
| 1276 | p += oid_size; |
| 1277 | *p++ = MBEDTLS_ASN1_NULL; |
| 1278 | *p++ = 0x00; |
| 1279 | *p++ = MBEDTLS_ASN1_OCTET_STRING; |
| 1280 | *p++ = hashlen; |
| 1281 | memcpy( p, hash, hashlen ); |
| 1282 | p += hashlen; |
| 1283 | |
| 1284 | /* Just a sanity-check, should be automatic |
| 1285 | * after the initial bounds check. */ |
| 1286 | if( p != dst + sig_len ) |
| 1287 | { |
| 1288 | mbedtls_zeroize( dst, sig_len ); |
| 1289 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1290 | } |
| 1291 | |
| 1292 | return( 0 ); |
| 1293 | } |
| 1294 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1295 | /* |
| 1296 | * Do an RSA operation to sign the message digest |
| 1297 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1298 | int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1299 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1300 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1301 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1302 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1303 | unsigned int hashlen, |
| 1304 | const unsigned char *hash, |
| 1305 | unsigned char *sig ) |
| 1306 | { |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 1307 | int ret; |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame^] | 1308 | unsigned char *sig_try = NULL, *verif = NULL; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1309 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1310 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 1311 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1312 | |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame^] | 1313 | /* |
| 1314 | * Prepare PKCS1-v1.5 encoding (padding and hash identifier) |
| 1315 | */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1316 | |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame^] | 1317 | if( ( ret = rsa_rsassa_pkcs1_v15_encode( md_alg, hashlen, hash, |
| 1318 | ctx->len, sig ) ) != 0 ) |
| 1319 | return( ret ); |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 1320 | |
| 1321 | /* |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame^] | 1322 | * Call respective RSA primitive |
| 1323 | */ |
| 1324 | |
| 1325 | if( mode == MBEDTLS_RSA_PUBLIC ) |
| 1326 | { |
| 1327 | /* Skip verification on a public key operation */ |
| 1328 | return( mbedtls_rsa_public( ctx, sig, sig ) ); |
| 1329 | } |
| 1330 | |
| 1331 | /* Private key operation |
| 1332 | * |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 1333 | * In order to prevent Lenstra's attack, make the signature in a |
| 1334 | * temporary buffer and check it before returning it. |
| 1335 | */ |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame^] | 1336 | |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 1337 | sig_try = mbedtls_calloc( 1, ctx->len ); |
Simon Butcher | 1285ab5 | 2016-01-01 21:42:47 +0000 | [diff] [blame] | 1338 | if( sig_try == NULL ) |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 1339 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 1340 | |
Hanno Becker | fdf3803 | 2017-09-06 12:35:55 +0100 | [diff] [blame^] | 1341 | verif = mbedtls_calloc( 1, ctx->len ); |
Simon Butcher | 1285ab5 | 2016-01-01 21:42:47 +0000 | [diff] [blame] | 1342 | if( verif == NULL ) |
| 1343 | { |
| 1344 | mbedtls_free( sig_try ); |
| 1345 | return( MBEDTLS_ERR_MPI_ALLOC_FAILED ); |
| 1346 | } |
| 1347 | |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 1348 | MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) ); |
| 1349 | MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) ); |
| 1350 | |
Hanno Becker | 171a8f1 | 2017-09-06 12:32:16 +0100 | [diff] [blame] | 1351 | if( mbedtls_safer_memcmp( verif, sig, ctx->len ) != 0 ) |
Manuel Pégourié-Gonnard | 5f50104 | 2015-09-03 20:03:15 +0200 | [diff] [blame] | 1352 | { |
| 1353 | ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED; |
| 1354 | goto cleanup; |
| 1355 | } |
| 1356 | |
| 1357 | memcpy( sig, sig_try, ctx->len ); |
| 1358 | |
| 1359 | cleanup: |
| 1360 | mbedtls_free( sig_try ); |
| 1361 | mbedtls_free( verif ); |
| 1362 | |
| 1363 | return( ret ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1364 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1365 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1366 | |
| 1367 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1368 | * Do an RSA operation to sign the message digest |
| 1369 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1370 | int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1371 | int (*f_rng)(void *, unsigned char *, size_t), |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1372 | void *p_rng, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1373 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1374 | mbedtls_md_type_t md_alg, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1375 | unsigned int hashlen, |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 1376 | const unsigned char *hash, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1377 | unsigned char *sig ) |
| 1378 | { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1379 | switch( ctx->padding ) |
| 1380 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1381 | #if defined(MBEDTLS_PKCS1_V15) |
| 1382 | case MBEDTLS_RSA_PKCS_V15: |
| 1383 | 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] | 1384 | hashlen, hash, sig ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1385 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1386 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1387 | #if defined(MBEDTLS_PKCS1_V21) |
| 1388 | case MBEDTLS_RSA_PKCS_V21: |
| 1389 | 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] | 1390 | hashlen, hash, sig ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1391 | #endif |
| 1392 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1393 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1394 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1395 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1398 | #if defined(MBEDTLS_PKCS1_V21) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1399 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1400 | * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1401 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1402 | 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] | 1403 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1404 | void *p_rng, |
| 1405 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1406 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1407 | unsigned int hashlen, |
| 1408 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1409 | mbedtls_md_type_t mgf1_hash_id, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1410 | int expected_salt_len, |
| 1411 | const unsigned char *sig ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1412 | { |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1413 | int ret; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1414 | size_t siglen; |
| 1415 | unsigned char *p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1416 | unsigned char result[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1417 | unsigned char zeros[8]; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1418 | unsigned int hlen; |
| 1419 | size_t slen, msb; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1420 | const mbedtls_md_info_t *md_info; |
| 1421 | mbedtls_md_context_t md_ctx; |
Nicholas Wilson | 409401c | 2016-04-13 11:48:25 +0100 | [diff] [blame] | 1422 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1423 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1424 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 ) |
| 1425 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1426 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1427 | siglen = ctx->len; |
| 1428 | |
Paul Bakker | 27fdf46 | 2011-06-09 13:55:13 +0000 | [diff] [blame] | 1429 | if( siglen < 16 || siglen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1430 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1431 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1432 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1433 | ? mbedtls_rsa_public( ctx, sig, buf ) |
| 1434 | : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1435 | |
| 1436 | if( ret != 0 ) |
| 1437 | return( ret ); |
| 1438 | |
| 1439 | p = buf; |
| 1440 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1441 | if( buf[siglen - 1] != 0xBC ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1442 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1443 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1444 | if( md_alg != MBEDTLS_MD_NONE ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1445 | { |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1446 | /* Gather length of hash to sign */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1447 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1448 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1449 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1450 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1451 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1452 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1453 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1454 | md_info = mbedtls_md_info_from_type( mgf1_hash_id ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1455 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1456 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1457 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1458 | hlen = mbedtls_md_get_size( md_info ); |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1459 | slen = siglen - hlen - 1; /* Currently length of salt + padding */ |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1460 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1461 | memset( zeros, 0, 8 ); |
Paul Bakker | 53019ae | 2011-03-25 13:58:48 +0000 | [diff] [blame] | 1462 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1463 | /* |
| 1464 | * Note: EMSA-PSS verification is over the length of N - 1 bits |
| 1465 | */ |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 1466 | msb = mbedtls_mpi_bitlen( &ctx->N ) - 1; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1467 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1468 | /* Compensate for boundary condition when applying mask */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1469 | if( msb % 8 == 0 ) |
| 1470 | { |
| 1471 | p++; |
| 1472 | siglen -= 1; |
| 1473 | } |
| 1474 | if( buf[0] >> ( 8 - siglen * 8 + msb ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1475 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1476 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1477 | mbedtls_md_init( &md_ctx ); |
Brian J Murray | e7be5bd | 2016-06-23 12:57:03 -0700 | [diff] [blame] | 1478 | if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 ) |
| 1479 | { |
| 1480 | mbedtls_md_free( &md_ctx ); |
| 1481 | return( ret ); |
| 1482 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1483 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1484 | mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx ); |
Paul Bakker | 02303e8 | 2013-01-03 11:08:31 +0100 | [diff] [blame] | 1485 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1486 | buf[0] &= 0xFF >> ( siglen * 8 - msb ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1487 | |
Paul Bakker | 4de44aa | 2013-12-31 11:43:01 +0100 | [diff] [blame] | 1488 | while( p < buf + siglen && *p == 0 ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1489 | p++; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1490 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1491 | if( p == buf + siglen || |
| 1492 | *p++ != 0x01 ) |
| 1493 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1494 | mbedtls_md_free( &md_ctx ); |
| 1495 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1496 | } |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1497 | |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1498 | /* Actual salt len */ |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1499 | slen -= p - buf; |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1500 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1501 | if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY && |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1502 | slen != (size_t) expected_salt_len ) |
| 1503 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1504 | mbedtls_md_free( &md_ctx ); |
| 1505 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1506 | } |
| 1507 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1508 | /* |
| 1509 | * Generate H = Hash( M' ) |
| 1510 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1511 | mbedtls_md_starts( &md_ctx ); |
| 1512 | mbedtls_md_update( &md_ctx, zeros, 8 ); |
| 1513 | mbedtls_md_update( &md_ctx, hash, hashlen ); |
| 1514 | mbedtls_md_update( &md_ctx, p, slen ); |
| 1515 | mbedtls_md_finish( &md_ctx, result ); |
Paul Bakker | 53019ae | 2011-03-25 13:58:48 +0000 | [diff] [blame] | 1516 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1517 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | 9dcc322 | 2011-03-08 14:16:06 +0000 | [diff] [blame] | 1518 | |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1519 | if( memcmp( p + slen, result, hlen ) == 0 ) |
| 1520 | return( 0 ); |
| 1521 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1522 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1523 | } |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1524 | |
| 1525 | /* |
| 1526 | * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function |
| 1527 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1528 | int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1529 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1530 | void *p_rng, |
| 1531 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1532 | mbedtls_md_type_t md_alg, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1533 | unsigned int hashlen, |
| 1534 | const unsigned char *hash, |
| 1535 | const unsigned char *sig ) |
| 1536 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1537 | mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE ) |
| 1538 | ? (mbedtls_md_type_t) ctx->hash_id |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1539 | : md_alg; |
| 1540 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1541 | 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] | 1542 | md_alg, hashlen, hash, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1543 | mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY, |
Manuel Pégourié-Gonnard | 5ec628a | 2014-06-03 11:44:06 +0200 | [diff] [blame] | 1544 | sig ) ); |
| 1545 | |
| 1546 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1547 | #endif /* MBEDTLS_PKCS1_V21 */ |
Paul Bakker | 40628ba | 2013-01-03 10:50:31 +0100 | [diff] [blame] | 1548 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1549 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1550 | /* |
| 1551 | * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function |
| 1552 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1553 | int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1554 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1555 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1556 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1557 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1558 | unsigned int hashlen, |
| 1559 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 1560 | const unsigned char *sig ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1561 | { |
| 1562 | int ret; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1563 | size_t len, siglen, asn1_len; |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 1564 | unsigned char *p, *p0, *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1565 | mbedtls_md_type_t msg_md_alg; |
| 1566 | const mbedtls_md_info_t *md_info; |
| 1567 | mbedtls_asn1_buf oid; |
Nicholas Wilson | 409401c | 2016-04-13 11:48:25 +0100 | [diff] [blame] | 1568 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1569 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1570 | if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 ) |
| 1571 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1572 | |
| 1573 | siglen = ctx->len; |
| 1574 | |
| 1575 | if( siglen < 16 || siglen > sizeof( buf ) ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1576 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1577 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1578 | ret = ( mode == MBEDTLS_RSA_PUBLIC ) |
| 1579 | ? mbedtls_rsa_public( ctx, sig, buf ) |
| 1580 | : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1581 | |
| 1582 | if( ret != 0 ) |
| 1583 | return( ret ); |
| 1584 | |
| 1585 | p = buf; |
| 1586 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1587 | if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN ) |
| 1588 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1589 | |
| 1590 | while( *p != 0 ) |
| 1591 | { |
| 1592 | if( p >= buf + siglen - 1 || *p != 0xFF ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1593 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1594 | p++; |
| 1595 | } |
Manuel Pégourié-Gonnard | c1380de | 2017-05-11 12:49:51 +0200 | [diff] [blame] | 1596 | p++; /* skip 00 byte */ |
| 1597 | |
| 1598 | /* We've read: 00 01 PS 00 where PS must be at least 8 bytes */ |
| 1599 | if( p - buf < 11 ) |
| 1600 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1601 | |
| 1602 | len = siglen - ( p - buf ); |
| 1603 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1604 | if( len == hashlen && md_alg == MBEDTLS_MD_NONE ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1605 | { |
| 1606 | if( memcmp( p, hash, hashlen ) == 0 ) |
| 1607 | return( 0 ); |
| 1608 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1609 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1612 | md_info = mbedtls_md_info_from_type( md_alg ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1613 | if( md_info == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1614 | return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA ); |
| 1615 | hashlen = mbedtls_md_get_size( md_info ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1616 | |
| 1617 | end = p + len; |
| 1618 | |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1619 | /* |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1620 | * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure. |
| 1621 | * Insist on 2-byte length tags, to protect against variants of |
| 1622 | * Bleichenbacher's forgery attack against lax PKCS#1v1.5 verification. |
Simon Butcher | 0203745 | 2016-03-01 21:19:12 +0000 | [diff] [blame] | 1623 | */ |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 1624 | p0 = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1625 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, |
| 1626 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 1627 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 1628 | if( p != p0 + 2 || asn1_len + 2 != len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1629 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1630 | |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1631 | p0 = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1632 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, |
| 1633 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 1634 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1635 | if( p != p0 + 2 || asn1_len + 6 + hashlen != len ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1636 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1637 | |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1638 | p0 = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1639 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 ) |
| 1640 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1641 | if( p != p0 + 2 ) |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 1642 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1643 | |
| 1644 | oid.p = p; |
| 1645 | p += oid.len; |
| 1646 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1647 | if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 ) |
| 1648 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1649 | |
| 1650 | if( md_alg != msg_md_alg ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1651 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1652 | |
| 1653 | /* |
| 1654 | * assume the algorithm parameters must be NULL |
| 1655 | */ |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1656 | p0 = p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1657 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 ) |
| 1658 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | e7e7650 | 2017-05-04 12:48:39 +0200 | [diff] [blame] | 1659 | if( p != p0 + 2 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1660 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1661 | |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 1662 | p0 = p; |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1663 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
| 1664 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Gilles Peskine | 0e17eb0 | 2017-05-03 18:32:21 +0200 | [diff] [blame] | 1665 | if( p != p0 + 2 || asn1_len != hashlen ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1666 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1667 | |
| 1668 | if( memcmp( p, hash, hashlen ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1669 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1670 | |
| 1671 | p += hashlen; |
| 1672 | |
| 1673 | if( p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1674 | return( MBEDTLS_ERR_RSA_VERIFY_FAILED ); |
Paul Bakker | c70b982 | 2013-04-07 22:00:46 +0200 | [diff] [blame] | 1675 | |
| 1676 | return( 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1677 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1678 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1679 | |
| 1680 | /* |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1681 | * Do an RSA operation and check the message digest |
| 1682 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1683 | int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx, |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame] | 1684 | int (*f_rng)(void *, unsigned char *, size_t), |
| 1685 | void *p_rng, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1686 | int mode, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1687 | mbedtls_md_type_t md_alg, |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1688 | unsigned int hashlen, |
| 1689 | const unsigned char *hash, |
Manuel Pégourié-Gonnard | cc0a9d0 | 2013-08-12 11:34:35 +0200 | [diff] [blame] | 1690 | const unsigned char *sig ) |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1691 | { |
| 1692 | switch( ctx->padding ) |
| 1693 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1694 | #if defined(MBEDTLS_PKCS1_V15) |
| 1695 | case MBEDTLS_RSA_PKCS_V15: |
| 1696 | 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] | 1697 | hashlen, hash, sig ); |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1698 | #endif |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1699 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1700 | #if defined(MBEDTLS_PKCS1_V21) |
| 1701 | case MBEDTLS_RSA_PKCS_V21: |
| 1702 | 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] | 1703 | hashlen, hash, sig ); |
| 1704 | #endif |
| 1705 | |
| 1706 | default: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1707 | return( MBEDTLS_ERR_RSA_INVALID_PADDING ); |
Paul Bakker | b386913 | 2013-02-28 17:21:01 +0100 | [diff] [blame] | 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | /* |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1712 | * Copy the components of an RSA key |
| 1713 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1714 | 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] | 1715 | { |
| 1716 | int ret; |
| 1717 | |
| 1718 | dst->ver = src->ver; |
| 1719 | dst->len = src->len; |
| 1720 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1721 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) ); |
| 1722 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1724 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) ); |
| 1725 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) ); |
| 1726 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) ); |
| 1727 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) ); |
| 1728 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) ); |
| 1729 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1730 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1731 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) ); |
| 1732 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) ); |
| 1733 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1734 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1735 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) ); |
| 1736 | MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) ); |
Manuel Pégourié-Gonnard | ea53a55 | 2013-09-10 13:29:30 +0200 | [diff] [blame] | 1737 | |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1738 | dst->padding = src->padding; |
Manuel Pégourié-Gonnard | fdddac9 | 2014-03-25 15:58:35 +0100 | [diff] [blame] | 1739 | dst->hash_id = src->hash_id; |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1740 | |
| 1741 | cleanup: |
| 1742 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1743 | mbedtls_rsa_free( dst ); |
Manuel Pégourié-Gonnard | 3053f5b | 2013-08-14 13:39:57 +0200 | [diff] [blame] | 1744 | |
| 1745 | return( ret ); |
| 1746 | } |
| 1747 | |
| 1748 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1749 | * Free the components of an RSA key |
| 1750 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1751 | void mbedtls_rsa_free( mbedtls_rsa_context *ctx ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1752 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1753 | mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf ); |
| 1754 | mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN ); |
| 1755 | mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP ); |
| 1756 | mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D ); |
| 1757 | mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 1758 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1759 | #if defined(MBEDTLS_THREADING_C) |
| 1760 | mbedtls_mutex_free( &ctx->mutex ); |
Paul Bakker | c9965dc | 2013-09-29 14:58:17 +0200 | [diff] [blame] | 1761 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1764 | #if defined(MBEDTLS_SELF_TEST) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1765 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 1766 | #include "mbedtls/sha1.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1767 | |
| 1768 | /* |
| 1769 | * Example RSA-1024 keypair, for test purposes |
| 1770 | */ |
| 1771 | #define KEY_LEN 128 |
| 1772 | |
| 1773 | #define RSA_N "9292758453063D803DD603D5E777D788" \ |
| 1774 | "8ED1D5BF35786190FA2F23EBC0848AEA" \ |
| 1775 | "DDA92CA6C3D80B32C4D109BE0F36D6AE" \ |
| 1776 | "7130B9CED7ACDF54CFC7555AC14EEBAB" \ |
| 1777 | "93A89813FBF3C4F8066D2D800F7C38A8" \ |
| 1778 | "1AE31942917403FF4946B0A83D3D3E05" \ |
| 1779 | "EE57C6F5F5606FB5D4BC6CD34EE0801A" \ |
| 1780 | "5E94BB77B07507233A0BC7BAC8F90F79" |
| 1781 | |
| 1782 | #define RSA_E "10001" |
| 1783 | |
| 1784 | #define RSA_D "24BF6185468786FDD303083D25E64EFC" \ |
| 1785 | "66CA472BC44D253102F8B4A9D3BFA750" \ |
| 1786 | "91386C0077937FE33FA3252D28855837" \ |
| 1787 | "AE1B484A8A9A45F7EE8C0C634F99E8CD" \ |
| 1788 | "DF79C5CE07EE72C7F123142198164234" \ |
| 1789 | "CABB724CF78B8173B9F880FC86322407" \ |
| 1790 | "AF1FEDFDDE2BEB674CA15F3E81A1521E" \ |
| 1791 | "071513A1E85B5DFA031F21ECAE91A34D" |
| 1792 | |
| 1793 | #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \ |
| 1794 | "2C01CAD19EA484A87EA4377637E75500" \ |
| 1795 | "FCB2005C5C7DD6EC4AC023CDA285D796" \ |
| 1796 | "C3D9E75E1EFC42488BB4F1D13AC30A57" |
| 1797 | |
| 1798 | #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \ |
| 1799 | "E211C2B9E5DB1ED0BF61D0D9899620F4" \ |
| 1800 | "910E4168387E3C30AA1E00C339A79508" \ |
| 1801 | "8452DD96A9A5EA5D9DCA68DA636032AF" |
| 1802 | |
| 1803 | #define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \ |
| 1804 | "3C94D22288ACD763FD8E5600ED4A702D" \ |
| 1805 | "F84198A5F06C2E72236AE490C93F07F8" \ |
| 1806 | "3CC559CD27BC2D1CA488811730BB5725" |
| 1807 | |
| 1808 | #define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \ |
| 1809 | "D8AAEA56749EA28623272E4F7D0592AF" \ |
| 1810 | "7C1F1313CAC9471B5C523BFE592F517B" \ |
| 1811 | "407A1BD76C164B93DA2D32A383E58357" |
| 1812 | |
| 1813 | #define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \ |
| 1814 | "F38D18D2B2F0E2DD275AA977E2BF4411" \ |
| 1815 | "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \ |
| 1816 | "A74206CEC169D74BF5A8C50D6F48EA08" |
| 1817 | |
| 1818 | #define PT_LEN 24 |
| 1819 | #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \ |
| 1820 | "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD" |
| 1821 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1822 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1823 | static int myrand( void *rng_state, unsigned char *output, size_t len ) |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 1824 | { |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 1825 | #if !defined(__OpenBSD__) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1826 | size_t i; |
| 1827 | |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 1828 | if( rng_state != NULL ) |
| 1829 | rng_state = NULL; |
| 1830 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1831 | for( i = 0; i < len; ++i ) |
| 1832 | output[i] = rand(); |
Paul Bakker | f96f7b6 | 2014-04-30 16:02:38 +0200 | [diff] [blame] | 1833 | #else |
| 1834 | if( rng_state != NULL ) |
| 1835 | rng_state = NULL; |
| 1836 | |
| 1837 | arc4random_buf( output, len ); |
| 1838 | #endif /* !OpenBSD */ |
Paul Bakker | 48377d9 | 2013-08-30 12:06:24 +0200 | [diff] [blame] | 1839 | |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 1840 | return( 0 ); |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 1841 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1842 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 545570e | 2010-07-18 09:00:25 +0000 | [diff] [blame] | 1843 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1844 | /* |
| 1845 | * Checkup routine |
| 1846 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1847 | int mbedtls_rsa_self_test( int verbose ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1848 | { |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 1849 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1850 | #if defined(MBEDTLS_PKCS1_V15) |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1851 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1852 | mbedtls_rsa_context rsa; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1853 | unsigned char rsa_plaintext[PT_LEN]; |
| 1854 | unsigned char rsa_decrypted[PT_LEN]; |
| 1855 | unsigned char rsa_ciphertext[KEY_LEN]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1856 | #if defined(MBEDTLS_SHA1_C) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 1857 | unsigned char sha1sum[20]; |
| 1858 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1859 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1860 | mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1861 | |
| 1862 | rsa.len = KEY_LEN; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1863 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) ); |
| 1864 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) ); |
| 1865 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) ); |
| 1866 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) ); |
| 1867 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) ); |
| 1868 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) ); |
| 1869 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) ); |
| 1870 | MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1871 | |
| 1872 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1873 | mbedtls_printf( " RSA key validation: " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1874 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1875 | if( mbedtls_rsa_check_pubkey( &rsa ) != 0 || |
| 1876 | mbedtls_rsa_check_privkey( &rsa ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1877 | { |
| 1878 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1879 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1880 | |
| 1881 | return( 1 ); |
| 1882 | } |
| 1883 | |
| 1884 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1885 | mbedtls_printf( "passed\n PKCS#1 encryption : " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1886 | |
| 1887 | memcpy( rsa_plaintext, RSA_PT, PT_LEN ); |
| 1888 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1889 | if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1890 | rsa_plaintext, rsa_ciphertext ) != 0 ) |
| 1891 | { |
| 1892 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1893 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1894 | |
| 1895 | return( 1 ); |
| 1896 | } |
| 1897 | |
| 1898 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1899 | mbedtls_printf( "passed\n PKCS#1 decryption : " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1900 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1901 | if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len, |
Paul Bakker | 060c568 | 2009-01-12 21:48:39 +0000 | [diff] [blame] | 1902 | rsa_ciphertext, rsa_decrypted, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 1903 | sizeof(rsa_decrypted) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1904 | { |
| 1905 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1906 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1907 | |
| 1908 | return( 1 ); |
| 1909 | } |
| 1910 | |
| 1911 | if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 ) |
| 1912 | { |
| 1913 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1914 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1915 | |
| 1916 | return( 1 ); |
| 1917 | } |
| 1918 | |
Manuel Pégourié-Gonnard | d1004f0 | 2015-08-07 10:46:54 +0200 | [diff] [blame] | 1919 | if( verbose != 0 ) |
| 1920 | mbedtls_printf( "passed\n" ); |
| 1921 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1922 | #if defined(MBEDTLS_SHA1_C) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1923 | if( verbose != 0 ) |
Brian Murray | 930a370 | 2016-05-18 14:38:02 -0700 | [diff] [blame] | 1924 | mbedtls_printf( " PKCS#1 data sign : " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1925 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1926 | mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1927 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1928 | if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1929 | sha1sum, rsa_ciphertext ) != 0 ) |
| 1930 | { |
| 1931 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1932 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1933 | |
| 1934 | return( 1 ); |
| 1935 | } |
| 1936 | |
| 1937 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1938 | mbedtls_printf( "passed\n PKCS#1 sig. verify: " ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1939 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1940 | if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1941 | sha1sum, rsa_ciphertext ) != 0 ) |
| 1942 | { |
| 1943 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1944 | mbedtls_printf( "failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1945 | |
| 1946 | return( 1 ); |
| 1947 | } |
| 1948 | |
| 1949 | if( verbose != 0 ) |
Manuel Pégourié-Gonnard | d1004f0 | 2015-08-07 10:46:54 +0200 | [diff] [blame] | 1950 | mbedtls_printf( "passed\n" ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1951 | #endif /* MBEDTLS_SHA1_C */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1952 | |
Manuel Pégourié-Gonnard | d1004f0 | 2015-08-07 10:46:54 +0200 | [diff] [blame] | 1953 | if( verbose != 0 ) |
| 1954 | mbedtls_printf( "\n" ); |
| 1955 | |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 1956 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1957 | mbedtls_rsa_free( &rsa ); |
| 1958 | #else /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 3e41fe8 | 2013-09-15 17:42:50 +0200 | [diff] [blame] | 1959 | ((void) verbose); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1960 | #endif /* MBEDTLS_PKCS1_V15 */ |
Paul Bakker | 3d8fb63 | 2014-04-17 12:42:41 +0200 | [diff] [blame] | 1961 | return( ret ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1964 | #endif /* MBEDTLS_SELF_TEST */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1965 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1966 | #endif /* MBEDTLS_RSA_C */ |