2015-04-08 17:48:22 +00:00
|
|
|
/*
|
|
|
|
* Elliptic curve DSA
|
|
|
|
*
|
2015-10-04 17:08:38 +00:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2017-02-26 11:55:27 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0
|
2015-04-08 17:48:22 +00:00
|
|
|
*
|
2017-02-26 11:55:27 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2015-04-08 17:48:22 +00:00
|
|
|
*
|
2017-02-26 11:55:27 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2015-04-08 17:48:22 +00:00
|
|
|
*
|
2017-02-26 11:55:27 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2015-04-08 17:48:22 +00:00
|
|
|
*
|
2015-10-04 17:08:38 +00:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2015-04-08 17:48:22 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* References:
|
|
|
|
*
|
|
|
|
* SEC1 http://www.secg.org/index.php?action=secg,docs_secg
|
|
|
|
*/
|
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
|
|
#include "mbedtls/config.h"
|
2015-04-08 17:48:22 +00:00
|
|
|
#else
|
2015-10-04 17:08:38 +00:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2015-04-08 17:48:22 +00:00
|
|
|
#endif
|
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
#if defined(MBEDTLS_ECDSA_C)
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
#include "mbedtls/ecdsa.h"
|
|
|
|
#include "mbedtls/asn1write.h"
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
#include <string.h>
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
|
|
|
#include "mbedtls/hmac_drbg.h"
|
|
|
|
#endif
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Derive a suitable integer for group grp from a buffer of length len
|
|
|
|
* SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
static int derive_mpi( const mbedtls_ecp_group *grp, mbedtls_mpi *x,
|
2015-04-08 17:48:22 +00:00
|
|
|
const unsigned char *buf, size_t blen )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n_size = ( grp->nbits + 7 ) / 8;
|
|
|
|
size_t use_size = blen > n_size ? n_size : blen;
|
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( x, buf, use_size ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
if( use_size * 8 > grp->nbits )
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( x, use_size * 8 - grp->nbits ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/* While at it, reduce modulo N */
|
2015-10-04 17:08:38 +00:00
|
|
|
if( mbedtls_mpi_cmp_mpi( x, &grp->N ) >= 0 )
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_sub_mpi( x, x, &grp->N ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2018-10-31 15:00:34 +00:00
|
|
|
#if !defined(MBEDTLS_ECDSA_SIGN_ALT)
|
2015-04-08 17:48:22 +00:00
|
|
|
/*
|
|
|
|
* Compute ECDSA signature of a hashed message (SEC1 4.1.3)
|
|
|
|
* Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
int mbedtls_ecdsa_sign( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
|
|
|
|
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
2015-04-08 17:48:22 +00:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
|
|
|
{
|
|
|
|
int ret, key_tries, sign_tries, blind_tries;
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_ecp_point R;
|
|
|
|
mbedtls_mpi k, e, t;
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
|
|
|
|
if( grp->N.p == NULL )
|
2015-10-04 17:08:38 +00:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2018-10-31 15:00:34 +00:00
|
|
|
/* Make sure d is in range 1..n-1 */
|
|
|
|
if( mbedtls_mpi_cmp_int( d, 1 ) < 0 || mbedtls_mpi_cmp_mpi( d, &grp->N ) >= 0 )
|
|
|
|
return( MBEDTLS_ERR_ECP_INVALID_KEY );
|
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_ecp_point_init( &R );
|
|
|
|
mbedtls_mpi_init( &k ); mbedtls_mpi_init( &e ); mbedtls_mpi_init( &t );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
sign_tries = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Steps 1-3: generate a suitable ephemeral keypair
|
|
|
|
* and set r = xR mod n
|
|
|
|
*/
|
|
|
|
key_tries = 0;
|
|
|
|
do
|
|
|
|
{
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_gen_keypair( grp, &k, &R, f_rng, p_rng ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( r, &R.X, &grp->N ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
if( key_tries++ > 10 )
|
|
|
|
{
|
2015-10-04 17:08:38 +00:00
|
|
|
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
|
2015-04-08 17:48:22 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 17:08:38 +00:00
|
|
|
while( mbedtls_mpi_cmp_int( r, 0 ) == 0 );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 5: derive MPI from hashed message
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a random value to blind inv_mod in next step,
|
|
|
|
* avoiding a potential timing leak.
|
|
|
|
*/
|
|
|
|
blind_tries = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
size_t n_size = ( grp->nbits + 7 ) / 8;
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &t, n_size, f_rng, p_rng ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &t, 8 * n_size - grp->nbits ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
/* See mbedtls_ecp_gen_keypair() */
|
2015-04-08 17:48:22 +00:00
|
|
|
if( ++blind_tries > 30 )
|
2015-10-04 17:08:38 +00:00
|
|
|
return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
|
2015-04-08 17:48:22 +00:00
|
|
|
}
|
2015-10-04 17:08:38 +00:00
|
|
|
while( mbedtls_mpi_cmp_int( &t, 1 ) < 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi( &t, &grp->N ) >= 0 );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, r, d ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi( &e, &e, s ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &e, &e, &t ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &k, &k, &t ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( s, &k, &grp->N ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( s, s, &e ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( s, s, &grp->N ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
if( sign_tries++ > 10 )
|
|
|
|
{
|
2015-10-04 17:08:38 +00:00
|
|
|
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
|
2015-04-08 17:48:22 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2015-10-04 17:08:38 +00:00
|
|
|
while( mbedtls_mpi_cmp_int( s, 0 ) == 0 );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
cleanup:
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_ecp_point_free( &R );
|
|
|
|
mbedtls_mpi_free( &k ); mbedtls_mpi_free( &e ); mbedtls_mpi_free( &t );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2018-10-31 15:00:34 +00:00
|
|
|
#endif /* MBEDTLS_ECDSA_SIGN_ALT */
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
2015-04-08 17:48:22 +00:00
|
|
|
/*
|
|
|
|
* Deterministic signature wrapper
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
int mbedtls_ecdsa_sign_det( mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
|
|
|
|
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
|
|
|
mbedtls_md_type_t md_alg )
|
2015-04-08 17:48:22 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_hmac_drbg_context rng_ctx;
|
|
|
|
unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
|
2015-04-08 17:48:22 +00:00
|
|
|
size_t grp_len = ( grp->nbits + 7 ) / 8;
|
2015-10-04 17:08:38 +00:00
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_mpi h;
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
|
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_mpi_init( &h );
|
|
|
|
mbedtls_hmac_drbg_init( &rng_ctx );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/* Use private key and message hash (reduced) to initialize HMAC_DRBG */
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( d, data, grp_len ) );
|
|
|
|
MBEDTLS_MPI_CHK( derive_mpi( grp, &h, buf, blen ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &h, data + grp_len, grp_len ) );
|
|
|
|
mbedtls_hmac_drbg_seed_buf( &rng_ctx, md_info, data, 2 * grp_len );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
ret = mbedtls_ecdsa_sign( grp, r, s, d, buf, blen,
|
|
|
|
mbedtls_hmac_drbg_random, &rng_ctx );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
cleanup:
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_hmac_drbg_free( &rng_ctx );
|
|
|
|
mbedtls_mpi_free( &h );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2015-10-04 17:08:38 +00:00
|
|
|
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2018-10-31 15:00:34 +00:00
|
|
|
#if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
|
2015-04-08 17:48:22 +00:00
|
|
|
/*
|
|
|
|
* Verify ECDSA signature of hashed message (SEC1 4.1.4)
|
|
|
|
* Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
int mbedtls_ecdsa_verify( mbedtls_ecp_group *grp,
|
2015-04-08 17:48:22 +00:00
|
|
|
const unsigned char *buf, size_t blen,
|
2015-10-04 17:08:38 +00:00
|
|
|
const mbedtls_ecp_point *Q, const mbedtls_mpi *r, const mbedtls_mpi *s)
|
2015-04-08 17:48:22 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_mpi e, s_inv, u1, u2;
|
|
|
|
mbedtls_ecp_point R;
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_ecp_point_init( &R );
|
|
|
|
mbedtls_mpi_init( &e ); mbedtls_mpi_init( &s_inv ); mbedtls_mpi_init( &u1 ); mbedtls_mpi_init( &u2 );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
|
|
|
|
if( grp->N.p == NULL )
|
2015-10-04 17:08:38 +00:00
|
|
|
return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 1: make sure r and s are in range 1..n-1
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
if( mbedtls_mpi_cmp_int( r, 1 ) < 0 || mbedtls_mpi_cmp_mpi( r, &grp->N ) >= 0 ||
|
|
|
|
mbedtls_mpi_cmp_int( s, 1 ) < 0 || mbedtls_mpi_cmp_mpi( s, &grp->N ) >= 0 )
|
2015-04-08 17:48:22 +00:00
|
|
|
{
|
2015-10-04 17:08:38 +00:00
|
|
|
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
|
2015-04-08 17:48:22 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Additional precaution: make sure Q is valid
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 3: derive MPI from hashed message
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( derive_mpi( grp, &e, buf, blen ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 4: u1 = e / s mod n, u2 = r / s mod n
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &s_inv, s, &grp->N ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u1, &e, &s_inv ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u1, &u1, &grp->N ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &u2, r, &s_inv ) );
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &u2, &u2, &grp->N ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 5: R = u1 G + u2 Q
|
|
|
|
*
|
|
|
|
* Since we're not using any secret data, no need to pass a RNG to
|
2015-10-04 17:08:38 +00:00
|
|
|
* mbedtls_ecp_mul() for countermesures.
|
2015-04-08 17:48:22 +00:00
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_muladd( grp, &R, &u1, &grp->G, &u2, Q ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
if( mbedtls_ecp_is_zero( &R ) )
|
2015-04-08 17:48:22 +00:00
|
|
|
{
|
2015-10-04 17:08:38 +00:00
|
|
|
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
|
2015-04-08 17:48:22 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 6: convert xR to an integer (no-op)
|
|
|
|
* Step 7: reduce xR mod n (gives v)
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &R.X, &R.X, &grp->N ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 8: check if v (that is, R.X) is equal to r
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
if( mbedtls_mpi_cmp_mpi( &R.X, r ) != 0 )
|
2015-04-08 17:48:22 +00:00
|
|
|
{
|
2015-10-04 17:08:38 +00:00
|
|
|
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
|
2015-04-08 17:48:22 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_ecp_point_free( &R );
|
|
|
|
mbedtls_mpi_free( &e ); mbedtls_mpi_free( &s_inv ); mbedtls_mpi_free( &u1 ); mbedtls_mpi_free( &u2 );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2018-10-31 15:00:34 +00:00
|
|
|
#endif /* MBEDTLS_ECDSA_VERIFY_ALT */
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert a signature (given by context) to ASN.1
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
static int ecdsa_signature_to_asn1( const mbedtls_mpi *r, const mbedtls_mpi *s,
|
2015-04-08 17:48:22 +00:00
|
|
|
unsigned char *sig, size_t *slen )
|
|
|
|
{
|
|
|
|
int ret;
|
2015-10-04 17:08:38 +00:00
|
|
|
unsigned char buf[MBEDTLS_ECDSA_MAX_LEN];
|
2015-04-08 17:48:22 +00:00
|
|
|
unsigned char *p = buf + sizeof( buf );
|
|
|
|
size_t len = 0;
|
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, s ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &p, buf, r ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &p, buf, len ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &p, buf,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
memcpy( sig, p, len );
|
|
|
|
*slen = len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute and write signature
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
int mbedtls_ecdsa_write_signature( mbedtls_ecdsa_context *ctx, mbedtls_md_type_t md_alg,
|
2015-04-08 17:48:22 +00:00
|
|
|
const unsigned char *hash, size_t hlen,
|
|
|
|
unsigned char *sig, size_t *slen,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
|
|
|
{
|
|
|
|
int ret;
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_mpi r, s;
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_mpi_init( &r );
|
|
|
|
mbedtls_mpi_init( &s );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
|
|
|
(void) f_rng;
|
|
|
|
(void) p_rng;
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ctx->grp, &r, &s, &ctx->d,
|
|
|
|
hash, hlen, md_alg ) );
|
|
|
|
#else
|
|
|
|
(void) md_alg;
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ctx->grp, &r, &s, &ctx->d,
|
|
|
|
hash, hlen, f_rng, p_rng ) );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK( ecdsa_signature_to_asn1( &r, &s, sig, slen ) );
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
mbedtls_mpi_free( &r );
|
|
|
|
mbedtls_mpi_free( &s );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
return( ret );
|
2015-04-08 17:48:22 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
#if ! defined(MBEDTLS_DEPRECATED_REMOVED) && \
|
|
|
|
defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
|
|
|
int mbedtls_ecdsa_write_signature_det( mbedtls_ecdsa_context *ctx,
|
2015-04-08 17:48:22 +00:00
|
|
|
const unsigned char *hash, size_t hlen,
|
|
|
|
unsigned char *sig, size_t *slen,
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_md_type_t md_alg )
|
2015-04-08 17:48:22 +00:00
|
|
|
{
|
2015-10-04 17:08:38 +00:00
|
|
|
return( mbedtls_ecdsa_write_signature( ctx, md_alg, hash, hlen, sig, slen,
|
|
|
|
NULL, NULL ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
}
|
2015-10-04 17:08:38 +00:00
|
|
|
#endif
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read and check signature
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
int mbedtls_ecdsa_read_signature( mbedtls_ecdsa_context *ctx,
|
2015-04-08 17:48:22 +00:00
|
|
|
const unsigned char *hash, size_t hlen,
|
|
|
|
const unsigned char *sig, size_t slen )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *p = (unsigned char *) sig;
|
|
|
|
const unsigned char *end = sig + slen;
|
|
|
|
size_t len;
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_mpi r, s;
|
|
|
|
|
|
|
|
mbedtls_mpi_init( &r );
|
|
|
|
mbedtls_mpi_init( &s );
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2015-04-08 17:48:22 +00:00
|
|
|
{
|
2015-10-04 17:08:38 +00:00
|
|
|
ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
goto cleanup;
|
2015-04-08 17:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( p + len != end )
|
2015-10-04 17:08:38 +00:00
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
if( ( ret = mbedtls_asn1_get_mpi( &p, end, &r ) ) != 0 ||
|
|
|
|
( ret = mbedtls_asn1_get_mpi( &p, end, &s ) ) != 0 )
|
|
|
|
{
|
|
|
|
ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
if( ( ret = mbedtls_ecdsa_verify( &ctx->grp, hash, hlen,
|
|
|
|
&ctx->Q, &r, &s ) ) != 0 )
|
|
|
|
goto cleanup;
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2018-10-31 15:00:34 +00:00
|
|
|
/* At this point we know that the buffer starts with a valid signature.
|
|
|
|
* Return 0 if the buffer just contains the signature, and a specific
|
|
|
|
* error code if the valid signature is followed by more data. */
|
2015-04-08 17:48:22 +00:00
|
|
|
if( p != end )
|
2015-10-04 17:08:38 +00:00
|
|
|
ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
|
2015-04-08 17:48:22 +00:00
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
cleanup:
|
|
|
|
mbedtls_mpi_free( &r );
|
|
|
|
mbedtls_mpi_free( &s );
|
|
|
|
|
|
|
|
return( ret );
|
2015-04-08 17:48:22 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 15:00:34 +00:00
|
|
|
#if !defined(MBEDTLS_ECDSA_GENKEY_ALT)
|
2015-04-08 17:48:22 +00:00
|
|
|
/*
|
|
|
|
* Generate key pair
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
int mbedtls_ecdsa_genkey( mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
|
2015-04-08 17:48:22 +00:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
|
|
|
|
{
|
2015-10-04 17:08:38 +00:00
|
|
|
return( mbedtls_ecp_group_load( &ctx->grp, gid ) ||
|
|
|
|
mbedtls_ecp_gen_keypair( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) );
|
2015-04-08 17:48:22 +00:00
|
|
|
}
|
2018-10-31 15:00:34 +00:00
|
|
|
#endif /* MBEDTLS_ECDSA_GENKEY_ALT */
|
2015-04-08 17:48:22 +00:00
|
|
|
|
|
|
|
/*
|
2015-10-04 17:08:38 +00:00
|
|
|
* Set context from an mbedtls_ecp_keypair
|
2015-04-08 17:48:22 +00:00
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
int mbedtls_ecdsa_from_keypair( mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key )
|
2015-04-08 17:48:22 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 ||
|
|
|
|
( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ||
|
|
|
|
( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 )
|
2015-04-08 17:48:22 +00:00
|
|
|
{
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_ecdsa_free( ctx );
|
2015-04-08 17:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize context
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
void mbedtls_ecdsa_init( mbedtls_ecdsa_context *ctx )
|
2015-04-08 17:48:22 +00:00
|
|
|
{
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_ecp_keypair_init( ctx );
|
2015-04-08 17:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free context
|
|
|
|
*/
|
2015-10-04 17:08:38 +00:00
|
|
|
void mbedtls_ecdsa_free( mbedtls_ecdsa_context *ctx )
|
2015-04-08 17:48:22 +00:00
|
|
|
{
|
2015-10-04 17:08:38 +00:00
|
|
|
mbedtls_ecp_keypair_free( ctx );
|
2015-04-08 17:48:22 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 17:08:38 +00:00
|
|
|
#endif /* MBEDTLS_ECDSA_C */
|