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