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