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