2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Elliptic curves over GF(p): generic functions
|
|
|
|
*
|
2020-09-05 12:53:20 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2024-01-30 14:09:13 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* References:
|
|
|
|
*
|
2023-08-04 18:09:03 +02:00
|
|
|
* SEC1 https://www.secg.org/sec1-v2.pdf
|
2018-02-08 19:04:43 +01:00
|
|
|
* GECC = Guide to Elliptic Curve Cryptography - Hankerson, Menezes, Vanstone
|
|
|
|
* FIPS 186-3 http://csrc.nist.gov/publications/fips/fips186-3/fips_186-3.pdf
|
|
|
|
* RFC 4492 for the related TLS structures and constants
|
2023-08-04 18:09:03 +02:00
|
|
|
* - https://www.rfc-editor.org/rfc/rfc4492
|
2018-06-07 16:25:01 +02:00
|
|
|
* RFC 7748 for the Curve448 and Curve25519 curve definitions
|
2023-08-04 18:09:03 +02:00
|
|
|
* - https://www.rfc-editor.org/rfc/rfc7748
|
2018-02-08 19:04:43 +01:00
|
|
|
*
|
2023-08-04 18:09:03 +02:00
|
|
|
* [Curve25519] https://cr.yp.to/ecdh/curve25519-20060209.pdf
|
2018-02-08 19:04:43 +01:00
|
|
|
*
|
|
|
|
* [2] CORON, Jean-S'ebastien. Resistance against differential power analysis
|
|
|
|
* for elliptic curve cryptosystems. In : Cryptographic Hardware and
|
|
|
|
* Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302.
|
|
|
|
* <http://link.springer.com/chapter/10.1007/3-540-48059-5_25>
|
|
|
|
*
|
|
|
|
* [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to
|
|
|
|
* render ECC resistant against Side Channel Attacks. IACR Cryptology
|
|
|
|
* ePrint Archive, 2004, vol. 2004, p. 342.
|
|
|
|
* <http://eprint.iacr.org/2004/342.pdf>
|
|
|
|
*/
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#include "common.h"
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
/**
|
|
|
|
* \brief Function level alternative implementation.
|
|
|
|
*
|
|
|
|
* The MBEDTLS_ECP_INTERNAL_ALT macro enables alternative implementations to
|
|
|
|
* replace certain functions in this module. The alternative implementations are
|
|
|
|
* typically hardware accelerators and need to activate the hardware before the
|
|
|
|
* computation starts and deactivate it after it finishes. The
|
|
|
|
* mbedtls_internal_ecp_init() and mbedtls_internal_ecp_free() functions serve
|
|
|
|
* this purpose.
|
|
|
|
*
|
|
|
|
* To preserve the correct functionality the following conditions must hold:
|
|
|
|
*
|
|
|
|
* - The alternative implementation must be activated by
|
|
|
|
* mbedtls_internal_ecp_init() before any of the replaceable functions is
|
|
|
|
* called.
|
|
|
|
* - mbedtls_internal_ecp_free() must \b only be called when the alternative
|
|
|
|
* implementation is activated.
|
|
|
|
* - mbedtls_internal_ecp_init() must \b not be called when the alternative
|
|
|
|
* implementation is activated.
|
|
|
|
* - Public functions must not return while the alternative implementation is
|
|
|
|
* activated.
|
|
|
|
* - Replaceable functions are guarded by \c MBEDTLS_ECP_XXX_ALT macros and
|
|
|
|
* before calling them an \code if( mbedtls_internal_ecp_grp_capable( grp ) )
|
|
|
|
* \endcode ensures that the alternative implementation supports the current
|
|
|
|
* group.
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
|
|
#endif
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_ECP_LIGHT)
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
#include "mbedtls/ecp.h"
|
|
|
|
#include "mbedtls/threading.h"
|
2018-06-07 16:25:01 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2021-12-21 12:54:05 +01:00
|
|
|
#include "mbedtls/error.h"
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#include "bn_mul.h"
|
2021-12-21 12:54:05 +01:00
|
|
|
#include "ecp_invasive.h"
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if !defined(MBEDTLS_ECP_ALT)
|
|
|
|
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#include "ecp_internal_alt.h"
|
2020-07-02 15:13:55 +02:00
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
/*
|
|
|
|
* Counts of point addition and doubling, and field multiplications.
|
|
|
|
* Used to test resistance of point multiplication to simple timing attacks.
|
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
static unsigned long add_count, dbl_count;
|
|
|
|
#endif /* MBEDTLS_ECP_C */
|
|
|
|
static unsigned long mul_count;
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif
|
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
/*
|
|
|
|
* Maximum number of "basic operations" to be done in a row.
|
|
|
|
*
|
|
|
|
* Default value 0 means that ECC operations will not yield.
|
|
|
|
* Note that regardless of the value of ecp_max_ops, always at
|
|
|
|
* least one step is performed before yielding.
|
|
|
|
*
|
|
|
|
* Setting ecp_max_ops=1 can be suitable for testing purposes
|
|
|
|
* as it will interrupt computation at all possible points.
|
|
|
|
*/
|
|
|
|
static unsigned ecp_max_ops = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set ecp_max_ops
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_ecp_set_max_ops(unsigned max_ops)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
|
|
|
ecp_max_ops = max_ops;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if restart is enabled
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_restart_is_enabled(void)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return ecp_max_ops != 0;
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restart sub-context for ecp_mul_comb()
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
struct mbedtls_ecp_restart_mul {
|
2019-02-16 17:19:46 +01:00
|
|
|
mbedtls_ecp_point R; /* current intermediate result */
|
|
|
|
size_t i; /* current index in various loops, 0 outside */
|
|
|
|
mbedtls_ecp_point *T; /* table for precomputed points */
|
|
|
|
unsigned char T_size; /* number of points in table T */
|
|
|
|
enum { /* what were we doing last time we returned? */
|
|
|
|
ecp_rsm_init = 0, /* nothing so far, dummy initial state */
|
|
|
|
ecp_rsm_pre_dbl, /* precompute 2^n multiples */
|
|
|
|
ecp_rsm_pre_norm_dbl, /* normalize precomputed 2^n multiples */
|
|
|
|
ecp_rsm_pre_add, /* precompute remaining points by adding */
|
|
|
|
ecp_rsm_pre_norm_add, /* normalize all precomputed points */
|
|
|
|
ecp_rsm_comb_core, /* ecp_mul_comb_core() */
|
|
|
|
ecp_rsm_final_norm, /* do the final normalization */
|
|
|
|
} state;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Init restart_mul sub-context
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static void ecp_restart_rsm_init(mbedtls_ecp_restart_mul_ctx *ctx)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_point_init(&ctx->R);
|
2019-02-16 17:19:46 +01:00
|
|
|
ctx->i = 0;
|
|
|
|
ctx->T = NULL;
|
|
|
|
ctx->T_size = 0;
|
|
|
|
ctx->state = ecp_rsm_init;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the components of a restart_mul sub-context
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static void ecp_restart_rsm_free(mbedtls_ecp_restart_mul_ctx *ctx)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
|
|
|
unsigned char i;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ctx == NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
return;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_point_free(&ctx->R);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ctx->T != NULL) {
|
|
|
|
for (i = 0; i < ctx->T_size; i++) {
|
|
|
|
mbedtls_ecp_point_free(ctx->T + i);
|
|
|
|
}
|
|
|
|
mbedtls_free(ctx->T);
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ecp_restart_rsm_init(ctx);
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restart context for ecp_muladd()
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
struct mbedtls_ecp_restart_muladd {
|
2019-02-16 17:19:46 +01:00
|
|
|
mbedtls_ecp_point mP; /* mP value */
|
|
|
|
mbedtls_ecp_point R; /* R intermediate result */
|
|
|
|
enum { /* what should we do next? */
|
|
|
|
ecp_rsma_mul1 = 0, /* first multiplication */
|
|
|
|
ecp_rsma_mul2, /* second multiplication */
|
|
|
|
ecp_rsma_add, /* addition */
|
|
|
|
ecp_rsma_norm, /* normalization */
|
|
|
|
} state;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Init restart_muladd sub-context
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static void ecp_restart_ma_init(mbedtls_ecp_restart_muladd_ctx *ctx)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_point_init(&ctx->mP);
|
|
|
|
mbedtls_ecp_point_init(&ctx->R);
|
2019-02-16 17:19:46 +01:00
|
|
|
ctx->state = ecp_rsma_mul1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the components of a restart_muladd sub-context
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static void ecp_restart_ma_free(mbedtls_ecp_restart_muladd_ctx *ctx)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ctx == NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
return;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_point_free(&ctx->mP);
|
|
|
|
mbedtls_ecp_point_free(&ctx->R);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ecp_restart_ma_init(ctx);
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize a restart context
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_ecp_restart_init(mbedtls_ecp_restart_ctx *ctx)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
|
|
|
ctx->ops_done = 0;
|
|
|
|
ctx->depth = 0;
|
|
|
|
ctx->rsm = NULL;
|
|
|
|
ctx->ma = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the components of a restart context
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_ecp_restart_free(mbedtls_ecp_restart_ctx *ctx)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ctx == NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
return;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ecp_restart_rsm_free(ctx->rsm);
|
|
|
|
mbedtls_free(ctx->rsm);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ecp_restart_ma_free(ctx->ma);
|
|
|
|
mbedtls_free(ctx->ma);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_restart_init(ctx);
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if we can do the next step
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_check_budget(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_restart_ctx *rs_ctx,
|
|
|
|
unsigned ops)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && ecp_max_ops != 0) {
|
2019-02-16 17:19:46 +01:00
|
|
|
/* scale depending on curve size: the chosen reference is 256-bit,
|
|
|
|
* and multiplication is quadratic. Round to the closest integer. */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (grp->pbits >= 512) {
|
2019-02-16 17:19:46 +01:00
|
|
|
ops *= 4;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else if (grp->pbits >= 384) {
|
2019-02-16 17:19:46 +01:00
|
|
|
ops *= 2;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
/* Avoid infinite loops: always allow first step.
|
|
|
|
* Because of that, however, it's not generally true
|
|
|
|
* that ops_done <= ecp_max_ops, so the check
|
|
|
|
* ops_done > ecp_max_ops below is mandatory. */
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((rs_ctx->ops_done != 0) &&
|
|
|
|
(rs_ctx->ops_done > ecp_max_ops ||
|
|
|
|
ops > ecp_max_ops - rs_ctx->ops_done)) {
|
|
|
|
return MBEDTLS_ERR_ECP_IN_PROGRESS;
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update running count */
|
|
|
|
rs_ctx->ops_done += ops;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Call this when entering a function that needs its own sub-context */
|
2023-04-18 10:38:24 +02:00
|
|
|
#define ECP_RS_ENTER(SUB) do { \
|
|
|
|
/* reset ops count for this call if top-level */ \
|
|
|
|
if (rs_ctx != NULL && rs_ctx->depth++ == 0) \
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->ops_done = 0; \
|
|
|
|
\
|
2023-04-18 10:38:24 +02: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; \
|
|
|
|
\
|
|
|
|
ecp_restart_## SUB ##_init(rs_ctx->SUB); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
/* Call this when leaving a function that needs its own sub-context */
|
2023-04-18 10:38:24 +02:00
|
|
|
#define ECP_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) \
|
|
|
|
{ \
|
|
|
|
ecp_restart_## SUB ##_free(rs_ctx->SUB); \
|
|
|
|
mbedtls_free(rs_ctx->SUB); \
|
|
|
|
rs_ctx->SUB = NULL; \
|
|
|
|
} \
|
2019-02-16 17:19:46 +01:00
|
|
|
\
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL) \
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->depth--; \
|
2023-04-18 10:38:24 +02:00
|
|
|
} while (0)
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
#else /* MBEDTLS_ECP_RESTARTABLE */
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
#define ECP_RS_ENTER(sub) (void) rs_ctx;
|
|
|
|
#define ECP_RS_LEAVE(sub) (void) rs_ctx;
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
#endif /* MBEDTLS_ECP_RESTARTABLE */
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
static void mpi_init_many(mbedtls_mpi *arr, size_t size)
|
|
|
|
{
|
|
|
|
while (size--) {
|
|
|
|
mbedtls_mpi_init(arr++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mpi_free_many(mbedtls_mpi *arr, size_t size)
|
|
|
|
{
|
|
|
|
while (size--) {
|
|
|
|
mbedtls_mpi_free(arr++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECP_C */
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* List of supported curves:
|
|
|
|
* - internal ID
|
2021-12-21 12:54:05 +01:00
|
|
|
* - TLS NamedCurve ID (RFC 4492 sec. 5.1.1, RFC 7071 sec. 2, RFC 8446 sec. 4.2.7)
|
2018-02-08 19:04:43 +01:00
|
|
|
* - size in bits
|
|
|
|
* - readable name
|
|
|
|
*
|
|
|
|
* Curves are listed in order: largest curves first, and for a given size,
|
2023-09-25 05:04:06 +02:00
|
|
|
* fastest curves first.
|
2018-02-08 19:04:43 +01:00
|
|
|
*
|
2023-09-25 05:04:06 +02:00
|
|
|
* Reminder: update profiles in x509_crt.c and ssl_tls.c when adding a new curve!
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
|
|
|
static const mbedtls_ecp_curve_info ecp_supported_curves[] =
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP521R1, 25, 521, "secp521r1" },
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_BP512R1, 28, 512, "brainpoolP512r1" },
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP384R1, 24, 384, "secp384r1" },
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_BP384R1, 27, 384, "brainpoolP384r1" },
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP256R1, 23, 256, "secp256r1" },
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP256K1, 22, 256, "secp256k1" },
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_BP256R1, 26, 256, "brainpoolP256r1" },
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP224R1, 21, 224, "secp224r1" },
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP224K1, 20, 224, "secp224k1" },
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP192R1, 19, 192, "secp192r1" },
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_SECP192K1, 18, 192, "secp192k1" },
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_CURVE25519, 29, 256, "x25519" },
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
|
|
|
|
{ MBEDTLS_ECP_DP_CURVE448, 30, 448, "x448" },
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif
|
|
|
|
{ MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
|
|
|
|
};
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
#define ECP_NB_CURVES sizeof(ecp_supported_curves) / \
|
|
|
|
sizeof(ecp_supported_curves[0])
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
static mbedtls_ecp_group_id ecp_supported_grp_id[ECP_NB_CURVES];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* List of supported curves and associated info
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_list(void)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return ecp_supported_curves;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* List of supported curves, group ID only
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
const mbedtls_ecp_group_id *mbedtls_ecp_grp_id_list(void)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
static int init_done = 0;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (!init_done) {
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t i = 0;
|
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (curve_info = mbedtls_ecp_curve_list();
|
2018-02-08 19:04:43 +01:00
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
2023-04-18 10:38:24 +02:00
|
|
|
curve_info++) {
|
2018-02-08 19:04:43 +01:00
|
|
|
ecp_supported_grp_id[i++] = curve_info->grp_id;
|
|
|
|
}
|
|
|
|
ecp_supported_grp_id[i] = MBEDTLS_ECP_DP_NONE;
|
|
|
|
|
|
|
|
init_done = 1;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ecp_supported_grp_id;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the curve info for the internal identifier
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_grp_id(mbedtls_ecp_group_id grp_id)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (curve_info = mbedtls_ecp_curve_list();
|
2018-02-08 19:04:43 +01:00
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
2023-04-18 10:38:24 +02:00
|
|
|
curve_info++) {
|
|
|
|
if (curve_info->grp_id == grp_id) {
|
|
|
|
return curve_info;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return NULL;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the curve info from the TLS identifier
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (curve_info = mbedtls_ecp_curve_list();
|
2018-02-08 19:04:43 +01:00
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
2023-04-18 10:38:24 +02:00
|
|
|
curve_info++) {
|
|
|
|
if (curve_info->tls_id == tls_id) {
|
|
|
|
return curve_info;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return NULL;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the curve info from the name
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
const mbedtls_ecp_curve_info *mbedtls_ecp_curve_info_from_name(const char *name)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (curve_info = mbedtls_ecp_curve_list();
|
2018-02-08 19:04:43 +01:00
|
|
|
curve_info->grp_id != MBEDTLS_ECP_DP_NONE;
|
2023-04-18 10:38:24 +02:00
|
|
|
curve_info++) {
|
|
|
|
if (strcmp(curve_info->name, name) == 0) {
|
|
|
|
return curve_info;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return NULL;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the type of a curve
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_curve_type mbedtls_ecp_get_type(const mbedtls_ecp_group *grp)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
if (grp->G.X.p == NULL) {
|
|
|
|
return MBEDTLS_ECP_TYPE_NONE;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (grp->G.Y.p == NULL) {
|
|
|
|
return MBEDTLS_ECP_TYPE_MONTGOMERY;
|
|
|
|
} else {
|
|
|
|
return MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize (the components of) a point
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_ecp_point_init(mbedtls_ecp_point *pt)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_init(&pt->X);
|
|
|
|
mbedtls_mpi_init(&pt->Y);
|
|
|
|
mbedtls_mpi_init(&pt->Z);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize (the components of) a group
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_ecp_group_init(mbedtls_ecp_group *grp)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2019-02-16 17:19:46 +01:00
|
|
|
grp->id = MBEDTLS_ECP_DP_NONE;
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_init(&grp->P);
|
|
|
|
mbedtls_mpi_init(&grp->A);
|
|
|
|
mbedtls_mpi_init(&grp->B);
|
|
|
|
mbedtls_ecp_point_init(&grp->G);
|
|
|
|
mbedtls_mpi_init(&grp->N);
|
2019-02-16 17:19:46 +01:00
|
|
|
grp->pbits = 0;
|
|
|
|
grp->nbits = 0;
|
|
|
|
grp->h = 0;
|
|
|
|
grp->modp = NULL;
|
|
|
|
grp->t_pre = NULL;
|
|
|
|
grp->t_post = NULL;
|
|
|
|
grp->t_data = NULL;
|
|
|
|
grp->T = NULL;
|
|
|
|
grp->T_size = 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize (the components of) a key pair
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_ecp_keypair_init(mbedtls_ecp_keypair *key)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_group_init(&key->grp);
|
|
|
|
mbedtls_mpi_init(&key->d);
|
|
|
|
mbedtls_ecp_point_init(&key->Q);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unallocate (the components of) a point
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_ecp_point_free(mbedtls_ecp_point *pt)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
if (pt == NULL) {
|
2018-02-08 19:04:43 +01:00
|
|
|
return;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_free(&(pt->X));
|
|
|
|
mbedtls_mpi_free(&(pt->Y));
|
|
|
|
mbedtls_mpi_free(&(pt->Z));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/*
|
|
|
|
* Check that the comb table (grp->T) is static initialized.
|
|
|
|
*/
|
|
|
|
static int ecp_group_is_static_comb_table(const mbedtls_ecp_group *grp)
|
|
|
|
{
|
|
|
|
#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
|
|
|
|
return grp->T != NULL && grp->T_size == 0;
|
|
|
|
#else
|
|
|
|
(void) grp;
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Unallocate (the components of) a group
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_ecp_group_free(mbedtls_ecp_group *grp)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (grp == NULL) {
|
2018-02-08 19:04:43 +01:00
|
|
|
return;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (grp->h != 1) {
|
|
|
|
mbedtls_mpi_free(&grp->A);
|
|
|
|
mbedtls_mpi_free(&grp->B);
|
|
|
|
mbedtls_ecp_point_free(&grp->G);
|
2023-09-25 05:04:06 +02:00
|
|
|
|
|
|
|
#if !defined(MBEDTLS_ECP_WITH_MPI_UINT)
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_free(&grp->N);
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi_free(&grp->P);
|
|
|
|
#endif
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
if (!ecp_group_is_static_comb_table(grp) && grp->T != NULL) {
|
2023-04-18 10:38:24 +02:00
|
|
|
for (i = 0; i < grp->T_size; i++) {
|
|
|
|
mbedtls_ecp_point_free(&grp->T[i]);
|
|
|
|
}
|
|
|
|
mbedtls_free(grp->T);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_platform_zeroize(grp, sizeof(mbedtls_ecp_group));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unallocate (the components of) a key pair
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_ecp_keypair_free(mbedtls_ecp_keypair *key)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
if (key == NULL) {
|
2018-02-08 19:04:43 +01:00
|
|
|
return;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_group_free(&key->grp);
|
|
|
|
mbedtls_mpi_free(&key->d);
|
|
|
|
mbedtls_ecp_point_free(&key->Q);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the contents of a point
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_copy(mbedtls_ecp_point *P, const mbedtls_ecp_point *Q)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->X, &Q->X));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Y, &Q->Y));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&P->Z, &Q->Z));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy the contents of a group object
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_group_copy(mbedtls_ecp_group *dst, const mbedtls_ecp_group *src)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_ecp_group_load(dst, src->id);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set point to zero
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_set_zero(mbedtls_ecp_point *pt)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->X, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Y, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 0));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell if a point is zero
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_is_zero(mbedtls_ecp_point *pt)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_mpi_cmp_int(&pt->Z, 0) == 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-02-16 17:19:46 +01:00
|
|
|
* Compare two points lazily
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_point_cmp(const mbedtls_ecp_point *P,
|
|
|
|
const mbedtls_ecp_point *Q)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_cmp_mpi(&P->X, &Q->X) == 0 &&
|
|
|
|
mbedtls_mpi_cmp_mpi(&P->Y, &Q->Y) == 0 &&
|
|
|
|
mbedtls_mpi_cmp_mpi(&P->Z, &Q->Z) == 0) {
|
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Import a non-zero point from ASCII strings
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_point_read_string(mbedtls_ecp_point *P, int radix,
|
|
|
|
const char *x, const char *y)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->X, radix, x));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(&P->Y, radix, y));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&P->Z, 1));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-12-21 12:54:05 +01:00
|
|
|
* Export a point into unsigned binary data (SEC1 2.3.3 and RFC7748)
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_point_write_binary(const mbedtls_ecp_group *grp,
|
|
|
|
const mbedtls_ecp_point *P,
|
|
|
|
int format, size_t *olen,
|
|
|
|
unsigned char *buf, size_t buflen)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t plen;
|
2023-09-25 05:04:06 +02:00
|
|
|
if (format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
|
|
|
|
format != MBEDTLS_ECP_PF_COMPRESSED) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
plen = mbedtls_mpi_size(&grp->P);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
|
|
|
(void) format; /* Montgomery curves always use the same point format */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
|
2021-12-21 12:54:05 +01:00
|
|
|
*olen = plen;
|
2023-04-18 10:38:24 +02:00
|
|
|
if (buflen < *olen) {
|
|
|
|
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&P->X, buf, plen));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
|
2021-12-21 12:54:05 +01:00
|
|
|
/*
|
|
|
|
* Common case: P == 0
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_cmp_int(&P->Z, 0) == 0) {
|
|
|
|
if (buflen < 1) {
|
|
|
|
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
buf[0] = 0x00;
|
|
|
|
*olen = 1;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (format == MBEDTLS_ECP_PF_UNCOMPRESSED) {
|
2021-12-21 12:54:05 +01:00
|
|
|
*olen = 2 * plen + 1;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (buflen < *olen) {
|
|
|
|
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
|
|
|
|
buf[0] = 0x04;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->Y, buf + 1 + plen, plen));
|
|
|
|
} else if (format == MBEDTLS_ECP_PF_COMPRESSED) {
|
2021-12-21 12:54:05 +01:00
|
|
|
*olen = plen + 1;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (buflen < *olen) {
|
|
|
|
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
buf[0] = 0x02 + mbedtls_mpi_get_bit(&P->Y, 0);
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&P->X, buf + 1, plen));
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
|
|
|
static int mbedtls_ecp_sw_derive_y(const mbedtls_ecp_group *grp,
|
|
|
|
const mbedtls_mpi *X,
|
|
|
|
mbedtls_mpi *Y,
|
|
|
|
int parity_bit);
|
|
|
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
2021-12-21 12:54:05 +01:00
|
|
|
* Import a point from unsigned binary data (SEC1 2.3.4 and RFC7748)
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_point_read_binary(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point *pt,
|
|
|
|
const unsigned char *buf, size_t ilen)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t plen;
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ilen < 1) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
plen = mbedtls_mpi_size(&grp->P);
|
2021-12-21 12:54:05 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
|
|
|
|
if (plen != ilen) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&pt->X, buf, plen));
|
|
|
|
mbedtls_mpi_free(&pt->Y);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (grp->id == MBEDTLS_ECP_DP_CURVE25519) {
|
2021-12-21 12:54:05 +01:00
|
|
|
/* Set most significant bit to 0 as prescribed in RFC7748 §5 */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&pt->X, plen * 8 - 1, 0));
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
|
|
|
|
if (buf[0] == 0x00) {
|
|
|
|
if (ilen == 1) {
|
|
|
|
return mbedtls_ecp_set_zero(pt);
|
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
if (ilen < 1 + plen) {
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&pt->X, buf + 1, plen));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&pt->Z, 1));
|
2023-09-25 05:04:06 +02:00
|
|
|
|
|
|
|
if (buf[0] == 0x04) {
|
|
|
|
/* format == MBEDTLS_ECP_PF_UNCOMPRESSED */
|
|
|
|
if (ilen != 1 + plen * 2) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
return mbedtls_mpi_read_binary(&pt->Y, buf + 1 + plen, plen);
|
|
|
|
} else if (buf[0] == 0x02 || buf[0] == 0x03) {
|
|
|
|
/* format == MBEDTLS_ECP_PF_COMPRESSED */
|
|
|
|
if (ilen != 1 + plen) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
return mbedtls_ecp_sw_derive_y(grp, &pt->X, &pt->Y,
|
|
|
|
(buf[0] & 1));
|
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
#endif
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Import a point from a TLS ECPoint record (RFC 4492)
|
|
|
|
* struct {
|
|
|
|
* opaque point <1..2^8-1>;
|
|
|
|
* } ECPoint;
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_tls_read_point(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point *pt,
|
|
|
|
const unsigned char **buf, size_t buf_len)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
unsigned char data_len;
|
|
|
|
const unsigned char *buf_start;
|
|
|
|
/*
|
|
|
|
* We must have at least two bytes (1 for length, at least one for data)
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (buf_len < 2) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
data_len = *(*buf)++;
|
2023-04-18 10:38:24 +02:00
|
|
|
if (data_len < 1 || data_len > buf_len - 1) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Save buffer start for read_binary and update buf
|
|
|
|
*/
|
|
|
|
buf_start = *buf;
|
|
|
|
*buf += data_len;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_ecp_point_read_binary(grp, pt, buf_start, data_len);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Export a point as a TLS ECPoint record (RFC 4492)
|
|
|
|
* struct {
|
|
|
|
* opaque point <1..2^8-1>;
|
|
|
|
* } ECPoint;
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_tls_write_point(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt,
|
|
|
|
int format, size_t *olen,
|
|
|
|
unsigned char *buf, size_t blen)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-09-25 05:04:06 +02:00
|
|
|
if (format != MBEDTLS_ECP_PF_UNCOMPRESSED &&
|
|
|
|
format != MBEDTLS_ECP_PF_COMPRESSED) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* buffer length must be at least one, for our length byte
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (blen < 1) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_ecp_point_write_binary(grp, pt, format,
|
|
|
|
olen, buf + 1, blen - 1)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* write length to the first byte and update total length
|
|
|
|
*/
|
|
|
|
buf[0] = (unsigned char) *olen;
|
|
|
|
++*olen;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a group from an ECParameters record (RFC 4492)
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_tls_read_group(mbedtls_ecp_group *grp,
|
|
|
|
const unsigned char **buf, size_t len)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-02-16 17:19:46 +01:00
|
|
|
mbedtls_ecp_group_id grp_id;
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, len)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_ecp_group_load(grp, grp_id);
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a group id from an ECParameters record (RFC 4492) and convert it to
|
|
|
|
* mbedtls_ecp_group_id.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_tls_read_group_id(mbedtls_ecp_group_id *grp,
|
|
|
|
const unsigned char **buf, size_t len)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
uint16_t tls_id;
|
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
|
|
|
/*
|
|
|
|
* We expect at least three bytes (see below)
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (len < 3) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* First byte is curve_type; only named_curve is handled
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (*(*buf)++ != MBEDTLS_ECP_TLS_NAMED_CURVE) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Next two bytes are the namedcurve value
|
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
tls_id = MBEDTLS_GET_UINT16_BE(*buf, 0);
|
|
|
|
*buf += 2;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((curve_info = mbedtls_ecp_curve_info_from_tls_id(tls_id)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
*grp = curve_info->grp_id;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the ECParameters record corresponding to a group (RFC 4492)
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_tls_write_group(const mbedtls_ecp_group *grp, size_t *olen,
|
|
|
|
unsigned char *buf, size_t blen)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
const mbedtls_ecp_curve_info *curve_info;
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((curve_info = mbedtls_ecp_curve_info_from_grp_id(grp->id)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We are going to write 3 bytes (see below)
|
|
|
|
*/
|
|
|
|
*olen = 3;
|
2023-04-18 10:38:24 +02:00
|
|
|
if (blen < *olen) {
|
|
|
|
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* First byte is curve_type, always named_curve
|
|
|
|
*/
|
|
|
|
*buf++ = MBEDTLS_ECP_TLS_NAMED_CURVE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Next two bytes are the namedcurve value
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_PUT_UINT16_BE(curve_info->tls_id, buf, 0);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapper around fast quasi-modp functions, with fall-back to mbedtls_mpi_mod_mpi.
|
|
|
|
* See the documentation of struct mbedtls_ecp_group.
|
|
|
|
*
|
|
|
|
* This function is in the critial loop for mbedtls_ecp_mul, so pay attention to perf.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_modp(mbedtls_mpi *N, const mbedtls_ecp_group *grp)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (grp->modp == NULL) {
|
|
|
|
return mbedtls_mpi_mod_mpi(N, N, &grp->P);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* N->s < 0 is a much faster test, which fails only if N is 0 */
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) ||
|
|
|
|
mbedtls_mpi_bitlen(N) > 2 * grp->pbits) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(grp->modp(N));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* N->s < 0 is a much faster test, which fails only if N is 0 */
|
2023-04-18 10:38:24 +02:00
|
|
|
while (N->s < 0 && mbedtls_mpi_cmp_int(N, 0) != 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(N, N, &grp->P));
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
while (mbedtls_mpi_cmp_mpi(N, &grp->P) >= 0) {
|
2018-02-08 19:04:43 +01:00
|
|
|
/* we known P, N and the result are positive */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs(N, N, &grp->P));
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fast mod-p functions expect their argument to be in the 0..p^2 range.
|
|
|
|
*
|
|
|
|
* In order to guarantee that, we need to ensure that operands of
|
|
|
|
* mbedtls_mpi_mul_mpi are in the 0..p range. So, after each operation we will
|
|
|
|
* bring the result back to this range.
|
|
|
|
*
|
|
|
|
* The following macros are shortcuts for doing that.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reduce a mbedtls_mpi mod p in-place, general case, to use after mbedtls_mpi_mul_mpi
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
#define INC_MUL_COUNT mul_count++;
|
|
|
|
#else
|
|
|
|
#define INC_MUL_COUNT
|
|
|
|
#endif
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
#define MOD_MUL(N) \
|
2019-07-10 16:15:01 +02:00
|
|
|
do \
|
|
|
|
{ \
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_modp(&(N), grp)); \
|
2019-07-10 16:15:01 +02:00
|
|
|
INC_MUL_COUNT \
|
2023-04-18 10:38:24 +02:00
|
|
|
} while (0)
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
static inline int mbedtls_mpi_mul_mod(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *X,
|
|
|
|
const mbedtls_mpi *A,
|
|
|
|
const mbedtls_mpi *B)
|
2021-12-21 12:54:05 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(X, A, B));
|
|
|
|
MOD_MUL(*X);
|
2021-12-21 12:54:05 +01:00
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_sub_mpi
|
|
|
|
* N->s < 0 is a very fast test, which fails only if N is 0
|
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
#define MOD_SUB(N) \
|
|
|
|
do { \
|
|
|
|
while ((N)->s < 0 && mbedtls_mpi_cmp_int((N), 0) != 0) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi((N), (N), &grp->P)); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
MBEDTLS_MAYBE_UNUSED
|
2023-04-18 10:38:24 +02:00
|
|
|
static inline int mbedtls_mpi_sub_mod(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *X,
|
|
|
|
const mbedtls_mpi *A,
|
|
|
|
const mbedtls_mpi *B)
|
2021-12-21 12:54:05 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(X, A, B));
|
2023-09-25 05:04:06 +02:00
|
|
|
MOD_SUB(X);
|
2021-12-21 12:54:05 +01:00
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Reduce a mbedtls_mpi mod p in-place, to use after mbedtls_mpi_add_mpi and mbedtls_mpi_mul_int.
|
|
|
|
* We known P, N and the result are positive, so sub_abs is correct, and
|
|
|
|
* a bit faster.
|
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
#define MOD_ADD(N) \
|
|
|
|
while (mbedtls_mpi_cmp_mpi((N), &grp->P) >= 0) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_abs((N), (N), &grp->P))
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
static inline int mbedtls_mpi_add_mod(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *X,
|
|
|
|
const mbedtls_mpi *A,
|
|
|
|
const mbedtls_mpi *B)
|
2021-12-21 12:54:05 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(X, A, B));
|
2023-09-25 05:04:06 +02:00
|
|
|
MOD_ADD(X);
|
2021-12-21 12:54:05 +01:00
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MAYBE_UNUSED
|
|
|
|
static inline int mbedtls_mpi_mul_int_mod(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *X,
|
|
|
|
const mbedtls_mpi *A,
|
|
|
|
mbedtls_mpi_uint c)
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int(X, A, c));
|
|
|
|
MOD_ADD(X);
|
|
|
|
cleanup:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_MAYBE_UNUSED
|
|
|
|
static inline int mbedtls_mpi_sub_int_mod(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *X,
|
|
|
|
const mbedtls_mpi *A,
|
|
|
|
mbedtls_mpi_uint c)
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(X, A, c));
|
|
|
|
MOD_SUB(X);
|
|
|
|
cleanup:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MPI_ECP_SUB_INT(X, A, c) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int_mod(grp, X, A, c))
|
|
|
|
|
|
|
|
MBEDTLS_MAYBE_UNUSED
|
2023-04-18 10:38:24 +02:00
|
|
|
static inline int mbedtls_mpi_shift_l_mod(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *X,
|
|
|
|
size_t count)
|
2021-12-21 12:54:05 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, count));
|
2023-09-25 05:04:06 +02:00
|
|
|
MOD_ADD(X);
|
|
|
|
cleanup:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Macro wrappers around ECP modular arithmetic
|
|
|
|
*
|
|
|
|
* Currently, these wrappers are defined via the bignum module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define MPI_ECP_ADD(X, A, B) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mod(grp, X, A, B))
|
|
|
|
|
|
|
|
#define MPI_ECP_SUB(X, A, B) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mod(grp, X, A, B))
|
|
|
|
|
|
|
|
#define MPI_ECP_MUL(X, A, B) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, X, A, B))
|
|
|
|
|
|
|
|
#define MPI_ECP_SQR(X, A) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mod(grp, X, A, A))
|
|
|
|
|
|
|
|
#define MPI_ECP_MUL_INT(X, A, c) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_mul_int_mod(grp, X, A, c))
|
|
|
|
|
|
|
|
#define MPI_ECP_INV(dst, src) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod((dst), (src), &grp->P))
|
|
|
|
|
|
|
|
#define MPI_ECP_MOV(X, A) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(X, A))
|
|
|
|
|
|
|
|
#define MPI_ECP_SHIFT_L(X, count) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l_mod(grp, X, count))
|
|
|
|
|
|
|
|
#define MPI_ECP_LSET(X, c) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, c))
|
|
|
|
|
|
|
|
#define MPI_ECP_CMP_INT(X, c) \
|
|
|
|
mbedtls_mpi_cmp_int(X, c)
|
|
|
|
|
|
|
|
#define MPI_ECP_CMP(X, Y) \
|
|
|
|
mbedtls_mpi_cmp_mpi(X, Y)
|
|
|
|
|
|
|
|
/* Needs f_rng, p_rng to be defined. */
|
|
|
|
#define MPI_ECP_RAND(X) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_random((X), 2, &grp->P, f_rng, p_rng))
|
|
|
|
|
|
|
|
/* Conditional negation
|
|
|
|
* Needs grp and a temporary MPI tmp to be defined. */
|
|
|
|
#define MPI_ECP_COND_NEG(X, cond) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
unsigned char nonzero = mbedtls_mpi_cmp_int((X), 0) != 0; \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&tmp, &grp->P, (X))); \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign((X), &tmp, \
|
|
|
|
nonzero & cond)); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define MPI_ECP_NEG(X) MPI_ECP_COND_NEG((X), 1)
|
|
|
|
|
|
|
|
#define MPI_ECP_VALID(X) \
|
|
|
|
((X)->p != NULL)
|
|
|
|
|
|
|
|
#define MPI_ECP_COND_ASSIGN(X, Y, cond) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign((X), (Y), (cond)))
|
|
|
|
|
|
|
|
#define MPI_ECP_COND_SWAP(X, Y, cond) \
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_swap((X), (Y), (cond)))
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Computes the right-hand side of the Short Weierstrass equation
|
|
|
|
* RHS = X^3 + A X + B
|
|
|
|
*/
|
|
|
|
static int ecp_sw_rhs(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *rhs,
|
|
|
|
const mbedtls_mpi *X)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Compute X^3 + A X + B as X (X^2 + A) + B */
|
|
|
|
MPI_ECP_SQR(rhs, X);
|
|
|
|
|
|
|
|
/* Special case for A = -3 */
|
|
|
|
if (mbedtls_ecp_group_a_is_minus_3(grp)) {
|
|
|
|
MPI_ECP_SUB_INT(rhs, rhs, 3);
|
|
|
|
} else {
|
|
|
|
MPI_ECP_ADD(rhs, rhs, &grp->A);
|
|
|
|
}
|
|
|
|
|
|
|
|
MPI_ECP_MUL(rhs, rhs, X);
|
|
|
|
MPI_ECP_ADD(rhs, rhs, &grp->B);
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/*
|
|
|
|
* Derive Y from X and a parity bit
|
|
|
|
*/
|
|
|
|
static int mbedtls_ecp_sw_derive_y(const mbedtls_ecp_group *grp,
|
|
|
|
const mbedtls_mpi *X,
|
|
|
|
mbedtls_mpi *Y,
|
|
|
|
int parity_bit)
|
|
|
|
{
|
|
|
|
/* w = y^2 = x^3 + ax + b
|
|
|
|
* y = sqrt(w) = w^((p+1)/4) mod p (for prime p where p = 3 mod 4)
|
|
|
|
*
|
|
|
|
* Note: this method for extracting square root does not validate that w
|
|
|
|
* was indeed a square so this function will return garbage in Y if X
|
|
|
|
* does not correspond to a point on the curve.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Check prerequisite p = 3 mod 4 */
|
|
|
|
if (mbedtls_mpi_get_bit(&grp->P, 0) != 1 ||
|
|
|
|
mbedtls_mpi_get_bit(&grp->P, 1) != 1) {
|
|
|
|
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
mbedtls_mpi exp;
|
|
|
|
mbedtls_mpi_init(&exp);
|
|
|
|
|
|
|
|
/* use Y to store intermediate result, actually w above */
|
|
|
|
MBEDTLS_MPI_CHK(ecp_sw_rhs(grp, Y, X));
|
|
|
|
|
|
|
|
/* w = y^2 */ /* Y contains y^2 intermediate result */
|
|
|
|
/* exp = ((p+1)/4) */
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&exp, &grp->P, 1));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&exp, 2));
|
|
|
|
/* sqrt(w) = w^((p+1)/4) mod p (for prime p where p = 3 mod 4) */
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(Y, Y /*y^2*/, &exp, &grp->P, NULL));
|
|
|
|
|
|
|
|
/* check parity bit match or else invert Y */
|
|
|
|
/* This quick inversion implementation is valid because Y != 0 for all
|
|
|
|
* Short Weierstrass curves supported by mbedtls, as each supported curve
|
|
|
|
* has an order that is a large prime, so each supported curve does not
|
|
|
|
* have any point of order 2, and a point with Y == 0 would be of order 2 */
|
|
|
|
if (mbedtls_mpi_get_bit(Y, 0) != parity_bit) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(Y, &grp->P, Y));
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
mbedtls_mpi_free(&exp);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* For curves in short Weierstrass form, we do all the internal operations in
|
|
|
|
* Jacobian coordinates.
|
|
|
|
*
|
2022-07-18 14:48:00 +02:00
|
|
|
* For multiplication, we'll use a comb method with countermeasures against
|
2018-02-08 19:04:43 +01:00
|
|
|
* SPA, hence timing attacks.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normalize jacobian coordinates so that Z == 0 || Z == 1 (GECC 3.2.1)
|
|
|
|
* Cost: 1N := 1I + 3M + 1S
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_normalize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-09-25 05:04:06 +02:00
|
|
|
if (MPI_ECP_CMP_INT(&pt->Z, 0) == 0) {
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_internal_ecp_grp_capable(grp)) {
|
|
|
|
return mbedtls_internal_ecp_normalize_jac(grp, pt);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif /* MBEDTLS_ECP_NORMALIZE_JAC_ALT */
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
2021-12-21 12:54:05 +01:00
|
|
|
#else
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi T;
|
|
|
|
mbedtls_mpi_init(&T);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_INV(&T, &pt->Z); /* T <- 1 / Z */
|
|
|
|
MPI_ECP_MUL(&pt->Y, &pt->Y, &T); /* Y' <- Y*T = Y / Z */
|
|
|
|
MPI_ECP_SQR(&T, &T); /* T <- T^2 = 1 / Z^2 */
|
|
|
|
MPI_ECP_MUL(&pt->X, &pt->X, &T); /* X <- X * T = X / Z^2 */
|
|
|
|
MPI_ECP_MUL(&pt->Y, &pt->Y, &T); /* Y'' <- Y' * T = Y / Z^3 */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_LSET(&pt->Z, 1);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi_free(&T);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) */
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normalize jacobian coordinates of an array of (pointers to) points,
|
|
|
|
* using Montgomery's trick to perform only one inversion mod P.
|
|
|
|
* (See for example Cohen's "A Course in Computational Algebraic Number
|
|
|
|
* Theory", Algorithm 10.3.4.)
|
|
|
|
*
|
|
|
|
* Warning: fails (returning an error) if one of the points is zero!
|
|
|
|
* This should never happen, see choice of w in ecp_mul_comb().
|
|
|
|
*
|
|
|
|
* Cost: 1N(t) := 1I + (6t - 3)M + 1S
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_normalize_jac_many(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point *T[], size_t T_size)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
if (T_size < 2) {
|
|
|
|
return ecp_normalize_jac(grp, *T);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_internal_ecp_grp_capable(grp)) {
|
|
|
|
return mbedtls_internal_ecp_normalize_jac_many(grp, T, T_size);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
2021-12-21 12:54:05 +01:00
|
|
|
#else
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t i;
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi *c, t;
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((c = mbedtls_calloc(T_size, sizeof(mbedtls_mpi))) == NULL) {
|
|
|
|
return MBEDTLS_ERR_ECP_ALLOC_FAILED;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi_init(&t);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
mpi_init_many(c, T_size);
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
2023-09-25 05:04:06 +02:00
|
|
|
* c[i] = Z_0 * ... * Z_i, i = 0,..,n := T_size-1
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_MOV(&c[0], &T[0]->Z);
|
2023-04-18 10:38:24 +02:00
|
|
|
for (i = 1; i < T_size; i++) {
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_MUL(&c[i], &c[i-1], &T[i]->Z);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-09-25 05:04:06 +02:00
|
|
|
* c[n] = 1 / (Z_0 * ... * Z_n) mod P
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_INV(&c[T_size-1], &c[T_size-1]);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (i = T_size - 1;; i--) {
|
2023-09-25 05:04:06 +02:00
|
|
|
/* At the start of iteration i (note that i decrements), we have
|
|
|
|
* - c[j] = Z_0 * .... * Z_j for j < i,
|
|
|
|
* - c[j] = 1 / (Z_0 * .... * Z_j) for j == i,
|
|
|
|
*
|
|
|
|
* This is maintained via
|
|
|
|
* - c[i-1] <- c[i] * Z_i
|
|
|
|
*
|
|
|
|
* We also derive 1/Z_i = c[i] * c[i-1] for i>0 and use that
|
|
|
|
* to do the actual normalization. For i==0, we already have
|
|
|
|
* c[0] = 1 / Z_0.
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
|
|
|
|
if (i > 0) {
|
|
|
|
/* Compute 1/Z_i and establish invariant for the next iteration. */
|
|
|
|
MPI_ECP_MUL(&t, &c[i], &c[i-1]);
|
|
|
|
MPI_ECP_MUL(&c[i-1], &c[i], &T[i]->Z);
|
2023-04-18 10:38:24 +02:00
|
|
|
} else {
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_MOV(&t, &c[0]);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* Now t holds 1 / Z_i; normalize as in ecp_normalize_jac() */
|
|
|
|
MPI_ECP_MUL(&T[i]->Y, &T[i]->Y, &t);
|
|
|
|
MPI_ECP_SQR(&t, &t);
|
|
|
|
MPI_ECP_MUL(&T[i]->X, &T[i]->X, &t);
|
|
|
|
MPI_ECP_MUL(&T[i]->Y, &T[i]->Y, &t);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Post-precessing: reclaim some memory by shrinking coordinates
|
|
|
|
* - not storing Z (always 1)
|
|
|
|
* - shrinking other coordinates, but still keeping the same number of
|
|
|
|
* limbs as P, as otherwise it will too likely be regrown too fast.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->X, grp->P.n));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(&T[i]->Y, grp->P.n));
|
2023-09-25 05:04:06 +02:00
|
|
|
|
|
|
|
MPI_ECP_LSET(&T[i]->Z, 1);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (i == 0) {
|
2018-02-08 19:04:43 +01:00
|
|
|
break;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi_free(&t);
|
|
|
|
mpi_free_many(c, T_size);
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_free(c);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) */
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Conditional point inversion: Q -> -Q = (Q.X, -Q.Y, Q.Z) without leak.
|
|
|
|
* "inv" must be 0 (don't invert) or 1 (invert) or the result will be invalid
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_safe_invert_jac(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point *Q,
|
|
|
|
unsigned char inv)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi tmp;
|
|
|
|
mbedtls_mpi_init(&tmp);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_COND_NEG(&Q->Y, inv);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi_free(&tmp);
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Point doubling R = 2 P, Jacobian coordinates
|
|
|
|
*
|
|
|
|
* Based on http://www.hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-1998-cmo-2 .
|
|
|
|
*
|
|
|
|
* We follow the variable naming fairly closely. The formula variations that trade a MUL for a SQR
|
|
|
|
* (plus a few ADDs) aren't useful as our bignum implementation doesn't distinguish squaring.
|
|
|
|
*
|
|
|
|
* Standard optimizations are applied when curve parameter A is one of { 0, -3 }.
|
|
|
|
*
|
|
|
|
* Cost: 1D := 3M + 4S (A == 0)
|
|
|
|
* 4M + 4S (A == -3)
|
|
|
|
* 3M + 6S + 1a otherwise
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_double_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
2023-09-25 05:04:06 +02:00
|
|
|
const mbedtls_ecp_point *P,
|
|
|
|
mbedtls_mpi tmp[4])
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
dbl_count++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_internal_ecp_grp_capable(grp)) {
|
|
|
|
return mbedtls_internal_ecp_double_jac(grp, R, P);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif /* MBEDTLS_ECP_DOUBLE_JAC_ALT */
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_JAC_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
2021-12-21 12:54:05 +01:00
|
|
|
#else
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Special case for A = -3 */
|
2023-09-25 05:04:06 +02:00
|
|
|
if (mbedtls_ecp_group_a_is_minus_3(grp)) {
|
|
|
|
/* tmp[0] <- M = 3(X + Z^2)(X - Z^2) */
|
|
|
|
MPI_ECP_SQR(&tmp[1], &P->Z);
|
|
|
|
MPI_ECP_ADD(&tmp[2], &P->X, &tmp[1]);
|
|
|
|
MPI_ECP_SUB(&tmp[3], &P->X, &tmp[1]);
|
|
|
|
MPI_ECP_MUL(&tmp[1], &tmp[2], &tmp[3]);
|
|
|
|
MPI_ECP_MUL_INT(&tmp[0], &tmp[1], 3);
|
2023-04-18 10:38:24 +02:00
|
|
|
} else {
|
2023-09-25 05:04:06 +02:00
|
|
|
/* tmp[0] <- M = 3.X^2 + A.Z^4 */
|
|
|
|
MPI_ECP_SQR(&tmp[1], &P->X);
|
|
|
|
MPI_ECP_MUL_INT(&tmp[0], &tmp[1], 3);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Optimize away for "koblitz" curves with A = 0 */
|
2023-09-25 05:04:06 +02:00
|
|
|
if (MPI_ECP_CMP_INT(&grp->A, 0) != 0) {
|
2018-02-08 19:04:43 +01:00
|
|
|
/* M += A.Z^4 */
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_SQR(&tmp[1], &P->Z);
|
|
|
|
MPI_ECP_SQR(&tmp[2], &tmp[1]);
|
|
|
|
MPI_ECP_MUL(&tmp[1], &tmp[2], &grp->A);
|
|
|
|
MPI_ECP_ADD(&tmp[0], &tmp[0], &tmp[1]);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* tmp[1] <- S = 4.X.Y^2 */
|
|
|
|
MPI_ECP_SQR(&tmp[2], &P->Y);
|
|
|
|
MPI_ECP_SHIFT_L(&tmp[2], 1);
|
|
|
|
MPI_ECP_MUL(&tmp[1], &P->X, &tmp[2]);
|
|
|
|
MPI_ECP_SHIFT_L(&tmp[1], 1);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* tmp[3] <- U = 8.Y^4 */
|
|
|
|
MPI_ECP_SQR(&tmp[3], &tmp[2]);
|
|
|
|
MPI_ECP_SHIFT_L(&tmp[3], 1);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* tmp[2] <- T = M^2 - 2.S */
|
|
|
|
MPI_ECP_SQR(&tmp[2], &tmp[0]);
|
|
|
|
MPI_ECP_SUB(&tmp[2], &tmp[2], &tmp[1]);
|
|
|
|
MPI_ECP_SUB(&tmp[2], &tmp[2], &tmp[1]);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* tmp[1] <- S = M(S - T) - U */
|
|
|
|
MPI_ECP_SUB(&tmp[1], &tmp[1], &tmp[2]);
|
|
|
|
MPI_ECP_MUL(&tmp[1], &tmp[1], &tmp[0]);
|
|
|
|
MPI_ECP_SUB(&tmp[1], &tmp[1], &tmp[3]);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* tmp[3] <- U = 2.Y.Z */
|
|
|
|
MPI_ECP_MUL(&tmp[3], &P->Y, &P->Z);
|
|
|
|
MPI_ECP_SHIFT_L(&tmp[3], 1);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* Store results */
|
|
|
|
MPI_ECP_MOV(&R->X, &tmp[2]);
|
|
|
|
MPI_ECP_MOV(&R->Y, &tmp[1]);
|
|
|
|
MPI_ECP_MOV(&R->Z, &tmp[3]);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) */
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Addition: R = P + Q, mixed affine-Jacobian coordinates (GECC 3.22)
|
|
|
|
*
|
|
|
|
* The coordinates of Q must be normalized (= affine),
|
|
|
|
* but those of P don't need to. R is not normalized.
|
|
|
|
*
|
2023-09-25 05:04:06 +02:00
|
|
|
* P,Q,R may alias, but only at the level of EC points: they must be either
|
|
|
|
* equal as pointers, or disjoint (including the coordinate data buffers).
|
|
|
|
* Fine-grained aliasing at the level of coordinates is not supported.
|
|
|
|
*
|
2018-02-08 19:04:43 +01:00
|
|
|
* Special cases: (1) P or Q is zero, (2) R is zero, (3) P == Q.
|
|
|
|
* None of these cases can happen as intermediate step in ecp_mul_comb():
|
|
|
|
* - at each step, P, Q and R are multiples of the base point, the factor
|
|
|
|
* being less than its order, so none of them is zero;
|
|
|
|
* - Q is an odd multiple of the base point, P an even multiple,
|
|
|
|
* due to the choice of precomputed points in the modified comb method.
|
|
|
|
* So branches for these cases do not leak secret information.
|
|
|
|
*
|
|
|
|
* Cost: 1A := 8M + 3S
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_add_mixed(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
2023-09-25 05:04:06 +02:00
|
|
|
const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
|
|
|
|
mbedtls_mpi tmp[4])
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
add_count++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_ADD_MIXED_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_internal_ecp_grp_capable(grp)) {
|
|
|
|
return mbedtls_internal_ecp_add_mixed(grp, R, P, Q);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif /* MBEDTLS_ECP_ADD_MIXED_ALT */
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_ADD_MIXED_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
2021-12-21 12:54:05 +01:00
|
|
|
#else
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-09-25 05:04:06 +02:00
|
|
|
|
|
|
|
/* NOTE: Aliasing between input and output is allowed, so one has to make
|
|
|
|
* sure that at the point X,Y,Z are written, {P,Q}->{X,Y,Z} are no
|
|
|
|
* longer read from. */
|
|
|
|
mbedtls_mpi * const X = &R->X;
|
|
|
|
mbedtls_mpi * const Y = &R->Y;
|
|
|
|
mbedtls_mpi * const Z = &R->Z;
|
|
|
|
|
|
|
|
if (!MPI_ECP_VALID(&Q->Z)) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Trivial cases: P == 0 or Q == 0 (case 1)
|
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
if (MPI_ECP_CMP_INT(&P->Z, 0) == 0) {
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_ecp_copy(R, Q);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
if (MPI_ECP_CMP_INT(&Q->Z, 0) == 0) {
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_ecp_copy(R, P);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure Q coordinates are normalized
|
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
if (MPI_ECP_CMP_INT(&Q->Z, 1) != 0) {
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_SQR(&tmp[0], &P->Z);
|
|
|
|
MPI_ECP_MUL(&tmp[1], &tmp[0], &P->Z);
|
|
|
|
MPI_ECP_MUL(&tmp[0], &tmp[0], &Q->X);
|
|
|
|
MPI_ECP_MUL(&tmp[1], &tmp[1], &Q->Y);
|
|
|
|
MPI_ECP_SUB(&tmp[0], &tmp[0], &P->X);
|
|
|
|
MPI_ECP_SUB(&tmp[1], &tmp[1], &P->Y);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Special cases (2) and (3) */
|
2023-09-25 05:04:06 +02:00
|
|
|
if (MPI_ECP_CMP_INT(&tmp[0], 0) == 0) {
|
|
|
|
if (MPI_ECP_CMP_INT(&tmp[1], 0) == 0) {
|
|
|
|
ret = ecp_double_jac(grp, R, P, tmp);
|
2018-02-08 19:04:43 +01:00
|
|
|
goto cleanup;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else {
|
|
|
|
ret = mbedtls_ecp_set_zero(R);
|
2018-02-08 19:04:43 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* {P,Q}->Z no longer used, so OK to write to Z even if there's aliasing. */
|
|
|
|
MPI_ECP_MUL(Z, &P->Z, &tmp[0]);
|
|
|
|
MPI_ECP_SQR(&tmp[2], &tmp[0]);
|
|
|
|
MPI_ECP_MUL(&tmp[3], &tmp[2], &tmp[0]);
|
|
|
|
MPI_ECP_MUL(&tmp[2], &tmp[2], &P->X);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_MOV(&tmp[0], &tmp[2]);
|
|
|
|
MPI_ECP_SHIFT_L(&tmp[0], 1);
|
|
|
|
|
|
|
|
/* {P,Q}->X no longer used, so OK to write to X even if there's aliasing. */
|
|
|
|
MPI_ECP_SQR(X, &tmp[1]);
|
|
|
|
MPI_ECP_SUB(X, X, &tmp[0]);
|
|
|
|
MPI_ECP_SUB(X, X, &tmp[3]);
|
|
|
|
MPI_ECP_SUB(&tmp[2], &tmp[2], X);
|
|
|
|
MPI_ECP_MUL(&tmp[2], &tmp[2], &tmp[1]);
|
|
|
|
MPI_ECP_MUL(&tmp[3], &tmp[3], &P->Y);
|
|
|
|
/* {P,Q}->Y no longer used, so OK to write to Y even if there's aliasing. */
|
|
|
|
MPI_ECP_SUB(Y, &tmp[2], &tmp[3]);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
cleanup:
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_ADD_MIXED_ALT) */
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Randomize jacobian coordinates:
|
|
|
|
* (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l
|
|
|
|
* This is sort of the reverse operation of ecp_normalize_jac().
|
|
|
|
*
|
|
|
|
* This countermeasure was first suggested in [2].
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_randomize_jac(const mbedtls_ecp_group *grp, mbedtls_ecp_point *pt,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_internal_ecp_grp_capable(grp)) {
|
|
|
|
return mbedtls_internal_ecp_randomize_jac(grp, pt, f_rng, p_rng);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif /* MBEDTLS_ECP_RANDOMIZE_JAC_ALT */
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
2021-12-21 12:54:05 +01:00
|
|
|
#else
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi l;
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi_init(&l);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Generate l such that 1 < l < p */
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_RAND(&l);
|
|
|
|
|
|
|
|
/* Z' = l * Z */
|
|
|
|
MPI_ECP_MUL(&pt->Z, &pt->Z, &l);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* Y' = l * Y */
|
|
|
|
MPI_ECP_MUL(&pt->Y, &pt->Y, &l);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* X' = l^2 * X */
|
|
|
|
MPI_ECP_SQR(&l, &l);
|
|
|
|
MPI_ECP_MUL(&pt->X, &pt->X, &l);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* Y'' = l^2 * Y' = l^3 * Y */
|
|
|
|
MPI_ECP_MUL(&pt->Y, &pt->Y, &l);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi_free(&l);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
|
2021-12-21 12:54:05 +01:00
|
|
|
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) */
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check and define parameters used by the comb method (see below for details)
|
|
|
|
*/
|
|
|
|
#if MBEDTLS_ECP_WINDOW_SIZE < 2 || MBEDTLS_ECP_WINDOW_SIZE > 7
|
|
|
|
#error "MBEDTLS_ECP_WINDOW_SIZE out of bounds"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* d = ceil( n / w ) */
|
2023-04-18 10:38:24 +02:00
|
|
|
#define COMB_MAX_D (MBEDTLS_ECP_MAX_BITS + 1) / 2
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* number of precomputed points */
|
2023-04-18 10:38:24 +02:00
|
|
|
#define COMB_MAX_PRE (1 << (MBEDTLS_ECP_WINDOW_SIZE - 1))
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the representation of m that will be used with our comb method.
|
|
|
|
*
|
|
|
|
* The basic comb method is described in GECC 3.44 for example. We use a
|
|
|
|
* modified version that provides resistance to SPA by avoiding zero
|
|
|
|
* digits in the representation as in [3]. We modify the method further by
|
|
|
|
* requiring that all K_i be odd, which has the small cost that our
|
2019-02-16 17:19:46 +01:00
|
|
|
* representation uses one more K_i, due to carries, but saves on the size of
|
|
|
|
* the precomputed table.
|
2018-02-08 19:04:43 +01:00
|
|
|
*
|
2019-02-16 17:19:46 +01:00
|
|
|
* Summary of the comb method and its modifications:
|
|
|
|
*
|
|
|
|
* - The goal is to compute m*P for some w*d-bit integer m.
|
|
|
|
*
|
|
|
|
* - The basic comb method splits m into the w-bit integers
|
|
|
|
* x[0] .. x[d-1] where x[i] consists of the bits in m whose
|
|
|
|
* index has residue i modulo d, and computes m * P as
|
|
|
|
* S[x[0]] + 2 * S[x[1]] + .. + 2^(d-1) S[x[d-1]], where
|
|
|
|
* S[i_{w-1} .. i_0] := i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + i_0 P.
|
|
|
|
*
|
|
|
|
* - If it happens that, say, x[i+1]=0 (=> S[x[i+1]]=0), one can replace the sum by
|
|
|
|
* .. + 2^{i-1} S[x[i-1]] - 2^i S[x[i]] + 2^{i+1} S[x[i]] + 2^{i+2} S[x[i+2]] ..,
|
|
|
|
* thereby successively converting it into a form where all summands
|
|
|
|
* are nonzero, at the cost of negative summands. This is the basic idea of [3].
|
|
|
|
*
|
|
|
|
* - More generally, even if x[i+1] != 0, we can first transform the sum as
|
|
|
|
* .. - 2^i S[x[i]] + 2^{i+1} ( S[x[i]] + S[x[i+1]] ) + 2^{i+2} S[x[i+2]] ..,
|
|
|
|
* and then replace S[x[i]] + S[x[i+1]] = S[x[i] ^ x[i+1]] + 2 S[x[i] & x[i+1]].
|
|
|
|
* Performing and iterating this procedure for those x[i] that are even
|
|
|
|
* (keeping track of carry), we can transform the original sum into one of the form
|
|
|
|
* S[x'[0]] +- 2 S[x'[1]] +- .. +- 2^{d-1} S[x'[d-1]] + 2^d S[x'[d]]
|
|
|
|
* with all x'[i] odd. It is therefore only necessary to know S at odd indices,
|
|
|
|
* which is why we are only computing half of it in the first place in
|
|
|
|
* ecp_precompute_comb and accessing it with index abs(i) / 2 in ecp_select_comb.
|
|
|
|
*
|
|
|
|
* - For the sake of compactness, only the seven low-order bits of x[i]
|
|
|
|
* are used to represent its absolute value (K_i in the paper), and the msb
|
|
|
|
* of x[i] encodes the sign (s_i in the paper): it is set if and only if
|
|
|
|
* if s_i == -1;
|
2018-02-08 19:04:43 +01:00
|
|
|
*
|
|
|
|
* Calling conventions:
|
|
|
|
* - x is an array of size d + 1
|
|
|
|
* - w is the size, ie number of teeth, of the comb, and must be between
|
|
|
|
* 2 and 7 (in practice, between 2 and MBEDTLS_ECP_WINDOW_SIZE)
|
|
|
|
* - m is the MPI, expected to be odd and such that bitlength(m) <= w * d
|
|
|
|
* (the result will be incorrect if these assumptions are not satisfied)
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static void ecp_comb_recode_core(unsigned char x[], size_t d,
|
|
|
|
unsigned char w, const mbedtls_mpi *m)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
unsigned char c, cc, adjust;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
memset(x, 0, d+1);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* First get the classical comb values (except for x_d = 0) */
|
2023-04-18 10:38:24 +02:00
|
|
|
for (i = 0; i < d; i++) {
|
|
|
|
for (j = 0; j < w; j++) {
|
|
|
|
x[i] |= mbedtls_mpi_get_bit(m, i + d * j) << j;
|
|
|
|
}
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Now make sure x_1 .. x_d are odd */
|
|
|
|
c = 0;
|
2023-04-18 10:38:24 +02:00
|
|
|
for (i = 1; i <= d; i++) {
|
2018-02-08 19:04:43 +01:00
|
|
|
/* Add carry and update it */
|
|
|
|
cc = x[i] & c;
|
|
|
|
x[i] = x[i] ^ c;
|
|
|
|
c = cc;
|
|
|
|
|
|
|
|
/* Adjust if needed, avoiding branches */
|
2023-04-18 10:38:24 +02:00
|
|
|
adjust = 1 - (x[i] & 0x01);
|
|
|
|
c |= x[i] & (x[i-1] * adjust);
|
|
|
|
x[i] = x[i] ^ (x[i-1] * adjust);
|
2018-02-08 19:04:43 +01:00
|
|
|
x[i-1] |= adjust << 7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-02-16 17:19:46 +01:00
|
|
|
* Precompute points for the adapted comb method
|
2018-02-08 19:04:43 +01:00
|
|
|
*
|
2019-02-16 17:19:46 +01:00
|
|
|
* Assumption: T must be able to hold 2^{w - 1} elements.
|
2018-02-08 19:04:43 +01:00
|
|
|
*
|
2019-02-16 17:19:46 +01:00
|
|
|
* Operation: If i = i_{w-1} ... i_1 is the binary representation of i,
|
|
|
|
* sets T[i] = i_{w-1} 2^{(w-1)d} P + ... + i_1 2^d P + P.
|
2018-02-08 19:04:43 +01:00
|
|
|
*
|
|
|
|
* Cost: d(w-1) D + (2^{w-1} - 1) A + 1 N(w-1) + 1 N(2^{w-1} - 1)
|
2019-02-16 17:19:46 +01:00
|
|
|
*
|
|
|
|
* Note: Even comb values (those where P would be omitted from the
|
|
|
|
* sum defining T[i] above) are not needed in our adaption
|
|
|
|
* the comb method. See ecp_comb_recode_core().
|
|
|
|
*
|
|
|
|
* This function currently works in four steps:
|
|
|
|
* (1) [dbl] Computation of intermediate T[i] for 2-power values of i
|
|
|
|
* (2) [norm_dbl] Normalization of coordinates of these T[i]
|
|
|
|
* (3) [add] Computation of all T[i]
|
|
|
|
* (4) [norm_add] Normalization of all T[i]
|
|
|
|
*
|
|
|
|
* Step 1 can be interrupted but not the others; together with the final
|
|
|
|
* coordinate normalization they are the largest steps done at once, depending
|
|
|
|
* on the window size. Here are operation counts for P-256:
|
|
|
|
*
|
|
|
|
* step (2) (3) (4)
|
|
|
|
* w = 5 142 165 208
|
|
|
|
* w = 4 136 77 160
|
|
|
|
* w = 3 130 33 136
|
|
|
|
* w = 2 124 11 124
|
|
|
|
*
|
|
|
|
* So if ECC operations are blocking for too long even with a low max_ops
|
|
|
|
* value, it's useful to set MBEDTLS_ECP_WINDOW_SIZE to a lower value in order
|
|
|
|
* to minimize maximum blocking time.
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_precompute_comb(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point T[], const mbedtls_ecp_point *P,
|
|
|
|
unsigned char w, size_t d,
|
|
|
|
mbedtls_ecp_restart_ctx *rs_ctx)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-02-16 17:19:46 +01:00
|
|
|
unsigned char i;
|
|
|
|
size_t j = 0;
|
2023-04-18 10:38:24 +02:00
|
|
|
const unsigned char T_size = 1U << (w - 1);
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_ecp_point *cur, *TT[COMB_MAX_PRE - 1] = { NULL };
|
|
|
|
|
|
|
|
mbedtls_mpi tmp[4];
|
|
|
|
|
|
|
|
mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
|
|
|
|
if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) {
|
2019-02-16 17:19:46 +01:00
|
|
|
goto dbl;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
|
|
|
if (rs_ctx->rsm->state == ecp_rsm_pre_norm_dbl) {
|
2019-02-16 17:19:46 +01:00
|
|
|
goto norm_dbl;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
|
|
|
if (rs_ctx->rsm->state == ecp_rsm_pre_add) {
|
2019-02-16 17:19:46 +01:00
|
|
|
goto add;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
|
|
|
if (rs_ctx->rsm->state == ecp_rsm_pre_norm_add) {
|
2019-02-16 17:19:46 +01:00
|
|
|
goto norm_add;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void) rs_ctx;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->rsm->state = ecp_rsm_pre_dbl;
|
|
|
|
|
|
|
|
/* initial state for the loop */
|
|
|
|
rs_ctx->rsm->i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbl:
|
|
|
|
#endif
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Set T[0] = P and
|
|
|
|
* T[2^{l-1}] = 2^{dl} P for l = 1 .. w-1 (this is not the final value)
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&T[0], P));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) {
|
2019-02-16 17:19:46 +01:00
|
|
|
j = rs_ctx->rsm->i;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else
|
2019-02-16 17:19:46 +01:00
|
|
|
#endif
|
2023-04-18 10:38:24 +02:00
|
|
|
j = 0;
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (; j < d * (w - 1); j++) {
|
|
|
|
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
i = 1U << (j / d);
|
2018-02-08 19:04:43 +01:00
|
|
|
cur = T + i;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (j % d == 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(cur, T + (i >> 1)));
|
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_double_jac(grp, cur, cur, tmp));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->rsm->state = ecp_rsm_pre_norm_dbl;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
norm_dbl:
|
|
|
|
#endif
|
|
|
|
/*
|
2023-09-25 05:04:06 +02:00
|
|
|
* Normalize current elements in T to allow them to be used in
|
|
|
|
* ecp_add_mixed() below, which requires one normalized input.
|
|
|
|
*
|
|
|
|
* As T has holes, use an auxiliary array of pointers to elements in T.
|
|
|
|
*
|
2019-02-16 17:19:46 +01:00
|
|
|
*/
|
|
|
|
j = 0;
|
2023-04-18 10:38:24 +02:00
|
|
|
for (i = 1; i < T_size; i <<= 1) {
|
2019-02-16 17:19:46 +01:00
|
|
|
TT[j++] = T + i;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j));
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->rsm->state = ecp_rsm_pre_add;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
add:
|
|
|
|
#endif
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Compute the remaining ones using the minimal number of additions
|
|
|
|
* Be careful to update T[2^l] only after using it!
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ECP_BUDGET((T_size - 1) * MBEDTLS_ECP_OPS_ADD);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (i = 1; i < T_size; i <<= 1) {
|
2018-02-08 19:04:43 +01:00
|
|
|
j = i;
|
2023-04-18 10:38:24 +02:00
|
|
|
while (j--) {
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_add_mixed(grp, &T[i + j], &T[j], &T[i], tmp));
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->rsm->state = ecp_rsm_pre_norm_add;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
norm_add:
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* Normalize final elements in T. Even though there are no holes now, we
|
|
|
|
* still need the auxiliary array for homogeneity with the previous
|
|
|
|
* call. Also, skip T[0] which is already normalised, being a copy of P.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
for (j = 0; j + 1 < T_size; j++) {
|
2019-02-16 17:19:46 +01:00
|
|
|
TT[j] = T + j + 1;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV + 6 * j - 2);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_normalize_jac_many(grp, TT, j));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* Free Z coordinate (=1 after normalization) to save RAM.
|
|
|
|
* This makes T[i] invalid as mbedtls_ecp_points, but this is OK
|
|
|
|
* since from this point onwards, they are only accessed indirectly
|
|
|
|
* via the getter function ecp_select_comb() which does set the
|
|
|
|
* target's Z coordinate to 1. */
|
|
|
|
for (i = 0; i < T_size; i++) {
|
|
|
|
mbedtls_mpi_free(&T[i].Z);
|
|
|
|
}
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
cleanup:
|
2023-09-25 05:04:06 +02:00
|
|
|
|
|
|
|
mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
|
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
|
|
|
|
ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
|
|
|
|
if (rs_ctx->rsm->state == ecp_rsm_pre_dbl) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->rsm->i = j;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
#endif
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Select precomputed point: R = sign(i) * T[ abs(i) / 2 ]
|
2019-02-16 17:19:46 +01:00
|
|
|
*
|
|
|
|
* See ecp_comb_recode_core() for background
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_select_comb(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_ecp_point T[], unsigned char T_size,
|
|
|
|
unsigned char i)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
unsigned char ii, j;
|
|
|
|
|
|
|
|
/* Ignore the "sign" bit and scale down */
|
2023-04-18 10:38:24 +02:00
|
|
|
ii = (i & 0x7Fu) >> 1;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Read the whole table to thwart cache-based timing attacks */
|
2023-04-18 10:38:24 +02:00
|
|
|
for (j = 0; j < T_size; j++) {
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_COND_ASSIGN(&R->X, &T[j].X, j == ii);
|
|
|
|
MPI_ECP_COND_ASSIGN(&R->Y, &T[j].Y, j == ii);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Safely invert result if i is "negative" */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, R, i >> 7));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_LSET(&R->Z, 1);
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Core multiplication algorithm for the (modified) comb method.
|
|
|
|
* This part is actually common with the basic comb method (GECC 3.44)
|
|
|
|
*
|
|
|
|
* Cost: d A + d D + 1 R
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_mul_comb_core(const mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_ecp_point T[], unsigned char T_size,
|
|
|
|
const unsigned char x[], size_t d,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng,
|
|
|
|
mbedtls_ecp_restart_ctx *rs_ctx)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
mbedtls_ecp_point Txi;
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi tmp[4];
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t i;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_point_init(&Txi);
|
2023-09-25 05:04:06 +02:00
|
|
|
mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
#if !defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
(void) rs_ctx;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
|
|
|
|
rs_ctx->rsm->state != ecp_rsm_comb_core) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->rsm->i = 0;
|
|
|
|
rs_ctx->rsm->state = ecp_rsm_comb_core;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* new 'if' instead of nested for the sake of the 'else' branch */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->i != 0) {
|
2019-02-16 17:19:46 +01:00
|
|
|
/* restore current index (R already pointing to rs_ctx->rsm->R) */
|
|
|
|
i = rs_ctx->rsm->i;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else
|
2019-02-16 17:19:46 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Start with a non-zero point and randomize its coordinates */
|
|
|
|
i = d;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_select_comb(grp, R, T, T_size, x[i]));
|
2023-09-25 05:04:06 +02:00
|
|
|
if (f_rng != 0) {
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, R, f_rng, p_rng));
|
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
while (i != 0) {
|
|
|
|
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_DBL + MBEDTLS_ECP_OPS_ADD);
|
2019-02-16 17:19:46 +01:00
|
|
|
--i;
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_double_jac(grp, R, R, tmp));
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_select_comb(grp, &Txi, T, T_size, x[i]));
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_add_mixed(grp, R, R, &Txi, tmp));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_point_free(&Txi);
|
2023-09-25 05:04:06 +02:00
|
|
|
mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL &&
|
|
|
|
ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->rsm->i = i;
|
|
|
|
/* no need to save R, already pointing to rs_ctx->rsm->R */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-02-16 17:19:46 +01:00
|
|
|
* Recode the scalar to get constant-time comb multiplication
|
|
|
|
*
|
|
|
|
* As the actual scalar recoding needs an odd scalar as a starting point,
|
|
|
|
* this wrapper ensures that by replacing m by N - m if necessary, and
|
|
|
|
* informs the caller that the result of multiplication will be negated.
|
|
|
|
*
|
|
|
|
* This works because we only support large prime order for Short Weierstrass
|
|
|
|
* curves, so N is always odd hence either m or N - m is.
|
|
|
|
*
|
|
|
|
* See ecp_comb_recode_core() for background.
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_comb_recode_scalar(const mbedtls_ecp_group *grp,
|
|
|
|
const mbedtls_mpi *m,
|
|
|
|
unsigned char k[COMB_MAX_D + 1],
|
|
|
|
size_t d,
|
|
|
|
unsigned char w,
|
|
|
|
unsigned char *parity_trick)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
mbedtls_mpi M, mm;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_init(&M);
|
|
|
|
mbedtls_mpi_init(&mm);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
/* N is always odd (see above), just make extra sure */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_get_bit(&grp->N, 0) != 1) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
/* do we need the parity trick? */
|
2023-04-18 10:38:24 +02:00
|
|
|
*parity_trick = (mbedtls_mpi_get_bit(m, 0) == 0);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
/* execute parity fix in constant time */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&M, m));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&mm, &grp->N, m));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_safe_cond_assign(&M, &mm, *parity_trick));
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
/* actual scalar recoding */
|
2023-04-18 10:38:24 +02:00
|
|
|
ecp_comb_recode_core(k, d, w, &M);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_free(&mm);
|
|
|
|
mbedtls_mpi_free(&M);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform comb multiplication (for short Weierstrass curves)
|
|
|
|
* once the auxiliary table has been pre-computed.
|
|
|
|
*
|
|
|
|
* Scalar recoding may use a parity trick that makes us compute -m * P,
|
|
|
|
* if that is the case we'll need to recover m * P at the end.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_mul_comb_after_precomp(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m,
|
|
|
|
const mbedtls_ecp_point *T,
|
|
|
|
unsigned char T_size,
|
|
|
|
unsigned char w,
|
|
|
|
size_t d,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng,
|
|
|
|
mbedtls_ecp_restart_ctx *rs_ctx)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-02-16 17:19:46 +01:00
|
|
|
unsigned char parity_trick;
|
|
|
|
unsigned char k[COMB_MAX_D + 1];
|
|
|
|
mbedtls_ecp_point *RR = R;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
RR = &rs_ctx->rsm->R;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx->rsm->state == ecp_rsm_final_norm) {
|
2019-02-16 17:19:46 +01:00
|
|
|
goto final_norm;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_comb_recode_scalar(grp, m, k, d, w,
|
|
|
|
&parity_trick));
|
|
|
|
MBEDTLS_MPI_CHK(ecp_mul_comb_core(grp, RR, T, T_size, k, d,
|
|
|
|
f_rng, p_rng, rs_ctx));
|
|
|
|
MBEDTLS_MPI_CHK(ecp_safe_invert_jac(grp, RR, parity_trick));
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->rsm->state = ecp_rsm_final_norm;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
final_norm:
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV);
|
2019-02-16 17:19:46 +01:00
|
|
|
#endif
|
2020-04-21 10:37:14 +02:00
|
|
|
/*
|
|
|
|
* Knowledge of the jacobian coordinates may leak the last few bits of the
|
|
|
|
* scalar [1], and since our MPI implementation isn't constant-flow,
|
|
|
|
* inversion (used for coordinate normalization) may leak the full value
|
|
|
|
* of its input via side-channels [2].
|
|
|
|
*
|
|
|
|
* [1] https://eprint.iacr.org/2003/191
|
|
|
|
* [2] https://eprint.iacr.org/2020/055
|
|
|
|
*
|
|
|
|
* Avoid the leak by randomizing coordinates before we normalize them.
|
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
if (f_rng != 0) {
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_randomize_jac(grp, RR, f_rng, p_rng));
|
|
|
|
}
|
2020-04-21 10:37:14 +02:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, RR));
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, RR));
|
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pick window size based on curve size and whether we optimize for base point
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static unsigned char ecp_pick_window_size(const mbedtls_ecp_group *grp,
|
|
|
|
unsigned char p_eq_g)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
|
|
|
unsigned char w;
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Minimize the number of multiplications, that is minimize
|
|
|
|
* 10 * d * w + 18 * 2^(w-1) + 11 * d + 7 * w, with d = ceil( nbits / w )
|
|
|
|
* (see costs of the various parts, with 1S = 1M)
|
|
|
|
*/
|
|
|
|
w = grp->nbits >= 384 ? 5 : 4;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If P == G, pre-compute a bit more, since this may be re-used later.
|
|
|
|
* Just adding one avoids upping the cost of the first mul too much,
|
|
|
|
* and the memory cost too.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p_eq_g) {
|
2018-02-08 19:04:43 +01:00
|
|
|
w++;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
2023-09-25 05:04:06 +02:00
|
|
|
* If static comb table may not be used (!p_eq_g) or static comb table does
|
|
|
|
* not exists, make sure w is within bounds.
|
2018-02-08 19:04:43 +01:00
|
|
|
* (The last test is useful only for very small curves in the test suite.)
|
2023-09-25 05:04:06 +02:00
|
|
|
*
|
|
|
|
* The user reduces MBEDTLS_ECP_WINDOW_SIZE does not changes the size of
|
|
|
|
* static comb table, because the size of static comb table is fixed when
|
|
|
|
* it is generated.
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
#if (MBEDTLS_ECP_WINDOW_SIZE < 6)
|
2023-09-25 05:04:06 +02:00
|
|
|
if ((!p_eq_g || !ecp_group_is_static_comb_table(grp)) && w > MBEDTLS_ECP_WINDOW_SIZE) {
|
2018-02-08 19:04:43 +01:00
|
|
|
w = MBEDTLS_ECP_WINDOW_SIZE;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif
|
2023-04-18 10:38:24 +02:00
|
|
|
if (w >= grp->nbits) {
|
2018-02-08 19:04:43 +01:00
|
|
|
w = 2;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return w;
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Multiplication using the comb method - for curves in short Weierstrass form
|
|
|
|
*
|
|
|
|
* This function is mainly responsible for administrative work:
|
|
|
|
* - managing the restart context if enabled
|
|
|
|
* - managing the table of precomputed points (passed between the below two
|
2022-07-18 14:48:00 +02:00
|
|
|
* functions): allocation, computation, ownership transfer, freeing.
|
2019-02-16 17:19:46 +01:00
|
|
|
*
|
|
|
|
* It delegates the actual arithmetic work to:
|
|
|
|
* ecp_precompute_comb() and ecp_mul_comb_with_precomp()
|
|
|
|
*
|
|
|
|
* See comments on ecp_comb_recode_core() regarding the computation strategy.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_mul_comb(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng,
|
|
|
|
mbedtls_ecp_restart_ctx *rs_ctx)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-02-16 17:19:46 +01:00
|
|
|
unsigned char w, p_eq_g, i;
|
|
|
|
size_t d;
|
2020-07-02 15:13:55 +02:00
|
|
|
unsigned char T_size = 0, T_ok = 0;
|
|
|
|
mbedtls_ecp_point *T = NULL;
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ECP_RS_ENTER(rsm);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
/* Is P the base point ? */
|
|
|
|
#if MBEDTLS_ECP_FIXED_POINT_OPTIM == 1
|
2023-09-25 05:04:06 +02:00
|
|
|
p_eq_g = (MPI_ECP_CMP(&P->Y, &grp->G.Y) == 0 &&
|
|
|
|
MPI_ECP_CMP(&P->X, &grp->G.X) == 0);
|
2019-02-16 17:19:46 +01:00
|
|
|
#else
|
|
|
|
p_eq_g = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Pick window size and deduce related sizes */
|
2023-04-18 10:38:24 +02:00
|
|
|
w = ecp_pick_window_size(grp, p_eq_g);
|
|
|
|
T_size = 1U << (w - 1);
|
|
|
|
d = (grp->nbits + w - 1) / w;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
/* Pre-computed table: do we have it already for the base point? */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p_eq_g && grp->T != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
/* second pointer to the same table, will be deleted on exit */
|
|
|
|
T = grp->T;
|
|
|
|
T_ok = 1;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
/* Pre-computed table: do we have one in progress? complete? */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL && rs_ctx->rsm->T != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
/* transfer ownership of T from rsm to local function */
|
|
|
|
T = rs_ctx->rsm->T;
|
|
|
|
rs_ctx->rsm->T = NULL;
|
|
|
|
rs_ctx->rsm->T_size = 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
/* This effectively jumps to the call to mul_comb_after_precomp() */
|
|
|
|
T_ok = rs_ctx->rsm->state >= ecp_rsm_comb_core;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else
|
2019-02-16 17:19:46 +01:00
|
|
|
#endif
|
|
|
|
/* Allocate table if we didn't have any */
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
T = mbedtls_calloc(T_size, sizeof(mbedtls_ecp_point));
|
|
|
|
if (T == NULL) {
|
2018-02-08 19:04:43 +01:00
|
|
|
ret = MBEDTLS_ERR_ECP_ALLOC_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (i = 0; i < T_size; i++) {
|
|
|
|
mbedtls_ecp_point_init(&T[i]);
|
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
T_ok = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compute table (or finish computing it) if not done already */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (!T_ok) {
|
|
|
|
MBEDTLS_MPI_CHK(ecp_precompute_comb(grp, T, P, w, d, rs_ctx));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p_eq_g) {
|
2019-02-16 17:19:46 +01:00
|
|
|
/* almost transfer ownership of T to the group, but keep a copy of
|
|
|
|
* the pointer to use for calling the next function more easily */
|
2018-02-08 19:04:43 +01:00
|
|
|
grp->T = T;
|
2019-02-16 17:19:46 +01:00
|
|
|
grp->T_size = T_size;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
/* Actual comb multiplication using precomputed points */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_mul_comb_after_precomp(grp, R, m,
|
|
|
|
T, T_size, w, d,
|
|
|
|
f_rng, p_rng, rs_ctx));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
cleanup:
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
/* does T belong to the group? */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (T == grp->T) {
|
2019-02-16 17:19:46 +01:00
|
|
|
T = NULL;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
/* does T belong to the restart context? */
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->rsm != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS && T != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
/* transfer ownership of T from local function to rsm */
|
|
|
|
rs_ctx->rsm->T_size = T_size;
|
|
|
|
rs_ctx->rsm->T = T;
|
|
|
|
T = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
/* did T belong to us? then let's destroy it! */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (T != NULL) {
|
|
|
|
for (i = 0; i < T_size; i++) {
|
|
|
|
mbedtls_ecp_point_free(&T[i]);
|
|
|
|
}
|
|
|
|
mbedtls_free(T);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2022-12-21 12:05:54 +01:00
|
|
|
/* prevent caller from using invalid value */
|
2023-04-18 10:38:24 +02:00
|
|
|
int should_free_R = (ret != 0);
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2022-12-21 12:05:54 +01:00
|
|
|
/* don't free R while in progress in case R == P */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
|
2022-12-21 12:05:54 +01:00
|
|
|
should_free_R = 0;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
#endif
|
2023-04-18 10:38:24 +02:00
|
|
|
if (should_free_R) {
|
|
|
|
mbedtls_ecp_point_free(R);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ECP_RS_LEAVE(rsm);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* For Montgomery curves, we do all the internal arithmetic in projective
|
|
|
|
* coordinates. Import/export of points uses only the x coordinates, which is
|
2022-07-18 14:48:00 +02:00
|
|
|
* internally represented as X / Z.
|
2018-02-08 19:04:43 +01:00
|
|
|
*
|
|
|
|
* For scalar multiplication, we'll use a Montgomery ladder.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normalize Montgomery x/z coordinates: X = X/Z, Z = 1
|
|
|
|
* Cost: 1M + 1I
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_normalize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_internal_ecp_grp_capable(grp)) {
|
|
|
|
return mbedtls_internal_ecp_normalize_mxz(grp, P);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif /* MBEDTLS_ECP_NORMALIZE_MXZ_ALT */
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
2021-12-21 12:54:05 +01:00
|
|
|
#else
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_INV(&P->Z, &P->Z);
|
|
|
|
MPI_ECP_MUL(&P->X, &P->X, &P->Z);
|
|
|
|
MPI_ECP_LSET(&P->Z, 1);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) */
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Randomize projective x/z coordinates:
|
|
|
|
* (X, Z) -> (l X, l Z) for random l
|
|
|
|
* This is sort of the reverse operation of ecp_normalize_mxz().
|
|
|
|
*
|
|
|
|
* This countermeasure was first suggested in [2].
|
|
|
|
* Cost: 2M
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_randomize_mxz(const mbedtls_ecp_group *grp, mbedtls_ecp_point *P,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_internal_ecp_grp_capable(grp)) {
|
|
|
|
return mbedtls_internal_ecp_randomize_mxz(grp, P, f_rng, p_rng);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif /* MBEDTLS_ECP_RANDOMIZE_MXZ_ALT */
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
2021-12-21 12:54:05 +01:00
|
|
|
#else
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_mpi l;
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_init(&l);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Generate l such that 1 < l < p */
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_RAND(&l);
|
2021-07-20 12:32:46 +02:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_MUL(&P->X, &P->X, &l);
|
|
|
|
MPI_ECP_MUL(&P->Z, &P->Z, &l);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_free(&l);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE) {
|
2021-12-21 12:54:05 +01:00
|
|
|
ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) */
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Double-and-add: R = 2P, S = P + Q, with d = X(P - Q),
|
|
|
|
* for Montgomery curves in x/z coordinates.
|
|
|
|
*
|
|
|
|
* http://www.hyperelliptic.org/EFD/g1p/auto-code/montgom/xz/ladder/mladd-1987-m.op3
|
|
|
|
* with
|
|
|
|
* d = X1
|
|
|
|
* P = (X2, Z2)
|
|
|
|
* Q = (X3, Z3)
|
|
|
|
* R = (X4, Z4)
|
|
|
|
* S = (X5, Z5)
|
|
|
|
* and eliminating temporary variables tO, ..., t4.
|
|
|
|
*
|
|
|
|
* Cost: 5M + 4S
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_double_add_mxz(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point *R, mbedtls_ecp_point *S,
|
|
|
|
const mbedtls_ecp_point *P, const mbedtls_ecp_point *Q,
|
2023-09-25 05:04:06 +02:00
|
|
|
const mbedtls_mpi *d,
|
|
|
|
mbedtls_mpi T[4])
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_internal_ecp_grp_capable(grp)) {
|
|
|
|
return mbedtls_internal_ecp_double_add_mxz(grp, R, S, P, Q, d);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif /* MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT */
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_NO_FALLBACK) && defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
2021-12-21 12:54:05 +01:00
|
|
|
#else
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-09-25 05:04:06 +02:00
|
|
|
|
|
|
|
MPI_ECP_ADD(&T[0], &P->X, &P->Z); /* Pp := PX + PZ */
|
|
|
|
MPI_ECP_SUB(&T[1], &P->X, &P->Z); /* Pm := PX - PZ */
|
|
|
|
MPI_ECP_ADD(&T[2], &Q->X, &Q->Z); /* Qp := QX + XZ */
|
|
|
|
MPI_ECP_SUB(&T[3], &Q->X, &Q->Z); /* Qm := QX - QZ */
|
|
|
|
MPI_ECP_MUL(&T[3], &T[3], &T[0]); /* Qm * Pp */
|
|
|
|
MPI_ECP_MUL(&T[2], &T[2], &T[1]); /* Qp * Pm */
|
|
|
|
MPI_ECP_SQR(&T[0], &T[0]); /* Pp^2 */
|
|
|
|
MPI_ECP_SQR(&T[1], &T[1]); /* Pm^2 */
|
|
|
|
MPI_ECP_MUL(&R->X, &T[0], &T[1]); /* Pp^2 * Pm^2 */
|
|
|
|
MPI_ECP_SUB(&T[0], &T[0], &T[1]); /* Pp^2 - Pm^2 */
|
|
|
|
MPI_ECP_MUL(&R->Z, &grp->A, &T[0]); /* A * (Pp^2 - Pm^2) */
|
|
|
|
MPI_ECP_ADD(&R->Z, &T[1], &R->Z); /* [ A * (Pp^2-Pm^2) ] + Pm^2 */
|
|
|
|
MPI_ECP_ADD(&S->X, &T[3], &T[2]); /* Qm*Pp + Qp*Pm */
|
|
|
|
MPI_ECP_SQR(&S->X, &S->X); /* (Qm*Pp + Qp*Pm)^2 */
|
|
|
|
MPI_ECP_SUB(&S->Z, &T[3], &T[2]); /* Qm*Pp - Qp*Pm */
|
|
|
|
MPI_ECP_SQR(&S->Z, &S->Z); /* (Qm*Pp - Qp*Pm)^2 */
|
|
|
|
MPI_ECP_MUL(&S->Z, d, &S->Z); /* d * ( Qm*Pp - Qp*Pm )^2 */
|
|
|
|
MPI_ECP_MUL(&R->Z, &T[0], &R->Z); /* [A*(Pp^2-Pm^2)+Pm^2]*(Pp^2-Pm^2) */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* !defined(MBEDTLS_ECP_NO_FALLBACK) || !defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) */
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Multiplication with Montgomery ladder in x/z coordinates,
|
|
|
|
* for curves in Montgomery form
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_mul_mxz(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t i;
|
|
|
|
unsigned char b;
|
|
|
|
mbedtls_ecp_point RP;
|
|
|
|
mbedtls_mpi PX;
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi tmp[4];
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_point_init(&RP); mbedtls_mpi_init(&PX);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (f_rng == NULL) {
|
2023-09-25 05:04:06 +02:00
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2020-07-02 15:13:55 +02:00
|
|
|
}
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
/* Save PX and read from P before writing to R, in case P == R */
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_MOV(&PX, &P->X);
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(&RP, P));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Set R to zero in modified x/z coordinates */
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_LSET(&R->X, 1);
|
|
|
|
MPI_ECP_LSET(&R->Z, 0);
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_free(&R->Y);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2022-07-18 14:48:00 +02:00
|
|
|
/* RP.X might be slightly larger than P, so reduce it */
|
2023-09-25 05:04:06 +02:00
|
|
|
MOD_ADD(&RP.X);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2024-04-04 18:05:30 +02:00
|
|
|
/* Randomize coordinates of the starting point */
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, &RP, f_rng, p_rng));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Loop invariant: R = result so far, RP = R + P */
|
2022-12-21 12:05:54 +01:00
|
|
|
i = grp->nbits + 1; /* one past the (zero-based) required msb for private keys */
|
2023-04-18 10:38:24 +02:00
|
|
|
while (i-- > 0) {
|
|
|
|
b = mbedtls_mpi_get_bit(m, i);
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* if (b) R = 2R + P else R = 2R,
|
|
|
|
* which is:
|
|
|
|
* if (b) double_add( RP, R, RP, R )
|
|
|
|
* else double_add( R, RP, R, RP )
|
|
|
|
* but using safe conditional swaps to avoid leaks
|
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_COND_SWAP(&R->X, &RP.X, b);
|
|
|
|
MPI_ECP_COND_SWAP(&R->Z, &RP.Z, b);
|
|
|
|
MBEDTLS_MPI_CHK(ecp_double_add_mxz(grp, R, &RP, R, &RP, &PX, tmp));
|
|
|
|
MPI_ECP_COND_SWAP(&R->X, &RP.X, b);
|
|
|
|
MPI_ECP_COND_SWAP(&R->Z, &RP.Z, b);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2020-04-21 10:37:14 +02:00
|
|
|
/*
|
|
|
|
* Knowledge of the projective coordinates may leak the last few bits of the
|
|
|
|
* scalar [1], and since our MPI implementation isn't constant-flow,
|
|
|
|
* inversion (used for coordinate normalization) may leak the full value
|
|
|
|
* of its input via side-channels [2].
|
|
|
|
*
|
|
|
|
* [1] https://eprint.iacr.org/2003/191
|
|
|
|
* [2] https://eprint.iacr.org/2020/055
|
|
|
|
*
|
|
|
|
* Avoid the leak by randomizing coordinates before we normalize them.
|
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_randomize_mxz(grp, R, f_rng, p_rng));
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_normalize_mxz(grp, R));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_point_free(&RP); mbedtls_mpi_free(&PX);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
2019-02-16 17:19:46 +01:00
|
|
|
* Restartable multiplication R = m * P
|
2023-09-25 05:04:06 +02:00
|
|
|
*
|
|
|
|
* This internal function can be called without an RNG in case where we know
|
|
|
|
* the inputs are not sensitive.
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
static int ecp_mul_restartable_internal(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
|
|
|
|
mbedtls_ecp_restart_ctx *rs_ctx)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
|
|
char is_grp_capable = 0;
|
|
|
|
#endif
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
|
|
|
/* reset ops count for this call if top-level */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->depth++ == 0) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->ops_done = 0;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#else
|
|
|
|
(void) rs_ctx;
|
2019-02-16 17:19:46 +01:00
|
|
|
#endif
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((is_grp_capable = mbedtls_internal_ecp_grp_capable(grp))) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_internal_ecp_init(grp));
|
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
#endif /* MBEDTLS_ECP_INTERNAL_ALT */
|
|
|
|
|
2022-12-21 12:05:54 +01:00
|
|
|
int restarting = 0;
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL);
|
2019-02-16 17:19:46 +01:00
|
|
|
#endif
|
2022-12-21 12:05:54 +01:00
|
|
|
/* skip argument check when restarting */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (!restarting) {
|
2019-02-16 17:19:46 +01:00
|
|
|
/* check_privkey is free */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_CHK);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
/* Common sanity checks */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(grp, m));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
|
|
|
|
MBEDTLS_MPI_CHK(ecp_mul_mxz(grp, R, m, P, f_rng, p_rng));
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
|
|
|
|
MBEDTLS_MPI_CHK(ecp_mul_comb(grp, R, m, P, f_rng, p_rng, rs_ctx));
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
cleanup:
|
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (is_grp_capable) {
|
|
|
|
mbedtls_internal_ecp_free(grp);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif /* MBEDTLS_ECP_INTERNAL_ALT */
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->depth--;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
#endif
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/*
|
|
|
|
* Restartable multiplication R = m * P
|
|
|
|
*/
|
|
|
|
int mbedtls_ecp_mul_restartable(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
|
|
|
|
mbedtls_ecp_restart_ctx *rs_ctx)
|
|
|
|
{
|
|
|
|
if (f_rng == NULL) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ecp_mul_restartable_internal(grp, R, m, P, f_rng, p_rng, rs_ctx);
|
|
|
|
}
|
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
/*
|
|
|
|
* Multiplication R = m * P
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_mul(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_ecp_mul_restartable(grp, R, m, P, f_rng, p_rng, NULL);
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
2023-09-25 05:04:06 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Check that an affine point is valid as a public key,
|
|
|
|
* short weierstrass curves (SEC1 3.2.3.1)
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_check_pubkey_sw(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
mbedtls_mpi YY, RHS;
|
|
|
|
|
|
|
|
/* pt coordinates must be normalized for our checks */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0 ||
|
|
|
|
mbedtls_mpi_cmp_int(&pt->Y, 0) < 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi(&pt->X, &grp->P) >= 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi(&pt->Y, &grp->P) >= 0) {
|
|
|
|
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_init(&YY); mbedtls_mpi_init(&RHS);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* YY = Y^2
|
2023-09-25 05:04:06 +02:00
|
|
|
* RHS = X^3 + A X + B
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_SQR(&YY, &pt->Y);
|
|
|
|
MBEDTLS_MPI_CHK(ecp_sw_rhs(grp, &RHS, &pt->X));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
if (MPI_ECP_CMP(&YY, &RHS) != 0) {
|
2018-02-08 19:04:43 +01:00
|
|
|
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_free(&YY); mbedtls_mpi_free(&RHS);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
2021-12-21 12:54:05 +01:00
|
|
|
* R = m * P with shortcuts for m == 0, m == 1 and m == -1
|
2018-02-08 19:04:43 +01:00
|
|
|
* NOT constant-time - ONLY for short Weierstrass!
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int mbedtls_ecp_mul_shortcuts(mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m,
|
|
|
|
const mbedtls_ecp_point *P,
|
|
|
|
mbedtls_ecp_restart_ctx *rs_ctx)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi tmp;
|
|
|
|
mbedtls_mpi_init(&tmp);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_cmp_int(m, 0) == 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_set_zero(R));
|
|
|
|
} else if (mbedtls_mpi_cmp_int(m, 1) == 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P));
|
|
|
|
} else if (mbedtls_mpi_cmp_int(m, -1) == 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_check_pubkey(grp, P));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, P));
|
2023-09-25 05:04:06 +02:00
|
|
|
MPI_ECP_NEG(&R->Y);
|
2023-04-18 10:38:24 +02:00
|
|
|
} else {
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_mul_restartable_internal(grp, R, m, P,
|
|
|
|
NULL, NULL, rs_ctx));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi_free(&tmp);
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-02-16 17:19:46 +01:00
|
|
|
* Restartable linear combination
|
2018-02-08 19:04:43 +01:00
|
|
|
* NOT constant-time
|
|
|
|
*/
|
2019-02-16 17:19:46 +01:00
|
|
|
int mbedtls_ecp_muladd_restartable(
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
|
|
|
const mbedtls_mpi *n, const mbedtls_ecp_point *Q,
|
|
|
|
mbedtls_ecp_restart_ctx *rs_ctx)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
mbedtls_ecp_point mP;
|
2019-02-16 17:19:46 +01:00
|
|
|
mbedtls_ecp_point *pmP = &mP;
|
|
|
|
mbedtls_ecp_point *pR = R;
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_mpi tmp[4];
|
2018-02-08 19:04:43 +01:00
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
|
|
|
char is_grp_capable = 0;
|
|
|
|
#endif
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) != MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
|
|
|
|
return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_point_init(&mP);
|
2023-09-25 05:04:06 +02:00
|
|
|
mpi_init_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ECP_RS_ENTER(ma);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->ma != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
/* redirect intermediate results to restart context */
|
|
|
|
pmP = &rs_ctx->ma->mP;
|
|
|
|
pR = &rs_ctx->ma->R;
|
|
|
|
|
|
|
|
/* jump to next operation */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx->ma->state == ecp_rsma_mul2) {
|
2019-02-16 17:19:46 +01:00
|
|
|
goto mul2;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
|
|
|
if (rs_ctx->ma->state == ecp_rsma_add) {
|
2019-02-16 17:19:46 +01:00
|
|
|
goto add;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
|
|
|
if (rs_ctx->ma->state == ecp_rsma_norm) {
|
2019-02-16 17:19:46 +01:00
|
|
|
goto norm;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
#endif /* MBEDTLS_ECP_RESTARTABLE */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pmP, m, P, rs_ctx));
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->ma != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->ma->state = ecp_rsma_mul2;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
mul2:
|
|
|
|
#endif
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_mul_shortcuts(grp, pR, n, Q, rs_ctx));
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((is_grp_capable = mbedtls_internal_ecp_grp_capable(grp))) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_internal_ecp_init(grp));
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif /* MBEDTLS_ECP_INTERNAL_ALT */
|
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->ma != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->ma->state = ecp_rsma_add;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
add:
|
|
|
|
#endif
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_ADD);
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(ecp_add_mixed(grp, pR, pmP, pR, tmp));
|
2019-02-16 17:19:46 +01:00
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->ma != NULL) {
|
2019-02-16 17:19:46 +01:00
|
|
|
rs_ctx->ma->state = ecp_rsma_norm;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
norm:
|
|
|
|
#endif
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ECP_BUDGET(MBEDTLS_ECP_OPS_INV);
|
|
|
|
MBEDTLS_MPI_CHK(ecp_normalize_jac(grp, pR));
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_RESTARTABLE)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (rs_ctx != NULL && rs_ctx->ma != NULL) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_copy(R, pR));
|
|
|
|
}
|
2019-02-16 17:19:46 +01:00
|
|
|
#endif
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
cleanup:
|
2023-09-25 05:04:06 +02:00
|
|
|
|
|
|
|
mpi_free_many(tmp, sizeof(tmp) / sizeof(mbedtls_mpi));
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
#if defined(MBEDTLS_ECP_INTERNAL_ALT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (is_grp_capable) {
|
|
|
|
mbedtls_internal_ecp_free(grp);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif /* MBEDTLS_ECP_INTERNAL_ALT */
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_point_free(&mP);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ECP_RS_LEAVE(ma);
|
2019-02-16 17:19:46 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 17:19:46 +01:00
|
|
|
/*
|
|
|
|
* Linear combination
|
|
|
|
* NOT constant-time
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_muladd(mbedtls_ecp_group *grp, mbedtls_ecp_point *R,
|
|
|
|
const mbedtls_mpi *m, const mbedtls_ecp_point *P,
|
|
|
|
const mbedtls_mpi *n, const mbedtls_ecp_point *Q)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_ecp_muladd_restartable(grp, R, m, P, n, Q, NULL);
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
2023-09-25 05:04:06 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
2021-07-20 12:32:46 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
|
2023-09-25 05:04:06 +02:00
|
|
|
#define ECP_MPI_INIT(_p, _n) { .p = (mbedtls_mpi_uint *) (_p), .s = 1, .n = (_n) }
|
2021-07-20 12:32:46 +02:00
|
|
|
#define ECP_MPI_INIT_ARRAY(x) \
|
2023-09-25 05:04:06 +02:00
|
|
|
ECP_MPI_INIT(x, sizeof(x) / sizeof(mbedtls_mpi_uint))
|
2021-07-20 12:32:46 +02:00
|
|
|
/*
|
|
|
|
* Constants for the two points other than 0, 1, -1 (mod p) in
|
|
|
|
* https://cr.yp.to/ecdh.html#validate
|
|
|
|
* See ecp_check_pubkey_x25519().
|
|
|
|
*/
|
|
|
|
static const mbedtls_mpi_uint x25519_bad_point_1[] = {
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_BYTES_TO_T_UINT_8(0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae),
|
|
|
|
MBEDTLS_BYTES_TO_T_UINT_8(0x16, 0x56, 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a),
|
|
|
|
MBEDTLS_BYTES_TO_T_UINT_8(0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32, 0xb1, 0xfd),
|
|
|
|
MBEDTLS_BYTES_TO_T_UINT_8(0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00),
|
2021-07-20 12:32:46 +02:00
|
|
|
};
|
|
|
|
static const mbedtls_mpi_uint x25519_bad_point_2[] = {
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_BYTES_TO_T_UINT_8(0x5f, 0x9c, 0x95, 0xbc, 0xa3, 0x50, 0x8c, 0x24),
|
|
|
|
MBEDTLS_BYTES_TO_T_UINT_8(0xb1, 0xd0, 0xb1, 0x55, 0x9c, 0x83, 0xef, 0x5b),
|
|
|
|
MBEDTLS_BYTES_TO_T_UINT_8(0x04, 0x44, 0x5c, 0xc4, 0x58, 0x1c, 0x8e, 0x86),
|
|
|
|
MBEDTLS_BYTES_TO_T_UINT_8(0xd8, 0x22, 0x4e, 0xdd, 0xd0, 0x9f, 0x11, 0x57),
|
2021-07-20 12:32:46 +02:00
|
|
|
};
|
|
|
|
static const mbedtls_mpi ecp_x25519_bad_point_1 = ECP_MPI_INIT_ARRAY(
|
2023-04-18 10:38:24 +02:00
|
|
|
x25519_bad_point_1);
|
2021-07-20 12:32:46 +02:00
|
|
|
static const mbedtls_mpi ecp_x25519_bad_point_2 = ECP_MPI_INIT_ARRAY(
|
2023-04-18 10:38:24 +02:00
|
|
|
x25519_bad_point_2);
|
2021-07-20 12:32:46 +02:00
|
|
|
#endif /* MBEDTLS_ECP_DP_CURVE25519_ENABLED */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that the input point is not one of the low-order points.
|
|
|
|
* This is recommended by the "May the Fourth" paper:
|
|
|
|
* https://eprint.iacr.org/2017/806.pdf
|
|
|
|
* Those points are never sent by an honest peer.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_check_bad_points_mx(const mbedtls_mpi *X, const mbedtls_mpi *P,
|
|
|
|
const mbedtls_ecp_group_id grp_id)
|
2021-07-20 12:32:46 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
mbedtls_mpi XmP;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_init(&XmP);
|
2021-07-20 12:32:46 +02:00
|
|
|
|
|
|
|
/* Reduce X mod P so that we only need to check values less than P.
|
|
|
|
* We know X < 2^256 so we can proceed by subtraction. */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_copy(&XmP, X));
|
|
|
|
while (mbedtls_mpi_cmp_mpi(&XmP, P) >= 0) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(&XmP, &XmP, P));
|
|
|
|
}
|
2021-07-20 12:32:46 +02:00
|
|
|
|
|
|
|
/* Check against the known bad values that are less than P. For Curve448
|
|
|
|
* these are 0, 1 and -1. For Curve25519 we check the values less than P
|
|
|
|
* from the following list: https://cr.yp.to/ecdh.html#validate */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_cmp_int(&XmP, 1) <= 0) { /* takes care of 0 and 1 */
|
2021-07-20 12:32:46 +02:00
|
|
|
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (grp_id == MBEDTLS_ECP_DP_CURVE25519) {
|
|
|
|
if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_1) == 0) {
|
2021-07-20 12:32:46 +02:00
|
|
|
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_cmp_mpi(&XmP, &ecp_x25519_bad_point_2) == 0) {
|
2021-07-20 12:32:46 +02:00
|
|
|
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void) grp_id;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Final check: check if XmP + 1 is P (final because it changes XmP!) */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_add_int(&XmP, &XmP, 1));
|
|
|
|
if (mbedtls_mpi_cmp_mpi(&XmP, P) == 0) {
|
2021-07-20 12:32:46 +02:00
|
|
|
ret = MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_free(&XmP);
|
2021-07-20 12:32:46 +02:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-07-20 12:32:46 +02:00
|
|
|
}
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Check validity of a public key for Montgomery curves with x-only schemes
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int ecp_check_pubkey_mx(const mbedtls_ecp_group *grp, const mbedtls_ecp_point *pt)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
/* [Curve25519 p. 5] Just check X is the correct number of bytes */
|
2018-06-07 16:25:01 +02:00
|
|
|
/* Allow any public value, if it's too big then we'll just reduce it mod p
|
|
|
|
* (RFC 7748 sec. 5 para. 3). */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_size(&pt->X) > (grp->nbits + 7) / 8) {
|
|
|
|
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2021-07-20 12:32:46 +02:00
|
|
|
/* Implicit in all standards (as they don't consider negative numbers):
|
|
|
|
* X must be non-negative. This is normally ensured by the way it's
|
|
|
|
* encoded for transmission, but let's be extra sure. */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_cmp_int(&pt->X, 0) < 0) {
|
|
|
|
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
}
|
2021-07-20 12:32:46 +02:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ecp_check_bad_points_mx(&pt->X, &grp->P, grp->id);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that a point is valid as a public key
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_check_pubkey(const mbedtls_ecp_group *grp,
|
|
|
|
const mbedtls_ecp_point *pt)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
/* Must use affine coordinates */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_cmp_int(&pt->Z, 1) != 0) {
|
|
|
|
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
|
|
|
|
return ecp_check_pubkey_mx(grp, pt);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
|
|
|
|
return ecp_check_pubkey_sw(grp, pt);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that an mbedtls_mpi is valid as a private key
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_check_privkey(const mbedtls_ecp_group *grp,
|
|
|
|
const mbedtls_mpi *d)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
|
2018-06-07 16:25:01 +02:00
|
|
|
/* see RFC 7748 sec. 5 para. 5 */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_get_bit(d, 0) != 0 ||
|
|
|
|
mbedtls_mpi_get_bit(d, 1) != 0 ||
|
|
|
|
mbedtls_mpi_bitlen(d) - 1 != grp->nbits) { /* mbedtls_mpi_bitlen is one-based! */
|
|
|
|
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
}
|
2018-06-07 16:25:01 +02:00
|
|
|
|
|
|
|
/* see [Curve25519] page 5 */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (grp->nbits == 254 && mbedtls_mpi_get_bit(d, 2) != 0) {
|
|
|
|
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
}
|
2018-06-07 16:25:01 +02:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
|
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
|
2018-02-08 19:04:43 +01:00
|
|
|
/* see SEC1 3.2 */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_cmp_int(d, 1) < 0 ||
|
|
|
|
mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) {
|
|
|
|
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
|
|
|
MBEDTLS_STATIC_TESTABLE
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_gen_privkey_mx(size_t high_bit,
|
|
|
|
mbedtls_mpi *d,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2021-12-21 12:54:05 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
size_t n_random_bytes = high_bit / 8 + 1;
|
|
|
|
|
|
|
|
/* [Curve25519] page 5 */
|
|
|
|
/* Generate a (high_bit+1)-bit random number by generating just enough
|
|
|
|
* random bytes, then shifting out extra bits from the top (necessary
|
|
|
|
* when (high_bit+1) is not a multiple of 8). */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_fill_random(d, n_random_bytes,
|
|
|
|
f_rng, p_rng));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(d, 8 * n_random_bytes - high_bit - 1));
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, high_bit, 1));
|
2021-12-21 12:54:05 +01:00
|
|
|
|
|
|
|
/* Make sure the last two bits are unset for Curve448, three bits for
|
|
|
|
Curve25519 */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 0, 0));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 1, 0));
|
|
|
|
if (high_bit == 254) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(d, 2, 0));
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
|
|
|
static int mbedtls_ecp_gen_privkey_sw(
|
|
|
|
const mbedtls_mpi *N, mbedtls_mpi *d,
|
2023-04-18 10:38:24 +02:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
2021-12-21 12:54:05 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
int ret = mbedtls_mpi_random(d, 1, N, f_rng, p_rng);
|
|
|
|
switch (ret) {
|
2021-12-21 12:54:05 +01:00
|
|
|
case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_RANDOM_FAILED;
|
2021-12-21 12:54:05 +01:00
|
|
|
default:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
2019-02-16 17:19:46 +01:00
|
|
|
* Generate a private key
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_gen_privkey(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *d,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
|
|
|
|
return mbedtls_ecp_gen_privkey_mx(grp->nbits, d, f_rng, p_rng);
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
|
|
|
|
return mbedtls_ecp_gen_privkey_sw(&grp->N, d, f_rng, p_rng);
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2019-02-16 17:19:46 +01:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2019-02-16 17:19:46 +01:00
|
|
|
/*
|
|
|
|
* Generate a keypair with configurable base point
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_gen_keypair_base(mbedtls_ecp_group *grp,
|
|
|
|
const mbedtls_ecp_point *G,
|
|
|
|
mbedtls_mpi *d, mbedtls_ecp_point *Q,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2019-02-16 17:19:46 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, d, f_rng, p_rng));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, Q, d, G, f_rng, p_rng));
|
2019-02-16 17:19:46 +01:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate key pair, wrapper for conventional base point
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_gen_keypair(mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *d, mbedtls_ecp_point *Q,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_ecp_gen_keypair_base(grp, &grp->G, d, Q, f_rng, p_rng);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a keypair, prettier wrapper
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_gen_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_ecp_gen_keypair(&key->grp, &key->d, &key->Q, f_rng, p_rng);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2023-09-25 05:04:06 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|
|
|
|
|
|
|
|
int mbedtls_ecp_set_public_key(mbedtls_ecp_group_id grp_id,
|
|
|
|
mbedtls_ecp_keypair *key,
|
|
|
|
const mbedtls_ecp_point *Q)
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
if (key->grp.id == MBEDTLS_ECP_DP_NONE) {
|
|
|
|
/* Group not set yet */
|
|
|
|
if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else if (key->grp.id != grp_id) {
|
|
|
|
/* Group mismatch */
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
return mbedtls_ecp_copy(&key->Q, Q);
|
|
|
|
}
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#define ECP_CURVE25519_KEY_SIZE 32
|
2023-09-25 05:04:06 +02:00
|
|
|
#define ECP_CURVE448_KEY_SIZE 56
|
2021-12-21 12:54:05 +01:00
|
|
|
/*
|
|
|
|
* Read a private key.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_read_key(mbedtls_ecp_group_id grp_id, mbedtls_ecp_keypair *key,
|
|
|
|
const unsigned char *buf, size_t buflen)
|
2021-12-21 12:54:05 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_ecp_group_load(&key->grp, grp_id)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
|
|
|
|
ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
|
2021-12-21 12:54:05 +01:00
|
|
|
/*
|
2023-09-25 05:04:06 +02:00
|
|
|
* Mask the key as mandated by RFC7748 for Curve25519 and Curve448.
|
2021-12-21 12:54:05 +01:00
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (grp_id == MBEDTLS_ECP_DP_CURVE25519) {
|
|
|
|
if (buflen != ECP_CURVE25519_KEY_SIZE) {
|
2021-12-21 12:54:05 +01:00
|
|
|
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen));
|
2021-12-21 12:54:05 +01:00
|
|
|
|
|
|
|
/* Set the three least significant bits to 0 */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 2, 0));
|
2021-12-21 12:54:05 +01:00
|
|
|
|
|
|
|
/* Set the most significant bit to 0 */
|
|
|
|
MBEDTLS_MPI_CHK(
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_set_bit(&key->d,
|
|
|
|
ECP_CURVE25519_KEY_SIZE * 8 - 1, 0)
|
|
|
|
);
|
2021-12-21 12:54:05 +01:00
|
|
|
|
|
|
|
/* Set the second most significant bit to 1 */
|
|
|
|
MBEDTLS_MPI_CHK(
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_set_bit(&key->d,
|
|
|
|
ECP_CURVE25519_KEY_SIZE * 8 - 2, 1)
|
|
|
|
);
|
2023-09-25 05:04:06 +02:00
|
|
|
} else if (grp_id == MBEDTLS_ECP_DP_CURVE448) {
|
|
|
|
if (buflen != ECP_CURVE448_KEY_SIZE) {
|
|
|
|
return MBEDTLS_ERR_ECP_INVALID_KEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary_le(&key->d, buf, buflen));
|
|
|
|
|
|
|
|
/* Set the two least significant bits to 0 */
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 0, 0));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&key->d, 1, 0));
|
|
|
|
|
|
|
|
/* Set the most significant bit to 1 */
|
|
|
|
MBEDTLS_MPI_CHK(
|
|
|
|
mbedtls_mpi_set_bit(&key->d,
|
|
|
|
ECP_CURVE448_KEY_SIZE * 8 - 1, 1)
|
|
|
|
);
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&key->d, buf, buflen));
|
2023-09-25 05:04:06 +02:00
|
|
|
}
|
|
|
|
#endif
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
if (ret == 0) {
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_check_privkey(&key->grp, &key->d));
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_mpi_free(&key->d);
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a private key.
|
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
#if !defined MBEDTLS_DEPRECATED_REMOVED
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_write_key(mbedtls_ecp_keypair *key,
|
|
|
|
unsigned char *buf, size_t buflen)
|
2021-12-21 12:54:05 +01:00
|
|
|
{
|
2024-04-04 18:05:30 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-12-21 12:54:05 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
|
|
|
|
if (key->grp.id == MBEDTLS_ECP_DP_CURVE25519) {
|
|
|
|
if (buflen < ECP_CURVE25519_KEY_SIZE) {
|
2021-12-21 12:54:05 +01:00
|
|
|
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
} else if (key->grp.id == MBEDTLS_ECP_DP_CURVE448) {
|
|
|
|
if (buflen < ECP_CURVE448_KEY_SIZE) {
|
|
|
|
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary_le(&key->d, buf, buflen));
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&key->d, buf, buflen));
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
cleanup:
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
2023-09-25 05:04:06 +02:00
|
|
|
#endif /* MBEDTLS_DEPRECATED_REMOVED */
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
int mbedtls_ecp_write_key_ext(const mbedtls_ecp_keypair *key,
|
|
|
|
size_t *olen, unsigned char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
size_t len = (key->grp.nbits + 7) / 8;
|
|
|
|
if (len > buflen) {
|
|
|
|
/* For robustness, ensure *olen <= buflen even on error. */
|
|
|
|
*olen = 0;
|
|
|
|
return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
|
|
|
|
}
|
|
|
|
*olen = len;
|
|
|
|
|
|
|
|
/* Private key not set */
|
|
|
|
if (key->d.n == 0) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
|
|
|
if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
|
|
|
|
return mbedtls_mpi_write_binary_le(&key->d, buf, len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
|
|
|
if (mbedtls_ecp_get_type(&key->grp) == MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS) {
|
|
|
|
return mbedtls_mpi_write_binary(&key->d, buf, len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Private key set but no recognized curve type? This shouldn't happen. */
|
|
|
|
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a public key.
|
|
|
|
*/
|
|
|
|
int mbedtls_ecp_write_public_key(const mbedtls_ecp_keypair *key,
|
|
|
|
int format, size_t *olen,
|
|
|
|
unsigned char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
return mbedtls_ecp_point_write_binary(&key->grp, &key->Q,
|
|
|
|
format, olen, buf, buflen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Check a public-private key pair
|
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
int mbedtls_ecp_check_pub_priv(
|
|
|
|
const mbedtls_ecp_keypair *pub, const mbedtls_ecp_keypair *prv,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
mbedtls_ecp_point Q;
|
|
|
|
mbedtls_ecp_group grp;
|
2023-04-18 10:38:24 +02:00
|
|
|
if (pub->grp.id == MBEDTLS_ECP_DP_NONE ||
|
2018-02-08 19:04:43 +01:00
|
|
|
pub->grp.id != prv->grp.id ||
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_cmp_mpi(&pub->Q.X, &prv->Q.X) ||
|
|
|
|
mbedtls_mpi_cmp_mpi(&pub->Q.Y, &prv->Q.Y) ||
|
|
|
|
mbedtls_mpi_cmp_mpi(&pub->Q.Z, &prv->Q.Z)) {
|
|
|
|
return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_point_init(&Q);
|
|
|
|
mbedtls_ecp_group_init(&grp);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* mbedtls_ecp_mul() needs a non-const group... */
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_group_copy(&grp, &prv->grp);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Also checks d is valid */
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &Q, &prv->d, &prv->grp.G, f_rng, p_rng));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_mpi_cmp_mpi(&Q.X, &prv->Q.X) ||
|
|
|
|
mbedtls_mpi_cmp_mpi(&Q.Y, &prv->Q.Y) ||
|
|
|
|
mbedtls_mpi_cmp_mpi(&Q.Z, &prv->Q.Z)) {
|
2018-02-08 19:04:43 +01:00
|
|
|
ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_point_free(&Q);
|
|
|
|
mbedtls_ecp_group_free(&grp);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
int mbedtls_ecp_keypair_calc_public(mbedtls_ecp_keypair *key,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
|
|
|
{
|
|
|
|
return mbedtls_ecp_mul(&key->grp, &key->Q, &key->d, &key->grp.G,
|
|
|
|
f_rng, p_rng);
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECP_C */
|
|
|
|
|
|
|
|
mbedtls_ecp_group_id mbedtls_ecp_keypair_get_group_id(
|
|
|
|
const mbedtls_ecp_keypair *key)
|
|
|
|
{
|
|
|
|
return key->grp.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Export generic key-pair parameters.
|
|
|
|
*/
|
|
|
|
int mbedtls_ecp_export(const mbedtls_ecp_keypair *key, mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *d, mbedtls_ecp_point *Q)
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
if (grp != NULL && (ret = mbedtls_ecp_group_copy(grp, &key->grp)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d != NULL && (ret = mbedtls_mpi_copy(d, &key->d)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Q != NULL && (ret = mbedtls_ecp_copy(Q, &key->Q)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
/*
|
|
|
|
* PRNG for test - !!!INSECURE NEVER USE IN PRODUCTION!!!
|
|
|
|
*
|
|
|
|
* This is the linear congruential generator from numerical recipes,
|
|
|
|
* except we only use the low byte as the output. See
|
|
|
|
* https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
|
|
|
|
*/
|
|
|
|
static int self_test_rng(void *ctx, unsigned char *out, size_t len)
|
|
|
|
{
|
|
|
|
static uint32_t state = 42;
|
|
|
|
|
|
|
|
(void) ctx;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
|
|
|
state = state * 1664525u + 1013904223u;
|
|
|
|
out[i] = (unsigned char) state;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
/* Adjust the exponent to be a valid private point for the specified curve.
|
|
|
|
* This is sometimes necessary because we use a single set of exponents
|
|
|
|
* for all curves but the validity of values depends on the curve. */
|
2023-04-18 10:38:24 +02:00
|
|
|
static int self_test_adjust_exponent(const mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_mpi *m)
|
2021-12-21 12:54:05 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2023-04-18 10:38:24 +02:00
|
|
|
switch (grp->id) {
|
|
|
|
/* If Curve25519 is available, then that's what we use for the
|
|
|
|
* Montgomery test, so we don't need the adjustment code. */
|
|
|
|
#if !defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
|
|
|
|
case MBEDTLS_ECP_DP_CURVE448:
|
|
|
|
/* Move highest bit from 254 to N-1. Setting bit N-1 is
|
|
|
|
* necessary to enforce the highest-bit-set constraint. */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, 254, 0));
|
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(m, grp->nbits, 1));
|
2021-12-21 12:54:05 +01:00
|
|
|
/* Copy second-highest bit from 253 to N-2. This is not
|
|
|
|
* necessary but improves the test variety a bit. */
|
|
|
|
MBEDTLS_MPI_CHK(
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_mpi_set_bit(m, grp->nbits - 1,
|
|
|
|
mbedtls_mpi_get_bit(m, 253)));
|
2021-12-21 12:54:05 +01:00
|
|
|
break;
|
2020-07-02 15:13:55 +02:00
|
|
|
#endif
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* ! defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) */
|
|
|
|
default:
|
|
|
|
/* Non-Montgomery curves and Curve25519 need no adjustment. */
|
|
|
|
(void) grp;
|
|
|
|
(void) m;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
2020-07-02 15:13:55 +02:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
/* Calculate R = m.P for each m in exponents. Check that the number of
|
|
|
|
* basic operations doesn't depend on the value of m. */
|
2023-04-18 10:38:24 +02:00
|
|
|
static int self_test_point(int verbose,
|
|
|
|
mbedtls_ecp_group *grp,
|
|
|
|
mbedtls_ecp_point *R,
|
|
|
|
mbedtls_mpi *m,
|
|
|
|
const mbedtls_ecp_point *P,
|
|
|
|
const char *const *exponents,
|
|
|
|
size_t n_exponents)
|
2020-07-02 15:13:55 +02:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
unsigned long add_c_prev, dbl_c_prev, mul_c_prev;
|
|
|
|
add_count = 0;
|
|
|
|
dbl_count = 0;
|
|
|
|
mul_count = 0;
|
2020-07-02 15:13:55 +02:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[0]));
|
|
|
|
MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m));
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, self_test_rng, NULL));
|
2020-07-02 15:13:55 +02:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (i = 1; i < n_exponents; i++) {
|
2021-12-21 12:54:05 +01:00
|
|
|
add_c_prev = add_count;
|
|
|
|
dbl_c_prev = dbl_count;
|
|
|
|
mul_c_prev = mul_count;
|
|
|
|
add_count = 0;
|
|
|
|
dbl_count = 0;
|
|
|
|
mul_count = 0;
|
2020-07-02 15:13:55 +02:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_read_string(m, 16, exponents[i]));
|
|
|
|
MBEDTLS_MPI_CHK(self_test_adjust_exponent(grp, m));
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(grp, R, m, P, self_test_rng, NULL));
|
2020-07-02 15:13:55 +02:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (add_count != add_c_prev ||
|
2021-12-21 12:54:05 +01:00
|
|
|
dbl_count != dbl_c_prev ||
|
2023-04-18 10:38:24 +02:00
|
|
|
mul_count != mul_c_prev) {
|
2021-12-21 12:54:05 +01:00
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-07-02 15:13:55 +02:00
|
|
|
|
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
if (verbose != 0) {
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_printf("failed (%u)\n", (unsigned int) i);
|
|
|
|
} else {
|
|
|
|
mbedtls_printf("passed\n");
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
}
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2020-07-02 15:13:55 +02:00
|
|
|
}
|
2023-09-25 05:04:06 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2020-07-02 15:13:55 +02:00
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_ecp_self_test(int verbose)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
mbedtls_ecp_group grp;
|
|
|
|
mbedtls_ecp_point R, P;
|
|
|
|
mbedtls_mpi m;
|
2021-12-21 12:54:05 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
|
|
|
/* Exponents especially adapted for secp192k1, which has the lowest
|
|
|
|
* order n of all supported curves (secp192r1 is in a slightly larger
|
|
|
|
* field but the order of its base point is slightly smaller). */
|
|
|
|
const char *sw_exponents[] =
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
"000000000000000000000000000000000000000000000001", /* one */
|
2021-12-21 12:54:05 +01:00
|
|
|
"FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8C", /* n - 1 */
|
2018-02-08 19:04:43 +01:00
|
|
|
"5EA6F389A38B8BC81E767753B15AA5569E1782E30ABE7D25", /* random */
|
|
|
|
"400000000000000000000000000000000000000000000000", /* one and zeros */
|
|
|
|
"7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", /* all ones */
|
|
|
|
"555555555555555555555555555555555555555555555555", /* 101010... */
|
|
|
|
};
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
|
|
|
const char *m_exponents[] =
|
|
|
|
{
|
|
|
|
/* Valid private values for Curve25519. In a build with Curve448
|
|
|
|
* but not Curve25519, they will be adjusted in
|
|
|
|
* self_test_adjust_exponent(). */
|
|
|
|
"4000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"5C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C3C30",
|
|
|
|
"5715ECCE24583F7A7023C24164390586842E816D7280A49EF6DF4EAE6B280BF8",
|
|
|
|
"41A2B017516F6D254E1F002BCCBADD54BE30F8CEC737A0E912B4963B6BA74460",
|
|
|
|
"5555555555555555555555555555555555555555555555555555555555555550",
|
|
|
|
"7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8",
|
|
|
|
};
|
|
|
|
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_group_init(&grp);
|
|
|
|
mbedtls_ecp_point_init(&R);
|
|
|
|
mbedtls_ecp_point_init(&P);
|
|
|
|
mbedtls_mpi_init(&m);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED)
|
2018-02-08 19:04:43 +01:00
|
|
|
/* Use secp192r1 if available, or any available curve */
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_SECP192R1));
|
2018-02-08 19:04:43 +01:00
|
|
|
#else
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, mbedtls_ecp_curve_list()->grp_id));
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" ECP SW test #1 (constant op_count, base point G): ");
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
/* Do a dummy multiplication first to trigger precomputation */
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_mpi_lset(&m, 2));
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_mul(&grp, &P, &m, &grp.G, self_test_rng, NULL));
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = self_test_point(verbose,
|
|
|
|
&grp, &R, &m, &grp.G,
|
|
|
|
sw_exponents,
|
|
|
|
sizeof(sw_exponents) / sizeof(sw_exponents[0]));
|
|
|
|
if (ret != 0) {
|
2021-12-21 12:54:05 +01:00
|
|
|
goto cleanup;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" ECP SW test #2 (constant op_count, other point): ");
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
/* We computed P = 2G last time, use it */
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = self_test_point(verbose,
|
|
|
|
&grp, &R, &m, &P,
|
|
|
|
sw_exponents,
|
|
|
|
sizeof(sw_exponents) / sizeof(sw_exponents[0]));
|
|
|
|
if (ret != 0) {
|
2021-12-21 12:54:05 +01:00
|
|
|
goto cleanup;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_group_free(&grp);
|
|
|
|
mbedtls_ecp_point_free(&R);
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" ECP Montgomery test (constant op_count): ");
|
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE25519));
|
2021-12-21 12:54:05 +01:00
|
|
|
#elif defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_MPI_CHK(mbedtls_ecp_group_load(&grp, MBEDTLS_ECP_DP_CURVE448));
|
2021-12-21 12:54:05 +01:00
|
|
|
#else
|
|
|
|
#error "MBEDTLS_ECP_MONTGOMERY_ENABLED is defined, but no curve is supported for self-test"
|
|
|
|
#endif
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = self_test_point(verbose,
|
|
|
|
&grp, &R, &m, &grp.G,
|
|
|
|
m_exponents,
|
|
|
|
sizeof(m_exponents) / sizeof(m_exponents[0]));
|
|
|
|
if (ret != 0) {
|
2020-07-02 15:13:55 +02:00
|
|
|
goto cleanup;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2021-12-21 12:54:05 +01:00
|
|
|
#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */
|
2020-07-02 15:13:55 +02:00
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
cleanup:
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ret < 0 && verbose != 0) {
|
|
|
|
mbedtls_printf("Unexpected error, return code = %08X\n", (unsigned int) ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_ecp_group_free(&grp);
|
|
|
|
mbedtls_ecp_point_free(&R);
|
|
|
|
mbedtls_ecp_point_free(&P);
|
|
|
|
mbedtls_mpi_free(&m);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("\n");
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2023-09-25 05:04:06 +02:00
|
|
|
#else /* MBEDTLS_ECP_C */
|
|
|
|
(void) verbose;
|
|
|
|
return 0;
|
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
|
|
|
|
|
|
|
#endif /* !MBEDTLS_ECP_ALT */
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#endif /* MBEDTLS_ECP_LIGHT */
|