mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
williequesada
Date:
Tue Jun 04 16:03:38 2019 +0000
Revision:
1:1a219dea6cb5
Parent:
0:cdf462088d13
compartir a Pablo

Who changed what in which revision?

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