mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-19 13:27:35 +02:00
efad3506a8 Merge #906: Use modified divsteps with initial delta=1/2 for constant-time cc2c09e3a7 Merge #918: Clean up configuration in gen_context 07067967ee add ECMULT_GEN_PREC_BITS to basic_config.h a3aa2628c7 gen_context: Don't include basic-config.h be0609fd54 Add unit tests for edge cases with delta=1/2 variant of divsteps cd393ce228 Optimization: only do 59 hddivsteps per iteration instead of 62 277b224b6a Use modified divsteps with initial delta=1/2 for constant-time 376ca366db Fix typo in explanation 1e5d50fa93 Merge #889: fix uninitialized read in tests c083cc6e52 Merge #903: Make argument of fe_normalizes_to_zero{_var} const 6e898534ff Merge #907: changed import to use brackets <> for openssl 4504472269 changed import to use brackets <> for openssl as they are not local to the project 26de4dfeb1 Merge #831: Safegcd inverses, drop Jacobi symbols, remove libgmp 23c3fb629b Make argument of fe_normalizes_to_zero{_var} const 24ad04fc06 Make scalar_inverse{,_var} benchmark scale with SECP256K1_BENCH_ITERS ebc1af700f Optimization: track f,g limb count and pass to new variable-time update_fg_var b306935ac1 Optimization: use formulas instead of lookup tables for cancelling g bits 9164a1b658 Optimization: special-case zero modulus limbs in modinv64 1f233b3fa0 Remove num/gmp support 20448b8d09 Remove unused Jacobi symbol support 5437e7bdfb Remove unused scalar_sqr aa9cc52180 Improve field/scalar inverse tests 1e0e885c8a Make field/scalar code use the new modinv modules for inverses 436281afdc Move secp256k1_fe_inverse{_var} to per-impl files aa404d53be Move secp256k1_scalar_{inverse{_var},is_even} to per-impl files 08d54964e5 Improve bounds checks in modinv modules 151aac00d3 Add tests for modinv modules d8a92fcc4c Add extensive comments on the safegcd algorithm and implementation 8e415acba2 Add safegcd based modular inverse modules de0a643c3d Add secp256k1_ctz{32,64}_var functions 4c3ba88c3a Merge #901: ci: Switch all Linux builds to Debian and more improvements 9361f360bb ci: Select number of parallel make jobs depending on CI environment 28eccdf806 ci: Split output of logs into multiple sections c7f754fe4d ci: Run PRs on merge result instead of on the source branch b994a8be3c ci: Print information about binaries using "file" f24e122d13 ci: Switch all Linux builds to Debian ebdba03cb5 Merge #891: build: Add workaround for automake 1.13 and older 3a8b47bc6d Merge #894: ctime_test: move context randomization test to the end 7d3497cdc4 ctime_test: move context randomization test to the end 99a1cfec17 print warnings for conditional-uninitialized 3d2cf6c5bd initialize variable in tests f329bba244 build: Add workaround for automake 1.13 and older 24d1656c32 Merge #882: Use bit ops instead of int mult for constant-time logic in gej_add_ge e491d06b98 Use bit ops instead of int mult for constant-time logic in gej_add_ge f8c0b57e6b Merge #864: Add support for Cirrus CI cc2a5451dc ci: Refactor Nix shell files 2480e55c8f ci: Remove support for Travis CI 2b359f1c1d ci: Enable simple cache for brewing valgrind on macOS 8c02e465c5 ci: Add support for Cirrus CI 659d0d4798 Merge #880: Add parens around ROUND_TO_ALIGN's parameter. b6f649889a Add parens around ROUND_TO_ALIGN's parameter. This makes the macro robust against a hypothetical ROUND_TO_ALIGN(foo ? sizeA : size B) invocation. a4abaab793 Merge #877: Add missing secp256k1_ge_set_gej_var decl. 5671e5f3fd Merge #874: Remove underscores from header defs. db726782fa Merge #878: Remove unused secp256k1_fe_inv_all_var b732701faa Merge #875: Avoid casting (void**) values. 75d2ae149e Remove unused secp256k1_fe_inv_all_var 482e4a9cfc Add missing secp256k1_ge_set_gej_var decl. 2730618604 Avoid casting (void**) values. Replaced with an expression that only casts (void*) values. fb390c5299 Remove underscores from header defs. This makes them consistent with other files and avoids reserved identifiers. f2d9aeae6d Merge #862: Autoconf improvements 328aaef22a Merge #845: Extract the secret key from a keypair 3c15130709 Improve CC_FOR_BUILD detection 47802a4762 Restructure and tidy configure.ac 252c19dfc6 Ask brew for valgrind include path 8c727b9087 Merge #860: fixed trivial typo b7bc3a4aaa fixed typo 33cb3c2b1f Add secret key extraction from keypair to constant time tests 36d9dc1e8e Add seckey extraction from keypair to the extrakeys tests fc96aa73f5 Add a function to extract the secretkey from a keypair 98dac87839 Merge #858: Fix insecure links 07aa4c70ff Fix insecure links b61f9da54e Merge #857: docs: fix simple typo, dependecy -> dependency 18aadf9d28 docs: fix simple typo, dependecy -> dependency 2d9e7175c6 Merge #852: Add sage script for generating scalar_split_lambda constants dc6e5c3a5c Merge #854: Rename msg32 to msghash32 in ecdsa_sign/verify and add explanation 6e85d675aa Rename tweak to tweak32 in public API f587f04e35 Rename msg32 to msghash32 in ecdsa_sign/verify and add explanation 329a2e0a3f sage: Add script for generating scalar_split_lambda constants 8f0c6f1545 Merge #851: make test count iteration configurable by environment variable f4fa8d226a forbid a test iteration of 0 or less f554dfc708 sage: Reorganize files 3a106966aa Merge #849: Convert Sage code to Python 3 (as used by Sage >= 9) 13c88efed0 Convert Sage code to Python 3 (as used by Sage >= 9) 0ce4554881 make test count iteration configurable by environment variable 9e5939d284 Merge #835: Don't use reserved identifiers memczero and benchmark_verify_t d0a83f7328 Merge #839: Prevent arithmetic on NULL pointer if the scratch space is too small 903b16aa6c Merge #840: Return NULL early in context_preallocated_create if flags invalid 1f4dd03838 Typedef (u)int128_t only when they're not provided by the compiler ebfa2058e9 Return NULL early in context_preallocated_create if flags invalid 29a299e373 Run the undefined behaviour sanitizer on Travis 7506e064d7 Prevent arithmetic on NULL pointer if the scratch space is too small e89278f211 Don't use reserved identifiers memczero and benchmark_verify_t git-subtree-dir: src/secp256k1 git-subtree-split: efad3506a8937162e8010f5839fdf3771dfcf516
315 lines
10 KiB
C
315 lines
10 KiB
C
/***********************************************************************
|
|
* Copyright (c) 2013-2015 Pieter Wuille *
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
|
***********************************************************************/
|
|
|
|
|
|
#ifndef SECP256K1_ECDSA_IMPL_H
|
|
#define SECP256K1_ECDSA_IMPL_H
|
|
|
|
#include "scalar.h"
|
|
#include "field.h"
|
|
#include "group.h"
|
|
#include "ecmult.h"
|
|
#include "ecmult_gen.h"
|
|
#include "ecdsa.h"
|
|
|
|
/** Group order for secp256k1 defined as 'n' in "Standards for Efficient Cryptography" (SEC2) 2.7.1
|
|
* sage: for t in xrange(1023, -1, -1):
|
|
* .. p = 2**256 - 2**32 - t
|
|
* .. if p.is_prime():
|
|
* .. print '%x'%p
|
|
* .. break
|
|
* 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f'
|
|
* sage: a = 0
|
|
* sage: b = 7
|
|
* sage: F = FiniteField (p)
|
|
* sage: '%x' % (EllipticCurve ([F (a), F (b)]).order())
|
|
* 'fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141'
|
|
*/
|
|
static const secp256k1_fe secp256k1_ecdsa_const_order_as_fe = SECP256K1_FE_CONST(
|
|
0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFFUL, 0xFFFFFFFEUL,
|
|
0xBAAEDCE6UL, 0xAF48A03BUL, 0xBFD25E8CUL, 0xD0364141UL
|
|
);
|
|
|
|
/** Difference between field and order, values 'p' and 'n' values defined in
|
|
* "Standards for Efficient Cryptography" (SEC2) 2.7.1.
|
|
* sage: p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
|
|
* sage: a = 0
|
|
* sage: b = 7
|
|
* sage: F = FiniteField (p)
|
|
* sage: '%x' % (p - EllipticCurve ([F (a), F (b)]).order())
|
|
* '14551231950b75fc4402da1722fc9baee'
|
|
*/
|
|
static const secp256k1_fe secp256k1_ecdsa_const_p_minus_order = SECP256K1_FE_CONST(
|
|
0, 0, 0, 1, 0x45512319UL, 0x50B75FC4UL, 0x402DA172UL, 0x2FC9BAEEUL
|
|
);
|
|
|
|
static int secp256k1_der_read_len(size_t *len, const unsigned char **sigp, const unsigned char *sigend) {
|
|
size_t lenleft;
|
|
unsigned char b1;
|
|
VERIFY_CHECK(len != NULL);
|
|
*len = 0;
|
|
if (*sigp >= sigend) {
|
|
return 0;
|
|
}
|
|
b1 = *((*sigp)++);
|
|
if (b1 == 0xFF) {
|
|
/* X.690-0207 8.1.3.5.c the value 0xFF shall not be used. */
|
|
return 0;
|
|
}
|
|
if ((b1 & 0x80) == 0) {
|
|
/* X.690-0207 8.1.3.4 short form length octets */
|
|
*len = b1;
|
|
return 1;
|
|
}
|
|
if (b1 == 0x80) {
|
|
/* Indefinite length is not allowed in DER. */
|
|
return 0;
|
|
}
|
|
/* X.690-207 8.1.3.5 long form length octets */
|
|
lenleft = b1 & 0x7F; /* lenleft is at least 1 */
|
|
if (lenleft > (size_t)(sigend - *sigp)) {
|
|
return 0;
|
|
}
|
|
if (**sigp == 0) {
|
|
/* Not the shortest possible length encoding. */
|
|
return 0;
|
|
}
|
|
if (lenleft > sizeof(size_t)) {
|
|
/* The resulting length would exceed the range of a size_t, so
|
|
* certainly longer than the passed array size.
|
|
*/
|
|
return 0;
|
|
}
|
|
while (lenleft > 0) {
|
|
*len = (*len << 8) | **sigp;
|
|
(*sigp)++;
|
|
lenleft--;
|
|
}
|
|
if (*len > (size_t)(sigend - *sigp)) {
|
|
/* Result exceeds the length of the passed array. */
|
|
return 0;
|
|
}
|
|
if (*len < 128) {
|
|
/* Not the shortest possible length encoding. */
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int secp256k1_der_parse_integer(secp256k1_scalar *r, const unsigned char **sig, const unsigned char *sigend) {
|
|
int overflow = 0;
|
|
unsigned char ra[32] = {0};
|
|
size_t rlen;
|
|
|
|
if (*sig == sigend || **sig != 0x02) {
|
|
/* Not a primitive integer (X.690-0207 8.3.1). */
|
|
return 0;
|
|
}
|
|
(*sig)++;
|
|
if (secp256k1_der_read_len(&rlen, sig, sigend) == 0) {
|
|
return 0;
|
|
}
|
|
if (rlen == 0 || *sig + rlen > sigend) {
|
|
/* Exceeds bounds or not at least length 1 (X.690-0207 8.3.1). */
|
|
return 0;
|
|
}
|
|
if (**sig == 0x00 && rlen > 1 && (((*sig)[1]) & 0x80) == 0x00) {
|
|
/* Excessive 0x00 padding. */
|
|
return 0;
|
|
}
|
|
if (**sig == 0xFF && rlen > 1 && (((*sig)[1]) & 0x80) == 0x80) {
|
|
/* Excessive 0xFF padding. */
|
|
return 0;
|
|
}
|
|
if ((**sig & 0x80) == 0x80) {
|
|
/* Negative. */
|
|
overflow = 1;
|
|
}
|
|
/* There is at most one leading zero byte:
|
|
* if there were two leading zero bytes, we would have failed and returned 0
|
|
* because of excessive 0x00 padding already. */
|
|
if (rlen > 0 && **sig == 0) {
|
|
/* Skip leading zero byte */
|
|
rlen--;
|
|
(*sig)++;
|
|
}
|
|
if (rlen > 32) {
|
|
overflow = 1;
|
|
}
|
|
if (!overflow) {
|
|
memcpy(ra + 32 - rlen, *sig, rlen);
|
|
secp256k1_scalar_set_b32(r, ra, &overflow);
|
|
}
|
|
if (overflow) {
|
|
secp256k1_scalar_set_int(r, 0);
|
|
}
|
|
(*sig) += rlen;
|
|
return 1;
|
|
}
|
|
|
|
static int secp256k1_ecdsa_sig_parse(secp256k1_scalar *rr, secp256k1_scalar *rs, const unsigned char *sig, size_t size) {
|
|
const unsigned char *sigend = sig + size;
|
|
size_t rlen;
|
|
if (sig == sigend || *(sig++) != 0x30) {
|
|
/* The encoding doesn't start with a constructed sequence (X.690-0207 8.9.1). */
|
|
return 0;
|
|
}
|
|
if (secp256k1_der_read_len(&rlen, &sig, sigend) == 0) {
|
|
return 0;
|
|
}
|
|
if (rlen != (size_t)(sigend - sig)) {
|
|
/* Tuple exceeds bounds or garage after tuple. */
|
|
return 0;
|
|
}
|
|
|
|
if (!secp256k1_der_parse_integer(rr, &sig, sigend)) {
|
|
return 0;
|
|
}
|
|
if (!secp256k1_der_parse_integer(rs, &sig, sigend)) {
|
|
return 0;
|
|
}
|
|
|
|
if (sig != sigend) {
|
|
/* Trailing garbage inside tuple. */
|
|
return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
static int secp256k1_ecdsa_sig_serialize(unsigned char *sig, size_t *size, const secp256k1_scalar* ar, const secp256k1_scalar* as) {
|
|
unsigned char r[33] = {0}, s[33] = {0};
|
|
unsigned char *rp = r, *sp = s;
|
|
size_t lenR = 33, lenS = 33;
|
|
secp256k1_scalar_get_b32(&r[1], ar);
|
|
secp256k1_scalar_get_b32(&s[1], as);
|
|
while (lenR > 1 && rp[0] == 0 && rp[1] < 0x80) { lenR--; rp++; }
|
|
while (lenS > 1 && sp[0] == 0 && sp[1] < 0x80) { lenS--; sp++; }
|
|
if (*size < 6+lenS+lenR) {
|
|
*size = 6 + lenS + lenR;
|
|
return 0;
|
|
}
|
|
*size = 6 + lenS + lenR;
|
|
sig[0] = 0x30;
|
|
sig[1] = 4 + lenS + lenR;
|
|
sig[2] = 0x02;
|
|
sig[3] = lenR;
|
|
memcpy(sig+4, rp, lenR);
|
|
sig[4+lenR] = 0x02;
|
|
sig[5+lenR] = lenS;
|
|
memcpy(sig+lenR+6, sp, lenS);
|
|
return 1;
|
|
}
|
|
|
|
static int secp256k1_ecdsa_sig_verify(const secp256k1_ecmult_context *ctx, const secp256k1_scalar *sigr, const secp256k1_scalar *sigs, const secp256k1_ge *pubkey, const secp256k1_scalar *message) {
|
|
unsigned char c[32];
|
|
secp256k1_scalar sn, u1, u2;
|
|
#if !defined(EXHAUSTIVE_TEST_ORDER)
|
|
secp256k1_fe xr;
|
|
#endif
|
|
secp256k1_gej pubkeyj;
|
|
secp256k1_gej pr;
|
|
|
|
if (secp256k1_scalar_is_zero(sigr) || secp256k1_scalar_is_zero(sigs)) {
|
|
return 0;
|
|
}
|
|
|
|
secp256k1_scalar_inverse_var(&sn, sigs);
|
|
secp256k1_scalar_mul(&u1, &sn, message);
|
|
secp256k1_scalar_mul(&u2, &sn, sigr);
|
|
secp256k1_gej_set_ge(&pubkeyj, pubkey);
|
|
secp256k1_ecmult(ctx, &pr, &pubkeyj, &u2, &u1);
|
|
if (secp256k1_gej_is_infinity(&pr)) {
|
|
return 0;
|
|
}
|
|
|
|
#if defined(EXHAUSTIVE_TEST_ORDER)
|
|
{
|
|
secp256k1_scalar computed_r;
|
|
secp256k1_ge pr_ge;
|
|
secp256k1_ge_set_gej(&pr_ge, &pr);
|
|
secp256k1_fe_normalize(&pr_ge.x);
|
|
|
|
secp256k1_fe_get_b32(c, &pr_ge.x);
|
|
secp256k1_scalar_set_b32(&computed_r, c, NULL);
|
|
return secp256k1_scalar_eq(sigr, &computed_r);
|
|
}
|
|
#else
|
|
secp256k1_scalar_get_b32(c, sigr);
|
|
secp256k1_fe_set_b32(&xr, c);
|
|
|
|
/** We now have the recomputed R point in pr, and its claimed x coordinate (modulo n)
|
|
* in xr. Naively, we would extract the x coordinate from pr (requiring a inversion modulo p),
|
|
* compute the remainder modulo n, and compare it to xr. However:
|
|
*
|
|
* xr == X(pr) mod n
|
|
* <=> exists h. (xr + h * n < p && xr + h * n == X(pr))
|
|
* [Since 2 * n > p, h can only be 0 or 1]
|
|
* <=> (xr == X(pr)) || (xr + n < p && xr + n == X(pr))
|
|
* [In Jacobian coordinates, X(pr) is pr.x / pr.z^2 mod p]
|
|
* <=> (xr == pr.x / pr.z^2 mod p) || (xr + n < p && xr + n == pr.x / pr.z^2 mod p)
|
|
* [Multiplying both sides of the equations by pr.z^2 mod p]
|
|
* <=> (xr * pr.z^2 mod p == pr.x) || (xr + n < p && (xr + n) * pr.z^2 mod p == pr.x)
|
|
*
|
|
* Thus, we can avoid the inversion, but we have to check both cases separately.
|
|
* secp256k1_gej_eq_x implements the (xr * pr.z^2 mod p == pr.x) test.
|
|
*/
|
|
if (secp256k1_gej_eq_x_var(&xr, &pr)) {
|
|
/* xr * pr.z^2 mod p == pr.x, so the signature is valid. */
|
|
return 1;
|
|
}
|
|
if (secp256k1_fe_cmp_var(&xr, &secp256k1_ecdsa_const_p_minus_order) >= 0) {
|
|
/* xr + n >= p, so we can skip testing the second case. */
|
|
return 0;
|
|
}
|
|
secp256k1_fe_add(&xr, &secp256k1_ecdsa_const_order_as_fe);
|
|
if (secp256k1_gej_eq_x_var(&xr, &pr)) {
|
|
/* (xr + n) * pr.z^2 mod p == pr.x, so the signature is valid. */
|
|
return 1;
|
|
}
|
|
return 0;
|
|
#endif
|
|
}
|
|
|
|
static int secp256k1_ecdsa_sig_sign(const secp256k1_ecmult_gen_context *ctx, secp256k1_scalar *sigr, secp256k1_scalar *sigs, const secp256k1_scalar *seckey, const secp256k1_scalar *message, const secp256k1_scalar *nonce, int *recid) {
|
|
unsigned char b[32];
|
|
secp256k1_gej rp;
|
|
secp256k1_ge r;
|
|
secp256k1_scalar n;
|
|
int overflow = 0;
|
|
int high;
|
|
|
|
secp256k1_ecmult_gen(ctx, &rp, nonce);
|
|
secp256k1_ge_set_gej(&r, &rp);
|
|
secp256k1_fe_normalize(&r.x);
|
|
secp256k1_fe_normalize(&r.y);
|
|
secp256k1_fe_get_b32(b, &r.x);
|
|
secp256k1_scalar_set_b32(sigr, b, &overflow);
|
|
if (recid) {
|
|
/* The overflow condition is cryptographically unreachable as hitting it requires finding the discrete log
|
|
* of some P where P.x >= order, and only 1 in about 2^127 points meet this criteria.
|
|
*/
|
|
*recid = (overflow << 1) | secp256k1_fe_is_odd(&r.y);
|
|
}
|
|
secp256k1_scalar_mul(&n, sigr, seckey);
|
|
secp256k1_scalar_add(&n, &n, message);
|
|
secp256k1_scalar_inverse(sigs, nonce);
|
|
secp256k1_scalar_mul(sigs, sigs, &n);
|
|
secp256k1_scalar_clear(&n);
|
|
secp256k1_gej_clear(&rp);
|
|
secp256k1_ge_clear(&r);
|
|
high = secp256k1_scalar_is_high(sigs);
|
|
secp256k1_scalar_cond_negate(sigs, high);
|
|
if (recid) {
|
|
*recid ^= high;
|
|
}
|
|
/* P.x = order is on the curve, so technically sig->r could end up being zero, which would be an invalid signature.
|
|
* This is cryptographically unreachable as hitting it requires finding the discrete log of P.x = N.
|
|
*/
|
|
return !secp256k1_scalar_is_zero(sigr) & !secp256k1_scalar_is_zero(sigs);
|
|
}
|
|
|
|
#endif /* SECP256K1_ECDSA_IMPL_H */
|