2020-12-17 08:33:49 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* Copyright (c) 2013, 2014, 2015 Pieter Wuille, Gregory Maxwell *
|
|
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
|
|
|
***********************************************************************/
|
2014-10-26 03:42:24 -07:00
|
|
|
|
2017-08-26 18:44:21 +03:00
|
|
|
#ifndef SECP256K1_ECMULT_GEN_IMPL_H
|
|
|
|
#define SECP256K1_ECMULT_GEN_IMPL_H
|
2014-10-26 03:42:24 -07:00
|
|
|
|
2018-10-22 16:23:09 +02:00
|
|
|
#include "util.h"
|
2014-10-28 04:08:15 -07:00
|
|
|
#include "scalar.h"
|
2014-10-26 03:42:24 -07:00
|
|
|
#include "group.h"
|
|
|
|
#include "ecmult_gen.h"
|
2015-04-15 21:35:50 +00:00
|
|
|
#include "hash_impl.h"
|
2015-05-19 17:32:35 -07:00
|
|
|
#include "ecmult_static_context.h"
|
2018-10-22 16:23:09 +02:00
|
|
|
|
2021-09-09 16:46:19 +02:00
|
|
|
static const size_t SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE = 0;
|
2018-10-22 16:23:09 +02:00
|
|
|
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context *ctx) {
|
2015-02-03 17:27:00 -08:00
|
|
|
ctx->prec = NULL;
|
|
|
|
}
|
2014-10-26 03:42:24 -07:00
|
|
|
|
2018-10-22 16:25:26 +02:00
|
|
|
static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx, void **prealloc) {
|
2015-02-03 17:27:00 -08:00
|
|
|
if (ctx->prec != NULL) {
|
2014-10-26 03:42:24 -07:00
|
|
|
return;
|
2015-03-27 23:14:17 +00:00
|
|
|
}
|
2018-10-22 16:25:26 +02:00
|
|
|
(void)prealloc;
|
2015-10-18 10:35:16 +02:00
|
|
|
ctx->prec = (secp256k1_ge_storage (*)[ECMULT_GEN_PREC_N][ECMULT_GEN_PREC_G])secp256k1_ecmult_static_context;
|
2015-04-15 21:35:50 +00:00
|
|
|
secp256k1_ecmult_gen_blind(ctx, NULL);
|
2014-10-26 03:42:24 -07:00
|
|
|
}
|
|
|
|
|
2015-09-21 20:57:54 +02:00
|
|
|
static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_context* ctx) {
|
2015-02-03 17:27:00 -08:00
|
|
|
return ctx->prec != NULL;
|
|
|
|
}
|
2014-10-26 03:42:24 -07:00
|
|
|
|
2018-10-22 16:25:26 +02:00
|
|
|
static void secp256k1_ecmult_gen_context_finalize_memcpy(secp256k1_ecmult_gen_context *dst, const secp256k1_ecmult_gen_context *src) {
|
|
|
|
(void)dst, (void)src;
|
2015-04-11 14:06:54 -05:00
|
|
|
}
|
|
|
|
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_ecmult_gen_context_clear(secp256k1_ecmult_gen_context *ctx) {
|
2015-04-15 21:35:50 +00:00
|
|
|
secp256k1_scalar_clear(&ctx->blind);
|
|
|
|
secp256k1_gej_clear(&ctx->initial);
|
2015-02-03 17:27:00 -08:00
|
|
|
ctx->prec = NULL;
|
2014-10-26 03:42:24 -07:00
|
|
|
}
|
|
|
|
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *gn) {
|
|
|
|
secp256k1_ge add;
|
|
|
|
secp256k1_ge_storage adds;
|
|
|
|
secp256k1_scalar gnb;
|
2015-01-25 17:32:08 +00:00
|
|
|
int bits;
|
|
|
|
int i, j;
|
2015-04-03 17:16:09 -04:00
|
|
|
memset(&adds, 0, sizeof(adds));
|
2015-04-15 21:35:50 +00:00
|
|
|
*r = ctx->initial;
|
|
|
|
/* Blind scalar/point multiplication by computing (n-b)G + bG instead of nG. */
|
|
|
|
secp256k1_scalar_add(&gnb, gn, &ctx->blind);
|
2014-12-02 20:20:13 +01:00
|
|
|
add.infinity = 0;
|
2015-10-18 10:35:16 +02:00
|
|
|
for (j = 0; j < ECMULT_GEN_PREC_N; j++) {
|
|
|
|
bits = secp256k1_scalar_get_bits(&gnb, j * ECMULT_GEN_PREC_B, ECMULT_GEN_PREC_B);
|
|
|
|
for (i = 0; i < ECMULT_GEN_PREC_G; i++) {
|
2015-04-06 03:48:08 +00:00
|
|
|
/** This uses a conditional move to avoid any secret data in array indexes.
|
|
|
|
* _Any_ use of secret indexes has been demonstrated to result in timing
|
|
|
|
* sidechannels, even when the cache-line access patterns are uniform.
|
|
|
|
* See also:
|
|
|
|
* "A word of warning", CHES 2013 Rump Session, by Daniel J. Bernstein and Peter Schwabe
|
|
|
|
* (https://cryptojedi.org/peter/data/chesrump-20130822.pdf) and
|
|
|
|
* "Cache Attacks and Countermeasures: the Case of AES", RSA 2006,
|
|
|
|
* by Dag Arne Osvik, Adi Shamir, and Eran Tromer
|
2020-12-17 08:33:49 +02:00
|
|
|
* (https://www.tau.ac.il/~tromer/papers/cache.pdf)
|
2015-04-06 03:48:08 +00:00
|
|
|
*/
|
2015-02-03 17:27:00 -08:00
|
|
|
secp256k1_ge_storage_cmov(&adds, &(*ctx->prec)[j][i], i == bits);
|
2014-12-02 20:20:13 +01:00
|
|
|
}
|
2015-01-25 00:46:31 -04:00
|
|
|
secp256k1_ge_from_storage(&add, &adds);
|
2014-11-11 10:32:50 -08:00
|
|
|
secp256k1_gej_add_ge(r, r, &add);
|
2014-10-26 03:42:24 -07:00
|
|
|
}
|
|
|
|
bits = 0;
|
|
|
|
secp256k1_ge_clear(&add);
|
2015-04-15 21:35:50 +00:00
|
|
|
secp256k1_scalar_clear(&gnb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup blinding values for secp256k1_ecmult_gen. */
|
2015-09-21 20:57:54 +02:00
|
|
|
static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const unsigned char *seed32) {
|
|
|
|
secp256k1_scalar b;
|
|
|
|
secp256k1_gej gb;
|
|
|
|
secp256k1_fe s;
|
2015-04-15 21:35:50 +00:00
|
|
|
unsigned char nonce32[32];
|
2017-09-27 15:01:26 -07:00
|
|
|
secp256k1_rfc6979_hmac_sha256 rng;
|
2020-01-11 01:01:05 +00:00
|
|
|
int overflow;
|
2015-07-08 18:10:25 -04:00
|
|
|
unsigned char keydata[64] = {0};
|
2015-09-23 21:56:04 +00:00
|
|
|
if (seed32 == NULL) {
|
2015-04-15 21:35:50 +00:00
|
|
|
/* When seed is NULL, reset the initial point and blinding value. */
|
|
|
|
secp256k1_gej_set_ge(&ctx->initial, &secp256k1_ge_const_g);
|
|
|
|
secp256k1_gej_neg(&ctx->initial, &ctx->initial);
|
|
|
|
secp256k1_scalar_set_int(&ctx->blind, 1);
|
|
|
|
}
|
|
|
|
/* The prior blinding value (if not reset) is chained forward by including it in the hash. */
|
|
|
|
secp256k1_scalar_get_b32(nonce32, &ctx->blind);
|
|
|
|
/** Using a CSPRNG allows a failure free interface, avoids needing large amounts of random data,
|
|
|
|
* and guards against weak or adversarial seeds. This is a simpler and safer interface than
|
|
|
|
* asking the caller for blinding values directly and expecting them to retry on failure.
|
|
|
|
*/
|
2015-07-08 18:10:25 -04:00
|
|
|
memcpy(keydata, nonce32, 32);
|
2015-09-23 21:56:04 +00:00
|
|
|
if (seed32 != NULL) {
|
2015-07-08 18:10:25 -04:00
|
|
|
memcpy(keydata + 32, seed32, 32);
|
|
|
|
}
|
|
|
|
secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, seed32 ? 64 : 32);
|
|
|
|
memset(keydata, 0, sizeof(keydata));
|
2020-01-11 01:01:05 +00:00
|
|
|
/* Accept unobservably small non-uniformity. */
|
|
|
|
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
|
|
|
|
overflow = !secp256k1_fe_set_b32(&s, nonce32);
|
|
|
|
overflow |= secp256k1_fe_is_zero(&s);
|
|
|
|
secp256k1_fe_cmov(&s, &secp256k1_fe_one, overflow);
|
2015-04-15 21:35:50 +00:00
|
|
|
/* Randomize the projection to defend against multiplier sidechannels. */
|
|
|
|
secp256k1_gej_rescale(&ctx->initial, &s);
|
|
|
|
secp256k1_fe_clear(&s);
|
2020-01-11 01:01:05 +00:00
|
|
|
secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
|
|
|
|
secp256k1_scalar_set_b32(&b, nonce32, NULL);
|
|
|
|
/* A blinding value of 0 works, but would undermine the projection hardening. */
|
|
|
|
secp256k1_scalar_cmov(&b, &secp256k1_scalar_one, secp256k1_scalar_is_zero(&b));
|
2015-04-15 21:35:50 +00:00
|
|
|
secp256k1_rfc6979_hmac_sha256_finalize(&rng);
|
|
|
|
memset(nonce32, 0, 32);
|
|
|
|
secp256k1_ecmult_gen(ctx, &gb, &b);
|
|
|
|
secp256k1_scalar_negate(&b, &b);
|
|
|
|
ctx->blind = b;
|
|
|
|
ctx->initial = gb;
|
|
|
|
secp256k1_scalar_clear(&b);
|
|
|
|
secp256k1_gej_clear(&gb);
|
2014-10-26 03:42:24 -07:00
|
|
|
}
|
|
|
|
|
2017-08-26 18:44:21 +03:00
|
|
|
#endif /* SECP256K1_ECMULT_GEN_IMPL_H */
|