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