Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * Elliptic curve DSA
nexpaq 0:6c56fb4bc5f0 3 *
nexpaq 0:6c56fb4bc5f0 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
nexpaq 0:6c56fb4bc5f0 5 * SPDX-License-Identifier: Apache-2.0
nexpaq 0:6c56fb4bc5f0 6 *
nexpaq 0:6c56fb4bc5f0 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
nexpaq 0:6c56fb4bc5f0 8 * not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 9 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 10 *
nexpaq 0:6c56fb4bc5f0 11 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 12 *
nexpaq 0:6c56fb4bc5f0 13 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
nexpaq 0:6c56fb4bc5f0 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 16 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 17 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 18 *
nexpaq 0:6c56fb4bc5f0 19 * This file is part of mbed TLS (https://tls.mbed.org)
nexpaq 0:6c56fb4bc5f0 20 */
nexpaq 0:6c56fb4bc5f0 21
nexpaq 0:6c56fb4bc5f0 22 /*
nexpaq 0:6c56fb4bc5f0 23 * References:
nexpaq 0:6c56fb4bc5f0 24 *
nexpaq 0:6c56fb4bc5f0 25 * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
nexpaq 0:6c56fb4bc5f0 26 */
nexpaq 0:6c56fb4bc5f0 27
nexpaq 0:6c56fb4bc5f0 28 #if !defined(MBEDTLS_CONFIG_FILE)
nexpaq 0:6c56fb4bc5f0 29 #include "mbedtls/config.h"
nexpaq 0:6c56fb4bc5f0 30 #else
nexpaq 0:6c56fb4bc5f0 31 #include MBEDTLS_CONFIG_FILE
nexpaq 0:6c56fb4bc5f0 32 #endif
nexpaq 0:6c56fb4bc5f0 33
nexpaq 0:6c56fb4bc5f0 34 #if defined(MBEDTLS_ECDSA_C)
nexpaq 0:6c56fb4bc5f0 35
nexpaq 0:6c56fb4bc5f0 36 #include "mbedtls/ecdsa.h"
nexpaq 0:6c56fb4bc5f0 37 #include "mbedtls/asn1write.h"
nexpaq 0:6c56fb4bc5f0 38
nexpaq 0:6c56fb4bc5f0 39 #include <string.h>
nexpaq 0:6c56fb4bc5f0 40
nexpaq 0:6c56fb4bc5f0 41 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
nexpaq 0:6c56fb4bc5f0 42 #include "mbedtls/hmac_drbg.h"
nexpaq 0:6c56fb4bc5f0 43 #endif
nexpaq 0:6c56fb4bc5f0 44
nexpaq 0:6c56fb4bc5f0 45 /*
nexpaq 0:6c56fb4bc5f0 46 * Derive a suitable integer for group grp from a buffer of length len
nexpaq 0:6c56fb4bc5f0 47 * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
nexpaq 0:6c56fb4bc5f0 48 */
nexpaq 0:6c56fb4bc5f0 49 static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x,
nexpaq 0:6c56fb4bc5f0 50 const unsigned char *buf, size_t blen )
nexpaq 0:6c56fb4bc5f0 51 {
nexpaq 0:6c56fb4bc5f0 52 int ret;
nexpaq 0:6c56fb4bc5f0 53 size_t n_size = ( grp->nbits + 7 ) / 8;
nexpaq 0:6c56fb4bc5f0 54 size_t use_size = blen > n_size ? n_size : blen;
nexpaq 0:6c56fb4bc5f0 55
nexpaq 0:6c56fb4bc5f0 56 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) );
nexpaq 0:6c56fb4bc5f0 57 if( use_size * 8 > grp->nbits )
nexpaq 0:6c56fb4bc5f0 58 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) );
nexpaq 0:6c56fb4bc5f0 59
nexpaq 0:6c56fb4bc5f0 60 /* While at it, reduce modulo N */
nexpaq 0:6c56fb4bc5f0 61 if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 )
nexpaq 0:6c56fb4bc5f0 62 MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) );
nexpaq 0:6c56fb4bc5f0 63
nexpaq 0:6c56fb4bc5f0 64 cleanup:
nexpaq 0:6c56fb4bc5f0 65 return( ret );
nexpaq 0:6c56fb4bc5f0 66 }
nexpaq 0:6c56fb4bc5f0 67
nexpaq 0:6c56fb4bc5f0 68 /*
nexpaq 0:6c56fb4bc5f0 69 * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
nexpaq 0:6c56fb4bc5f0 70 * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
nexpaq 0:6c56fb4bc5f0 71 */
nexpaq 0:6c56fb4bc5f0 72 int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
nexpaq 0:6c56fb4bc5f0 73 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
nexpaq 0:6c56fb4bc5f0 74 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
nexpaq 0:6c56fb4bc5f0 75 {
nexpaq 0:6c56fb4bc5f0 76 int ret, key_tries, sign_tries, blind_tries;
nexpaq 0:6c56fb4bc5f0 77 mbedtls_ecp_point R;
nexpaq 0:6c56fb4bc5f0 78 mbedtls_mpi k, e, t;
nexpaq 0:6c56fb4bc5f0 79
nexpaq 0:6c56fb4bc5f0 80 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
nexpaq 0:6c56fb4bc5f0 81 if( grp->N.p == NULL )
nexpaq 0:6c56fb4bc5f0 82 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 83
nexpaq 0:6c56fb4bc5f0 84 mbedtls_ecp_point_init( &R );
nexpaq 0:6c56fb4bc5f0 85 mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t );
nexpaq 0:6c56fb4bc5f0 86
nexpaq 0:6c56fb4bc5f0 87 sign_tries = 0;
nexpaq 0:6c56fb4bc5f0 88 do
nexpaq 0:6c56fb4bc5f0 89 {
nexpaq 0:6c56fb4bc5f0 90 /*
nexpaq 0:6c56fb4bc5f0 91 * Steps 1-3: generate a suitable ephemeral keypair
nexpaq 0:6c56fb4bc5f0 92 * and set r = xR mod n
nexpaq 0:6c56fb4bc5f0 93 */
nexpaq 0:6c56fb4bc5f0 94 key_tries = 0;
nexpaq 0:6c56fb4bc5f0 95 do
nexpaq 0:6c56fb4bc5f0 96 {
nexpaq 0:6c56fb4bc5f0 97 MBEDTLS_MPI_CHK( mbedtls_ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
nexpaq 0:6c56fb4bc5f0 98 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( r, &R.X, &grp->N ) );
nexpaq 0:6c56fb4bc5f0 99
nexpaq 0:6c56fb4bc5f0 100 if( key_tries++ > 10 )
nexpaq 0:6c56fb4bc5f0 101 {
nexpaq 0:6c56fb4bc5f0 102 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
nexpaq 0:6c56fb4bc5f0 103 goto cleanup;
nexpaq 0:6c56fb4bc5f0 104 }
nexpaq 0:6c56fb4bc5f0 105 }
nexpaq 0:6c56fb4bc5f0 106 while( mbedtls_mpi_cmp_int( r, 0 ) == 0 );
nexpaq 0:6c56fb4bc5f0 107
nexpaq 0:6c56fb4bc5f0 108 /*
nexpaq 0:6c56fb4bc5f0 109 * Step 5: derive MPI from hashed message
nexpaq 0:6c56fb4bc5f0 110 */
nexpaq 0:6c56fb4bc5f0 111 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
nexpaq 0:6c56fb4bc5f0 112
nexpaq 0:6c56fb4bc5f0 113 /*
nexpaq 0:6c56fb4bc5f0 114 * Generate a random value to blind inv_mod in next step,
nexpaq 0:6c56fb4bc5f0 115 * avoiding a potential timing leak.
nexpaq 0:6c56fb4bc5f0 116 */
nexpaq 0:6c56fb4bc5f0 117 blind_tries = 0;
nexpaq 0:6c56fb4bc5f0 118 do
nexpaq 0:6c56fb4bc5f0 119 {
nexpaq 0:6c56fb4bc5f0 120 size_t n_size = ( grp->nbits + 7 ) / 8;
nexpaq 0:6c56fb4bc5f0 121 MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &t, n_size, f_rng, p_rng ) );
nexpaq 0:6c56fb4bc5f0 122 MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &t, 8 * n_size - grp->nbits ) );
nexpaq 0:6c56fb4bc5f0 123
nexpaq 0:6c56fb4bc5f0 124 /* See mbedtls_ecp_gen_keypair() */
nexpaq 0:6c56fb4bc5f0 125 if( ++blind_tries > 30 )
nexpaq 0:6c56fb4bc5f0 126 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
nexpaq 0:6c56fb4bc5f0 127 }
nexpaq 0:6c56fb4bc5f0 128 while( mbedtls_mpi_cmp_int( &t, 1 ) < 0 ||
nexpaq 0:6c56fb4bc5f0 129 mbedtls_mpi_cmp_mpi( &t, &grp->N ) >= 0 );
nexpaq 0:6c56fb4bc5f0 130
nexpaq 0:6c56fb4bc5f0 131 /*
nexpaq 0:6c56fb4bc5f0 132 * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
nexpaq 0:6c56fb4bc5f0 133 */
nexpaq 0:6c56fb4bc5f0 134 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, r, d ) );
nexpaq 0:6c56fb4bc5f0 135 MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
nexpaq 0:6c56fb4bc5f0 136 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
nexpaq 0:6c56fb4bc5f0 137 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &k, &k, &t ) );
nexpaq 0:6c56fb4bc5f0 138 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, &k, &grp->N ) );
nexpaq 0:6c56fb4bc5f0 139 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
nexpaq 0:6c56fb4bc5f0 140 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
nexpaq 0:6c56fb4bc5f0 141
nexpaq 0:6c56fb4bc5f0 142 if( sign_tries++ > 10 )
nexpaq 0:6c56fb4bc5f0 143 {
nexpaq 0:6c56fb4bc5f0 144 ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
nexpaq 0:6c56fb4bc5f0 145 goto cleanup;
nexpaq 0:6c56fb4bc5f0 146 }
nexpaq 0:6c56fb4bc5f0 147 }
nexpaq 0:6c56fb4bc5f0 148 while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
nexpaq 0:6c56fb4bc5f0 149
nexpaq 0:6c56fb4bc5f0 150 cleanup:
nexpaq 0:6c56fb4bc5f0 151 mbedtls_ecp_point_free( &R );
nexpaq 0:6c56fb4bc5f0 152 mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
nexpaq 0:6c56fb4bc5f0 153
nexpaq 0:6c56fb4bc5f0 154 return( ret );
nexpaq 0:6c56fb4bc5f0 155 }
nexpaq 0:6c56fb4bc5f0 156
nexpaq 0:6c56fb4bc5f0 157 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
nexpaq 0:6c56fb4bc5f0 158 /*
nexpaq 0:6c56fb4bc5f0 159 * Deterministic signature wrapper
nexpaq 0:6c56fb4bc5f0 160 */
nexpaq 0:6c56fb4bc5f0 161 int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
nexpaq 0:6c56fb4bc5f0 162 const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
nexpaq 0:6c56fb4bc5f0 163 mbedtls_md_type_t md_alg )
nexpaq 0:6c56fb4bc5f0 164 {
nexpaq 0:6c56fb4bc5f0 165 int ret;
nexpaq 0:6c56fb4bc5f0 166 mbedtls_hmac_drbg_context rng_ctx;
nexpaq 0:6c56fb4bc5f0 167 unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
nexpaq 0:6c56fb4bc5f0 168 size_t grp_len = ( grp->nbits + 7 ) / 8;
nexpaq 0:6c56fb4bc5f0 169 const mbedtls_md_info_t *md_info;
nexpaq 0:6c56fb4bc5f0 170 mbedtls_mpi h;
nexpaq 0:6c56fb4bc5f0 171
nexpaq 0:6c56fb4bc5f0 172 if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
nexpaq 0:6c56fb4bc5f0 173 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 174
nexpaq 0:6c56fb4bc5f0 175 mbedtls_mpi_init( &h );
nexpaq 0:6c56fb4bc5f0 176 mbedtls_hmac_drbg_init( &rng_ctx );
nexpaq 0:6c56fb4bc5f0 177
nexpaq 0:6c56fb4bc5f0 178 /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
nexpaq 0:6c56fb4bc5f0 179 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
nexpaq 0:6c56fb4bc5f0 180 MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
nexpaq 0:6c56fb4bc5f0 181 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
nexpaq 0:6c56fb4bc5f0 182 mbedtls_hmac_drbg_seed_buf( &rng_ctx, md_info, data, 2 * grp_len );
nexpaq 0:6c56fb4bc5f0 183
nexpaq 0:6c56fb4bc5f0 184 ret = mbedtls_ecdsa_sign( grp, r, s, d, buf, blen,
nexpaq 0:6c56fb4bc5f0 185 mbedtls_hmac_drbg_random, &rng_ctx );
nexpaq 0:6c56fb4bc5f0 186
nexpaq 0:6c56fb4bc5f0 187 cleanup:
nexpaq 0:6c56fb4bc5f0 188 mbedtls_hmac_drbg_free( &rng_ctx );
nexpaq 0:6c56fb4bc5f0 189 mbedtls_mpi_free( &h );
nexpaq 0:6c56fb4bc5f0 190
nexpaq 0:6c56fb4bc5f0 191 return( ret );
nexpaq 0:6c56fb4bc5f0 192 }
nexpaq 0:6c56fb4bc5f0 193 #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
nexpaq 0:6c56fb4bc5f0 194
nexpaq 0:6c56fb4bc5f0 195 /*
nexpaq 0:6c56fb4bc5f0 196 * Verify ECDSA signature of hashed message (SEC1 4.1.4)
nexpaq 0:6c56fb4bc5f0 197 * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
nexpaq 0:6c56fb4bc5f0 198 */
nexpaq 0:6c56fb4bc5f0 199 int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
nexpaq 0:6c56fb4bc5f0 200 const unsigned char *buf, size_t blen,
nexpaq 0:6c56fb4bc5f0 201 const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s)
nexpaq 0:6c56fb4bc5f0 202 {
nexpaq 0:6c56fb4bc5f0 203 int ret;
nexpaq 0:6c56fb4bc5f0 204 mbedtls_mpi e, s_inv, u1, u2;
nexpaq 0:6c56fb4bc5f0 205 mbedtls_ecp_point R;
nexpaq 0:6c56fb4bc5f0 206
nexpaq 0:6c56fb4bc5f0 207 mbedtls_ecp_point_init( &R );
nexpaq 0:6c56fb4bc5f0 208 mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv ); mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
nexpaq 0:6c56fb4bc5f0 209
nexpaq 0:6c56fb4bc5f0 210 /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
nexpaq 0:6c56fb4bc5f0 211 if( grp->N.p == NULL )
nexpaq 0:6c56fb4bc5f0 212 return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
nexpaq 0:6c56fb4bc5f0 213
nexpaq 0:6c56fb4bc5f0 214 /*
nexpaq 0:6c56fb4bc5f0 215 * Step 1: make sure r and s are in range 1..n-1
nexpaq 0:6c56fb4bc5f0 216 */
nexpaq 0:6c56fb4bc5f0 217 if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 ||
nexpaq 0:6c56fb4bc5f0 218 mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 )
nexpaq 0:6c56fb4bc5f0 219 {
nexpaq 0:6c56fb4bc5f0 220 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
nexpaq 0:6c56fb4bc5f0 221 goto cleanup;
nexpaq 0:6c56fb4bc5f0 222 }
nexpaq 0:6c56fb4bc5f0 223
nexpaq 0:6c56fb4bc5f0 224 /*
nexpaq 0:6c56fb4bc5f0 225 * Additional precaution: make sure Q is valid
nexpaq 0:6c56fb4bc5f0 226 */
nexpaq 0:6c56fb4bc5f0 227 MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
nexpaq 0:6c56fb4bc5f0 228
nexpaq 0:6c56fb4bc5f0 229 /*
nexpaq 0:6c56fb4bc5f0 230 * Step 3: derive MPI from hashed message
nexpaq 0:6c56fb4bc5f0 231 */
nexpaq 0:6c56fb4bc5f0 232 MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
nexpaq 0:6c56fb4bc5f0 233
nexpaq 0:6c56fb4bc5f0 234 /*
nexpaq 0:6c56fb4bc5f0 235 * Step 4: u1 = e / s mod n, u2 = r / s mod n
nexpaq 0:6c56fb4bc5f0 236 */
nexpaq 0:6c56fb4bc5f0 237 MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) );
nexpaq 0:6c56fb4bc5f0 238
nexpaq 0:6c56fb4bc5f0 239 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u1, &e, &s_inv ) );
nexpaq 0:6c56fb4bc5f0 240 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u1, &u1, &grp->N ) );
nexpaq 0:6c56fb4bc5f0 241
nexpaq 0:6c56fb4bc5f0 242 MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u2, r, &s_inv ) );
nexpaq 0:6c56fb4bc5f0 243 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u2, &u2, &grp->N ) );
nexpaq 0:6c56fb4bc5f0 244
nexpaq 0:6c56fb4bc5f0 245 /*
nexpaq 0:6c56fb4bc5f0 246 * Step 5: R = u1 G + u2 Q
nexpaq 0:6c56fb4bc5f0 247 *
nexpaq 0:6c56fb4bc5f0 248 * Since we're not using any secret data, no need to pass a RNG to
nexpaq 0:6c56fb4bc5f0 249 * mbedtls_ecp_mul() for countermesures.
nexpaq 0:6c56fb4bc5f0 250 */
nexpaq 0:6c56fb4bc5f0 251 MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( grp, &R, &u1, &grp->G, &u2, Q ) );
nexpaq 0:6c56fb4bc5f0 252
nexpaq 0:6c56fb4bc5f0 253 if( mbedtls_ecp_is_zero( &R ) )
nexpaq 0:6c56fb4bc5f0 254 {
nexpaq 0:6c56fb4bc5f0 255 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
nexpaq 0:6c56fb4bc5f0 256 goto cleanup;
nexpaq 0:6c56fb4bc5f0 257 }
nexpaq 0:6c56fb4bc5f0 258
nexpaq 0:6c56fb4bc5f0 259 /*
nexpaq 0:6c56fb4bc5f0 260 * Step 6: convert xR to an integer (no-op)
nexpaq 0:6c56fb4bc5f0 261 * Step 7: reduce xR mod n (gives v)
nexpaq 0:6c56fb4bc5f0 262 */
nexpaq 0:6c56fb4bc5f0 263 MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
nexpaq 0:6c56fb4bc5f0 264
nexpaq 0:6c56fb4bc5f0 265 /*
nexpaq 0:6c56fb4bc5f0 266 * Step 8: check if v (that is, R.X) is equal to r
nexpaq 0:6c56fb4bc5f0 267 */
nexpaq 0:6c56fb4bc5f0 268 if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 )
nexpaq 0:6c56fb4bc5f0 269 {
nexpaq 0:6c56fb4bc5f0 270 ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
nexpaq 0:6c56fb4bc5f0 271 goto cleanup;
nexpaq 0:6c56fb4bc5f0 272 }
nexpaq 0:6c56fb4bc5f0 273
nexpaq 0:6c56fb4bc5f0 274 cleanup:
nexpaq 0:6c56fb4bc5f0 275 mbedtls_ecp_point_free( &R );
nexpaq 0:6c56fb4bc5f0 276 mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv ); mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
nexpaq 0:6c56fb4bc5f0 277
nexpaq 0:6c56fb4bc5f0 278 return( ret );
nexpaq 0:6c56fb4bc5f0 279 }
nexpaq 0:6c56fb4bc5f0 280
nexpaq 0:6c56fb4bc5f0 281 /*
nexpaq 0:6c56fb4bc5f0 282 * Convert a signature (given by context) to ASN.1
nexpaq 0:6c56fb4bc5f0 283 */
nexpaq 0:6c56fb4bc5f0 284 static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
nexpaq 0:6c56fb4bc5f0 285 unsigned char *sig, size_t *slen )
nexpaq 0:6c56fb4bc5f0 286 {
nexpaq 0:6c56fb4bc5f0 287 int ret;
nexpaq 0:6c56fb4bc5f0 288 unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
nexpaq 0:6c56fb4bc5f0 289 unsigned char *p = buf + sizeof( buf );
nexpaq 0:6c56fb4bc5f0 290 size_t len = 0;
nexpaq 0:6c56fb4bc5f0 291
nexpaq 0:6c56fb4bc5f0 292 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
nexpaq 0:6c56fb4bc5f0 293 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
nexpaq 0:6c56fb4bc5f0 294
nexpaq 0:6c56fb4bc5f0 295 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
nexpaq 0:6c56fb4bc5f0 296 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
nexpaq 0:6c56fb4bc5f0 297 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
nexpaq 0:6c56fb4bc5f0 298
nexpaq 0:6c56fb4bc5f0 299 memcpy( sig, p, len );
nexpaq 0:6c56fb4bc5f0 300 *slen = len;
nexpaq 0:6c56fb4bc5f0 301
nexpaq 0:6c56fb4bc5f0 302 return( 0 );
nexpaq 0:6c56fb4bc5f0 303 }
nexpaq 0:6c56fb4bc5f0 304
nexpaq 0:6c56fb4bc5f0 305 /*
nexpaq 0:6c56fb4bc5f0 306 * Compute and write signature
nexpaq 0:6c56fb4bc5f0 307 */
nexpaq 0:6c56fb4bc5f0 308 int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
nexpaq 0:6c56fb4bc5f0 309 const unsigned char *hash, size_t hlen,
nexpaq 0:6c56fb4bc5f0 310 unsigned char *sig, size_t *slen,
nexpaq 0:6c56fb4bc5f0 311 int (*f_rng)(void *, unsigned char *, size_t),
nexpaq 0:6c56fb4bc5f0 312 void *p_rng )
nexpaq 0:6c56fb4bc5f0 313 {
nexpaq 0:6c56fb4bc5f0 314 int ret;
nexpaq 0:6c56fb4bc5f0 315 mbedtls_mpi r, s;
nexpaq 0:6c56fb4bc5f0 316
nexpaq 0:6c56fb4bc5f0 317 mbedtls_mpi_init( &r );
nexpaq 0:6c56fb4bc5f0 318 mbedtls_mpi_init( &s );
nexpaq 0:6c56fb4bc5f0 319
nexpaq 0:6c56fb4bc5f0 320 #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
nexpaq 0:6c56fb4bc5f0 321 (void) f_rng;
nexpaq 0:6c56fb4bc5f0 322 (void) p_rng;
nexpaq 0:6c56fb4bc5f0 323
nexpaq 0:6c56fb4bc5f0 324 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ctx->grp, &r, &s, &ctx->d,
nexpaq 0:6c56fb4bc5f0 325 hash, hlen, md_alg ) );
nexpaq 0:6c56fb4bc5f0 326 #else
nexpaq 0:6c56fb4bc5f0 327 (void) md_alg;
nexpaq 0:6c56fb4bc5f0 328
nexpaq 0:6c56fb4bc5f0 329 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
nexpaq 0:6c56fb4bc5f0 330 hash, hlen, f_rng, p_rng ) );
nexpaq 0:6c56fb4bc5f0 331 #endif
nexpaq 0:6c56fb4bc5f0 332
nexpaq 0:6c56fb4bc5f0 333 MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
nexpaq 0:6c56fb4bc5f0 334
nexpaq 0:6c56fb4bc5f0 335 cleanup:
nexpaq 0:6c56fb4bc5f0 336 mbedtls_mpi_free( &r );
nexpaq 0:6c56fb4bc5f0 337 mbedtls_mpi_free( &s );
nexpaq 0:6c56fb4bc5f0 338
nexpaq 0:6c56fb4bc5f0 339 return( ret );
nexpaq 0:6c56fb4bc5f0 340 }
nexpaq 0:6c56fb4bc5f0 341
nexpaq 0:6c56fb4bc5f0 342 #if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \
nexpaq 0:6c56fb4bc5f0 343 defined(MBEDTLS_ECDSA_DETERMINISTIC)
nexpaq 0:6c56fb4bc5f0 344 int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
nexpaq 0:6c56fb4bc5f0 345 const unsigned char *hash, size_t hlen,
nexpaq 0:6c56fb4bc5f0 346 unsigned char *sig, size_t *slen,
nexpaq 0:6c56fb4bc5f0 347 mbedtls_md_type_t md_alg )
nexpaq 0:6c56fb4bc5f0 348 {
nexpaq 0:6c56fb4bc5f0 349 return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
nexpaq 0:6c56fb4bc5f0 350 NULL, NULL ) );
nexpaq 0:6c56fb4bc5f0 351 }
nexpaq 0:6c56fb4bc5f0 352 #endif
nexpaq 0:6c56fb4bc5f0 353
nexpaq 0:6c56fb4bc5f0 354 /*
nexpaq 0:6c56fb4bc5f0 355 * Read and check signature
nexpaq 0:6c56fb4bc5f0 356 */
nexpaq 0:6c56fb4bc5f0 357 int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
nexpaq 0:6c56fb4bc5f0 358 const unsigned char *hash, size_t hlen,
nexpaq 0:6c56fb4bc5f0 359 const unsigned char *sig, size_t slen )
nexpaq 0:6c56fb4bc5f0 360 {
nexpaq 0:6c56fb4bc5f0 361 int ret;
nexpaq 0:6c56fb4bc5f0 362 unsigned char *p = (unsigned char *) sig;
nexpaq 0:6c56fb4bc5f0 363 const unsigned char *end = sig + slen;
nexpaq 0:6c56fb4bc5f0 364 size_t len;
nexpaq 0:6c56fb4bc5f0 365 mbedtls_mpi r, s;
nexpaq 0:6c56fb4bc5f0 366
nexpaq 0:6c56fb4bc5f0 367 mbedtls_mpi_init( &r );
nexpaq 0:6c56fb4bc5f0 368 mbedtls_mpi_init( &s );
nexpaq 0:6c56fb4bc5f0 369
nexpaq 0:6c56fb4bc5f0 370 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
nexpaq 0:6c56fb4bc5f0 371 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 372 {
nexpaq 0:6c56fb4bc5f0 373 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
nexpaq 0:6c56fb4bc5f0 374 goto cleanup;
nexpaq 0:6c56fb4bc5f0 375 }
nexpaq 0:6c56fb4bc5f0 376
nexpaq 0:6c56fb4bc5f0 377 if( p + len != end )
nexpaq 0:6c56fb4bc5f0 378 {
nexpaq 0:6c56fb4bc5f0 379 ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
nexpaq 0:6c56fb4bc5f0 380 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
nexpaq 0:6c56fb4bc5f0 381 goto cleanup;
nexpaq 0:6c56fb4bc5f0 382 }
nexpaq 0:6c56fb4bc5f0 383
nexpaq 0:6c56fb4bc5f0 384 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
nexpaq 0:6c56fb4bc5f0 385 ( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 386 {
nexpaq 0:6c56fb4bc5f0 387 ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
nexpaq 0:6c56fb4bc5f0 388 goto cleanup;
nexpaq 0:6c56fb4bc5f0 389 }
nexpaq 0:6c56fb4bc5f0 390
nexpaq 0:6c56fb4bc5f0 391 if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen,
nexpaq 0:6c56fb4bc5f0 392 &ctx->Q, &r, &s ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 393 goto cleanup;
nexpaq 0:6c56fb4bc5f0 394
nexpaq 0:6c56fb4bc5f0 395 if( p != end )
nexpaq 0:6c56fb4bc5f0 396 ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
nexpaq 0:6c56fb4bc5f0 397
nexpaq 0:6c56fb4bc5f0 398 cleanup:
nexpaq 0:6c56fb4bc5f0 399 mbedtls_mpi_free( &r );
nexpaq 0:6c56fb4bc5f0 400 mbedtls_mpi_free( &s );
nexpaq 0:6c56fb4bc5f0 401
nexpaq 0:6c56fb4bc5f0 402 return( ret );
nexpaq 0:6c56fb4bc5f0 403 }
nexpaq 0:6c56fb4bc5f0 404
nexpaq 0:6c56fb4bc5f0 405 /*
nexpaq 0:6c56fb4bc5f0 406 * Generate key pair
nexpaq 0:6c56fb4bc5f0 407 */
nexpaq 0:6c56fb4bc5f0 408 int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
nexpaq 0:6c56fb4bc5f0 409 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
nexpaq 0:6c56fb4bc5f0 410 {
nexpaq 0:6c56fb4bc5f0 411 return( mbedtls_ecp_group_load( &ctx->grp, gid ) ||
nexpaq 0:6c56fb4bc5f0 412 mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
nexpaq 0:6c56fb4bc5f0 413 }
nexpaq 0:6c56fb4bc5f0 414
nexpaq 0:6c56fb4bc5f0 415 /*
nexpaq 0:6c56fb4bc5f0 416 * Set context from an mbedtls_ecp_keypair
nexpaq 0:6c56fb4bc5f0 417 */
nexpaq 0:6c56fb4bc5f0 418 int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
nexpaq 0:6c56fb4bc5f0 419 {
nexpaq 0:6c56fb4bc5f0 420 int ret;
nexpaq 0:6c56fb4bc5f0 421
nexpaq 0:6c56fb4bc5f0 422 if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
nexpaq 0:6c56fb4bc5f0 423 ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
nexpaq 0:6c56fb4bc5f0 424 ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
nexpaq 0:6c56fb4bc5f0 425 {
nexpaq 0:6c56fb4bc5f0 426 mbedtls_ecdsa_free( ctx );
nexpaq 0:6c56fb4bc5f0 427 }
nexpaq 0:6c56fb4bc5f0 428
nexpaq 0:6c56fb4bc5f0 429 return( ret );
nexpaq 0:6c56fb4bc5f0 430 }
nexpaq 0:6c56fb4bc5f0 431
nexpaq 0:6c56fb4bc5f0 432 /*
nexpaq 0:6c56fb4bc5f0 433 * Initialize context
nexpaq 0:6c56fb4bc5f0 434 */
nexpaq 0:6c56fb4bc5f0 435 void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
nexpaq 0:6c56fb4bc5f0 436 {
nexpaq 0:6c56fb4bc5f0 437 mbedtls_ecp_keypair_init( ctx );
nexpaq 0:6c56fb4bc5f0 438 }
nexpaq 0:6c56fb4bc5f0 439
nexpaq 0:6c56fb4bc5f0 440 /*
nexpaq 0:6c56fb4bc5f0 441 * Free context
nexpaq 0:6c56fb4bc5f0 442 */
nexpaq 0:6c56fb4bc5f0 443 void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
nexpaq 0:6c56fb4bc5f0 444 {
nexpaq 0:6c56fb4bc5f0 445 mbedtls_ecp_keypair_free( ctx );
nexpaq 0:6c56fb4bc5f0 446 }
nexpaq 0:6c56fb4bc5f0 447
nexpaq 0:6c56fb4bc5f0 448 #endif /* MBEDTLS_ECDSA_C */