2013-01-26 16:33:44 +01:00
|
|
|
/*
|
|
|
|
* Elliptic curve DSA
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-01-26 16:33:44 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2013-01-26 16:33:44 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-01-26 16:33:44 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2013-01-26 16:33:44 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* References:
|
|
|
|
*
|
2023-04-19 04:59:15 +02:00
|
|
|
* SEC1 https://www.secg.org/sec1-v2.pdf
|
2013-01-26 16:33:44 +01:00
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2013-01-26 16:33:44 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_C)
|
2013-01-26 16:33:44 +01:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ecdsa.h"
|
|
|
|
#include "mbedtls/asn1write.h"
|
2013-01-26 16:33:44 +01:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/hmac_drbg.h"
|
2014-01-27 14:24:03 +01:00
|
|
|
#endif
|
2014-01-07 16:17:53 +01:00
|
|
|
|
2017-04-21 11:33:57 +02:00
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
2018-12-14 17:43:29 +01:00
|
|
|
#include "mbedtls/platform_util.h"
|
2019-11-22 14:21:35 +01:00
|
|
|
#include "mbedtls/error.h"
|
2018-12-14 17:43:29 +01:00
|
|
|
|
2017-04-21 11:33:57 +02:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2017-04-21 12:36:59 +02:00
|
|
|
|
2017-04-21 11:33:57 +02:00
|
|
|
/*
|
2017-09-07 11:11:39 +02:00
|
|
|
* Sub-context for ecdsa_verify()
|
2017-04-21 11:33:57 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
struct mbedtls_ecdsa_restart_ver {
|
2017-04-21 12:36:59 +02:00
|
|
|
mbedtls_mpi u1, u2; /* intermediate values */
|
|
|
|
enum { /* what to do next? */
|
|
|
|
ecdsa_ver_init = 0, /* getting started */
|
|
|
|
ecdsa_ver_muladd, /* muladd step */
|
|
|
|
} state;
|
2017-04-21 11:33:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Init verify restart sub-context
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ecdsa_restart_ver_init(mbedtls_ecdsa_restart_ver_ctx *ctx)
|
2017-04-21 11:33:57 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&ctx->u1);
|
|
|
|
mbedtls_mpi_init(&ctx->u2);
|
2017-08-23 16:55:59 +02:00
|
|
|
ctx->state = ecdsa_ver_init;
|
2017-04-21 11:33:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the components of a verify restart sub-context
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ecdsa_restart_ver_free(mbedtls_ecdsa_restart_ver_ctx *ctx)
|
2017-04-21 11:33:57 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx == NULL) {
|
2017-04-21 11:33:57 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-04-21 11:33:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&ctx->u1);
|
|
|
|
mbedtls_mpi_free(&ctx->u2);
|
2017-04-21 12:36:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ecdsa_restart_ver_init(ctx);
|
2017-04-21 11:33:57 +02:00
|
|
|
}
|
|
|
|
|
2017-04-25 11:33:10 +02:00
|
|
|
/*
|
2017-09-07 11:11:39 +02:00
|
|
|
* Sub-context for ecdsa_sign()
|
2017-04-25 11:33:10 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
struct mbedtls_ecdsa_restart_sig {
|
2017-04-25 13:44:19 +02:00
|
|
|
int sign_tries;
|
|
|
|
int key_tries;
|
|
|
|
mbedtls_mpi k; /* per-signature random */
|
|
|
|
mbedtls_mpi r; /* r value */
|
2017-04-25 11:33:10 +02:00
|
|
|
enum { /* what to do next? */
|
|
|
|
ecdsa_sig_init = 0, /* getting started */
|
2017-04-25 13:44:19 +02:00
|
|
|
ecdsa_sig_mul, /* doing ecp_mul() */
|
|
|
|
ecdsa_sig_modn, /* mod N computations */
|
2017-04-25 11:33:10 +02:00
|
|
|
} state;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Init verify sign sub-context
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ecdsa_restart_sig_init(mbedtls_ecdsa_restart_sig_ctx *ctx)
|
2017-04-25 11:33:10 +02:00
|
|
|
{
|
2017-08-23 16:55:59 +02:00
|
|
|
ctx->sign_tries = 0;
|
|
|
|
ctx->key_tries = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&ctx->k);
|
|
|
|
mbedtls_mpi_init(&ctx->r);
|
2017-08-23 16:55:59 +02:00
|
|
|
ctx->state = ecdsa_sig_init;
|
2017-04-25 11:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the components of a sign restart sub-context
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ecdsa_restart_sig_free(mbedtls_ecdsa_restart_sig_ctx *ctx)
|
2017-04-25 11:33:10 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx == NULL) {
|
2017-04-25 11:33:10 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-04-25 11:33:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&ctx->k);
|
|
|
|
mbedtls_mpi_free(&ctx->r);
|
2017-04-25 11:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
|
|
|
/*
|
2017-09-07 11:11:39 +02:00
|
|
|
* Sub-context for ecdsa_sign_det()
|
2017-04-25 11:33:10 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
struct mbedtls_ecdsa_restart_det {
|
2017-04-25 13:44:19 +02:00
|
|
|
mbedtls_hmac_drbg_context rng_ctx; /* DRBG state */
|
2017-04-25 11:33:10 +02:00
|
|
|
enum { /* what to do next? */
|
2017-04-25 13:44:19 +02:00
|
|
|
ecdsa_det_init = 0, /* getting started */
|
|
|
|
ecdsa_det_sign, /* make signature */
|
2017-04-25 11:33:10 +02:00
|
|
|
} state;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Init verify sign_det sub-context
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ecdsa_restart_det_init(mbedtls_ecdsa_restart_det_ctx *ctx)
|
2017-04-25 11:33:10 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_hmac_drbg_init(&ctx->rng_ctx);
|
2017-08-23 16:55:59 +02:00
|
|
|
ctx->state = ecdsa_det_init;
|
2017-04-25 11:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the components of a sign_det restart sub-context
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ecdsa_restart_det_free(mbedtls_ecdsa_restart_det_ctx *ctx)
|
2017-04-25 11:33:10 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx == NULL) {
|
2017-04-25 11:33:10 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-04-25 11:33:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_hmac_drbg_free(&ctx->rng_ctx);
|
2017-04-25 13:44:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ecdsa_restart_det_init(ctx);
|
2017-04-25 11:33:10 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define ECDSA_RS_ECP (rs_ctx == NULL ? NULL : &rs_ctx->ecp)
|
2017-04-21 11:33:57 +02:00
|
|
|
|
2017-04-21 12:36:59 +02:00
|
|
|
/* Utility macro for checking and updating ops budget */
|
2023-01-11 14:50:10 +01:00
|
|
|
#define ECDSA_BUDGET(ops) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_check_budget(grp, ECDSA_RS_ECP, ops));
|
2017-04-21 12:36:59 +02:00
|
|
|
|
2017-08-23 17:58:40 +02:00
|
|
|
/* Call this when entering a function that needs its own sub-context */
|
2023-01-11 14:50:10 +01:00
|
|
|
#define ECDSA_RS_ENTER(SUB) do { \
|
|
|
|
/* reset ops count for this call if top-level */ \
|
|
|
|
if (rs_ctx != NULL && rs_ctx->ecp.depth++ == 0) \
|
2017-04-25 11:33:10 +02:00
|
|
|
rs_ctx->ecp.ops_done = 0; \
|
|
|
|
\
|
2023-01-11 14:50:10 +01:00
|
|
|
/* set up our own sub-context if needed */ \
|
|
|
|
if (mbedtls_ecp_restart_is_enabled() && \
|
|
|
|
rs_ctx != NULL && rs_ctx->SUB == NULL) \
|
|
|
|
{ \
|
|
|
|
rs_ctx->SUB = mbedtls_calloc(1, sizeof(*rs_ctx->SUB)); \
|
|
|
|
if (rs_ctx->SUB == NULL) \
|
|
|
|
return MBEDTLS_ERR_ECP_ALLOC_FAILED; \
|
|
|
|
\
|
|
|
|
ecdsa_restart_## SUB ##_init(rs_ctx->SUB); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2017-04-25 11:33:10 +02:00
|
|
|
|
2017-08-23 17:58:40 +02:00
|
|
|
/* Call this when leaving a function that needs its own sub-context */
|
2023-01-11 14:50:10 +01:00
|
|
|
#define ECDSA_RS_LEAVE(SUB) do { \
|
|
|
|
/* clear our sub-context when not in progress (done or error) */ \
|
|
|
|
if (rs_ctx != NULL && rs_ctx->SUB != NULL && \
|
|
|
|
ret != MBEDTLS_ERR_ECP_IN_PROGRESS) \
|
|
|
|
{ \
|
|
|
|
ecdsa_restart_## SUB ##_free(rs_ctx->SUB); \
|
|
|
|
mbedtls_free(rs_ctx->SUB); \
|
|
|
|
rs_ctx->SUB = NULL; \
|
|
|
|
} \
|
2017-04-25 11:33:10 +02:00
|
|
|
\
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rs_ctx != NULL) \
|
2017-04-25 11:33:10 +02:00
|
|
|
rs_ctx->ecp.depth--; \
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (0)
|
2017-04-25 11:33:10 +02:00
|
|
|
|
2017-04-21 11:33:57 +02:00
|
|
|
#else /* MBEDTLS_ECP_RESTARTABLE */
|
|
|
|
|
|
|
|
#define ECDSA_RS_ECP NULL
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define ECDSA_BUDGET(ops) /* no-op; for compatibility */
|
2017-04-21 12:36:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define ECDSA_RS_ENTER(SUB) (void) rs_ctx
|
|
|
|
#define ECDSA_RS_LEAVE(SUB) (void) rs_ctx
|
2017-04-25 11:33:10 +02:00
|
|
|
|
2017-04-21 11:33:57 +02:00
|
|
|
#endif /* MBEDTLS_ECP_RESTARTABLE */
|
|
|
|
|
2021-01-11 17:11:39 +01:00
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC) || \
|
2021-01-26 12:01:22 +01:00
|
|
|
!defined(MBEDTLS_ECDSA_SIGN_ALT) || \
|
|
|
|
!defined(MBEDTLS_ECDSA_VERIFY_ALT)
|
2013-01-26 18:05:50 +01: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
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int derive_mpi(const mbedtls_ecp_group *grp, mbedtls_mpi *x,
|
|
|
|
const unsigned char *buf, size_t blen)
|
2013-01-26 18:05:50 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t n_size = (grp->nbits + 7) / 8;
|
2014-01-03 12:55:15 +01:00
|
|
|
size_t use_size = blen > n_size ? n_size : blen;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(x, buf, use_size));
|
|
|
|
if (use_size * 8 > grp->nbits) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(x, use_size * 8 - grp->nbits));
|
|
|
|
}
|
2014-01-03 12:55:15 +01:00
|
|
|
|
2014-01-06 10:16:28 +01:00
|
|
|
/* While at it, reduce modulo N */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_mpi(x, &grp->N) >= 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(x, x, &grp->N));
|
|
|
|
}
|
2014-01-06 10:16:28 +01:00
|
|
|
|
2014-01-03 12:55:15 +01:00
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-01-26 18:05:50 +01:00
|
|
|
}
|
2021-01-11 17:11:39 +01:00
|
|
|
#endif /* ECDSA_DETERMINISTIC || !ECDSA_SIGN_ALT || !ECDSA_VERIFY_ALT */
|
2013-01-26 18:05:50 +01:00
|
|
|
|
2023-04-26 17:24:12 +02:00
|
|
|
int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid)
|
|
|
|
{
|
|
|
|
switch (gid) {
|
|
|
|
#ifdef MBEDTLS_ECP_DP_CURVE25519_ENABLED
|
|
|
|
case MBEDTLS_ECP_DP_CURVE25519: return 0;
|
|
|
|
#endif
|
|
|
|
#ifdef MBEDTLS_ECP_DP_CURVE448_ENABLED
|
|
|
|
case MBEDTLS_ECP_DP_CURVE448: return 0;
|
|
|
|
#endif
|
|
|
|
default: return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 12:05:52 +01:00
|
|
|
#if !defined(MBEDTLS_ECDSA_SIGN_ALT)
|
2013-01-26 17:24:59 +01: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)
|
|
|
|
*/
|
2022-12-09 19:59:26 +01:00
|
|
|
int mbedtls_ecdsa_sign_restartable(mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *r, mbedtls_mpi *s,
|
|
|
|
const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
|
|
|
|
int (*f_rng_blind)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng_blind,
|
|
|
|
mbedtls_ecdsa_restart_ctx *rs_ctx)
|
2013-01-26 17:24:59 +01:00
|
|
|
{
|
2017-04-25 12:57:22 +02:00
|
|
|
int ret, key_tries, sign_tries;
|
2017-04-25 13:44:19 +02:00
|
|
|
int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_point R;
|
|
|
|
mbedtls_mpi k, e, t;
|
2017-04-25 13:44:19 +02:00
|
|
|
mbedtls_mpi *pk = &k, *pr = r;
|
2013-01-26 17:24:59 +01:00
|
|
|
|
2013-12-04 20:52:04 +01:00
|
|
|
/* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2013-12-04 20:52:04 +01:00
|
|
|
|
2017-11-17 18:09:17 +01:00
|
|
|
/* Make sure d is in range 1..n-1 */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_int(d, 1) < 0 || mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) {
|
|
|
|
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
}
|
2017-11-17 18:09:17 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_point_init(&R);
|
|
|
|
mbedtls_mpi_init(&k); mbedtls_mpi_init(&e); mbedtls_mpi_init(&t);
|
2013-01-26 17:24:59 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ECDSA_RS_ENTER(sig);
|
2017-04-25 11:33:10 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rs_ctx != NULL && rs_ctx->sig != NULL) {
|
2017-04-25 11:33:10 +02:00
|
|
|
/* redirect to our context */
|
2017-04-25 13:44:19 +02:00
|
|
|
p_sign_tries = &rs_ctx->sig->sign_tries;
|
|
|
|
p_key_tries = &rs_ctx->sig->key_tries;
|
|
|
|
pk = &rs_ctx->sig->k;
|
|
|
|
pr = &rs_ctx->sig->r;
|
|
|
|
|
2017-04-25 11:33:10 +02:00
|
|
|
/* jump to current step */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rs_ctx->sig->state == ecdsa_sig_mul) {
|
2017-04-25 13:44:19 +02:00
|
|
|
goto mul;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (rs_ctx->sig->state == ecdsa_sig_modn) {
|
2017-04-25 13:44:19 +02:00
|
|
|
goto modn;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-04-25 11:33:10 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECP_RESTARTABLE */
|
|
|
|
|
2017-04-25 13:44:19 +02:00
|
|
|
*p_sign_tries = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
do {
|
|
|
|
if ((*p_sign_tries)++ > 10) {
|
2017-04-21 13:19:43 +02:00
|
|
|
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-01-26 17:24:59 +01:00
|
|
|
/*
|
|
|
|
* Steps 1-3: generate a suitable ephemeral keypair
|
2013-10-29 10:45:28 +01:00
|
|
|
* and set r = xR mod n
|
2013-01-26 17:24:59 +01:00
|
|
|
*/
|
2017-04-25 13:44:19 +02:00
|
|
|
*p_key_tries = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
do {
|
|
|
|
if ((*p_key_tries)++ > 10) {
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
|
2013-07-26 14:20:53 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2017-04-21 13:19:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, pk, f_rng, p_rng));
|
2017-04-25 13:44:19 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rs_ctx != NULL && rs_ctx->sig != NULL) {
|
2017-08-24 11:16:01 +02:00
|
|
|
rs_ctx->sig->state = ecdsa_sig_mul;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-04-25 12:57:22 +02:00
|
|
|
|
2017-04-25 13:44:19 +02:00
|
|
|
mul:
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, &R, pk, &grp->G,
|
|
|
|
f_rng_blind,
|
|
|
|
p_rng_blind,
|
|
|
|
ECDSA_RS_ECP));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pr, &R.X, &grp->N));
|
|
|
|
} while (mbedtls_mpi_cmp_int(pr, 0) == 0);
|
2017-04-25 13:44:19 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rs_ctx != NULL && rs_ctx->sig != NULL) {
|
2017-08-24 11:16:01 +02:00
|
|
|
rs_ctx->sig->state = ecdsa_sig_modn;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-04-25 13:44:19 +02:00
|
|
|
|
|
|
|
modn:
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Accounting for everything up to the end of the loop
|
|
|
|
* (step 6, but checking now avoids saving e and t)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ECDSA_BUDGET(MBEDTLS_ECP_OPS_INV + 4);
|
2013-01-26 17:24:59 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 5: derive MPI from hashed message
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen));
|
2013-01-26 17:24:59 +01:00
|
|
|
|
|
|
|
/*
|
2014-03-31 11:55:42 +02:00
|
|
|
* Generate a random value to blind inv_mod in next step,
|
|
|
|
* avoiding a potential timing leak.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, &t, f_rng_blind,
|
|
|
|
p_rng_blind));
|
2014-03-31 11:55:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
|
2013-01-26 17:24:59 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, pr, 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(pk, pk, &t));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pk, pk, &grp->N));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(s, pk, &grp->N));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, s, &e));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(s, s, &grp->N));
|
|
|
|
} while (mbedtls_mpi_cmp_int(s, 0) == 0);
|
2013-01-26 17:24:59 +01:00
|
|
|
|
2017-04-25 13:44:19 +02:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rs_ctx != NULL && rs_ctx->sig != NULL) {
|
|
|
|
mbedtls_mpi_copy(r, pr);
|
|
|
|
}
|
2017-04-25 13:44:19 +02:00
|
|
|
#endif
|
|
|
|
|
2013-01-26 17:24:59 +01:00
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_point_free(&R);
|
|
|
|
mbedtls_mpi_free(&k); mbedtls_mpi_free(&e); mbedtls_mpi_free(&t);
|
2013-01-26 17:24:59 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ECDSA_RS_LEAVE(sig);
|
2017-04-25 11:33:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-01-26 17:24:59 +01:00
|
|
|
}
|
2013-01-26 16:33:44 +01:00
|
|
|
|
2017-04-21 12:54:46 +02:00
|
|
|
/*
|
|
|
|
* Compute ECDSA signature of a hashed message
|
|
|
|
*/
|
2023-01-11 14:50:10 +01: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,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
2017-04-21 12:54:46 +02:00
|
|
|
{
|
2019-01-04 15:32:30 +01:00
|
|
|
/* Use the same RNG for both blinding and ephemeral key generation */
|
2022-12-09 19:59:26 +01:00
|
|
|
return mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen,
|
|
|
|
f_rng, p_rng, f_rng, p_rng, NULL);
|
2017-04-21 12:54:46 +02:00
|
|
|
}
|
2018-11-01 12:05:52 +01:00
|
|
|
#endif /* !MBEDTLS_ECDSA_SIGN_ALT */
|
2017-04-21 12:54:46 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
2014-01-06 14:25:56 +01:00
|
|
|
/*
|
|
|
|
* Deterministic signature wrapper
|
2021-04-29 23:12:19 +02:00
|
|
|
*
|
2021-04-30 10:32:58 +02:00
|
|
|
* note: The f_rng_blind parameter must not be NULL.
|
2021-04-29 23:12:19 +02:00
|
|
|
*
|
2014-01-06 14:25:56 +01:00
|
|
|
*/
|
2022-12-09 19:59:26 +01:00
|
|
|
int mbedtls_ecdsa_sign_det_restartable(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,
|
|
|
|
int (*f_rng_blind)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng_blind,
|
|
|
|
mbedtls_ecdsa_restart_ctx *rs_ctx)
|
2014-01-06 14:25:56 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_hmac_drbg_context rng_ctx;
|
2017-04-25 13:44:19 +02:00
|
|
|
mbedtls_hmac_drbg_context *p_rng = &rng_ctx;
|
2015-04-08 12:49:31 +02:00
|
|
|
unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t grp_len = (grp->nbits + 7) / 8;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_mpi h;
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((md_info = mbedtls_md_info_from_type(md_alg)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&h);
|
|
|
|
mbedtls_hmac_drbg_init(&rng_ctx);
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ECDSA_RS_ENTER(det);
|
2017-04-25 11:33:10 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rs_ctx != NULL && rs_ctx->det != NULL) {
|
2017-04-25 11:33:10 +02:00
|
|
|
/* redirect to our context */
|
2017-04-25 13:44:19 +02:00
|
|
|
p_rng = &rs_ctx->det->rng_ctx;
|
2017-04-25 11:33:10 +02:00
|
|
|
|
|
|
|
/* jump to current step */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rs_ctx->det->state == ecdsa_det_sign) {
|
2017-04-25 13:44:19 +02:00
|
|
|
goto sign;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-04-25 11:33:10 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECP_RESTARTABLE */
|
|
|
|
|
2014-01-06 15:05:01 +01:00
|
|
|
/* Use private key and message hash (reduced) to initialize HMAC_DRBG */
|
2023-01-11 14:50:10 +01: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(p_rng, md_info, data, 2 * grp_len);
|
2017-04-25 13:44:19 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rs_ctx != NULL && rs_ctx->det != NULL) {
|
2017-08-24 11:16:01 +02:00
|
|
|
rs_ctx->det->state = ecdsa_det_sign;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2017-04-25 13:44:19 +02:00
|
|
|
sign:
|
|
|
|
#endif
|
2018-11-01 10:32:15 +01:00
|
|
|
#if defined(MBEDTLS_ECDSA_SIGN_ALT)
|
2021-02-10 17:07:20 +01:00
|
|
|
(void) f_rng_blind;
|
|
|
|
(void) p_rng_blind;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ecdsa_sign(grp, r, s, d, buf, blen,
|
|
|
|
mbedtls_hmac_drbg_random, p_rng);
|
2018-11-01 10:32:15 +01:00
|
|
|
#else
|
2022-12-09 19:59:26 +01:00
|
|
|
ret = mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen,
|
|
|
|
mbedtls_hmac_drbg_random, p_rng,
|
|
|
|
f_rng_blind, p_rng_blind, rs_ctx);
|
2018-11-01 12:05:52 +01:00
|
|
|
#endif /* MBEDTLS_ECDSA_SIGN_ALT */
|
2014-01-06 14:25:56 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_hmac_drbg_free(&rng_ctx);
|
|
|
|
mbedtls_mpi_free(&h);
|
2014-01-06 14:25:56 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ECDSA_RS_LEAVE(det);
|
2017-04-25 11:33:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2014-01-06 14:25:56 +01:00
|
|
|
}
|
2017-04-21 12:54:46 +02:00
|
|
|
|
|
|
|
/*
|
2021-04-29 23:12:19 +02:00
|
|
|
* Deterministic signature wrapper
|
2017-04-21 12:54:46 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ecdsa_sign_det_ext(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,
|
|
|
|
int (*f_rng_blind)(void *, unsigned char *,
|
|
|
|
size_t),
|
|
|
|
void *p_rng_blind)
|
2017-04-21 12:54:46 +02:00
|
|
|
{
|
2022-12-09 19:59:26 +01:00
|
|
|
return mbedtls_ecdsa_sign_det_restartable(grp, r, s, d, buf, blen, md_alg,
|
|
|
|
f_rng_blind, p_rng_blind, NULL);
|
2017-04-21 12:54:46 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
|
2014-01-23 16:11:14 +01:00
|
|
|
|
2018-11-01 12:05:52 +01:00
|
|
|
#if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
|
2013-01-26 18:05:50 +01:00
|
|
|
/*
|
|
|
|
* Verify ECDSA signature of hashed message (SEC1 4.1.4)
|
|
|
|
* Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
|
|
|
|
*/
|
2022-12-09 19:59:26 +01:00
|
|
|
int mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp,
|
|
|
|
const unsigned char *buf, size_t blen,
|
|
|
|
const mbedtls_ecp_point *Q,
|
|
|
|
const mbedtls_mpi *r,
|
|
|
|
const mbedtls_mpi *s,
|
|
|
|
mbedtls_ecdsa_restart_ctx *rs_ctx)
|
2013-01-26 18:05:50 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi e, s_inv, u1, u2;
|
2015-05-11 18:40:45 +02:00
|
|
|
mbedtls_ecp_point R;
|
2017-04-21 12:36:59 +02:00
|
|
|
mbedtls_mpi *pu1 = &u1, *pu2 = &u2;
|
2013-01-26 18:05:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_point_init(&R);
|
|
|
|
mbedtls_mpi_init(&e); mbedtls_mpi_init(&s_inv);
|
|
|
|
mbedtls_mpi_init(&u1); mbedtls_mpi_init(&u2);
|
2013-01-26 18:05:50 +01:00
|
|
|
|
2013-12-04 20:52:04 +01:00
|
|
|
/* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2013-12-04 20:52:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ECDSA_RS_ENTER(ver);
|
2017-04-21 12:36:59 +02:00
|
|
|
|
2017-04-25 11:33:10 +02:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rs_ctx != NULL && rs_ctx->ver != NULL) {
|
2017-04-21 12:36:59 +02:00
|
|
|
/* redirect to our context */
|
|
|
|
pu1 = &rs_ctx->ver->u1;
|
|
|
|
pu2 = &rs_ctx->ver->u2;
|
|
|
|
|
|
|
|
/* jump to current step */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rs_ctx->ver->state == ecdsa_ver_muladd) {
|
2017-04-21 12:36:59 +02:00
|
|
|
goto muladd;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-04-21 12:36:59 +02:00
|
|
|
}
|
2017-04-21 11:33:57 +02:00
|
|
|
#endif /* MBEDTLS_ECP_RESTARTABLE */
|
|
|
|
|
2013-01-26 18:05:50 +01:00
|
|
|
/*
|
|
|
|
* Step 1: make sure r and s are in range 1..n-1
|
|
|
|
*/
|
2023-01-11 14:50:10 +01: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 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
|
2013-07-26 14:20:53 +02:00
|
|
|
goto cleanup;
|
2013-01-26 18:05:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 3: derive MPI from hashed message
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen));
|
2013-01-26 18:05:50 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 4: u1 = e / s mod n, u2 = r / s mod n
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ECDSA_BUDGET(MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2);
|
2017-08-23 17:39:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&s_inv, s, &grp->N));
|
2013-01-26 18:05:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu1, &e, &s_inv));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pu1, pu1, &grp->N));
|
2013-01-26 18:05:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu2, r, &s_inv));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pu2, pu2, &grp->N));
|
2013-01-26 18:05:50 +01:00
|
|
|
|
2017-04-21 12:36:59 +02:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (rs_ctx != NULL && rs_ctx->ver != NULL) {
|
2017-08-24 11:16:01 +02:00
|
|
|
rs_ctx->ver->state = ecdsa_ver_muladd;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-04-21 12:36:59 +02:00
|
|
|
|
|
|
|
muladd:
|
|
|
|
#endif
|
2013-01-26 18:05:50 +01:00
|
|
|
/*
|
|
|
|
* Step 5: R = u1 G + u2 Q
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_muladd_restartable(grp,
|
|
|
|
&R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP));
|
2013-01-26 18:05:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ecp_is_zero(&R)) {
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
|
2013-07-26 14:20:53 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-01-26 18:05:50 +01:00
|
|
|
|
|
|
|
/*
|
2013-10-29 10:45:28 +01:00
|
|
|
* Step 6: convert xR to an integer (no-op)
|
|
|
|
* Step 7: reduce xR mod n (gives v)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&R.X, &R.X, &grp->N));
|
2013-10-29 10:45:28 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Step 8: check if v (that is, R.X) is equal to r
|
2013-01-26 18:05:50 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_mpi_cmp_mpi(&R.X, r) != 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
|
2013-07-26 14:20:53 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-01-26 18:05:50 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_point_free(&R);
|
|
|
|
mbedtls_mpi_free(&e); mbedtls_mpi_free(&s_inv);
|
|
|
|
mbedtls_mpi_free(&u1); mbedtls_mpi_free(&u2);
|
2013-01-26 18:05:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ECDSA_RS_LEAVE(ver);
|
2017-04-21 11:33:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-01-26 18:05:50 +01:00
|
|
|
}
|
|
|
|
|
2017-04-21 10:29:13 +02:00
|
|
|
/*
|
|
|
|
* Verify ECDSA signature of hashed message
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
|
|
|
|
const unsigned char *buf, size_t blen,
|
|
|
|
const mbedtls_ecp_point *Q,
|
|
|
|
const mbedtls_mpi *r,
|
|
|
|
const mbedtls_mpi *s)
|
2017-04-21 10:29:13 +02:00
|
|
|
{
|
2022-12-09 19:59:26 +01:00
|
|
|
return mbedtls_ecdsa_verify_restartable(grp, buf, blen, Q, r, s, NULL);
|
2017-04-21 10:29:13 +02:00
|
|
|
}
|
2018-11-01 12:05:52 +01:00
|
|
|
#endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
|
2017-04-21 10:29:13 +02:00
|
|
|
|
2014-01-06 10:27:16 +01:00
|
|
|
/*
|
|
|
|
* Convert a signature (given by context) to ASN.1
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ecdsa_signature_to_asn1(const mbedtls_mpi *r, const mbedtls_mpi *s,
|
|
|
|
unsigned char *sig, size_t sig_size,
|
|
|
|
size_t *slen)
|
2014-01-06 10:27:16 +01:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char buf[MBEDTLS_ECDSA_MAX_LEN] = { 0 };
|
|
|
|
unsigned char *p = buf + sizeof(buf);
|
2014-01-06 10:27:16 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01: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));
|
2014-01-06 10:27:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01: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));
|
2014-01-06 10:27:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len > sig_size) {
|
|
|
|
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2021-06-22 00:09:00 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(sig, p, len);
|
2014-01-06 10:27:16 +01:00
|
|
|
*slen = len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-01-06 10:27:16 +01:00
|
|
|
}
|
|
|
|
|
2013-08-08 13:30:57 +02:00
|
|
|
/*
|
|
|
|
* Compute and write signature
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx,
|
|
|
|
mbedtls_md_type_t md_alg,
|
|
|
|
const unsigned char *hash, size_t hlen,
|
|
|
|
unsigned char *sig, size_t sig_size, size_t *slen,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng,
|
|
|
|
mbedtls_ecdsa_restart_ctx *rs_ctx)
|
2013-08-08 13:30:57 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi r, s;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (f_rng == NULL) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2015-03-31 13:06:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&r);
|
|
|
|
mbedtls_mpi_init(&s);
|
2013-08-08 13:30:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
2022-12-09 19:59:26 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_restartable(&ctx->grp, &r, &s, &ctx->d,
|
|
|
|
hash, hlen, md_alg, f_rng,
|
|
|
|
p_rng, rs_ctx));
|
2015-03-31 11:41:42 +02:00
|
|
|
#else
|
|
|
|
(void) md_alg;
|
|
|
|
|
2018-11-01 10:32:15 +01:00
|
|
|
#if defined(MBEDTLS_ECDSA_SIGN_ALT)
|
2021-01-11 17:11:39 +01:00
|
|
|
(void) rs_ctx;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ctx->grp, &r, &s, &ctx->d,
|
|
|
|
hash, hlen, f_rng, p_rng));
|
2018-11-01 10:32:15 +01:00
|
|
|
#else
|
2019-01-04 15:32:30 +01:00
|
|
|
/* Use the same RNG for both blinding and ephemeral key generation */
|
2022-12-09 19:59:26 +01:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_restartable(&ctx->grp, &r, &s, &ctx->d,
|
|
|
|
hash, hlen, f_rng, p_rng, f_rng,
|
|
|
|
p_rng, rs_ctx));
|
2018-11-01 12:05:52 +01:00
|
|
|
#endif /* MBEDTLS_ECDSA_SIGN_ALT */
|
2018-11-05 13:04:26 +01:00
|
|
|
#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
|
2013-08-08 13:30:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_MPI_CHK(ecdsa_signature_to_asn1(&r, &s, sig, sig_size, slen));
|
2015-03-31 13:06:41 +02:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&r);
|
|
|
|
mbedtls_mpi_free(&s);
|
2015-03-31 13:06:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2014-01-06 10:27:16 +01:00
|
|
|
}
|
2013-08-08 13:30:57 +02:00
|
|
|
|
2017-04-21 12:54:46 +02:00
|
|
|
/*
|
|
|
|
* Compute and write signature
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx,
|
|
|
|
mbedtls_md_type_t md_alg,
|
|
|
|
const unsigned char *hash, size_t hlen,
|
|
|
|
unsigned char *sig, size_t sig_size, size_t *slen,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2017-04-21 12:54:46 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ecdsa_write_signature_restartable(
|
|
|
|
ctx, md_alg, hash, hlen, sig, sig_size, slen,
|
|
|
|
f_rng, p_rng, NULL);
|
2017-04-21 12:54:46 +02:00
|
|
|
}
|
|
|
|
|
2013-08-08 13:30:57 +02:00
|
|
|
/*
|
|
|
|
* Read and check signature
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx,
|
|
|
|
const unsigned char *hash, size_t hlen,
|
|
|
|
const unsigned char *sig, size_t slen)
|
2017-04-21 10:29:13 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ecdsa_read_signature_restartable(
|
|
|
|
ctx, hash, hlen, sig, slen, NULL);
|
2017-04-21 10:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restartable read and check signature
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx,
|
|
|
|
const unsigned char *hash, size_t hlen,
|
|
|
|
const unsigned char *sig, size_t slen,
|
|
|
|
mbedtls_ecdsa_restart_ctx *rs_ctx)
|
2013-08-08 13:30:57 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-08-08 13:30:57 +02:00
|
|
|
unsigned char *p = (unsigned char *) sig;
|
|
|
|
const unsigned char *end = sig + slen;
|
|
|
|
size_t len;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi r, s;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_init(&r);
|
|
|
|
mbedtls_mpi_init(&s);
|
2013-08-08 13:30:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2015-03-31 13:06:41 +02:00
|
|
|
goto cleanup;
|
2013-08-08 13:30:57 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p + len != end) {
|
|
|
|
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
2015-03-31 13:06:41 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-08-08 13:30:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_mpi(&p, end, &r)) != 0 ||
|
|
|
|
(ret = mbedtls_asn1_get_mpi(&p, end, &s)) != 0) {
|
2015-04-08 12:49:31 +02:00
|
|
|
ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2015-03-31 13:06:41 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-11-01 10:32:15 +01:00
|
|
|
#if defined(MBEDTLS_ECDSA_VERIFY_ALT)
|
2021-01-11 17:11:39 +01:00
|
|
|
(void) rs_ctx;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ecdsa_verify(&ctx->grp, hash, hlen,
|
|
|
|
&ctx->Q, &r, &s)) != 0) {
|
2018-11-01 10:32:15 +01:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-01 10:32:15 +01:00
|
|
|
#else
|
2022-12-09 19:59:26 +01:00
|
|
|
if ((ret = mbedtls_ecdsa_verify_restartable(&ctx->grp, hash, hlen,
|
|
|
|
&ctx->Q, &r, &s, rs_ctx)) != 0) {
|
2015-03-31 13:06:41 +02:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-11-01 12:05:52 +01:00
|
|
|
#endif /* MBEDTLS_ECDSA_VERIFY_ALT */
|
2014-04-08 12:17:41 +02:00
|
|
|
|
2018-03-30 07:12:15 +02: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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p != end) {
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-08-08 13:30:57 +02:00
|
|
|
|
2015-03-31 13:06:41 +02:00
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&r);
|
|
|
|
mbedtls_mpi_free(&s);
|
2015-03-31 13:06:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-08-08 13:30:57 +02:00
|
|
|
}
|
|
|
|
|
2017-10-10 17:28:25 +02:00
|
|
|
#if !defined(MBEDTLS_ECDSA_GENKEY_ALT)
|
2013-08-09 16:21:34 +02:00
|
|
|
/*
|
|
|
|
* Generate key pair
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
2013-08-09 16:21:34 +02:00
|
|
|
{
|
2018-12-17 09:06:12 +01:00
|
|
|
int ret = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ecp_group_load(&ctx->grp, gid);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-12-17 09:06:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ecp_gen_keypair(&ctx->grp, &ctx->d,
|
|
|
|
&ctx->Q, f_rng, p_rng);
|
2013-08-09 16:21:34 +02:00
|
|
|
}
|
2018-11-01 12:05:52 +01:00
|
|
|
#endif /* !MBEDTLS_ECDSA_GENKEY_ALT */
|
2013-08-09 16:21:34 +02:00
|
|
|
|
2013-08-12 17:02:59 +02:00
|
|
|
/*
|
2015-04-08 12:49:31 +02:00
|
|
|
* Set context from an mbedtls_ecp_keypair
|
2013-08-12 17:02:59 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key)
|
2013-08-12 17:02:59 +02:00
|
|
|
{
|
2019-11-22 14:21:35 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01: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) {
|
|
|
|
mbedtls_ecdsa_free(ctx);
|
2013-10-27 14:53:48 +01:00
|
|
|
}
|
2013-08-12 17:02:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-08-12 17:02:59 +02:00
|
|
|
}
|
2013-08-09 16:21:34 +02:00
|
|
|
|
2013-06-27 12:54:02 +02:00
|
|
|
/*
|
|
|
|
* Initialize context
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx)
|
2013-06-27 12:54:02 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_keypair_init(ctx);
|
2013-06-27 12:54:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free context
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx)
|
2013-06-27 12:54:02 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx == NULL) {
|
2018-12-14 17:43:29 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-12-14 17:43:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_keypair_free(ctx);
|
2013-06-27 12:54:02 +02:00
|
|
|
}
|
2013-01-26 18:05:50 +01:00
|
|
|
|
2017-04-21 10:29:13 +02:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
/*
|
|
|
|
* Initialize a restart context
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ecdsa_restart_init(mbedtls_ecdsa_restart_ctx *ctx)
|
2017-04-21 10:29:13 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_restart_init(&ctx->ecp);
|
2017-04-21 11:33:57 +02:00
|
|
|
|
|
|
|
ctx->ver = NULL;
|
2017-04-25 11:33:10 +02:00
|
|
|
ctx->sig = NULL;
|
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
|
|
|
ctx->det = NULL;
|
|
|
|
#endif
|
2017-04-21 10:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the components of a restart context
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ecdsa_restart_free(mbedtls_ecdsa_restart_ctx *ctx)
|
2017-04-21 10:29:13 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx == NULL) {
|
2018-12-14 17:43:29 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2018-12-14 17:43:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_restart_free(&ctx->ecp);
|
2017-04-21 11:33:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ecdsa_restart_ver_free(ctx->ver);
|
|
|
|
mbedtls_free(ctx->ver);
|
2017-04-21 11:33:57 +02:00
|
|
|
ctx->ver = NULL;
|
2017-04-25 11:33:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ecdsa_restart_sig_free(ctx->sig);
|
|
|
|
mbedtls_free(ctx->sig);
|
2017-04-25 11:33:10 +02:00
|
|
|
ctx->sig = NULL;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
|
2023-01-11 14:50:10 +01:00
|
|
|
ecdsa_restart_det_free(ctx->det);
|
|
|
|
mbedtls_free(ctx->det);
|
2017-04-25 11:33:10 +02:00
|
|
|
ctx->det = NULL;
|
|
|
|
#endif
|
2017-04-21 10:29:13 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECP_RESTARTABLE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_ECDSA_C */
|