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