Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Simon Cooksey 0:fb7af294d5d9 1 /*
Simon Cooksey 0:fb7af294d5d9 2 * The RSA public-key cryptosystem
Simon Cooksey 0:fb7af294d5d9 3 *
Simon Cooksey 0:fb7af294d5d9 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Simon Cooksey 0:fb7af294d5d9 5 * SPDX-License-Identifier: Apache-2.0
Simon Cooksey 0:fb7af294d5d9 6 *
Simon Cooksey 0:fb7af294d5d9 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
Simon Cooksey 0:fb7af294d5d9 8 * not use this file except in compliance with the License.
Simon Cooksey 0:fb7af294d5d9 9 * You may obtain a copy of the License at
Simon Cooksey 0:fb7af294d5d9 10 *
Simon Cooksey 0:fb7af294d5d9 11 * http://www.apache.org/licenses/LICENSE-2.0
Simon Cooksey 0:fb7af294d5d9 12 *
Simon Cooksey 0:fb7af294d5d9 13 * Unless required by applicable law or agreed to in writing, software
Simon Cooksey 0:fb7af294d5d9 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
Simon Cooksey 0:fb7af294d5d9 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Simon Cooksey 0:fb7af294d5d9 16 * See the License for the specific language governing permissions and
Simon Cooksey 0:fb7af294d5d9 17 * limitations under the License.
Simon Cooksey 0:fb7af294d5d9 18 *
Simon Cooksey 0:fb7af294d5d9 19 * This file is part of mbed TLS (https://tls.mbed.org)
Simon Cooksey 0:fb7af294d5d9 20 */
Simon Cooksey 0:fb7af294d5d9 21 /*
Simon Cooksey 0:fb7af294d5d9 22 * The following sources were referenced in the design of this implementation
Simon Cooksey 0:fb7af294d5d9 23 * of the RSA algorithm:
Simon Cooksey 0:fb7af294d5d9 24 *
Simon Cooksey 0:fb7af294d5d9 25 * [1] A method for obtaining digital signatures and public-key cryptosystems
Simon Cooksey 0:fb7af294d5d9 26 * R Rivest, A Shamir, and L Adleman
Simon Cooksey 0:fb7af294d5d9 27 * http://people.csail.mit.edu/rivest/pubs.html#RSA78
Simon Cooksey 0:fb7af294d5d9 28 *
Simon Cooksey 0:fb7af294d5d9 29 * [2] Handbook of Applied Cryptography - 1997, Chapter 8
Simon Cooksey 0:fb7af294d5d9 30 * Menezes, van Oorschot and Vanstone
Simon Cooksey 0:fb7af294d5d9 31 *
Simon Cooksey 0:fb7af294d5d9 32 */
Simon Cooksey 0:fb7af294d5d9 33
Simon Cooksey 0:fb7af294d5d9 34 #if !defined(MBEDTLS_CONFIG_FILE)
Simon Cooksey 0:fb7af294d5d9 35 #include "mbedtls/config.h"
Simon Cooksey 0:fb7af294d5d9 36 #else
Simon Cooksey 0:fb7af294d5d9 37 #include MBEDTLS_CONFIG_FILE
Simon Cooksey 0:fb7af294d5d9 38 #endif
Simon Cooksey 0:fb7af294d5d9 39
Simon Cooksey 0:fb7af294d5d9 40 #if defined(MBEDTLS_RSA_C)
Simon Cooksey 0:fb7af294d5d9 41
Simon Cooksey 0:fb7af294d5d9 42 #include "mbedtls/rsa.h"
Simon Cooksey 0:fb7af294d5d9 43 #include "mbedtls/oid.h"
Simon Cooksey 0:fb7af294d5d9 44
Simon Cooksey 0:fb7af294d5d9 45 #include <string.h>
Simon Cooksey 0:fb7af294d5d9 46
Simon Cooksey 0:fb7af294d5d9 47 #if defined(MBEDTLS_PKCS1_V21)
Simon Cooksey 0:fb7af294d5d9 48 #include "mbedtls/md.h"
Simon Cooksey 0:fb7af294d5d9 49 #endif
Simon Cooksey 0:fb7af294d5d9 50
Simon Cooksey 0:fb7af294d5d9 51 #if defined(MBEDTLS_PKCS1_V15) && !defined(__OpenBSD__)
Simon Cooksey 0:fb7af294d5d9 52 #include <stdlib.h>
Simon Cooksey 0:fb7af294d5d9 53 #endif
Simon Cooksey 0:fb7af294d5d9 54
Simon Cooksey 0:fb7af294d5d9 55 #if defined(MBEDTLS_PLATFORM_C)
Simon Cooksey 0:fb7af294d5d9 56 #include "mbedtls/platform.h"
Simon Cooksey 0:fb7af294d5d9 57 #else
Simon Cooksey 0:fb7af294d5d9 58 #include <stdio.h>
Simon Cooksey 0:fb7af294d5d9 59 #define mbedtls_printf printf
Simon Cooksey 0:fb7af294d5d9 60 #define mbedtls_calloc calloc
Simon Cooksey 0:fb7af294d5d9 61 #define mbedtls_free free
Simon Cooksey 0:fb7af294d5d9 62 #endif
Simon Cooksey 0:fb7af294d5d9 63
Simon Cooksey 0:fb7af294d5d9 64 /*
Simon Cooksey 0:fb7af294d5d9 65 * Initialize an RSA context
Simon Cooksey 0:fb7af294d5d9 66 */
Simon Cooksey 0:fb7af294d5d9 67 void mbedtls_rsa_init( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 68 int padding,
Simon Cooksey 0:fb7af294d5d9 69 int hash_id )
Simon Cooksey 0:fb7af294d5d9 70 {
Simon Cooksey 0:fb7af294d5d9 71 memset( ctx, 0, sizeof( mbedtls_rsa_context ) );
Simon Cooksey 0:fb7af294d5d9 72
Simon Cooksey 0:fb7af294d5d9 73 mbedtls_rsa_set_padding( ctx, padding, hash_id );
Simon Cooksey 0:fb7af294d5d9 74
Simon Cooksey 0:fb7af294d5d9 75 #if defined(MBEDTLS_THREADING_C)
Simon Cooksey 0:fb7af294d5d9 76 mbedtls_mutex_init( &ctx->mutex );
Simon Cooksey 0:fb7af294d5d9 77 #endif
Simon Cooksey 0:fb7af294d5d9 78 }
Simon Cooksey 0:fb7af294d5d9 79
Simon Cooksey 0:fb7af294d5d9 80 /*
Simon Cooksey 0:fb7af294d5d9 81 * Set padding for an existing RSA context
Simon Cooksey 0:fb7af294d5d9 82 */
Simon Cooksey 0:fb7af294d5d9 83 void mbedtls_rsa_set_padding( mbedtls_rsa_context *ctx, int padding, int hash_id )
Simon Cooksey 0:fb7af294d5d9 84 {
Simon Cooksey 0:fb7af294d5d9 85 ctx->padding = padding;
Simon Cooksey 0:fb7af294d5d9 86 ctx->hash_id = hash_id;
Simon Cooksey 0:fb7af294d5d9 87 }
Simon Cooksey 0:fb7af294d5d9 88
Simon Cooksey 0:fb7af294d5d9 89 #if defined(MBEDTLS_GENPRIME)
Simon Cooksey 0:fb7af294d5d9 90
Simon Cooksey 0:fb7af294d5d9 91 /*
Simon Cooksey 0:fb7af294d5d9 92 * Generate an RSA keypair
Simon Cooksey 0:fb7af294d5d9 93 */
Simon Cooksey 0:fb7af294d5d9 94 int mbedtls_rsa_gen_key( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 95 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 96 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 97 unsigned int nbits, int exponent )
Simon Cooksey 0:fb7af294d5d9 98 {
Simon Cooksey 0:fb7af294d5d9 99 int ret;
Simon Cooksey 0:fb7af294d5d9 100 mbedtls_mpi P1, Q1, H, G;
Simon Cooksey 0:fb7af294d5d9 101
Simon Cooksey 0:fb7af294d5d9 102 if( f_rng == NULL || nbits < 128 || exponent < 3 )
Simon Cooksey 0:fb7af294d5d9 103 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 104
Simon Cooksey 0:fb7af294d5d9 105 if( nbits % 2 )
Simon Cooksey 0:fb7af294d5d9 106 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 107
Simon Cooksey 0:fb7af294d5d9 108 mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Simon Cooksey 0:fb7af294d5d9 109 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &G );
Simon Cooksey 0:fb7af294d5d9 110
Simon Cooksey 0:fb7af294d5d9 111 /*
Simon Cooksey 0:fb7af294d5d9 112 * find primes P and Q with Q < P so that:
Simon Cooksey 0:fb7af294d5d9 113 * GCD( E, (P-1)*(Q-1) ) == 1
Simon Cooksey 0:fb7af294d5d9 114 */
Simon Cooksey 0:fb7af294d5d9 115 MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->E, exponent ) );
Simon Cooksey 0:fb7af294d5d9 116
Simon Cooksey 0:fb7af294d5d9 117 do
Simon Cooksey 0:fb7af294d5d9 118 {
Simon Cooksey 0:fb7af294d5d9 119 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->P, nbits >> 1, 0,
Simon Cooksey 0:fb7af294d5d9 120 f_rng, p_rng ) );
Simon Cooksey 0:fb7af294d5d9 121
Simon Cooksey 0:fb7af294d5d9 122 MBEDTLS_MPI_CHK( mbedtls_mpi_gen_prime( &ctx->Q, nbits >> 1, 0,
Simon Cooksey 0:fb7af294d5d9 123 f_rng, p_rng ) );
Simon Cooksey 0:fb7af294d5d9 124
Simon Cooksey 0:fb7af294d5d9 125 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) == 0 )
Simon Cooksey 0:fb7af294d5d9 126 continue;
Simon Cooksey 0:fb7af294d5d9 127
Simon Cooksey 0:fb7af294d5d9 128 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) );
Simon Cooksey 0:fb7af294d5d9 129 if( mbedtls_mpi_bitlen( &ctx->N ) != nbits )
Simon Cooksey 0:fb7af294d5d9 130 continue;
Simon Cooksey 0:fb7af294d5d9 131
Simon Cooksey 0:fb7af294d5d9 132 if( mbedtls_mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 )
Simon Cooksey 0:fb7af294d5d9 133 mbedtls_mpi_swap( &ctx->P, &ctx->Q );
Simon Cooksey 0:fb7af294d5d9 134
Simon Cooksey 0:fb7af294d5d9 135 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
Simon Cooksey 0:fb7af294d5d9 136 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
Simon Cooksey 0:fb7af294d5d9 137 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
Simon Cooksey 0:fb7af294d5d9 138 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Simon Cooksey 0:fb7af294d5d9 139 }
Simon Cooksey 0:fb7af294d5d9 140 while( mbedtls_mpi_cmp_int( &G, 1 ) != 0 );
Simon Cooksey 0:fb7af294d5d9 141
Simon Cooksey 0:fb7af294d5d9 142 /*
Simon Cooksey 0:fb7af294d5d9 143 * D = E^-1 mod ((P-1)*(Q-1))
Simon Cooksey 0:fb7af294d5d9 144 * DP = D mod (P - 1)
Simon Cooksey 0:fb7af294d5d9 145 * DQ = D mod (Q - 1)
Simon Cooksey 0:fb7af294d5d9 146 * QP = Q^-1 mod P
Simon Cooksey 0:fb7af294d5d9 147 */
Simon Cooksey 0:fb7af294d5d9 148 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->D , &ctx->E, &H ) );
Simon Cooksey 0:fb7af294d5d9 149 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) );
Simon Cooksey 0:fb7af294d5d9 150 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) );
Simon Cooksey 0:fb7af294d5d9 151 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) );
Simon Cooksey 0:fb7af294d5d9 152
Simon Cooksey 0:fb7af294d5d9 153 ctx->len = ( mbedtls_mpi_bitlen( &ctx->N ) + 7 ) >> 3;
Simon Cooksey 0:fb7af294d5d9 154
Simon Cooksey 0:fb7af294d5d9 155 cleanup:
Simon Cooksey 0:fb7af294d5d9 156
Simon Cooksey 0:fb7af294d5d9 157 mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 ); mbedtls_mpi_free( &H ); mbedtls_mpi_free( &G );
Simon Cooksey 0:fb7af294d5d9 158
Simon Cooksey 0:fb7af294d5d9 159 if( ret != 0 )
Simon Cooksey 0:fb7af294d5d9 160 {
Simon Cooksey 0:fb7af294d5d9 161 mbedtls_rsa_free( ctx );
Simon Cooksey 0:fb7af294d5d9 162 return( MBEDTLS_ERR_RSA_KEY_GEN_FAILED + ret );
Simon Cooksey 0:fb7af294d5d9 163 }
Simon Cooksey 0:fb7af294d5d9 164
Simon Cooksey 0:fb7af294d5d9 165 return( 0 );
Simon Cooksey 0:fb7af294d5d9 166 }
Simon Cooksey 0:fb7af294d5d9 167
Simon Cooksey 0:fb7af294d5d9 168 #endif /* MBEDTLS_GENPRIME */
Simon Cooksey 0:fb7af294d5d9 169
Simon Cooksey 0:fb7af294d5d9 170 /*
Simon Cooksey 0:fb7af294d5d9 171 * Check a public RSA key
Simon Cooksey 0:fb7af294d5d9 172 */
Simon Cooksey 0:fb7af294d5d9 173 int mbedtls_rsa_check_pubkey( const mbedtls_rsa_context *ctx )
Simon Cooksey 0:fb7af294d5d9 174 {
Simon Cooksey 0:fb7af294d5d9 175 if( !ctx->N.p || !ctx->E.p )
Simon Cooksey 0:fb7af294d5d9 176 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Simon Cooksey 0:fb7af294d5d9 177
Simon Cooksey 0:fb7af294d5d9 178 if( ( ctx->N.p[0] & 1 ) == 0 ||
Simon Cooksey 0:fb7af294d5d9 179 ( ctx->E.p[0] & 1 ) == 0 )
Simon Cooksey 0:fb7af294d5d9 180 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Simon Cooksey 0:fb7af294d5d9 181
Simon Cooksey 0:fb7af294d5d9 182 if( mbedtls_mpi_bitlen( &ctx->N ) < 128 ||
Simon Cooksey 0:fb7af294d5d9 183 mbedtls_mpi_bitlen( &ctx->N ) > MBEDTLS_MPI_MAX_BITS )
Simon Cooksey 0:fb7af294d5d9 184 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Simon Cooksey 0:fb7af294d5d9 185
Simon Cooksey 0:fb7af294d5d9 186 if( mbedtls_mpi_bitlen( &ctx->E ) < 2 ||
Simon Cooksey 0:fb7af294d5d9 187 mbedtls_mpi_cmp_mpi( &ctx->E, &ctx->N ) >= 0 )
Simon Cooksey 0:fb7af294d5d9 188 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Simon Cooksey 0:fb7af294d5d9 189
Simon Cooksey 0:fb7af294d5d9 190 return( 0 );
Simon Cooksey 0:fb7af294d5d9 191 }
Simon Cooksey 0:fb7af294d5d9 192
Simon Cooksey 0:fb7af294d5d9 193 /*
Simon Cooksey 0:fb7af294d5d9 194 * Check a private RSA key
Simon Cooksey 0:fb7af294d5d9 195 */
Simon Cooksey 0:fb7af294d5d9 196 int mbedtls_rsa_check_privkey( const mbedtls_rsa_context *ctx )
Simon Cooksey 0:fb7af294d5d9 197 {
Simon Cooksey 0:fb7af294d5d9 198 int ret;
Simon Cooksey 0:fb7af294d5d9 199 mbedtls_mpi PQ, DE, P1, Q1, H, I, G, G2, L1, L2, DP, DQ, QP;
Simon Cooksey 0:fb7af294d5d9 200
Simon Cooksey 0:fb7af294d5d9 201 if( ( ret = mbedtls_rsa_check_pubkey( ctx ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 202 return( ret );
Simon Cooksey 0:fb7af294d5d9 203
Simon Cooksey 0:fb7af294d5d9 204 if( !ctx->P.p || !ctx->Q.p || !ctx->D.p )
Simon Cooksey 0:fb7af294d5d9 205 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Simon Cooksey 0:fb7af294d5d9 206
Simon Cooksey 0:fb7af294d5d9 207 mbedtls_mpi_init( &PQ ); mbedtls_mpi_init( &DE ); mbedtls_mpi_init( &P1 ); mbedtls_mpi_init( &Q1 );
Simon Cooksey 0:fb7af294d5d9 208 mbedtls_mpi_init( &H ); mbedtls_mpi_init( &I ); mbedtls_mpi_init( &G ); mbedtls_mpi_init( &G2 );
Simon Cooksey 0:fb7af294d5d9 209 mbedtls_mpi_init( &L1 ); mbedtls_mpi_init( &L2 ); mbedtls_mpi_init( &DP ); mbedtls_mpi_init( &DQ );
Simon Cooksey 0:fb7af294d5d9 210 mbedtls_mpi_init( &QP );
Simon Cooksey 0:fb7af294d5d9 211
Simon Cooksey 0:fb7af294d5d9 212 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &PQ, &ctx->P, &ctx->Q ) );
Simon Cooksey 0:fb7af294d5d9 213 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &DE, &ctx->D, &ctx->E ) );
Simon Cooksey 0:fb7af294d5d9 214 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &P1, &ctx->P, 1 ) );
Simon Cooksey 0:fb7af294d5d9 215 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &Q1, &ctx->Q, 1 ) );
Simon Cooksey 0:fb7af294d5d9 216 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &H, &P1, &Q1 ) );
Simon Cooksey 0:fb7af294d5d9 217 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G, &ctx->E, &H ) );
Simon Cooksey 0:fb7af294d5d9 218
Simon Cooksey 0:fb7af294d5d9 219 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &G2, &P1, &Q1 ) );
Simon Cooksey 0:fb7af294d5d9 220 MBEDTLS_MPI_CHK( mbedtls_mpi_div_mpi( &L1, &L2, &H, &G2 ) );
Simon Cooksey 0:fb7af294d5d9 221 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &I, &DE, &L1 ) );
Simon Cooksey 0:fb7af294d5d9 222
Simon Cooksey 0:fb7af294d5d9 223 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DP, &ctx->D, &P1 ) );
Simon Cooksey 0:fb7af294d5d9 224 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &DQ, &ctx->D, &Q1 ) );
Simon Cooksey 0:fb7af294d5d9 225 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &QP, &ctx->Q, &ctx->P ) );
Simon Cooksey 0:fb7af294d5d9 226 /*
Simon Cooksey 0:fb7af294d5d9 227 * Check for a valid PKCS1v2 private key
Simon Cooksey 0:fb7af294d5d9 228 */
Simon Cooksey 0:fb7af294d5d9 229 if( mbedtls_mpi_cmp_mpi( &PQ, &ctx->N ) != 0 ||
Simon Cooksey 0:fb7af294d5d9 230 mbedtls_mpi_cmp_mpi( &DP, &ctx->DP ) != 0 ||
Simon Cooksey 0:fb7af294d5d9 231 mbedtls_mpi_cmp_mpi( &DQ, &ctx->DQ ) != 0 ||
Simon Cooksey 0:fb7af294d5d9 232 mbedtls_mpi_cmp_mpi( &QP, &ctx->QP ) != 0 ||
Simon Cooksey 0:fb7af294d5d9 233 mbedtls_mpi_cmp_int( &L2, 0 ) != 0 ||
Simon Cooksey 0:fb7af294d5d9 234 mbedtls_mpi_cmp_int( &I, 1 ) != 0 ||
Simon Cooksey 0:fb7af294d5d9 235 mbedtls_mpi_cmp_int( &G, 1 ) != 0 )
Simon Cooksey 0:fb7af294d5d9 236 {
Simon Cooksey 0:fb7af294d5d9 237 ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Simon Cooksey 0:fb7af294d5d9 238 }
Simon Cooksey 0:fb7af294d5d9 239
Simon Cooksey 0:fb7af294d5d9 240 cleanup:
Simon Cooksey 0:fb7af294d5d9 241 mbedtls_mpi_free( &PQ ); mbedtls_mpi_free( &DE ); mbedtls_mpi_free( &P1 ); mbedtls_mpi_free( &Q1 );
Simon Cooksey 0:fb7af294d5d9 242 mbedtls_mpi_free( &H ); mbedtls_mpi_free( &I ); mbedtls_mpi_free( &G ); mbedtls_mpi_free( &G2 );
Simon Cooksey 0:fb7af294d5d9 243 mbedtls_mpi_free( &L1 ); mbedtls_mpi_free( &L2 ); mbedtls_mpi_free( &DP ); mbedtls_mpi_free( &DQ );
Simon Cooksey 0:fb7af294d5d9 244 mbedtls_mpi_free( &QP );
Simon Cooksey 0:fb7af294d5d9 245
Simon Cooksey 0:fb7af294d5d9 246 if( ret == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED )
Simon Cooksey 0:fb7af294d5d9 247 return( ret );
Simon Cooksey 0:fb7af294d5d9 248
Simon Cooksey 0:fb7af294d5d9 249 if( ret != 0 )
Simon Cooksey 0:fb7af294d5d9 250 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED + ret );
Simon Cooksey 0:fb7af294d5d9 251
Simon Cooksey 0:fb7af294d5d9 252 return( 0 );
Simon Cooksey 0:fb7af294d5d9 253 }
Simon Cooksey 0:fb7af294d5d9 254
Simon Cooksey 0:fb7af294d5d9 255 /*
Simon Cooksey 0:fb7af294d5d9 256 * Check if contexts holding a public and private key match
Simon Cooksey 0:fb7af294d5d9 257 */
Simon Cooksey 0:fb7af294d5d9 258 int mbedtls_rsa_check_pub_priv( const mbedtls_rsa_context *pub, const mbedtls_rsa_context *prv )
Simon Cooksey 0:fb7af294d5d9 259 {
Simon Cooksey 0:fb7af294d5d9 260 if( mbedtls_rsa_check_pubkey( pub ) != 0 ||
Simon Cooksey 0:fb7af294d5d9 261 mbedtls_rsa_check_privkey( prv ) != 0 )
Simon Cooksey 0:fb7af294d5d9 262 {
Simon Cooksey 0:fb7af294d5d9 263 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Simon Cooksey 0:fb7af294d5d9 264 }
Simon Cooksey 0:fb7af294d5d9 265
Simon Cooksey 0:fb7af294d5d9 266 if( mbedtls_mpi_cmp_mpi( &pub->N, &prv->N ) != 0 ||
Simon Cooksey 0:fb7af294d5d9 267 mbedtls_mpi_cmp_mpi( &pub->E, &prv->E ) != 0 )
Simon Cooksey 0:fb7af294d5d9 268 {
Simon Cooksey 0:fb7af294d5d9 269 return( MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
Simon Cooksey 0:fb7af294d5d9 270 }
Simon Cooksey 0:fb7af294d5d9 271
Simon Cooksey 0:fb7af294d5d9 272 return( 0 );
Simon Cooksey 0:fb7af294d5d9 273 }
Simon Cooksey 0:fb7af294d5d9 274
Simon Cooksey 0:fb7af294d5d9 275 /*
Simon Cooksey 0:fb7af294d5d9 276 * Do an RSA public key operation
Simon Cooksey 0:fb7af294d5d9 277 */
Simon Cooksey 0:fb7af294d5d9 278 int mbedtls_rsa_public( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 279 const unsigned char *input,
Simon Cooksey 0:fb7af294d5d9 280 unsigned char *output )
Simon Cooksey 0:fb7af294d5d9 281 {
Simon Cooksey 0:fb7af294d5d9 282 int ret;
Simon Cooksey 0:fb7af294d5d9 283 size_t olen;
Simon Cooksey 0:fb7af294d5d9 284 mbedtls_mpi T;
Simon Cooksey 0:fb7af294d5d9 285
Simon Cooksey 0:fb7af294d5d9 286 mbedtls_mpi_init( &T );
Simon Cooksey 0:fb7af294d5d9 287
Simon Cooksey 0:fb7af294d5d9 288 #if defined(MBEDTLS_THREADING_C)
Simon Cooksey 0:fb7af294d5d9 289 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 290 return( ret );
Simon Cooksey 0:fb7af294d5d9 291 #endif
Simon Cooksey 0:fb7af294d5d9 292
Simon Cooksey 0:fb7af294d5d9 293 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Simon Cooksey 0:fb7af294d5d9 294
Simon Cooksey 0:fb7af294d5d9 295 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Simon Cooksey 0:fb7af294d5d9 296 {
Simon Cooksey 0:fb7af294d5d9 297 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Simon Cooksey 0:fb7af294d5d9 298 goto cleanup;
Simon Cooksey 0:fb7af294d5d9 299 }
Simon Cooksey 0:fb7af294d5d9 300
Simon Cooksey 0:fb7af294d5d9 301 olen = ctx->len;
Simon Cooksey 0:fb7af294d5d9 302 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->E, &ctx->N, &ctx->RN ) );
Simon Cooksey 0:fb7af294d5d9 303 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Simon Cooksey 0:fb7af294d5d9 304
Simon Cooksey 0:fb7af294d5d9 305 cleanup:
Simon Cooksey 0:fb7af294d5d9 306 #if defined(MBEDTLS_THREADING_C)
Simon Cooksey 0:fb7af294d5d9 307 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Simon Cooksey 0:fb7af294d5d9 308 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Simon Cooksey 0:fb7af294d5d9 309 #endif
Simon Cooksey 0:fb7af294d5d9 310
Simon Cooksey 0:fb7af294d5d9 311 mbedtls_mpi_free( &T );
Simon Cooksey 0:fb7af294d5d9 312
Simon Cooksey 0:fb7af294d5d9 313 if( ret != 0 )
Simon Cooksey 0:fb7af294d5d9 314 return( MBEDTLS_ERR_RSA_PUBLIC_FAILED + ret );
Simon Cooksey 0:fb7af294d5d9 315
Simon Cooksey 0:fb7af294d5d9 316 return( 0 );
Simon Cooksey 0:fb7af294d5d9 317 }
Simon Cooksey 0:fb7af294d5d9 318
Simon Cooksey 0:fb7af294d5d9 319 /*
Simon Cooksey 0:fb7af294d5d9 320 * Generate or update blinding values, see section 10 of:
Simon Cooksey 0:fb7af294d5d9 321 * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
Simon Cooksey 0:fb7af294d5d9 322 * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
Simon Cooksey 0:fb7af294d5d9 323 * Berlin Heidelberg, 1996. p. 104-113.
Simon Cooksey 0:fb7af294d5d9 324 */
Simon Cooksey 0:fb7af294d5d9 325 static int rsa_prepare_blinding( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 326 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Simon Cooksey 0:fb7af294d5d9 327 {
Simon Cooksey 0:fb7af294d5d9 328 int ret, count = 0;
Simon Cooksey 0:fb7af294d5d9 329
Simon Cooksey 0:fb7af294d5d9 330 if( ctx->Vf.p != NULL )
Simon Cooksey 0:fb7af294d5d9 331 {
Simon Cooksey 0:fb7af294d5d9 332 /* We already have blinding values, just update them by squaring */
Simon Cooksey 0:fb7af294d5d9 333 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
Simon Cooksey 0:fb7af294d5d9 334 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->N ) );
Simon Cooksey 0:fb7af294d5d9 335 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
Simon Cooksey 0:fb7af294d5d9 336 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->N ) );
Simon Cooksey 0:fb7af294d5d9 337
Simon Cooksey 0:fb7af294d5d9 338 goto cleanup;
Simon Cooksey 0:fb7af294d5d9 339 }
Simon Cooksey 0:fb7af294d5d9 340
Simon Cooksey 0:fb7af294d5d9 341 /* Unblinding value: Vf = random number, invertible mod N */
Simon Cooksey 0:fb7af294d5d9 342 do {
Simon Cooksey 0:fb7af294d5d9 343 if( count++ > 10 )
Simon Cooksey 0:fb7af294d5d9 344 return( MBEDTLS_ERR_RSA_RNG_FAILED );
Simon Cooksey 0:fb7af294d5d9 345
Simon Cooksey 0:fb7af294d5d9 346 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vf, ctx->len - 1, f_rng, p_rng ) );
Simon Cooksey 0:fb7af294d5d9 347 MBEDTLS_MPI_CHK( mbedtls_mpi_gcd( &ctx->Vi, &ctx->Vf, &ctx->N ) );
Simon Cooksey 0:fb7af294d5d9 348 } while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 );
Simon Cooksey 0:fb7af294d5d9 349
Simon Cooksey 0:fb7af294d5d9 350 /* Blinding value: Vi = Vf^(-e) mod N */
Simon Cooksey 0:fb7af294d5d9 351 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vi, &ctx->Vf, &ctx->N ) );
Simon Cooksey 0:fb7af294d5d9 352 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vi, &ctx->Vi, &ctx->E, &ctx->N, &ctx->RN ) );
Simon Cooksey 0:fb7af294d5d9 353
Simon Cooksey 0:fb7af294d5d9 354
Simon Cooksey 0:fb7af294d5d9 355 cleanup:
Simon Cooksey 0:fb7af294d5d9 356 return( ret );
Simon Cooksey 0:fb7af294d5d9 357 }
Simon Cooksey 0:fb7af294d5d9 358
Simon Cooksey 0:fb7af294d5d9 359 /*
Simon Cooksey 0:fb7af294d5d9 360 * Do an RSA private key operation
Simon Cooksey 0:fb7af294d5d9 361 */
Simon Cooksey 0:fb7af294d5d9 362 int mbedtls_rsa_private( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 363 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 364 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 365 const unsigned char *input,
Simon Cooksey 0:fb7af294d5d9 366 unsigned char *output )
Simon Cooksey 0:fb7af294d5d9 367 {
Simon Cooksey 0:fb7af294d5d9 368 int ret;
Simon Cooksey 0:fb7af294d5d9 369 size_t olen;
Simon Cooksey 0:fb7af294d5d9 370 mbedtls_mpi T, T1, T2;
Simon Cooksey 0:fb7af294d5d9 371
Simon Cooksey 0:fb7af294d5d9 372 /* Make sure we have private key info, prevent possible misuse */
Simon Cooksey 0:fb7af294d5d9 373 if( ctx->P.p == NULL || ctx->Q.p == NULL || ctx->D.p == NULL )
Simon Cooksey 0:fb7af294d5d9 374 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 375
Simon Cooksey 0:fb7af294d5d9 376 mbedtls_mpi_init( &T ); mbedtls_mpi_init( &T1 ); mbedtls_mpi_init( &T2 );
Simon Cooksey 0:fb7af294d5d9 377
Simon Cooksey 0:fb7af294d5d9 378 #if defined(MBEDTLS_THREADING_C)
Simon Cooksey 0:fb7af294d5d9 379 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 380 return( ret );
Simon Cooksey 0:fb7af294d5d9 381 #endif
Simon Cooksey 0:fb7af294d5d9 382
Simon Cooksey 0:fb7af294d5d9 383 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &T, input, ctx->len ) );
Simon Cooksey 0:fb7af294d5d9 384 if( mbedtls_mpi_cmp_mpi( &T, &ctx->N ) >= 0 )
Simon Cooksey 0:fb7af294d5d9 385 {
Simon Cooksey 0:fb7af294d5d9 386 ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
Simon Cooksey 0:fb7af294d5d9 387 goto cleanup;
Simon Cooksey 0:fb7af294d5d9 388 }
Simon Cooksey 0:fb7af294d5d9 389
Simon Cooksey 0:fb7af294d5d9 390 if( f_rng != NULL )
Simon Cooksey 0:fb7af294d5d9 391 {
Simon Cooksey 0:fb7af294d5d9 392 /*
Simon Cooksey 0:fb7af294d5d9 393 * Blinding
Simon Cooksey 0:fb7af294d5d9 394 * T = T * Vi mod N
Simon Cooksey 0:fb7af294d5d9 395 */
Simon Cooksey 0:fb7af294d5d9 396 MBEDTLS_MPI_CHK( rsa_prepare_blinding( ctx, f_rng, p_rng ) );
Simon Cooksey 0:fb7af294d5d9 397 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vi ) );
Simon Cooksey 0:fb7af294d5d9 398 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Simon Cooksey 0:fb7af294d5d9 399 }
Simon Cooksey 0:fb7af294d5d9 400
Simon Cooksey 0:fb7af294d5d9 401 #if defined(MBEDTLS_RSA_NO_CRT)
Simon Cooksey 0:fb7af294d5d9 402 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T, &T, &ctx->D, &ctx->N, &ctx->RN ) );
Simon Cooksey 0:fb7af294d5d9 403 #else
Simon Cooksey 0:fb7af294d5d9 404 /*
Simon Cooksey 0:fb7af294d5d9 405 * faster decryption using the CRT
Simon Cooksey 0:fb7af294d5d9 406 *
Simon Cooksey 0:fb7af294d5d9 407 * T1 = input ^ dP mod P
Simon Cooksey 0:fb7af294d5d9 408 * T2 = input ^ dQ mod Q
Simon Cooksey 0:fb7af294d5d9 409 */
Simon Cooksey 0:fb7af294d5d9 410 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T1, &T, &ctx->DP, &ctx->P, &ctx->RP ) );
Simon Cooksey 0:fb7af294d5d9 411 MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &T2, &T, &ctx->DQ, &ctx->Q, &ctx->RQ ) );
Simon Cooksey 0:fb7af294d5d9 412
Simon Cooksey 0:fb7af294d5d9 413 /*
Simon Cooksey 0:fb7af294d5d9 414 * T = (T1 - T2) * (Q^-1 mod P) mod P
Simon Cooksey 0:fb7af294d5d9 415 */
Simon Cooksey 0:fb7af294d5d9 416 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( &T, &T1, &T2 ) );
Simon Cooksey 0:fb7af294d5d9 417 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->QP ) );
Simon Cooksey 0:fb7af294d5d9 418 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T1, &ctx->P ) );
Simon Cooksey 0:fb7af294d5d9 419
Simon Cooksey 0:fb7af294d5d9 420 /*
Simon Cooksey 0:fb7af294d5d9 421 * T = T2 + T * Q
Simon Cooksey 0:fb7af294d5d9 422 */
Simon Cooksey 0:fb7af294d5d9 423 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T1, &T, &ctx->Q ) );
Simon Cooksey 0:fb7af294d5d9 424 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &T, &T2, &T1 ) );
Simon Cooksey 0:fb7af294d5d9 425 #endif /* MBEDTLS_RSA_NO_CRT */
Simon Cooksey 0:fb7af294d5d9 426
Simon Cooksey 0:fb7af294d5d9 427 if( f_rng != NULL )
Simon Cooksey 0:fb7af294d5d9 428 {
Simon Cooksey 0:fb7af294d5d9 429 /*
Simon Cooksey 0:fb7af294d5d9 430 * Unblind
Simon Cooksey 0:fb7af294d5d9 431 * T = T * Vf mod N
Simon Cooksey 0:fb7af294d5d9 432 */
Simon Cooksey 0:fb7af294d5d9 433 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &T, &T, &ctx->Vf ) );
Simon Cooksey 0:fb7af294d5d9 434 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &T, &T, &ctx->N ) );
Simon Cooksey 0:fb7af294d5d9 435 }
Simon Cooksey 0:fb7af294d5d9 436
Simon Cooksey 0:fb7af294d5d9 437 olen = ctx->len;
Simon Cooksey 0:fb7af294d5d9 438 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &T, output, olen ) );
Simon Cooksey 0:fb7af294d5d9 439
Simon Cooksey 0:fb7af294d5d9 440 cleanup:
Simon Cooksey 0:fb7af294d5d9 441 #if defined(MBEDTLS_THREADING_C)
Simon Cooksey 0:fb7af294d5d9 442 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 )
Simon Cooksey 0:fb7af294d5d9 443 return( MBEDTLS_ERR_THREADING_MUTEX_ERROR );
Simon Cooksey 0:fb7af294d5d9 444 #endif
Simon Cooksey 0:fb7af294d5d9 445
Simon Cooksey 0:fb7af294d5d9 446 mbedtls_mpi_free( &T ); mbedtls_mpi_free( &T1 ); mbedtls_mpi_free( &T2 );
Simon Cooksey 0:fb7af294d5d9 447
Simon Cooksey 0:fb7af294d5d9 448 if( ret != 0 )
Simon Cooksey 0:fb7af294d5d9 449 return( MBEDTLS_ERR_RSA_PRIVATE_FAILED + ret );
Simon Cooksey 0:fb7af294d5d9 450
Simon Cooksey 0:fb7af294d5d9 451 return( 0 );
Simon Cooksey 0:fb7af294d5d9 452 }
Simon Cooksey 0:fb7af294d5d9 453
Simon Cooksey 0:fb7af294d5d9 454 #if defined(MBEDTLS_PKCS1_V21)
Simon Cooksey 0:fb7af294d5d9 455 /**
Simon Cooksey 0:fb7af294d5d9 456 * Generate and apply the MGF1 operation (from PKCS#1 v2.1) to a buffer.
Simon Cooksey 0:fb7af294d5d9 457 *
Simon Cooksey 0:fb7af294d5d9 458 * \param dst buffer to mask
Simon Cooksey 0:fb7af294d5d9 459 * \param dlen length of destination buffer
Simon Cooksey 0:fb7af294d5d9 460 * \param src source of the mask generation
Simon Cooksey 0:fb7af294d5d9 461 * \param slen length of the source buffer
Simon Cooksey 0:fb7af294d5d9 462 * \param md_ctx message digest context to use
Simon Cooksey 0:fb7af294d5d9 463 */
Simon Cooksey 0:fb7af294d5d9 464 static void mgf_mask( unsigned char *dst, size_t dlen, unsigned char *src,
Simon Cooksey 0:fb7af294d5d9 465 size_t slen, mbedtls_md_context_t *md_ctx )
Simon Cooksey 0:fb7af294d5d9 466 {
Simon Cooksey 0:fb7af294d5d9 467 unsigned char mask[MBEDTLS_MD_MAX_SIZE];
Simon Cooksey 0:fb7af294d5d9 468 unsigned char counter[4];
Simon Cooksey 0:fb7af294d5d9 469 unsigned char *p;
Simon Cooksey 0:fb7af294d5d9 470 unsigned int hlen;
Simon Cooksey 0:fb7af294d5d9 471 size_t i, use_len;
Simon Cooksey 0:fb7af294d5d9 472
Simon Cooksey 0:fb7af294d5d9 473 memset( mask, 0, MBEDTLS_MD_MAX_SIZE );
Simon Cooksey 0:fb7af294d5d9 474 memset( counter, 0, 4 );
Simon Cooksey 0:fb7af294d5d9 475
Simon Cooksey 0:fb7af294d5d9 476 hlen = mbedtls_md_get_size( md_ctx->md_info );
Simon Cooksey 0:fb7af294d5d9 477
Simon Cooksey 0:fb7af294d5d9 478 /* Generate and apply dbMask */
Simon Cooksey 0:fb7af294d5d9 479 p = dst;
Simon Cooksey 0:fb7af294d5d9 480
Simon Cooksey 0:fb7af294d5d9 481 while( dlen > 0 )
Simon Cooksey 0:fb7af294d5d9 482 {
Simon Cooksey 0:fb7af294d5d9 483 use_len = hlen;
Simon Cooksey 0:fb7af294d5d9 484 if( dlen < hlen )
Simon Cooksey 0:fb7af294d5d9 485 use_len = dlen;
Simon Cooksey 0:fb7af294d5d9 486
Simon Cooksey 0:fb7af294d5d9 487 mbedtls_md_starts( md_ctx );
Simon Cooksey 0:fb7af294d5d9 488 mbedtls_md_update( md_ctx, src, slen );
Simon Cooksey 0:fb7af294d5d9 489 mbedtls_md_update( md_ctx, counter, 4 );
Simon Cooksey 0:fb7af294d5d9 490 mbedtls_md_finish( md_ctx, mask );
Simon Cooksey 0:fb7af294d5d9 491
Simon Cooksey 0:fb7af294d5d9 492 for( i = 0; i < use_len; ++i )
Simon Cooksey 0:fb7af294d5d9 493 *p++ ^= mask[i];
Simon Cooksey 0:fb7af294d5d9 494
Simon Cooksey 0:fb7af294d5d9 495 counter[3]++;
Simon Cooksey 0:fb7af294d5d9 496
Simon Cooksey 0:fb7af294d5d9 497 dlen -= use_len;
Simon Cooksey 0:fb7af294d5d9 498 }
Simon Cooksey 0:fb7af294d5d9 499 }
Simon Cooksey 0:fb7af294d5d9 500 #endif /* MBEDTLS_PKCS1_V21 */
Simon Cooksey 0:fb7af294d5d9 501
Simon Cooksey 0:fb7af294d5d9 502 #if defined(MBEDTLS_PKCS1_V21)
Simon Cooksey 0:fb7af294d5d9 503 /*
Simon Cooksey 0:fb7af294d5d9 504 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-ENCRYPT function
Simon Cooksey 0:fb7af294d5d9 505 */
Simon Cooksey 0:fb7af294d5d9 506 int mbedtls_rsa_rsaes_oaep_encrypt( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 507 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 508 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 509 int mode,
Simon Cooksey 0:fb7af294d5d9 510 const unsigned char *label, size_t label_len,
Simon Cooksey 0:fb7af294d5d9 511 size_t ilen,
Simon Cooksey 0:fb7af294d5d9 512 const unsigned char *input,
Simon Cooksey 0:fb7af294d5d9 513 unsigned char *output )
Simon Cooksey 0:fb7af294d5d9 514 {
Simon Cooksey 0:fb7af294d5d9 515 size_t olen;
Simon Cooksey 0:fb7af294d5d9 516 int ret;
Simon Cooksey 0:fb7af294d5d9 517 unsigned char *p = output;
Simon Cooksey 0:fb7af294d5d9 518 unsigned int hlen;
Simon Cooksey 0:fb7af294d5d9 519 const mbedtls_md_info_t *md_info;
Simon Cooksey 0:fb7af294d5d9 520 mbedtls_md_context_t md_ctx;
Simon Cooksey 0:fb7af294d5d9 521
Simon Cooksey 0:fb7af294d5d9 522 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Simon Cooksey 0:fb7af294d5d9 523 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 524
Simon Cooksey 0:fb7af294d5d9 525 if( f_rng == NULL )
Simon Cooksey 0:fb7af294d5d9 526 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 527
Simon Cooksey 0:fb7af294d5d9 528 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Simon Cooksey 0:fb7af294d5d9 529 if( md_info == NULL )
Simon Cooksey 0:fb7af294d5d9 530 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 531
Simon Cooksey 0:fb7af294d5d9 532 olen = ctx->len;
Simon Cooksey 0:fb7af294d5d9 533 hlen = mbedtls_md_get_size( md_info );
Simon Cooksey 0:fb7af294d5d9 534
Simon Cooksey 0:fb7af294d5d9 535 /* first comparison checks for overflow */
Simon Cooksey 0:fb7af294d5d9 536 if( ilen + 2 * hlen + 2 < ilen || olen < ilen + 2 * hlen + 2 )
Simon Cooksey 0:fb7af294d5d9 537 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 538
Simon Cooksey 0:fb7af294d5d9 539 memset( output, 0, olen );
Simon Cooksey 0:fb7af294d5d9 540
Simon Cooksey 0:fb7af294d5d9 541 *p++ = 0;
Simon Cooksey 0:fb7af294d5d9 542
Simon Cooksey 0:fb7af294d5d9 543 /* Generate a random octet string seed */
Simon Cooksey 0:fb7af294d5d9 544 if( ( ret = f_rng( p_rng, p, hlen ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 545 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Simon Cooksey 0:fb7af294d5d9 546
Simon Cooksey 0:fb7af294d5d9 547 p += hlen;
Simon Cooksey 0:fb7af294d5d9 548
Simon Cooksey 0:fb7af294d5d9 549 /* Construct DB */
Simon Cooksey 0:fb7af294d5d9 550 mbedtls_md( md_info, label, label_len, p );
Simon Cooksey 0:fb7af294d5d9 551 p += hlen;
Simon Cooksey 0:fb7af294d5d9 552 p += olen - 2 * hlen - 2 - ilen;
Simon Cooksey 0:fb7af294d5d9 553 *p++ = 1;
Simon Cooksey 0:fb7af294d5d9 554 memcpy( p, input, ilen );
Simon Cooksey 0:fb7af294d5d9 555
Simon Cooksey 0:fb7af294d5d9 556 mbedtls_md_init( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 557 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 558 {
Simon Cooksey 0:fb7af294d5d9 559 mbedtls_md_free( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 560 return( ret );
Simon Cooksey 0:fb7af294d5d9 561 }
Simon Cooksey 0:fb7af294d5d9 562
Simon Cooksey 0:fb7af294d5d9 563 /* maskedDB: Apply dbMask to DB */
Simon Cooksey 0:fb7af294d5d9 564 mgf_mask( output + hlen + 1, olen - hlen - 1, output + 1, hlen,
Simon Cooksey 0:fb7af294d5d9 565 &md_ctx );
Simon Cooksey 0:fb7af294d5d9 566
Simon Cooksey 0:fb7af294d5d9 567 /* maskedSeed: Apply seedMask to seed */
Simon Cooksey 0:fb7af294d5d9 568 mgf_mask( output + 1, hlen, output + hlen + 1, olen - hlen - 1,
Simon Cooksey 0:fb7af294d5d9 569 &md_ctx );
Simon Cooksey 0:fb7af294d5d9 570
Simon Cooksey 0:fb7af294d5d9 571 mbedtls_md_free( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 572
Simon Cooksey 0:fb7af294d5d9 573 return( ( mode == MBEDTLS_RSA_PUBLIC )
Simon Cooksey 0:fb7af294d5d9 574 ? mbedtls_rsa_public( ctx, output, output )
Simon Cooksey 0:fb7af294d5d9 575 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Simon Cooksey 0:fb7af294d5d9 576 }
Simon Cooksey 0:fb7af294d5d9 577 #endif /* MBEDTLS_PKCS1_V21 */
Simon Cooksey 0:fb7af294d5d9 578
Simon Cooksey 0:fb7af294d5d9 579 #if defined(MBEDTLS_PKCS1_V15)
Simon Cooksey 0:fb7af294d5d9 580 /*
Simon Cooksey 0:fb7af294d5d9 581 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-ENCRYPT function
Simon Cooksey 0:fb7af294d5d9 582 */
Simon Cooksey 0:fb7af294d5d9 583 int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 584 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 585 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 586 int mode, size_t ilen,
Simon Cooksey 0:fb7af294d5d9 587 const unsigned char *input,
Simon Cooksey 0:fb7af294d5d9 588 unsigned char *output )
Simon Cooksey 0:fb7af294d5d9 589 {
Simon Cooksey 0:fb7af294d5d9 590 size_t nb_pad, olen;
Simon Cooksey 0:fb7af294d5d9 591 int ret;
Simon Cooksey 0:fb7af294d5d9 592 unsigned char *p = output;
Simon Cooksey 0:fb7af294d5d9 593
Simon Cooksey 0:fb7af294d5d9 594 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Simon Cooksey 0:fb7af294d5d9 595 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 596
Simon Cooksey 0:fb7af294d5d9 597 // We don't check p_rng because it won't be dereferenced here
Simon Cooksey 0:fb7af294d5d9 598 if( f_rng == NULL || input == NULL || output == NULL )
Simon Cooksey 0:fb7af294d5d9 599 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 600
Simon Cooksey 0:fb7af294d5d9 601 olen = ctx->len;
Simon Cooksey 0:fb7af294d5d9 602
Simon Cooksey 0:fb7af294d5d9 603 /* first comparison checks for overflow */
Simon Cooksey 0:fb7af294d5d9 604 if( ilen + 11 < ilen || olen < ilen + 11 )
Simon Cooksey 0:fb7af294d5d9 605 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 606
Simon Cooksey 0:fb7af294d5d9 607 nb_pad = olen - 3 - ilen;
Simon Cooksey 0:fb7af294d5d9 608
Simon Cooksey 0:fb7af294d5d9 609 *p++ = 0;
Simon Cooksey 0:fb7af294d5d9 610 if( mode == MBEDTLS_RSA_PUBLIC )
Simon Cooksey 0:fb7af294d5d9 611 {
Simon Cooksey 0:fb7af294d5d9 612 *p++ = MBEDTLS_RSA_CRYPT;
Simon Cooksey 0:fb7af294d5d9 613
Simon Cooksey 0:fb7af294d5d9 614 while( nb_pad-- > 0 )
Simon Cooksey 0:fb7af294d5d9 615 {
Simon Cooksey 0:fb7af294d5d9 616 int rng_dl = 100;
Simon Cooksey 0:fb7af294d5d9 617
Simon Cooksey 0:fb7af294d5d9 618 do {
Simon Cooksey 0:fb7af294d5d9 619 ret = f_rng( p_rng, p, 1 );
Simon Cooksey 0:fb7af294d5d9 620 } while( *p == 0 && --rng_dl && ret == 0 );
Simon Cooksey 0:fb7af294d5d9 621
Simon Cooksey 0:fb7af294d5d9 622 /* Check if RNG failed to generate data */
Simon Cooksey 0:fb7af294d5d9 623 if( rng_dl == 0 || ret != 0 )
Simon Cooksey 0:fb7af294d5d9 624 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Simon Cooksey 0:fb7af294d5d9 625
Simon Cooksey 0:fb7af294d5d9 626 p++;
Simon Cooksey 0:fb7af294d5d9 627 }
Simon Cooksey 0:fb7af294d5d9 628 }
Simon Cooksey 0:fb7af294d5d9 629 else
Simon Cooksey 0:fb7af294d5d9 630 {
Simon Cooksey 0:fb7af294d5d9 631 *p++ = MBEDTLS_RSA_SIGN;
Simon Cooksey 0:fb7af294d5d9 632
Simon Cooksey 0:fb7af294d5d9 633 while( nb_pad-- > 0 )
Simon Cooksey 0:fb7af294d5d9 634 *p++ = 0xFF;
Simon Cooksey 0:fb7af294d5d9 635 }
Simon Cooksey 0:fb7af294d5d9 636
Simon Cooksey 0:fb7af294d5d9 637 *p++ = 0;
Simon Cooksey 0:fb7af294d5d9 638 memcpy( p, input, ilen );
Simon Cooksey 0:fb7af294d5d9 639
Simon Cooksey 0:fb7af294d5d9 640 return( ( mode == MBEDTLS_RSA_PUBLIC )
Simon Cooksey 0:fb7af294d5d9 641 ? mbedtls_rsa_public( ctx, output, output )
Simon Cooksey 0:fb7af294d5d9 642 : mbedtls_rsa_private( ctx, f_rng, p_rng, output, output ) );
Simon Cooksey 0:fb7af294d5d9 643 }
Simon Cooksey 0:fb7af294d5d9 644 #endif /* MBEDTLS_PKCS1_V15 */
Simon Cooksey 0:fb7af294d5d9 645
Simon Cooksey 0:fb7af294d5d9 646 /*
Simon Cooksey 0:fb7af294d5d9 647 * Add the message padding, then do an RSA operation
Simon Cooksey 0:fb7af294d5d9 648 */
Simon Cooksey 0:fb7af294d5d9 649 int mbedtls_rsa_pkcs1_encrypt( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 650 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 651 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 652 int mode, size_t ilen,
Simon Cooksey 0:fb7af294d5d9 653 const unsigned char *input,
Simon Cooksey 0:fb7af294d5d9 654 unsigned char *output )
Simon Cooksey 0:fb7af294d5d9 655 {
Simon Cooksey 0:fb7af294d5d9 656 switch( ctx->padding )
Simon Cooksey 0:fb7af294d5d9 657 {
Simon Cooksey 0:fb7af294d5d9 658 #if defined(MBEDTLS_PKCS1_V15)
Simon Cooksey 0:fb7af294d5d9 659 case MBEDTLS_RSA_PKCS_V15:
Simon Cooksey 0:fb7af294d5d9 660 return mbedtls_rsa_rsaes_pkcs1_v15_encrypt( ctx, f_rng, p_rng, mode, ilen,
Simon Cooksey 0:fb7af294d5d9 661 input, output );
Simon Cooksey 0:fb7af294d5d9 662 #endif
Simon Cooksey 0:fb7af294d5d9 663
Simon Cooksey 0:fb7af294d5d9 664 #if defined(MBEDTLS_PKCS1_V21)
Simon Cooksey 0:fb7af294d5d9 665 case MBEDTLS_RSA_PKCS_V21:
Simon Cooksey 0:fb7af294d5d9 666 return mbedtls_rsa_rsaes_oaep_encrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Simon Cooksey 0:fb7af294d5d9 667 ilen, input, output );
Simon Cooksey 0:fb7af294d5d9 668 #endif
Simon Cooksey 0:fb7af294d5d9 669
Simon Cooksey 0:fb7af294d5d9 670 default:
Simon Cooksey 0:fb7af294d5d9 671 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Simon Cooksey 0:fb7af294d5d9 672 }
Simon Cooksey 0:fb7af294d5d9 673 }
Simon Cooksey 0:fb7af294d5d9 674
Simon Cooksey 0:fb7af294d5d9 675 #if defined(MBEDTLS_PKCS1_V21)
Simon Cooksey 0:fb7af294d5d9 676 /*
Simon Cooksey 0:fb7af294d5d9 677 * Implementation of the PKCS#1 v2.1 RSAES-OAEP-DECRYPT function
Simon Cooksey 0:fb7af294d5d9 678 */
Simon Cooksey 0:fb7af294d5d9 679 int mbedtls_rsa_rsaes_oaep_decrypt( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 680 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 681 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 682 int mode,
Simon Cooksey 0:fb7af294d5d9 683 const unsigned char *label, size_t label_len,
Simon Cooksey 0:fb7af294d5d9 684 size_t *olen,
Simon Cooksey 0:fb7af294d5d9 685 const unsigned char *input,
Simon Cooksey 0:fb7af294d5d9 686 unsigned char *output,
Simon Cooksey 0:fb7af294d5d9 687 size_t output_max_len )
Simon Cooksey 0:fb7af294d5d9 688 {
Simon Cooksey 0:fb7af294d5d9 689 int ret;
Simon Cooksey 0:fb7af294d5d9 690 size_t ilen, i, pad_len;
Simon Cooksey 0:fb7af294d5d9 691 unsigned char *p, bad, pad_done;
Simon Cooksey 0:fb7af294d5d9 692 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Simon Cooksey 0:fb7af294d5d9 693 unsigned char lhash[MBEDTLS_MD_MAX_SIZE];
Simon Cooksey 0:fb7af294d5d9 694 unsigned int hlen;
Simon Cooksey 0:fb7af294d5d9 695 const mbedtls_md_info_t *md_info;
Simon Cooksey 0:fb7af294d5d9 696 mbedtls_md_context_t md_ctx;
Simon Cooksey 0:fb7af294d5d9 697
Simon Cooksey 0:fb7af294d5d9 698 /*
Simon Cooksey 0:fb7af294d5d9 699 * Parameters sanity checks
Simon Cooksey 0:fb7af294d5d9 700 */
Simon Cooksey 0:fb7af294d5d9 701 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Simon Cooksey 0:fb7af294d5d9 702 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 703
Simon Cooksey 0:fb7af294d5d9 704 ilen = ctx->len;
Simon Cooksey 0:fb7af294d5d9 705
Simon Cooksey 0:fb7af294d5d9 706 if( ilen < 16 || ilen > sizeof( buf ) )
Simon Cooksey 0:fb7af294d5d9 707 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 708
Simon Cooksey 0:fb7af294d5d9 709 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Simon Cooksey 0:fb7af294d5d9 710 if( md_info == NULL )
Simon Cooksey 0:fb7af294d5d9 711 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 712
Simon Cooksey 0:fb7af294d5d9 713 hlen = mbedtls_md_get_size( md_info );
Simon Cooksey 0:fb7af294d5d9 714
Simon Cooksey 0:fb7af294d5d9 715 // checking for integer underflow
Simon Cooksey 0:fb7af294d5d9 716 if( 2 * hlen + 2 > ilen )
Simon Cooksey 0:fb7af294d5d9 717 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 718
Simon Cooksey 0:fb7af294d5d9 719 /*
Simon Cooksey 0:fb7af294d5d9 720 * RSA operation
Simon Cooksey 0:fb7af294d5d9 721 */
Simon Cooksey 0:fb7af294d5d9 722 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Simon Cooksey 0:fb7af294d5d9 723 ? mbedtls_rsa_public( ctx, input, buf )
Simon Cooksey 0:fb7af294d5d9 724 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Simon Cooksey 0:fb7af294d5d9 725
Simon Cooksey 0:fb7af294d5d9 726 if( ret != 0 )
Simon Cooksey 0:fb7af294d5d9 727 return( ret );
Simon Cooksey 0:fb7af294d5d9 728
Simon Cooksey 0:fb7af294d5d9 729 /*
Simon Cooksey 0:fb7af294d5d9 730 * Unmask data and generate lHash
Simon Cooksey 0:fb7af294d5d9 731 */
Simon Cooksey 0:fb7af294d5d9 732 mbedtls_md_init( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 733 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 734 {
Simon Cooksey 0:fb7af294d5d9 735 mbedtls_md_free( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 736 return( ret );
Simon Cooksey 0:fb7af294d5d9 737 }
Simon Cooksey 0:fb7af294d5d9 738
Simon Cooksey 0:fb7af294d5d9 739
Simon Cooksey 0:fb7af294d5d9 740 /* Generate lHash */
Simon Cooksey 0:fb7af294d5d9 741 mbedtls_md( md_info, label, label_len, lhash );
Simon Cooksey 0:fb7af294d5d9 742
Simon Cooksey 0:fb7af294d5d9 743 /* seed: Apply seedMask to maskedSeed */
Simon Cooksey 0:fb7af294d5d9 744 mgf_mask( buf + 1, hlen, buf + hlen + 1, ilen - hlen - 1,
Simon Cooksey 0:fb7af294d5d9 745 &md_ctx );
Simon Cooksey 0:fb7af294d5d9 746
Simon Cooksey 0:fb7af294d5d9 747 /* DB: Apply dbMask to maskedDB */
Simon Cooksey 0:fb7af294d5d9 748 mgf_mask( buf + hlen + 1, ilen - hlen - 1, buf + 1, hlen,
Simon Cooksey 0:fb7af294d5d9 749 &md_ctx );
Simon Cooksey 0:fb7af294d5d9 750
Simon Cooksey 0:fb7af294d5d9 751 mbedtls_md_free( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 752
Simon Cooksey 0:fb7af294d5d9 753 /*
Simon Cooksey 0:fb7af294d5d9 754 * Check contents, in "constant-time"
Simon Cooksey 0:fb7af294d5d9 755 */
Simon Cooksey 0:fb7af294d5d9 756 p = buf;
Simon Cooksey 0:fb7af294d5d9 757 bad = 0;
Simon Cooksey 0:fb7af294d5d9 758
Simon Cooksey 0:fb7af294d5d9 759 bad |= *p++; /* First byte must be 0 */
Simon Cooksey 0:fb7af294d5d9 760
Simon Cooksey 0:fb7af294d5d9 761 p += hlen; /* Skip seed */
Simon Cooksey 0:fb7af294d5d9 762
Simon Cooksey 0:fb7af294d5d9 763 /* Check lHash */
Simon Cooksey 0:fb7af294d5d9 764 for( i = 0; i < hlen; i++ )
Simon Cooksey 0:fb7af294d5d9 765 bad |= lhash[i] ^ *p++;
Simon Cooksey 0:fb7af294d5d9 766
Simon Cooksey 0:fb7af294d5d9 767 /* Get zero-padding len, but always read till end of buffer
Simon Cooksey 0:fb7af294d5d9 768 * (minus one, for the 01 byte) */
Simon Cooksey 0:fb7af294d5d9 769 pad_len = 0;
Simon Cooksey 0:fb7af294d5d9 770 pad_done = 0;
Simon Cooksey 0:fb7af294d5d9 771 for( i = 0; i < ilen - 2 * hlen - 2; i++ )
Simon Cooksey 0:fb7af294d5d9 772 {
Simon Cooksey 0:fb7af294d5d9 773 pad_done |= p[i];
Simon Cooksey 0:fb7af294d5d9 774 pad_len += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Simon Cooksey 0:fb7af294d5d9 775 }
Simon Cooksey 0:fb7af294d5d9 776
Simon Cooksey 0:fb7af294d5d9 777 p += pad_len;
Simon Cooksey 0:fb7af294d5d9 778 bad |= *p++ ^ 0x01;
Simon Cooksey 0:fb7af294d5d9 779
Simon Cooksey 0:fb7af294d5d9 780 /*
Simon Cooksey 0:fb7af294d5d9 781 * The only information "leaked" is whether the padding was correct or not
Simon Cooksey 0:fb7af294d5d9 782 * (eg, no data is copied if it was not correct). This meets the
Simon Cooksey 0:fb7af294d5d9 783 * recommendations in PKCS#1 v2.2: an opponent cannot distinguish between
Simon Cooksey 0:fb7af294d5d9 784 * the different error conditions.
Simon Cooksey 0:fb7af294d5d9 785 */
Simon Cooksey 0:fb7af294d5d9 786 if( bad != 0 )
Simon Cooksey 0:fb7af294d5d9 787 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Simon Cooksey 0:fb7af294d5d9 788
Simon Cooksey 0:fb7af294d5d9 789 if( ilen - ( p - buf ) > output_max_len )
Simon Cooksey 0:fb7af294d5d9 790 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Simon Cooksey 0:fb7af294d5d9 791
Simon Cooksey 0:fb7af294d5d9 792 *olen = ilen - (p - buf);
Simon Cooksey 0:fb7af294d5d9 793 memcpy( output, p, *olen );
Simon Cooksey 0:fb7af294d5d9 794
Simon Cooksey 0:fb7af294d5d9 795 return( 0 );
Simon Cooksey 0:fb7af294d5d9 796 }
Simon Cooksey 0:fb7af294d5d9 797 #endif /* MBEDTLS_PKCS1_V21 */
Simon Cooksey 0:fb7af294d5d9 798
Simon Cooksey 0:fb7af294d5d9 799 #if defined(MBEDTLS_PKCS1_V15)
Simon Cooksey 0:fb7af294d5d9 800 /*
Simon Cooksey 0:fb7af294d5d9 801 * Implementation of the PKCS#1 v2.1 RSAES-PKCS1-V1_5-DECRYPT function
Simon Cooksey 0:fb7af294d5d9 802 */
Simon Cooksey 0:fb7af294d5d9 803 int mbedtls_rsa_rsaes_pkcs1_v15_decrypt( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 804 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 805 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 806 int mode, size_t *olen,
Simon Cooksey 0:fb7af294d5d9 807 const unsigned char *input,
Simon Cooksey 0:fb7af294d5d9 808 unsigned char *output,
Simon Cooksey 0:fb7af294d5d9 809 size_t output_max_len)
Simon Cooksey 0:fb7af294d5d9 810 {
Simon Cooksey 0:fb7af294d5d9 811 int ret;
Simon Cooksey 0:fb7af294d5d9 812 size_t ilen, pad_count = 0, i;
Simon Cooksey 0:fb7af294d5d9 813 unsigned char *p, bad, pad_done = 0;
Simon Cooksey 0:fb7af294d5d9 814 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Simon Cooksey 0:fb7af294d5d9 815
Simon Cooksey 0:fb7af294d5d9 816 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Simon Cooksey 0:fb7af294d5d9 817 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 818
Simon Cooksey 0:fb7af294d5d9 819 ilen = ctx->len;
Simon Cooksey 0:fb7af294d5d9 820
Simon Cooksey 0:fb7af294d5d9 821 if( ilen < 16 || ilen > sizeof( buf ) )
Simon Cooksey 0:fb7af294d5d9 822 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 823
Simon Cooksey 0:fb7af294d5d9 824 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Simon Cooksey 0:fb7af294d5d9 825 ? mbedtls_rsa_public( ctx, input, buf )
Simon Cooksey 0:fb7af294d5d9 826 : mbedtls_rsa_private( ctx, f_rng, p_rng, input, buf );
Simon Cooksey 0:fb7af294d5d9 827
Simon Cooksey 0:fb7af294d5d9 828 if( ret != 0 )
Simon Cooksey 0:fb7af294d5d9 829 return( ret );
Simon Cooksey 0:fb7af294d5d9 830
Simon Cooksey 0:fb7af294d5d9 831 p = buf;
Simon Cooksey 0:fb7af294d5d9 832 bad = 0;
Simon Cooksey 0:fb7af294d5d9 833
Simon Cooksey 0:fb7af294d5d9 834 /*
Simon Cooksey 0:fb7af294d5d9 835 * Check and get padding len in "constant-time"
Simon Cooksey 0:fb7af294d5d9 836 */
Simon Cooksey 0:fb7af294d5d9 837 bad |= *p++; /* First byte must be 0 */
Simon Cooksey 0:fb7af294d5d9 838
Simon Cooksey 0:fb7af294d5d9 839 /* This test does not depend on secret data */
Simon Cooksey 0:fb7af294d5d9 840 if( mode == MBEDTLS_RSA_PRIVATE )
Simon Cooksey 0:fb7af294d5d9 841 {
Simon Cooksey 0:fb7af294d5d9 842 bad |= *p++ ^ MBEDTLS_RSA_CRYPT;
Simon Cooksey 0:fb7af294d5d9 843
Simon Cooksey 0:fb7af294d5d9 844 /* Get padding len, but always read till end of buffer
Simon Cooksey 0:fb7af294d5d9 845 * (minus one, for the 00 byte) */
Simon Cooksey 0:fb7af294d5d9 846 for( i = 0; i < ilen - 3; i++ )
Simon Cooksey 0:fb7af294d5d9 847 {
Simon Cooksey 0:fb7af294d5d9 848 pad_done |= ((p[i] | (unsigned char)-p[i]) >> 7) ^ 1;
Simon Cooksey 0:fb7af294d5d9 849 pad_count += ((pad_done | (unsigned char)-pad_done) >> 7) ^ 1;
Simon Cooksey 0:fb7af294d5d9 850 }
Simon Cooksey 0:fb7af294d5d9 851
Simon Cooksey 0:fb7af294d5d9 852 p += pad_count;
Simon Cooksey 0:fb7af294d5d9 853 bad |= *p++; /* Must be zero */
Simon Cooksey 0:fb7af294d5d9 854 }
Simon Cooksey 0:fb7af294d5d9 855 else
Simon Cooksey 0:fb7af294d5d9 856 {
Simon Cooksey 0:fb7af294d5d9 857 bad |= *p++ ^ MBEDTLS_RSA_SIGN;
Simon Cooksey 0:fb7af294d5d9 858
Simon Cooksey 0:fb7af294d5d9 859 /* Get padding len, but always read till end of buffer
Simon Cooksey 0:fb7af294d5d9 860 * (minus one, for the 00 byte) */
Simon Cooksey 0:fb7af294d5d9 861 for( i = 0; i < ilen - 3; i++ )
Simon Cooksey 0:fb7af294d5d9 862 {
Simon Cooksey 0:fb7af294d5d9 863 pad_done |= ( p[i] != 0xFF );
Simon Cooksey 0:fb7af294d5d9 864 pad_count += ( pad_done == 0 );
Simon Cooksey 0:fb7af294d5d9 865 }
Simon Cooksey 0:fb7af294d5d9 866
Simon Cooksey 0:fb7af294d5d9 867 p += pad_count;
Simon Cooksey 0:fb7af294d5d9 868 bad |= *p++; /* Must be zero */
Simon Cooksey 0:fb7af294d5d9 869 }
Simon Cooksey 0:fb7af294d5d9 870
Simon Cooksey 0:fb7af294d5d9 871 bad |= ( pad_count < 8 );
Simon Cooksey 0:fb7af294d5d9 872
Simon Cooksey 0:fb7af294d5d9 873 if( bad )
Simon Cooksey 0:fb7af294d5d9 874 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Simon Cooksey 0:fb7af294d5d9 875
Simon Cooksey 0:fb7af294d5d9 876 if( ilen - ( p - buf ) > output_max_len )
Simon Cooksey 0:fb7af294d5d9 877 return( MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE );
Simon Cooksey 0:fb7af294d5d9 878
Simon Cooksey 0:fb7af294d5d9 879 *olen = ilen - (p - buf);
Simon Cooksey 0:fb7af294d5d9 880 memcpy( output, p, *olen );
Simon Cooksey 0:fb7af294d5d9 881
Simon Cooksey 0:fb7af294d5d9 882 return( 0 );
Simon Cooksey 0:fb7af294d5d9 883 }
Simon Cooksey 0:fb7af294d5d9 884 #endif /* MBEDTLS_PKCS1_V15 */
Simon Cooksey 0:fb7af294d5d9 885
Simon Cooksey 0:fb7af294d5d9 886 /*
Simon Cooksey 0:fb7af294d5d9 887 * Do an RSA operation, then remove the message padding
Simon Cooksey 0:fb7af294d5d9 888 */
Simon Cooksey 0:fb7af294d5d9 889 int mbedtls_rsa_pkcs1_decrypt( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 890 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 891 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 892 int mode, size_t *olen,
Simon Cooksey 0:fb7af294d5d9 893 const unsigned char *input,
Simon Cooksey 0:fb7af294d5d9 894 unsigned char *output,
Simon Cooksey 0:fb7af294d5d9 895 size_t output_max_len)
Simon Cooksey 0:fb7af294d5d9 896 {
Simon Cooksey 0:fb7af294d5d9 897 switch( ctx->padding )
Simon Cooksey 0:fb7af294d5d9 898 {
Simon Cooksey 0:fb7af294d5d9 899 #if defined(MBEDTLS_PKCS1_V15)
Simon Cooksey 0:fb7af294d5d9 900 case MBEDTLS_RSA_PKCS_V15:
Simon Cooksey 0:fb7af294d5d9 901 return mbedtls_rsa_rsaes_pkcs1_v15_decrypt( ctx, f_rng, p_rng, mode, olen,
Simon Cooksey 0:fb7af294d5d9 902 input, output, output_max_len );
Simon Cooksey 0:fb7af294d5d9 903 #endif
Simon Cooksey 0:fb7af294d5d9 904
Simon Cooksey 0:fb7af294d5d9 905 #if defined(MBEDTLS_PKCS1_V21)
Simon Cooksey 0:fb7af294d5d9 906 case MBEDTLS_RSA_PKCS_V21:
Simon Cooksey 0:fb7af294d5d9 907 return mbedtls_rsa_rsaes_oaep_decrypt( ctx, f_rng, p_rng, mode, NULL, 0,
Simon Cooksey 0:fb7af294d5d9 908 olen, input, output,
Simon Cooksey 0:fb7af294d5d9 909 output_max_len );
Simon Cooksey 0:fb7af294d5d9 910 #endif
Simon Cooksey 0:fb7af294d5d9 911
Simon Cooksey 0:fb7af294d5d9 912 default:
Simon Cooksey 0:fb7af294d5d9 913 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Simon Cooksey 0:fb7af294d5d9 914 }
Simon Cooksey 0:fb7af294d5d9 915 }
Simon Cooksey 0:fb7af294d5d9 916
Simon Cooksey 0:fb7af294d5d9 917 #if defined(MBEDTLS_PKCS1_V21)
Simon Cooksey 0:fb7af294d5d9 918 /*
Simon Cooksey 0:fb7af294d5d9 919 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-SIGN function
Simon Cooksey 0:fb7af294d5d9 920 */
Simon Cooksey 0:fb7af294d5d9 921 int mbedtls_rsa_rsassa_pss_sign( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 922 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 923 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 924 int mode,
Simon Cooksey 0:fb7af294d5d9 925 mbedtls_md_type_t md_alg,
Simon Cooksey 0:fb7af294d5d9 926 unsigned int hashlen,
Simon Cooksey 0:fb7af294d5d9 927 const unsigned char *hash,
Simon Cooksey 0:fb7af294d5d9 928 unsigned char *sig )
Simon Cooksey 0:fb7af294d5d9 929 {
Simon Cooksey 0:fb7af294d5d9 930 size_t olen;
Simon Cooksey 0:fb7af294d5d9 931 unsigned char *p = sig;
Simon Cooksey 0:fb7af294d5d9 932 unsigned char salt[MBEDTLS_MD_MAX_SIZE];
Simon Cooksey 0:fb7af294d5d9 933 unsigned int slen, hlen, offset = 0;
Simon Cooksey 0:fb7af294d5d9 934 int ret;
Simon Cooksey 0:fb7af294d5d9 935 size_t msb;
Simon Cooksey 0:fb7af294d5d9 936 const mbedtls_md_info_t *md_info;
Simon Cooksey 0:fb7af294d5d9 937 mbedtls_md_context_t md_ctx;
Simon Cooksey 0:fb7af294d5d9 938
Simon Cooksey 0:fb7af294d5d9 939 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Simon Cooksey 0:fb7af294d5d9 940 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 941
Simon Cooksey 0:fb7af294d5d9 942 if( f_rng == NULL )
Simon Cooksey 0:fb7af294d5d9 943 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 944
Simon Cooksey 0:fb7af294d5d9 945 olen = ctx->len;
Simon Cooksey 0:fb7af294d5d9 946
Simon Cooksey 0:fb7af294d5d9 947 if( md_alg != MBEDTLS_MD_NONE )
Simon Cooksey 0:fb7af294d5d9 948 {
Simon Cooksey 0:fb7af294d5d9 949 /* Gather length of hash to sign */
Simon Cooksey 0:fb7af294d5d9 950 md_info = mbedtls_md_info_from_type( md_alg );
Simon Cooksey 0:fb7af294d5d9 951 if( md_info == NULL )
Simon Cooksey 0:fb7af294d5d9 952 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 953
Simon Cooksey 0:fb7af294d5d9 954 hashlen = mbedtls_md_get_size( md_info );
Simon Cooksey 0:fb7af294d5d9 955 }
Simon Cooksey 0:fb7af294d5d9 956
Simon Cooksey 0:fb7af294d5d9 957 md_info = mbedtls_md_info_from_type( (mbedtls_md_type_t) ctx->hash_id );
Simon Cooksey 0:fb7af294d5d9 958 if( md_info == NULL )
Simon Cooksey 0:fb7af294d5d9 959 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 960
Simon Cooksey 0:fb7af294d5d9 961 hlen = mbedtls_md_get_size( md_info );
Simon Cooksey 0:fb7af294d5d9 962 slen = hlen;
Simon Cooksey 0:fb7af294d5d9 963
Simon Cooksey 0:fb7af294d5d9 964 if( olen < hlen + slen + 2 )
Simon Cooksey 0:fb7af294d5d9 965 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 966
Simon Cooksey 0:fb7af294d5d9 967 memset( sig, 0, olen );
Simon Cooksey 0:fb7af294d5d9 968
Simon Cooksey 0:fb7af294d5d9 969 /* Generate salt of length slen */
Simon Cooksey 0:fb7af294d5d9 970 if( ( ret = f_rng( p_rng, salt, slen ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 971 return( MBEDTLS_ERR_RSA_RNG_FAILED + ret );
Simon Cooksey 0:fb7af294d5d9 972
Simon Cooksey 0:fb7af294d5d9 973 /* Note: EMSA-PSS encoding is over the length of N - 1 bits */
Simon Cooksey 0:fb7af294d5d9 974 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Simon Cooksey 0:fb7af294d5d9 975 p += olen - hlen * 2 - 2;
Simon Cooksey 0:fb7af294d5d9 976 *p++ = 0x01;
Simon Cooksey 0:fb7af294d5d9 977 memcpy( p, salt, slen );
Simon Cooksey 0:fb7af294d5d9 978 p += slen;
Simon Cooksey 0:fb7af294d5d9 979
Simon Cooksey 0:fb7af294d5d9 980 mbedtls_md_init( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 981 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 982 {
Simon Cooksey 0:fb7af294d5d9 983 mbedtls_md_free( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 984 return( ret );
Simon Cooksey 0:fb7af294d5d9 985 }
Simon Cooksey 0:fb7af294d5d9 986
Simon Cooksey 0:fb7af294d5d9 987 /* Generate H = Hash( M' ) */
Simon Cooksey 0:fb7af294d5d9 988 mbedtls_md_starts( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 989 mbedtls_md_update( &md_ctx, p, 8 );
Simon Cooksey 0:fb7af294d5d9 990 mbedtls_md_update( &md_ctx, hash, hashlen );
Simon Cooksey 0:fb7af294d5d9 991 mbedtls_md_update( &md_ctx, salt, slen );
Simon Cooksey 0:fb7af294d5d9 992 mbedtls_md_finish( &md_ctx, p );
Simon Cooksey 0:fb7af294d5d9 993
Simon Cooksey 0:fb7af294d5d9 994 /* Compensate for boundary condition when applying mask */
Simon Cooksey 0:fb7af294d5d9 995 if( msb % 8 == 0 )
Simon Cooksey 0:fb7af294d5d9 996 offset = 1;
Simon Cooksey 0:fb7af294d5d9 997
Simon Cooksey 0:fb7af294d5d9 998 /* maskedDB: Apply dbMask to DB */
Simon Cooksey 0:fb7af294d5d9 999 mgf_mask( sig + offset, olen - hlen - 1 - offset, p, hlen, &md_ctx );
Simon Cooksey 0:fb7af294d5d9 1000
Simon Cooksey 0:fb7af294d5d9 1001 mbedtls_md_free( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 1002
Simon Cooksey 0:fb7af294d5d9 1003 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Simon Cooksey 0:fb7af294d5d9 1004 sig[0] &= 0xFF >> ( olen * 8 - msb );
Simon Cooksey 0:fb7af294d5d9 1005
Simon Cooksey 0:fb7af294d5d9 1006 p += hlen;
Simon Cooksey 0:fb7af294d5d9 1007 *p++ = 0xBC;
Simon Cooksey 0:fb7af294d5d9 1008
Simon Cooksey 0:fb7af294d5d9 1009 return( ( mode == MBEDTLS_RSA_PUBLIC )
Simon Cooksey 0:fb7af294d5d9 1010 ? mbedtls_rsa_public( ctx, sig, sig )
Simon Cooksey 0:fb7af294d5d9 1011 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig ) );
Simon Cooksey 0:fb7af294d5d9 1012 }
Simon Cooksey 0:fb7af294d5d9 1013 #endif /* MBEDTLS_PKCS1_V21 */
Simon Cooksey 0:fb7af294d5d9 1014
Simon Cooksey 0:fb7af294d5d9 1015 #if defined(MBEDTLS_PKCS1_V15)
Simon Cooksey 0:fb7af294d5d9 1016 /*
Simon Cooksey 0:fb7af294d5d9 1017 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-V1_5-SIGN function
Simon Cooksey 0:fb7af294d5d9 1018 */
Simon Cooksey 0:fb7af294d5d9 1019 /*
Simon Cooksey 0:fb7af294d5d9 1020 * Do an RSA operation to sign the message digest
Simon Cooksey 0:fb7af294d5d9 1021 */
Simon Cooksey 0:fb7af294d5d9 1022 int mbedtls_rsa_rsassa_pkcs1_v15_sign( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 1023 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 1024 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 1025 int mode,
Simon Cooksey 0:fb7af294d5d9 1026 mbedtls_md_type_t md_alg,
Simon Cooksey 0:fb7af294d5d9 1027 unsigned int hashlen,
Simon Cooksey 0:fb7af294d5d9 1028 const unsigned char *hash,
Simon Cooksey 0:fb7af294d5d9 1029 unsigned char *sig )
Simon Cooksey 0:fb7af294d5d9 1030 {
Simon Cooksey 0:fb7af294d5d9 1031 size_t nb_pad, olen, oid_size = 0;
Simon Cooksey 0:fb7af294d5d9 1032 unsigned char *p = sig;
Simon Cooksey 0:fb7af294d5d9 1033 const char *oid = NULL;
Simon Cooksey 0:fb7af294d5d9 1034 unsigned char *sig_try = NULL, *verif = NULL;
Simon Cooksey 0:fb7af294d5d9 1035 size_t i;
Simon Cooksey 0:fb7af294d5d9 1036 unsigned char diff;
Simon Cooksey 0:fb7af294d5d9 1037 volatile unsigned char diff_no_optimize;
Simon Cooksey 0:fb7af294d5d9 1038 int ret;
Simon Cooksey 0:fb7af294d5d9 1039
Simon Cooksey 0:fb7af294d5d9 1040 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Simon Cooksey 0:fb7af294d5d9 1041 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 1042
Simon Cooksey 0:fb7af294d5d9 1043 olen = ctx->len;
Simon Cooksey 0:fb7af294d5d9 1044 nb_pad = olen - 3;
Simon Cooksey 0:fb7af294d5d9 1045
Simon Cooksey 0:fb7af294d5d9 1046 if( md_alg != MBEDTLS_MD_NONE )
Simon Cooksey 0:fb7af294d5d9 1047 {
Simon Cooksey 0:fb7af294d5d9 1048 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Simon Cooksey 0:fb7af294d5d9 1049 if( md_info == NULL )
Simon Cooksey 0:fb7af294d5d9 1050 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 1051
Simon Cooksey 0:fb7af294d5d9 1052 if( mbedtls_oid_get_oid_by_md( md_alg, &oid, &oid_size ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1053 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 1054
Simon Cooksey 0:fb7af294d5d9 1055 nb_pad -= 10 + oid_size;
Simon Cooksey 0:fb7af294d5d9 1056
Simon Cooksey 0:fb7af294d5d9 1057 hashlen = mbedtls_md_get_size( md_info );
Simon Cooksey 0:fb7af294d5d9 1058 }
Simon Cooksey 0:fb7af294d5d9 1059
Simon Cooksey 0:fb7af294d5d9 1060 nb_pad -= hashlen;
Simon Cooksey 0:fb7af294d5d9 1061
Simon Cooksey 0:fb7af294d5d9 1062 if( ( nb_pad < 8 ) || ( nb_pad > olen ) )
Simon Cooksey 0:fb7af294d5d9 1063 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 1064
Simon Cooksey 0:fb7af294d5d9 1065 *p++ = 0;
Simon Cooksey 0:fb7af294d5d9 1066 *p++ = MBEDTLS_RSA_SIGN;
Simon Cooksey 0:fb7af294d5d9 1067 memset( p, 0xFF, nb_pad );
Simon Cooksey 0:fb7af294d5d9 1068 p += nb_pad;
Simon Cooksey 0:fb7af294d5d9 1069 *p++ = 0;
Simon Cooksey 0:fb7af294d5d9 1070
Simon Cooksey 0:fb7af294d5d9 1071 if( md_alg == MBEDTLS_MD_NONE )
Simon Cooksey 0:fb7af294d5d9 1072 {
Simon Cooksey 0:fb7af294d5d9 1073 memcpy( p, hash, hashlen );
Simon Cooksey 0:fb7af294d5d9 1074 }
Simon Cooksey 0:fb7af294d5d9 1075 else
Simon Cooksey 0:fb7af294d5d9 1076 {
Simon Cooksey 0:fb7af294d5d9 1077 /*
Simon Cooksey 0:fb7af294d5d9 1078 * DigestInfo ::= SEQUENCE {
Simon Cooksey 0:fb7af294d5d9 1079 * digestAlgorithm DigestAlgorithmIdentifier,
Simon Cooksey 0:fb7af294d5d9 1080 * digest Digest }
Simon Cooksey 0:fb7af294d5d9 1081 *
Simon Cooksey 0:fb7af294d5d9 1082 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
Simon Cooksey 0:fb7af294d5d9 1083 *
Simon Cooksey 0:fb7af294d5d9 1084 * Digest ::= OCTET STRING
Simon Cooksey 0:fb7af294d5d9 1085 */
Simon Cooksey 0:fb7af294d5d9 1086 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Simon Cooksey 0:fb7af294d5d9 1087 *p++ = (unsigned char) ( 0x08 + oid_size + hashlen );
Simon Cooksey 0:fb7af294d5d9 1088 *p++ = MBEDTLS_ASN1_SEQUENCE | MBEDTLS_ASN1_CONSTRUCTED;
Simon Cooksey 0:fb7af294d5d9 1089 *p++ = (unsigned char) ( 0x04 + oid_size );
Simon Cooksey 0:fb7af294d5d9 1090 *p++ = MBEDTLS_ASN1_OID;
Simon Cooksey 0:fb7af294d5d9 1091 *p++ = oid_size & 0xFF;
Simon Cooksey 0:fb7af294d5d9 1092 memcpy( p, oid, oid_size );
Simon Cooksey 0:fb7af294d5d9 1093 p += oid_size;
Simon Cooksey 0:fb7af294d5d9 1094 *p++ = MBEDTLS_ASN1_NULL;
Simon Cooksey 0:fb7af294d5d9 1095 *p++ = 0x00;
Simon Cooksey 0:fb7af294d5d9 1096 *p++ = MBEDTLS_ASN1_OCTET_STRING;
Simon Cooksey 0:fb7af294d5d9 1097 *p++ = hashlen;
Simon Cooksey 0:fb7af294d5d9 1098 memcpy( p, hash, hashlen );
Simon Cooksey 0:fb7af294d5d9 1099 }
Simon Cooksey 0:fb7af294d5d9 1100
Simon Cooksey 0:fb7af294d5d9 1101 if( mode == MBEDTLS_RSA_PUBLIC )
Simon Cooksey 0:fb7af294d5d9 1102 return( mbedtls_rsa_public( ctx, sig, sig ) );
Simon Cooksey 0:fb7af294d5d9 1103
Simon Cooksey 0:fb7af294d5d9 1104 /*
Simon Cooksey 0:fb7af294d5d9 1105 * In order to prevent Lenstra's attack, make the signature in a
Simon Cooksey 0:fb7af294d5d9 1106 * temporary buffer and check it before returning it.
Simon Cooksey 0:fb7af294d5d9 1107 */
Simon Cooksey 0:fb7af294d5d9 1108 sig_try = mbedtls_calloc( 1, ctx->len );
Simon Cooksey 0:fb7af294d5d9 1109 if( sig_try == NULL )
Simon Cooksey 0:fb7af294d5d9 1110 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Simon Cooksey 0:fb7af294d5d9 1111
Simon Cooksey 0:fb7af294d5d9 1112 verif = mbedtls_calloc( 1, ctx->len );
Simon Cooksey 0:fb7af294d5d9 1113 if( verif == NULL )
Simon Cooksey 0:fb7af294d5d9 1114 {
Simon Cooksey 0:fb7af294d5d9 1115 mbedtls_free( sig_try );
Simon Cooksey 0:fb7af294d5d9 1116 return( MBEDTLS_ERR_MPI_ALLOC_FAILED );
Simon Cooksey 0:fb7af294d5d9 1117 }
Simon Cooksey 0:fb7af294d5d9 1118
Simon Cooksey 0:fb7af294d5d9 1119 MBEDTLS_MPI_CHK( mbedtls_rsa_private( ctx, f_rng, p_rng, sig, sig_try ) );
Simon Cooksey 0:fb7af294d5d9 1120 MBEDTLS_MPI_CHK( mbedtls_rsa_public( ctx, sig_try, verif ) );
Simon Cooksey 0:fb7af294d5d9 1121
Simon Cooksey 0:fb7af294d5d9 1122 /* Compare in constant time just in case */
Simon Cooksey 0:fb7af294d5d9 1123 for( diff = 0, i = 0; i < ctx->len; i++ )
Simon Cooksey 0:fb7af294d5d9 1124 diff |= verif[i] ^ sig[i];
Simon Cooksey 0:fb7af294d5d9 1125 diff_no_optimize = diff;
Simon Cooksey 0:fb7af294d5d9 1126
Simon Cooksey 0:fb7af294d5d9 1127 if( diff_no_optimize != 0 )
Simon Cooksey 0:fb7af294d5d9 1128 {
Simon Cooksey 0:fb7af294d5d9 1129 ret = MBEDTLS_ERR_RSA_PRIVATE_FAILED;
Simon Cooksey 0:fb7af294d5d9 1130 goto cleanup;
Simon Cooksey 0:fb7af294d5d9 1131 }
Simon Cooksey 0:fb7af294d5d9 1132
Simon Cooksey 0:fb7af294d5d9 1133 memcpy( sig, sig_try, ctx->len );
Simon Cooksey 0:fb7af294d5d9 1134
Simon Cooksey 0:fb7af294d5d9 1135 cleanup:
Simon Cooksey 0:fb7af294d5d9 1136 mbedtls_free( sig_try );
Simon Cooksey 0:fb7af294d5d9 1137 mbedtls_free( verif );
Simon Cooksey 0:fb7af294d5d9 1138
Simon Cooksey 0:fb7af294d5d9 1139 return( ret );
Simon Cooksey 0:fb7af294d5d9 1140 }
Simon Cooksey 0:fb7af294d5d9 1141 #endif /* MBEDTLS_PKCS1_V15 */
Simon Cooksey 0:fb7af294d5d9 1142
Simon Cooksey 0:fb7af294d5d9 1143 /*
Simon Cooksey 0:fb7af294d5d9 1144 * Do an RSA operation to sign the message digest
Simon Cooksey 0:fb7af294d5d9 1145 */
Simon Cooksey 0:fb7af294d5d9 1146 int mbedtls_rsa_pkcs1_sign( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 1147 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 1148 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 1149 int mode,
Simon Cooksey 0:fb7af294d5d9 1150 mbedtls_md_type_t md_alg,
Simon Cooksey 0:fb7af294d5d9 1151 unsigned int hashlen,
Simon Cooksey 0:fb7af294d5d9 1152 const unsigned char *hash,
Simon Cooksey 0:fb7af294d5d9 1153 unsigned char *sig )
Simon Cooksey 0:fb7af294d5d9 1154 {
Simon Cooksey 0:fb7af294d5d9 1155 switch( ctx->padding )
Simon Cooksey 0:fb7af294d5d9 1156 {
Simon Cooksey 0:fb7af294d5d9 1157 #if defined(MBEDTLS_PKCS1_V15)
Simon Cooksey 0:fb7af294d5d9 1158 case MBEDTLS_RSA_PKCS_V15:
Simon Cooksey 0:fb7af294d5d9 1159 return mbedtls_rsa_rsassa_pkcs1_v15_sign( ctx, f_rng, p_rng, mode, md_alg,
Simon Cooksey 0:fb7af294d5d9 1160 hashlen, hash, sig );
Simon Cooksey 0:fb7af294d5d9 1161 #endif
Simon Cooksey 0:fb7af294d5d9 1162
Simon Cooksey 0:fb7af294d5d9 1163 #if defined(MBEDTLS_PKCS1_V21)
Simon Cooksey 0:fb7af294d5d9 1164 case MBEDTLS_RSA_PKCS_V21:
Simon Cooksey 0:fb7af294d5d9 1165 return mbedtls_rsa_rsassa_pss_sign( ctx, f_rng, p_rng, mode, md_alg,
Simon Cooksey 0:fb7af294d5d9 1166 hashlen, hash, sig );
Simon Cooksey 0:fb7af294d5d9 1167 #endif
Simon Cooksey 0:fb7af294d5d9 1168
Simon Cooksey 0:fb7af294d5d9 1169 default:
Simon Cooksey 0:fb7af294d5d9 1170 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Simon Cooksey 0:fb7af294d5d9 1171 }
Simon Cooksey 0:fb7af294d5d9 1172 }
Simon Cooksey 0:fb7af294d5d9 1173
Simon Cooksey 0:fb7af294d5d9 1174 #if defined(MBEDTLS_PKCS1_V21)
Simon Cooksey 0:fb7af294d5d9 1175 /*
Simon Cooksey 0:fb7af294d5d9 1176 * Implementation of the PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Simon Cooksey 0:fb7af294d5d9 1177 */
Simon Cooksey 0:fb7af294d5d9 1178 int mbedtls_rsa_rsassa_pss_verify_ext( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 1179 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 1180 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 1181 int mode,
Simon Cooksey 0:fb7af294d5d9 1182 mbedtls_md_type_t md_alg,
Simon Cooksey 0:fb7af294d5d9 1183 unsigned int hashlen,
Simon Cooksey 0:fb7af294d5d9 1184 const unsigned char *hash,
Simon Cooksey 0:fb7af294d5d9 1185 mbedtls_md_type_t mgf1_hash_id,
Simon Cooksey 0:fb7af294d5d9 1186 int expected_salt_len,
Simon Cooksey 0:fb7af294d5d9 1187 const unsigned char *sig )
Simon Cooksey 0:fb7af294d5d9 1188 {
Simon Cooksey 0:fb7af294d5d9 1189 int ret;
Simon Cooksey 0:fb7af294d5d9 1190 size_t siglen;
Simon Cooksey 0:fb7af294d5d9 1191 unsigned char *p;
Simon Cooksey 0:fb7af294d5d9 1192 unsigned char result[MBEDTLS_MD_MAX_SIZE];
Simon Cooksey 0:fb7af294d5d9 1193 unsigned char zeros[8];
Simon Cooksey 0:fb7af294d5d9 1194 unsigned int hlen;
Simon Cooksey 0:fb7af294d5d9 1195 size_t slen, msb;
Simon Cooksey 0:fb7af294d5d9 1196 const mbedtls_md_info_t *md_info;
Simon Cooksey 0:fb7af294d5d9 1197 mbedtls_md_context_t md_ctx;
Simon Cooksey 0:fb7af294d5d9 1198 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Simon Cooksey 0:fb7af294d5d9 1199
Simon Cooksey 0:fb7af294d5d9 1200 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V21 )
Simon Cooksey 0:fb7af294d5d9 1201 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 1202
Simon Cooksey 0:fb7af294d5d9 1203 siglen = ctx->len;
Simon Cooksey 0:fb7af294d5d9 1204
Simon Cooksey 0:fb7af294d5d9 1205 if( siglen < 16 || siglen > sizeof( buf ) )
Simon Cooksey 0:fb7af294d5d9 1206 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 1207
Simon Cooksey 0:fb7af294d5d9 1208 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Simon Cooksey 0:fb7af294d5d9 1209 ? mbedtls_rsa_public( ctx, sig, buf )
Simon Cooksey 0:fb7af294d5d9 1210 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Simon Cooksey 0:fb7af294d5d9 1211
Simon Cooksey 0:fb7af294d5d9 1212 if( ret != 0 )
Simon Cooksey 0:fb7af294d5d9 1213 return( ret );
Simon Cooksey 0:fb7af294d5d9 1214
Simon Cooksey 0:fb7af294d5d9 1215 p = buf;
Simon Cooksey 0:fb7af294d5d9 1216
Simon Cooksey 0:fb7af294d5d9 1217 if( buf[siglen - 1] != 0xBC )
Simon Cooksey 0:fb7af294d5d9 1218 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Simon Cooksey 0:fb7af294d5d9 1219
Simon Cooksey 0:fb7af294d5d9 1220 if( md_alg != MBEDTLS_MD_NONE )
Simon Cooksey 0:fb7af294d5d9 1221 {
Simon Cooksey 0:fb7af294d5d9 1222 /* Gather length of hash to sign */
Simon Cooksey 0:fb7af294d5d9 1223 md_info = mbedtls_md_info_from_type( md_alg );
Simon Cooksey 0:fb7af294d5d9 1224 if( md_info == NULL )
Simon Cooksey 0:fb7af294d5d9 1225 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 1226
Simon Cooksey 0:fb7af294d5d9 1227 hashlen = mbedtls_md_get_size( md_info );
Simon Cooksey 0:fb7af294d5d9 1228 }
Simon Cooksey 0:fb7af294d5d9 1229
Simon Cooksey 0:fb7af294d5d9 1230 md_info = mbedtls_md_info_from_type( mgf1_hash_id );
Simon Cooksey 0:fb7af294d5d9 1231 if( md_info == NULL )
Simon Cooksey 0:fb7af294d5d9 1232 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 1233
Simon Cooksey 0:fb7af294d5d9 1234 hlen = mbedtls_md_get_size( md_info );
Simon Cooksey 0:fb7af294d5d9 1235 slen = siglen - hlen - 1; /* Currently length of salt + padding */
Simon Cooksey 0:fb7af294d5d9 1236
Simon Cooksey 0:fb7af294d5d9 1237 memset( zeros, 0, 8 );
Simon Cooksey 0:fb7af294d5d9 1238
Simon Cooksey 0:fb7af294d5d9 1239 /*
Simon Cooksey 0:fb7af294d5d9 1240 * Note: EMSA-PSS verification is over the length of N - 1 bits
Simon Cooksey 0:fb7af294d5d9 1241 */
Simon Cooksey 0:fb7af294d5d9 1242 msb = mbedtls_mpi_bitlen( &ctx->N ) - 1;
Simon Cooksey 0:fb7af294d5d9 1243
Simon Cooksey 0:fb7af294d5d9 1244 /* Compensate for boundary condition when applying mask */
Simon Cooksey 0:fb7af294d5d9 1245 if( msb % 8 == 0 )
Simon Cooksey 0:fb7af294d5d9 1246 {
Simon Cooksey 0:fb7af294d5d9 1247 p++;
Simon Cooksey 0:fb7af294d5d9 1248 siglen -= 1;
Simon Cooksey 0:fb7af294d5d9 1249 }
Simon Cooksey 0:fb7af294d5d9 1250 if( buf[0] >> ( 8 - siglen * 8 + msb ) )
Simon Cooksey 0:fb7af294d5d9 1251 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 1252
Simon Cooksey 0:fb7af294d5d9 1253 mbedtls_md_init( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 1254 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 0 ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1255 {
Simon Cooksey 0:fb7af294d5d9 1256 mbedtls_md_free( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 1257 return( ret );
Simon Cooksey 0:fb7af294d5d9 1258 }
Simon Cooksey 0:fb7af294d5d9 1259
Simon Cooksey 0:fb7af294d5d9 1260 mgf_mask( p, siglen - hlen - 1, p + siglen - hlen - 1, hlen, &md_ctx );
Simon Cooksey 0:fb7af294d5d9 1261
Simon Cooksey 0:fb7af294d5d9 1262 buf[0] &= 0xFF >> ( siglen * 8 - msb );
Simon Cooksey 0:fb7af294d5d9 1263
Simon Cooksey 0:fb7af294d5d9 1264 while( p < buf + siglen && *p == 0 )
Simon Cooksey 0:fb7af294d5d9 1265 p++;
Simon Cooksey 0:fb7af294d5d9 1266
Simon Cooksey 0:fb7af294d5d9 1267 if( p == buf + siglen ||
Simon Cooksey 0:fb7af294d5d9 1268 *p++ != 0x01 )
Simon Cooksey 0:fb7af294d5d9 1269 {
Simon Cooksey 0:fb7af294d5d9 1270 mbedtls_md_free( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 1271 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Simon Cooksey 0:fb7af294d5d9 1272 }
Simon Cooksey 0:fb7af294d5d9 1273
Simon Cooksey 0:fb7af294d5d9 1274 /* Actual salt len */
Simon Cooksey 0:fb7af294d5d9 1275 slen -= p - buf;
Simon Cooksey 0:fb7af294d5d9 1276
Simon Cooksey 0:fb7af294d5d9 1277 if( expected_salt_len != MBEDTLS_RSA_SALT_LEN_ANY &&
Simon Cooksey 0:fb7af294d5d9 1278 slen != (size_t) expected_salt_len )
Simon Cooksey 0:fb7af294d5d9 1279 {
Simon Cooksey 0:fb7af294d5d9 1280 mbedtls_md_free( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 1281 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Simon Cooksey 0:fb7af294d5d9 1282 }
Simon Cooksey 0:fb7af294d5d9 1283
Simon Cooksey 0:fb7af294d5d9 1284 /*
Simon Cooksey 0:fb7af294d5d9 1285 * Generate H = Hash( M' )
Simon Cooksey 0:fb7af294d5d9 1286 */
Simon Cooksey 0:fb7af294d5d9 1287 mbedtls_md_starts( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 1288 mbedtls_md_update( &md_ctx, zeros, 8 );
Simon Cooksey 0:fb7af294d5d9 1289 mbedtls_md_update( &md_ctx, hash, hashlen );
Simon Cooksey 0:fb7af294d5d9 1290 mbedtls_md_update( &md_ctx, p, slen );
Simon Cooksey 0:fb7af294d5d9 1291 mbedtls_md_finish( &md_ctx, result );
Simon Cooksey 0:fb7af294d5d9 1292
Simon Cooksey 0:fb7af294d5d9 1293 mbedtls_md_free( &md_ctx );
Simon Cooksey 0:fb7af294d5d9 1294
Simon Cooksey 0:fb7af294d5d9 1295 if( memcmp( p + slen, result, hlen ) == 0 )
Simon Cooksey 0:fb7af294d5d9 1296 return( 0 );
Simon Cooksey 0:fb7af294d5d9 1297 else
Simon Cooksey 0:fb7af294d5d9 1298 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1299 }
Simon Cooksey 0:fb7af294d5d9 1300
Simon Cooksey 0:fb7af294d5d9 1301 /*
Simon Cooksey 0:fb7af294d5d9 1302 * Simplified PKCS#1 v2.1 RSASSA-PSS-VERIFY function
Simon Cooksey 0:fb7af294d5d9 1303 */
Simon Cooksey 0:fb7af294d5d9 1304 int mbedtls_rsa_rsassa_pss_verify( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 1305 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 1306 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 1307 int mode,
Simon Cooksey 0:fb7af294d5d9 1308 mbedtls_md_type_t md_alg,
Simon Cooksey 0:fb7af294d5d9 1309 unsigned int hashlen,
Simon Cooksey 0:fb7af294d5d9 1310 const unsigned char *hash,
Simon Cooksey 0:fb7af294d5d9 1311 const unsigned char *sig )
Simon Cooksey 0:fb7af294d5d9 1312 {
Simon Cooksey 0:fb7af294d5d9 1313 mbedtls_md_type_t mgf1_hash_id = ( ctx->hash_id != MBEDTLS_MD_NONE )
Simon Cooksey 0:fb7af294d5d9 1314 ? (mbedtls_md_type_t) ctx->hash_id
Simon Cooksey 0:fb7af294d5d9 1315 : md_alg;
Simon Cooksey 0:fb7af294d5d9 1316
Simon Cooksey 0:fb7af294d5d9 1317 return( mbedtls_rsa_rsassa_pss_verify_ext( ctx, f_rng, p_rng, mode,
Simon Cooksey 0:fb7af294d5d9 1318 md_alg, hashlen, hash,
Simon Cooksey 0:fb7af294d5d9 1319 mgf1_hash_id, MBEDTLS_RSA_SALT_LEN_ANY,
Simon Cooksey 0:fb7af294d5d9 1320 sig ) );
Simon Cooksey 0:fb7af294d5d9 1321
Simon Cooksey 0:fb7af294d5d9 1322 }
Simon Cooksey 0:fb7af294d5d9 1323 #endif /* MBEDTLS_PKCS1_V21 */
Simon Cooksey 0:fb7af294d5d9 1324
Simon Cooksey 0:fb7af294d5d9 1325 #if defined(MBEDTLS_PKCS1_V15)
Simon Cooksey 0:fb7af294d5d9 1326 /*
Simon Cooksey 0:fb7af294d5d9 1327 * Implementation of the PKCS#1 v2.1 RSASSA-PKCS1-v1_5-VERIFY function
Simon Cooksey 0:fb7af294d5d9 1328 */
Simon Cooksey 0:fb7af294d5d9 1329 int mbedtls_rsa_rsassa_pkcs1_v15_verify( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 1330 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 1331 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 1332 int mode,
Simon Cooksey 0:fb7af294d5d9 1333 mbedtls_md_type_t md_alg,
Simon Cooksey 0:fb7af294d5d9 1334 unsigned int hashlen,
Simon Cooksey 0:fb7af294d5d9 1335 const unsigned char *hash,
Simon Cooksey 0:fb7af294d5d9 1336 const unsigned char *sig )
Simon Cooksey 0:fb7af294d5d9 1337 {
Simon Cooksey 0:fb7af294d5d9 1338 int ret;
Simon Cooksey 0:fb7af294d5d9 1339 size_t len, siglen, asn1_len;
Simon Cooksey 0:fb7af294d5d9 1340 unsigned char *p, *end;
Simon Cooksey 0:fb7af294d5d9 1341 mbedtls_md_type_t msg_md_alg;
Simon Cooksey 0:fb7af294d5d9 1342 const mbedtls_md_info_t *md_info;
Simon Cooksey 0:fb7af294d5d9 1343 mbedtls_asn1_buf oid;
Simon Cooksey 0:fb7af294d5d9 1344 unsigned char buf[MBEDTLS_MPI_MAX_SIZE];
Simon Cooksey 0:fb7af294d5d9 1345
Simon Cooksey 0:fb7af294d5d9 1346 if( mode == MBEDTLS_RSA_PRIVATE && ctx->padding != MBEDTLS_RSA_PKCS_V15 )
Simon Cooksey 0:fb7af294d5d9 1347 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 1348
Simon Cooksey 0:fb7af294d5d9 1349 siglen = ctx->len;
Simon Cooksey 0:fb7af294d5d9 1350
Simon Cooksey 0:fb7af294d5d9 1351 if( siglen < 16 || siglen > sizeof( buf ) )
Simon Cooksey 0:fb7af294d5d9 1352 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 1353
Simon Cooksey 0:fb7af294d5d9 1354 ret = ( mode == MBEDTLS_RSA_PUBLIC )
Simon Cooksey 0:fb7af294d5d9 1355 ? mbedtls_rsa_public( ctx, sig, buf )
Simon Cooksey 0:fb7af294d5d9 1356 : mbedtls_rsa_private( ctx, f_rng, p_rng, sig, buf );
Simon Cooksey 0:fb7af294d5d9 1357
Simon Cooksey 0:fb7af294d5d9 1358 if( ret != 0 )
Simon Cooksey 0:fb7af294d5d9 1359 return( ret );
Simon Cooksey 0:fb7af294d5d9 1360
Simon Cooksey 0:fb7af294d5d9 1361 p = buf;
Simon Cooksey 0:fb7af294d5d9 1362
Simon Cooksey 0:fb7af294d5d9 1363 if( *p++ != 0 || *p++ != MBEDTLS_RSA_SIGN )
Simon Cooksey 0:fb7af294d5d9 1364 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Simon Cooksey 0:fb7af294d5d9 1365
Simon Cooksey 0:fb7af294d5d9 1366 while( *p != 0 )
Simon Cooksey 0:fb7af294d5d9 1367 {
Simon Cooksey 0:fb7af294d5d9 1368 if( p >= buf + siglen - 1 || *p != 0xFF )
Simon Cooksey 0:fb7af294d5d9 1369 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Simon Cooksey 0:fb7af294d5d9 1370 p++;
Simon Cooksey 0:fb7af294d5d9 1371 }
Simon Cooksey 0:fb7af294d5d9 1372 p++;
Simon Cooksey 0:fb7af294d5d9 1373
Simon Cooksey 0:fb7af294d5d9 1374 len = siglen - ( p - buf );
Simon Cooksey 0:fb7af294d5d9 1375
Simon Cooksey 0:fb7af294d5d9 1376 if( len == hashlen && md_alg == MBEDTLS_MD_NONE )
Simon Cooksey 0:fb7af294d5d9 1377 {
Simon Cooksey 0:fb7af294d5d9 1378 if( memcmp( p, hash, hashlen ) == 0 )
Simon Cooksey 0:fb7af294d5d9 1379 return( 0 );
Simon Cooksey 0:fb7af294d5d9 1380 else
Simon Cooksey 0:fb7af294d5d9 1381 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1382 }
Simon Cooksey 0:fb7af294d5d9 1383
Simon Cooksey 0:fb7af294d5d9 1384 md_info = mbedtls_md_info_from_type( md_alg );
Simon Cooksey 0:fb7af294d5d9 1385 if( md_info == NULL )
Simon Cooksey 0:fb7af294d5d9 1386 return( MBEDTLS_ERR_RSA_BAD_INPUT_DATA );
Simon Cooksey 0:fb7af294d5d9 1387 hashlen = mbedtls_md_get_size( md_info );
Simon Cooksey 0:fb7af294d5d9 1388
Simon Cooksey 0:fb7af294d5d9 1389 end = p + len;
Simon Cooksey 0:fb7af294d5d9 1390
Simon Cooksey 0:fb7af294d5d9 1391 /*
Simon Cooksey 0:fb7af294d5d9 1392 * Parse the ASN.1 structure inside the PKCS#1 v1.5 structure
Simon Cooksey 0:fb7af294d5d9 1393 */
Simon Cooksey 0:fb7af294d5d9 1394 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
Simon Cooksey 0:fb7af294d5d9 1395 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1396 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1397
Simon Cooksey 0:fb7af294d5d9 1398 if( asn1_len + 2 != len )
Simon Cooksey 0:fb7af294d5d9 1399 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1400
Simon Cooksey 0:fb7af294d5d9 1401 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len,
Simon Cooksey 0:fb7af294d5d9 1402 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1403 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1404
Simon Cooksey 0:fb7af294d5d9 1405 if( asn1_len + 6 + hashlen != len )
Simon Cooksey 0:fb7af294d5d9 1406 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1407
Simon Cooksey 0:fb7af294d5d9 1408 if( ( ret = mbedtls_asn1_get_tag( &p, end, &oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1409 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1410
Simon Cooksey 0:fb7af294d5d9 1411 oid.p = p;
Simon Cooksey 0:fb7af294d5d9 1412 p += oid.len;
Simon Cooksey 0:fb7af294d5d9 1413
Simon Cooksey 0:fb7af294d5d9 1414 if( mbedtls_oid_get_md_alg( &oid, &msg_md_alg ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1415 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1416
Simon Cooksey 0:fb7af294d5d9 1417 if( md_alg != msg_md_alg )
Simon Cooksey 0:fb7af294d5d9 1418 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1419
Simon Cooksey 0:fb7af294d5d9 1420 /*
Simon Cooksey 0:fb7af294d5d9 1421 * assume the algorithm parameters must be NULL
Simon Cooksey 0:fb7af294d5d9 1422 */
Simon Cooksey 0:fb7af294d5d9 1423 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_NULL ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1424 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1425
Simon Cooksey 0:fb7af294d5d9 1426 if( ( ret = mbedtls_asn1_get_tag( &p, end, &asn1_len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1427 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1428
Simon Cooksey 0:fb7af294d5d9 1429 if( asn1_len != hashlen )
Simon Cooksey 0:fb7af294d5d9 1430 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1431
Simon Cooksey 0:fb7af294d5d9 1432 if( memcmp( p, hash, hashlen ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1433 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1434
Simon Cooksey 0:fb7af294d5d9 1435 p += hashlen;
Simon Cooksey 0:fb7af294d5d9 1436
Simon Cooksey 0:fb7af294d5d9 1437 if( p != end )
Simon Cooksey 0:fb7af294d5d9 1438 return( MBEDTLS_ERR_RSA_VERIFY_FAILED );
Simon Cooksey 0:fb7af294d5d9 1439
Simon Cooksey 0:fb7af294d5d9 1440 return( 0 );
Simon Cooksey 0:fb7af294d5d9 1441 }
Simon Cooksey 0:fb7af294d5d9 1442 #endif /* MBEDTLS_PKCS1_V15 */
Simon Cooksey 0:fb7af294d5d9 1443
Simon Cooksey 0:fb7af294d5d9 1444 /*
Simon Cooksey 0:fb7af294d5d9 1445 * Do an RSA operation and check the message digest
Simon Cooksey 0:fb7af294d5d9 1446 */
Simon Cooksey 0:fb7af294d5d9 1447 int mbedtls_rsa_pkcs1_verify( mbedtls_rsa_context *ctx,
Simon Cooksey 0:fb7af294d5d9 1448 int (*f_rng)(void *, unsigned char *, size_t),
Simon Cooksey 0:fb7af294d5d9 1449 void *p_rng,
Simon Cooksey 0:fb7af294d5d9 1450 int mode,
Simon Cooksey 0:fb7af294d5d9 1451 mbedtls_md_type_t md_alg,
Simon Cooksey 0:fb7af294d5d9 1452 unsigned int hashlen,
Simon Cooksey 0:fb7af294d5d9 1453 const unsigned char *hash,
Simon Cooksey 0:fb7af294d5d9 1454 const unsigned char *sig )
Simon Cooksey 0:fb7af294d5d9 1455 {
Simon Cooksey 0:fb7af294d5d9 1456 switch( ctx->padding )
Simon Cooksey 0:fb7af294d5d9 1457 {
Simon Cooksey 0:fb7af294d5d9 1458 #if defined(MBEDTLS_PKCS1_V15)
Simon Cooksey 0:fb7af294d5d9 1459 case MBEDTLS_RSA_PKCS_V15:
Simon Cooksey 0:fb7af294d5d9 1460 return mbedtls_rsa_rsassa_pkcs1_v15_verify( ctx, f_rng, p_rng, mode, md_alg,
Simon Cooksey 0:fb7af294d5d9 1461 hashlen, hash, sig );
Simon Cooksey 0:fb7af294d5d9 1462 #endif
Simon Cooksey 0:fb7af294d5d9 1463
Simon Cooksey 0:fb7af294d5d9 1464 #if defined(MBEDTLS_PKCS1_V21)
Simon Cooksey 0:fb7af294d5d9 1465 case MBEDTLS_RSA_PKCS_V21:
Simon Cooksey 0:fb7af294d5d9 1466 return mbedtls_rsa_rsassa_pss_verify( ctx, f_rng, p_rng, mode, md_alg,
Simon Cooksey 0:fb7af294d5d9 1467 hashlen, hash, sig );
Simon Cooksey 0:fb7af294d5d9 1468 #endif
Simon Cooksey 0:fb7af294d5d9 1469
Simon Cooksey 0:fb7af294d5d9 1470 default:
Simon Cooksey 0:fb7af294d5d9 1471 return( MBEDTLS_ERR_RSA_INVALID_PADDING );
Simon Cooksey 0:fb7af294d5d9 1472 }
Simon Cooksey 0:fb7af294d5d9 1473 }
Simon Cooksey 0:fb7af294d5d9 1474
Simon Cooksey 0:fb7af294d5d9 1475 /*
Simon Cooksey 0:fb7af294d5d9 1476 * Copy the components of an RSA key
Simon Cooksey 0:fb7af294d5d9 1477 */
Simon Cooksey 0:fb7af294d5d9 1478 int mbedtls_rsa_copy( mbedtls_rsa_context *dst, const mbedtls_rsa_context *src )
Simon Cooksey 0:fb7af294d5d9 1479 {
Simon Cooksey 0:fb7af294d5d9 1480 int ret;
Simon Cooksey 0:fb7af294d5d9 1481
Simon Cooksey 0:fb7af294d5d9 1482 dst->ver = src->ver;
Simon Cooksey 0:fb7af294d5d9 1483 dst->len = src->len;
Simon Cooksey 0:fb7af294d5d9 1484
Simon Cooksey 0:fb7af294d5d9 1485 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->N, &src->N ) );
Simon Cooksey 0:fb7af294d5d9 1486 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->E, &src->E ) );
Simon Cooksey 0:fb7af294d5d9 1487
Simon Cooksey 0:fb7af294d5d9 1488 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->D, &src->D ) );
Simon Cooksey 0:fb7af294d5d9 1489 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->P, &src->P ) );
Simon Cooksey 0:fb7af294d5d9 1490 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Q, &src->Q ) );
Simon Cooksey 0:fb7af294d5d9 1491 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DP, &src->DP ) );
Simon Cooksey 0:fb7af294d5d9 1492 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->DQ, &src->DQ ) );
Simon Cooksey 0:fb7af294d5d9 1493 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->QP, &src->QP ) );
Simon Cooksey 0:fb7af294d5d9 1494
Simon Cooksey 0:fb7af294d5d9 1495 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RN, &src->RN ) );
Simon Cooksey 0:fb7af294d5d9 1496 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RP, &src->RP ) );
Simon Cooksey 0:fb7af294d5d9 1497 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->RQ, &src->RQ ) );
Simon Cooksey 0:fb7af294d5d9 1498
Simon Cooksey 0:fb7af294d5d9 1499 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vi, &src->Vi ) );
Simon Cooksey 0:fb7af294d5d9 1500 MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &dst->Vf, &src->Vf ) );
Simon Cooksey 0:fb7af294d5d9 1501
Simon Cooksey 0:fb7af294d5d9 1502 dst->padding = src->padding;
Simon Cooksey 0:fb7af294d5d9 1503 dst->hash_id = src->hash_id;
Simon Cooksey 0:fb7af294d5d9 1504
Simon Cooksey 0:fb7af294d5d9 1505 cleanup:
Simon Cooksey 0:fb7af294d5d9 1506 if( ret != 0 )
Simon Cooksey 0:fb7af294d5d9 1507 mbedtls_rsa_free( dst );
Simon Cooksey 0:fb7af294d5d9 1508
Simon Cooksey 0:fb7af294d5d9 1509 return( ret );
Simon Cooksey 0:fb7af294d5d9 1510 }
Simon Cooksey 0:fb7af294d5d9 1511
Simon Cooksey 0:fb7af294d5d9 1512 /*
Simon Cooksey 0:fb7af294d5d9 1513 * Free the components of an RSA key
Simon Cooksey 0:fb7af294d5d9 1514 */
Simon Cooksey 0:fb7af294d5d9 1515 void mbedtls_rsa_free( mbedtls_rsa_context *ctx )
Simon Cooksey 0:fb7af294d5d9 1516 {
Simon Cooksey 0:fb7af294d5d9 1517 mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->Vf );
Simon Cooksey 0:fb7af294d5d9 1518 mbedtls_mpi_free( &ctx->RQ ); mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->RN );
Simon Cooksey 0:fb7af294d5d9 1519 mbedtls_mpi_free( &ctx->QP ); mbedtls_mpi_free( &ctx->DQ ); mbedtls_mpi_free( &ctx->DP );
Simon Cooksey 0:fb7af294d5d9 1520 mbedtls_mpi_free( &ctx->Q ); mbedtls_mpi_free( &ctx->P ); mbedtls_mpi_free( &ctx->D );
Simon Cooksey 0:fb7af294d5d9 1521 mbedtls_mpi_free( &ctx->E ); mbedtls_mpi_free( &ctx->N );
Simon Cooksey 0:fb7af294d5d9 1522
Simon Cooksey 0:fb7af294d5d9 1523 #if defined(MBEDTLS_THREADING_C)
Simon Cooksey 0:fb7af294d5d9 1524 mbedtls_mutex_free( &ctx->mutex );
Simon Cooksey 0:fb7af294d5d9 1525 #endif
Simon Cooksey 0:fb7af294d5d9 1526 }
Simon Cooksey 0:fb7af294d5d9 1527
Simon Cooksey 0:fb7af294d5d9 1528 #if defined(MBEDTLS_SELF_TEST)
Simon Cooksey 0:fb7af294d5d9 1529
Simon Cooksey 0:fb7af294d5d9 1530 #include "mbedtls/sha1.h"
Simon Cooksey 0:fb7af294d5d9 1531
Simon Cooksey 0:fb7af294d5d9 1532 /*
Simon Cooksey 0:fb7af294d5d9 1533 * Example RSA-1024 keypair, for test purposes
Simon Cooksey 0:fb7af294d5d9 1534 */
Simon Cooksey 0:fb7af294d5d9 1535 #define KEY_LEN 128
Simon Cooksey 0:fb7af294d5d9 1536
Simon Cooksey 0:fb7af294d5d9 1537 #define RSA_N "9292758453063D803DD603D5E777D788" \
Simon Cooksey 0:fb7af294d5d9 1538 "8ED1D5BF35786190FA2F23EBC0848AEA" \
Simon Cooksey 0:fb7af294d5d9 1539 "DDA92CA6C3D80B32C4D109BE0F36D6AE" \
Simon Cooksey 0:fb7af294d5d9 1540 "7130B9CED7ACDF54CFC7555AC14EEBAB" \
Simon Cooksey 0:fb7af294d5d9 1541 "93A89813FBF3C4F8066D2D800F7C38A8" \
Simon Cooksey 0:fb7af294d5d9 1542 "1AE31942917403FF4946B0A83D3D3E05" \
Simon Cooksey 0:fb7af294d5d9 1543 "EE57C6F5F5606FB5D4BC6CD34EE0801A" \
Simon Cooksey 0:fb7af294d5d9 1544 "5E94BB77B07507233A0BC7BAC8F90F79"
Simon Cooksey 0:fb7af294d5d9 1545
Simon Cooksey 0:fb7af294d5d9 1546 #define RSA_E "10001"
Simon Cooksey 0:fb7af294d5d9 1547
Simon Cooksey 0:fb7af294d5d9 1548 #define RSA_D "24BF6185468786FDD303083D25E64EFC" \
Simon Cooksey 0:fb7af294d5d9 1549 "66CA472BC44D253102F8B4A9D3BFA750" \
Simon Cooksey 0:fb7af294d5d9 1550 "91386C0077937FE33FA3252D28855837" \
Simon Cooksey 0:fb7af294d5d9 1551 "AE1B484A8A9A45F7EE8C0C634F99E8CD" \
Simon Cooksey 0:fb7af294d5d9 1552 "DF79C5CE07EE72C7F123142198164234" \
Simon Cooksey 0:fb7af294d5d9 1553 "CABB724CF78B8173B9F880FC86322407" \
Simon Cooksey 0:fb7af294d5d9 1554 "AF1FEDFDDE2BEB674CA15F3E81A1521E" \
Simon Cooksey 0:fb7af294d5d9 1555 "071513A1E85B5DFA031F21ECAE91A34D"
Simon Cooksey 0:fb7af294d5d9 1556
Simon Cooksey 0:fb7af294d5d9 1557 #define RSA_P "C36D0EB7FCD285223CFB5AABA5BDA3D8" \
Simon Cooksey 0:fb7af294d5d9 1558 "2C01CAD19EA484A87EA4377637E75500" \
Simon Cooksey 0:fb7af294d5d9 1559 "FCB2005C5C7DD6EC4AC023CDA285D796" \
Simon Cooksey 0:fb7af294d5d9 1560 "C3D9E75E1EFC42488BB4F1D13AC30A57"
Simon Cooksey 0:fb7af294d5d9 1561
Simon Cooksey 0:fb7af294d5d9 1562 #define RSA_Q "C000DF51A7C77AE8D7C7370C1FF55B69" \
Simon Cooksey 0:fb7af294d5d9 1563 "E211C2B9E5DB1ED0BF61D0D9899620F4" \
Simon Cooksey 0:fb7af294d5d9 1564 "910E4168387E3C30AA1E00C339A79508" \
Simon Cooksey 0:fb7af294d5d9 1565 "8452DD96A9A5EA5D9DCA68DA636032AF"
Simon Cooksey 0:fb7af294d5d9 1566
Simon Cooksey 0:fb7af294d5d9 1567 #define RSA_DP "C1ACF567564274FB07A0BBAD5D26E298" \
Simon Cooksey 0:fb7af294d5d9 1568 "3C94D22288ACD763FD8E5600ED4A702D" \
Simon Cooksey 0:fb7af294d5d9 1569 "F84198A5F06C2E72236AE490C93F07F8" \
Simon Cooksey 0:fb7af294d5d9 1570 "3CC559CD27BC2D1CA488811730BB5725"
Simon Cooksey 0:fb7af294d5d9 1571
Simon Cooksey 0:fb7af294d5d9 1572 #define RSA_DQ "4959CBF6F8FEF750AEE6977C155579C7" \
Simon Cooksey 0:fb7af294d5d9 1573 "D8AAEA56749EA28623272E4F7D0592AF" \
Simon Cooksey 0:fb7af294d5d9 1574 "7C1F1313CAC9471B5C523BFE592F517B" \
Simon Cooksey 0:fb7af294d5d9 1575 "407A1BD76C164B93DA2D32A383E58357"
Simon Cooksey 0:fb7af294d5d9 1576
Simon Cooksey 0:fb7af294d5d9 1577 #define RSA_QP "9AE7FBC99546432DF71896FC239EADAE" \
Simon Cooksey 0:fb7af294d5d9 1578 "F38D18D2B2F0E2DD275AA977E2BF4411" \
Simon Cooksey 0:fb7af294d5d9 1579 "F5A3B2A5D33605AEBBCCBA7FEB9F2D2F" \
Simon Cooksey 0:fb7af294d5d9 1580 "A74206CEC169D74BF5A8C50D6F48EA08"
Simon Cooksey 0:fb7af294d5d9 1581
Simon Cooksey 0:fb7af294d5d9 1582 #define PT_LEN 24
Simon Cooksey 0:fb7af294d5d9 1583 #define RSA_PT "\xAA\xBB\xCC\x03\x02\x01\x00\xFF\xFF\xFF\xFF\xFF" \
Simon Cooksey 0:fb7af294d5d9 1584 "\x11\x22\x33\x0A\x0B\x0C\xCC\xDD\xDD\xDD\xDD\xDD"
Simon Cooksey 0:fb7af294d5d9 1585
Simon Cooksey 0:fb7af294d5d9 1586 #if defined(MBEDTLS_PKCS1_V15)
Simon Cooksey 0:fb7af294d5d9 1587 static int myrand( void *rng_state, unsigned char *output, size_t len )
Simon Cooksey 0:fb7af294d5d9 1588 {
Simon Cooksey 0:fb7af294d5d9 1589 #if !defined(__OpenBSD__)
Simon Cooksey 0:fb7af294d5d9 1590 size_t i;
Simon Cooksey 0:fb7af294d5d9 1591
Simon Cooksey 0:fb7af294d5d9 1592 if( rng_state != NULL )
Simon Cooksey 0:fb7af294d5d9 1593 rng_state = NULL;
Simon Cooksey 0:fb7af294d5d9 1594
Simon Cooksey 0:fb7af294d5d9 1595 for( i = 0; i < len; ++i )
Simon Cooksey 0:fb7af294d5d9 1596 output[i] = rand();
Simon Cooksey 0:fb7af294d5d9 1597 #else
Simon Cooksey 0:fb7af294d5d9 1598 if( rng_state != NULL )
Simon Cooksey 0:fb7af294d5d9 1599 rng_state = NULL;
Simon Cooksey 0:fb7af294d5d9 1600
Simon Cooksey 0:fb7af294d5d9 1601 arc4random_buf( output, len );
Simon Cooksey 0:fb7af294d5d9 1602 #endif /* !OpenBSD */
Simon Cooksey 0:fb7af294d5d9 1603
Simon Cooksey 0:fb7af294d5d9 1604 return( 0 );
Simon Cooksey 0:fb7af294d5d9 1605 }
Simon Cooksey 0:fb7af294d5d9 1606 #endif /* MBEDTLS_PKCS1_V15 */
Simon Cooksey 0:fb7af294d5d9 1607
Simon Cooksey 0:fb7af294d5d9 1608 /*
Simon Cooksey 0:fb7af294d5d9 1609 * Checkup routine
Simon Cooksey 0:fb7af294d5d9 1610 */
Simon Cooksey 0:fb7af294d5d9 1611 int mbedtls_rsa_self_test( int verbose )
Simon Cooksey 0:fb7af294d5d9 1612 {
Simon Cooksey 0:fb7af294d5d9 1613 int ret = 0;
Simon Cooksey 0:fb7af294d5d9 1614 #if defined(MBEDTLS_PKCS1_V15)
Simon Cooksey 0:fb7af294d5d9 1615 size_t len;
Simon Cooksey 0:fb7af294d5d9 1616 mbedtls_rsa_context rsa;
Simon Cooksey 0:fb7af294d5d9 1617 unsigned char rsa_plaintext[PT_LEN];
Simon Cooksey 0:fb7af294d5d9 1618 unsigned char rsa_decrypted[PT_LEN];
Simon Cooksey 0:fb7af294d5d9 1619 unsigned char rsa_ciphertext[KEY_LEN];
Simon Cooksey 0:fb7af294d5d9 1620 #if defined(MBEDTLS_SHA1_C)
Simon Cooksey 0:fb7af294d5d9 1621 unsigned char sha1sum[20];
Simon Cooksey 0:fb7af294d5d9 1622 #endif
Simon Cooksey 0:fb7af294d5d9 1623
Simon Cooksey 0:fb7af294d5d9 1624 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Simon Cooksey 0:fb7af294d5d9 1625
Simon Cooksey 0:fb7af294d5d9 1626 rsa.len = KEY_LEN;
Simon Cooksey 0:fb7af294d5d9 1627 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.N , 16, RSA_N ) );
Simon Cooksey 0:fb7af294d5d9 1628 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.E , 16, RSA_E ) );
Simon Cooksey 0:fb7af294d5d9 1629 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.D , 16, RSA_D ) );
Simon Cooksey 0:fb7af294d5d9 1630 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.P , 16, RSA_P ) );
Simon Cooksey 0:fb7af294d5d9 1631 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.Q , 16, RSA_Q ) );
Simon Cooksey 0:fb7af294d5d9 1632 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DP, 16, RSA_DP ) );
Simon Cooksey 0:fb7af294d5d9 1633 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.DQ, 16, RSA_DQ ) );
Simon Cooksey 0:fb7af294d5d9 1634 MBEDTLS_MPI_CHK( mbedtls_mpi_read_string( &rsa.QP, 16, RSA_QP ) );
Simon Cooksey 0:fb7af294d5d9 1635
Simon Cooksey 0:fb7af294d5d9 1636 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1637 mbedtls_printf( " RSA key validation: " );
Simon Cooksey 0:fb7af294d5d9 1638
Simon Cooksey 0:fb7af294d5d9 1639 if( mbedtls_rsa_check_pubkey( &rsa ) != 0 ||
Simon Cooksey 0:fb7af294d5d9 1640 mbedtls_rsa_check_privkey( &rsa ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1641 {
Simon Cooksey 0:fb7af294d5d9 1642 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1643 mbedtls_printf( "failed\n" );
Simon Cooksey 0:fb7af294d5d9 1644
Simon Cooksey 0:fb7af294d5d9 1645 return( 1 );
Simon Cooksey 0:fb7af294d5d9 1646 }
Simon Cooksey 0:fb7af294d5d9 1647
Simon Cooksey 0:fb7af294d5d9 1648 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1649 mbedtls_printf( "passed\n PKCS#1 encryption : " );
Simon Cooksey 0:fb7af294d5d9 1650
Simon Cooksey 0:fb7af294d5d9 1651 memcpy( rsa_plaintext, RSA_PT, PT_LEN );
Simon Cooksey 0:fb7af294d5d9 1652
Simon Cooksey 0:fb7af294d5d9 1653 if( mbedtls_rsa_pkcs1_encrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PUBLIC, PT_LEN,
Simon Cooksey 0:fb7af294d5d9 1654 rsa_plaintext, rsa_ciphertext ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1655 {
Simon Cooksey 0:fb7af294d5d9 1656 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1657 mbedtls_printf( "failed\n" );
Simon Cooksey 0:fb7af294d5d9 1658
Simon Cooksey 0:fb7af294d5d9 1659 return( 1 );
Simon Cooksey 0:fb7af294d5d9 1660 }
Simon Cooksey 0:fb7af294d5d9 1661
Simon Cooksey 0:fb7af294d5d9 1662 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1663 mbedtls_printf( "passed\n PKCS#1 decryption : " );
Simon Cooksey 0:fb7af294d5d9 1664
Simon Cooksey 0:fb7af294d5d9 1665 if( mbedtls_rsa_pkcs1_decrypt( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, &len,
Simon Cooksey 0:fb7af294d5d9 1666 rsa_ciphertext, rsa_decrypted,
Simon Cooksey 0:fb7af294d5d9 1667 sizeof(rsa_decrypted) ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1668 {
Simon Cooksey 0:fb7af294d5d9 1669 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1670 mbedtls_printf( "failed\n" );
Simon Cooksey 0:fb7af294d5d9 1671
Simon Cooksey 0:fb7af294d5d9 1672 return( 1 );
Simon Cooksey 0:fb7af294d5d9 1673 }
Simon Cooksey 0:fb7af294d5d9 1674
Simon Cooksey 0:fb7af294d5d9 1675 if( memcmp( rsa_decrypted, rsa_plaintext, len ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1676 {
Simon Cooksey 0:fb7af294d5d9 1677 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1678 mbedtls_printf( "failed\n" );
Simon Cooksey 0:fb7af294d5d9 1679
Simon Cooksey 0:fb7af294d5d9 1680 return( 1 );
Simon Cooksey 0:fb7af294d5d9 1681 }
Simon Cooksey 0:fb7af294d5d9 1682
Simon Cooksey 0:fb7af294d5d9 1683 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1684 mbedtls_printf( "passed\n" );
Simon Cooksey 0:fb7af294d5d9 1685
Simon Cooksey 0:fb7af294d5d9 1686 #if defined(MBEDTLS_SHA1_C)
Simon Cooksey 0:fb7af294d5d9 1687 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1688 mbedtls_printf( " PKCS#1 data sign : " );
Simon Cooksey 0:fb7af294d5d9 1689
Simon Cooksey 0:fb7af294d5d9 1690 mbedtls_sha1( rsa_plaintext, PT_LEN, sha1sum );
Simon Cooksey 0:fb7af294d5d9 1691
Simon Cooksey 0:fb7af294d5d9 1692 if( mbedtls_rsa_pkcs1_sign( &rsa, myrand, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA1, 0,
Simon Cooksey 0:fb7af294d5d9 1693 sha1sum, rsa_ciphertext ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1694 {
Simon Cooksey 0:fb7af294d5d9 1695 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1696 mbedtls_printf( "failed\n" );
Simon Cooksey 0:fb7af294d5d9 1697
Simon Cooksey 0:fb7af294d5d9 1698 return( 1 );
Simon Cooksey 0:fb7af294d5d9 1699 }
Simon Cooksey 0:fb7af294d5d9 1700
Simon Cooksey 0:fb7af294d5d9 1701 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1702 mbedtls_printf( "passed\n PKCS#1 sig. verify: " );
Simon Cooksey 0:fb7af294d5d9 1703
Simon Cooksey 0:fb7af294d5d9 1704 if( mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_SHA1, 0,
Simon Cooksey 0:fb7af294d5d9 1705 sha1sum, rsa_ciphertext ) != 0 )
Simon Cooksey 0:fb7af294d5d9 1706 {
Simon Cooksey 0:fb7af294d5d9 1707 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1708 mbedtls_printf( "failed\n" );
Simon Cooksey 0:fb7af294d5d9 1709
Simon Cooksey 0:fb7af294d5d9 1710 return( 1 );
Simon Cooksey 0:fb7af294d5d9 1711 }
Simon Cooksey 0:fb7af294d5d9 1712
Simon Cooksey 0:fb7af294d5d9 1713 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1714 mbedtls_printf( "passed\n" );
Simon Cooksey 0:fb7af294d5d9 1715 #endif /* MBEDTLS_SHA1_C */
Simon Cooksey 0:fb7af294d5d9 1716
Simon Cooksey 0:fb7af294d5d9 1717 if( verbose != 0 )
Simon Cooksey 0:fb7af294d5d9 1718 mbedtls_printf( "\n" );
Simon Cooksey 0:fb7af294d5d9 1719
Simon Cooksey 0:fb7af294d5d9 1720 cleanup:
Simon Cooksey 0:fb7af294d5d9 1721 mbedtls_rsa_free( &rsa );
Simon Cooksey 0:fb7af294d5d9 1722 #else /* MBEDTLS_PKCS1_V15 */
Simon Cooksey 0:fb7af294d5d9 1723 ((void) verbose);
Simon Cooksey 0:fb7af294d5d9 1724 #endif /* MBEDTLS_PKCS1_V15 */
Simon Cooksey 0:fb7af294d5d9 1725 return( ret );
Simon Cooksey 0:fb7af294d5d9 1726 }
Simon Cooksey 0:fb7af294d5d9 1727
Simon Cooksey 0:fb7af294d5d9 1728 #endif /* MBEDTLS_SELF_TEST */
Simon Cooksey 0:fb7af294d5d9 1729
Simon Cooksey 0:fb7af294d5d9 1730 #endif /* MBEDTLS_RSA_C */