Pinned to some recent date

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

Who changed what in which revision?

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