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