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