mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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