Skip to content

Commit 7648f6b

Browse files
committed
Squashed 'src/secp256k1/' changes from 1758a92ffd..be8d9c262f
be8d9c262f Merge bitcoin-core/secp256k1#965: gen_context: Don't use any ASM aeece44599 gen_context: Don't use any ASM 7688a4f13a Merge bitcoin-core/secp256k1#963: "Schnorrsig API overhaul" fixups 90e83449b2 ci: Add C++ test f698caaff6 Use unsigned char consistently for byte arrays b5b8e7b719 Don't declare constants twice 769528f307 Don't use string literals for char arrays without NUL termination 2cc3cfa583 Fix -Wmissing-braces warning in clang 0440945fb5 Merge zcash#844: schnorrsig API overhaul ec3aaa5014 Merge zcash#960: tests_exhaustive: check the result of secp256k1_ecdsa_sign a1ee83c654 tests_exhaustive: check the result of secp256k1_ecdsa_sign 253f90cdeb Merge bitcoin-core/secp256k1#951: configure: replace AC_PATH_PROG to AC_CHECK_PROG 446d28d9de Merge bitcoin-core/secp256k1#944: Various improvements related to CFLAGS 0302138f75 ci: Make compiler warning into errors on CI b924e1e605 build: Ensure that configure's compile checks default to -O2 7939cd571c build: List *CPPFLAGS before *CFLAGS like on the compiler command line 595e8a35d8 build: Enable -Wcast-align=strict warning 07256267ff build: Use own variable SECP_CFLAGS instead of touching user CFLAGS 4866178dfc Merge bitcoin-core/secp256k1#955: Add random field multiply/square tests 75ce488c2a Merge bitcoin-core/secp256k1#959: tests: really test the non-var scalar inverse 41ed13942b tests: really test the non-var scalar inverse 5f6ceafcfa schnorrsig: allow setting MSGLEN != 32 in benchmark fdd06b7967 schnorrsig: add tests for sign_custom and varlen msg verification d8d806aaf3 schnorrsig: add extra parameter struct for sign_custom a0c3fc177f schnorrsig: allow signing and verification of variable length msgs 5a8e4991ad Add secp256k1_tagged_sha256 as defined in BIP-340 b6c0b72fb0 schnorrsig: remove noncefp args from sign; add sign_custom function bdf19f105c Add random field multiply/square tests 8ae56e33e7 Merge zcash#879: Avoid passing out-of-bound pointers to 0-size memcpy a4642fa15e configure: replace AC_PATH_PROG to AC_CHECK_PROG 442cee5baf schnorrsig: add algolen argument to nonce_function_hardened df3bfa12c3 schnorrsig: clarify result of calling nonce_function_bip340 without data 99e8614812 README: mention schnorrsig module 9570f674cc Avoid passing out-of-bound pointers to 0-size memcpy git-subtree-dir: src/secp256k1 git-subtree-split: be8d9c262f46309d9b4165b0498b71d704aba8fe
1 parent ed3e5b0 commit 7648f6b

23 files changed

+581
-218
lines changed

.cirrus.yml

+34-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
env:
2-
WIDEMUL: auto
2+
### compiler options
3+
HOST:
4+
# Specific warnings can be disabled with -Wno-error=foo.
5+
# -pedantic-errors is not equivalent to -Werror=pedantic and thus not implied by -Werror according to the GCC manual.
6+
WERROR_CFLAGS: -Werror -pedantic-errors
7+
MAKEFLAGS: -j2
8+
BUILD: check
9+
### secp256k1 config
310
STATICPRECOMPUTATION: yes
411
ECMULTGENPRECISION: auto
512
ASM: no
6-
BUILD: check
13+
WIDEMUL: auto
714
WITH_VALGRIND: yes
815
EXTRAFLAGS:
9-
HOST:
16+
### secp256k1 modules
17+
EXPERIMENTAL: no
1018
ECDH: no
1119
RECOVERY: no
1220
SCHNORRSIG: no
13-
EXPERIMENTAL: no
14-
CTIMETEST: yes
15-
BENCH: yes
21+
### test options
1622
TEST_ITERS:
23+
BENCH: yes
1724
BENCH_ITERS: 2
18-
MAKEFLAGS: -j2
25+
CTIMETEST: yes
1926

2027
cat_logs_snippet: &CAT_LOGS
2128
always:
@@ -313,3 +320,23 @@ task:
313320
- ./ci/cirrus.sh
314321
<< : *CAT_LOGS
315322

323+
task:
324+
name: "C++ -fpermissive"
325+
container:
326+
dockerfile: ci/linux-debian.Dockerfile
327+
cpu: 1
328+
memory: 1G
329+
env:
330+
# ./configure correctly errors out when given CC=g++.
331+
# We hack around this by passing CC=g++ only to make.
332+
CC: gcc
333+
MAKEFLAGS: -j2 CC=g++ CFLAGS=-fpermissive
334+
WERROR_CFLAGS:
335+
EXPERIMENTAL: yes
336+
ECDH: yes
337+
RECOVERY: yes
338+
SCHNORRSIG: yes
339+
<< : *MERGE_BASE
340+
test_script:
341+
- ./ci/cirrus.sh
342+
<< : *CAT_LOGS

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ aclocal.m4
2323
autom4te.cache/
2424
config.log
2525
config.status
26+
conftest*
2627
*.tar.gz
2728
*.la
2829
libtool

Makefile.am

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
ACLOCAL_AMFLAGS = -I build-aux/m4
22

3+
# AM_CFLAGS will be automatically prepended to CFLAGS by Automake when compiling some foo
4+
# which does not have an explicit foo_CFLAGS variable set.
5+
AM_CFLAGS = $(SECP_CFLAGS)
6+
37
lib_LTLIBRARIES = libsecp256k1.la
48
include_HEADERS = include/secp256k1.h
59
include_HEADERS += include/secp256k1_preallocated.h
@@ -129,10 +133,10 @@ CPPFLAGS_FOR_BUILD +=-I$(top_srcdir) -I$(builddir)/src
129133
gen_context_OBJECTS = gen_context.o
130134
gen_context_BIN = gen_context$(BUILD_EXEEXT)
131135
gen_%.o: src/gen_%.c src/libsecp256k1-config.h
132-
$(CC_FOR_BUILD) $(DEFS) $(CPPFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) -c $< -o $@
136+
$(CC_FOR_BUILD) $(DEFS) $(CPPFLAGS_FOR_BUILD) $(SECP_CFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) -c $< -o $@
133137

134138
$(gen_context_BIN): $(gen_context_OBJECTS)
135-
$(CC_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) $^ -o $@
139+
$(CC_FOR_BUILD) $(SECP_CFLAGS_FOR_BUILD) $(CFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) $^ -o $@
136140

137141
$(libsecp256k1_la_OBJECTS): src/ecmult_static_context.h
138142
$(tests_OBJECTS): src/ecmult_static_context.h

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Features:
1717
* Suitable for embedded systems.
1818
* Optional module for public key recovery.
1919
* Optional module for ECDH key exchange.
20+
* Optional module for Schnorr signatures according to [BIP-340](https://github.com/bitcoin/bips/blob/master/bip-0340.mediawiki) (experimental).
2021

2122
Experimental features have not received enough scrutiny to satisfy the standard of quality of this library but are made available for testing and review by the community. The APIs of these features should not be considered stable.
2223

build-aux/m4/bitcoin_secp.m4

+16
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,19 @@ if test x"$has_valgrind" != x"yes"; then
8282
AC_CHECK_HEADER([valgrind/memcheck.h], [has_valgrind=yes; AC_DEFINE(HAVE_VALGRIND,1,[Define this symbol if valgrind is installed])])
8383
fi
8484
])
85+
86+
dnl SECP_TRY_APPEND_CFLAGS(flags, VAR)
87+
dnl Append flags to VAR if CC accepts them.
88+
AC_DEFUN([SECP_TRY_APPEND_CFLAGS], [
89+
AC_MSG_CHECKING([if ${CC} supports $1])
90+
SECP_TRY_APPEND_CFLAGS_saved_CFLAGS="$CFLAGS"
91+
CFLAGS="$1 $CFLAGS"
92+
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])], [flag_works=yes], [flag_works=no])
93+
AC_MSG_RESULT($flag_works)
94+
CFLAGS="$SECP_TRY_APPEND_CFLAGS_saved_CFLAGS"
95+
if test x"$flag_works" = x"yes"; then
96+
$2="$$2 $1"
97+
fi
98+
unset flag_works
99+
AC_SUBST($2)
100+
])

ci/linux-debian.Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ RUN apt-get install --no-install-recommends --no-upgrade -y \
1313
git ca-certificates \
1414
make automake libtool pkg-config dpkg-dev valgrind qemu-user \
1515
gcc clang llvm libc6-dbg \
16+
g++ \
1617
gcc-i686-linux-gnu libc6-dev-i386-cross libc6-dbg:i386 libubsan1:i386 libasan5:i386 \
1718
gcc-s390x-linux-gnu libc6-dev-s390x-cross libc6-dbg:s390x \
1819
gcc-arm-linux-gnueabihf libc6-dev-armhf-cross libc6-dbg:armhf \

configure.ac

+62-58
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ AH_TOP([#define LIBSECP256K1_CONFIG_H])
88
AH_BOTTOM([#endif /*LIBSECP256K1_CONFIG_H*/])
99
AM_INIT_AUTOMAKE([foreign subdir-objects])
1010

11-
# Set -g if CFLAGS are not already set, which matches the default autoconf
12-
# behavior (see PROG_CC in the Autoconf manual) with the exception that we don't
13-
# set -O2 here because we set it in any case (see further down).
14-
: ${CFLAGS="-g"}
1511
LT_INIT
1612

1713
# Make the compilation flags quiet unless V=1 is used.
@@ -42,8 +38,8 @@ AM_PROG_AS
4238
case $host_os in
4339
*darwin*)
4440
if test x$cross_compiling != xyes; then
45-
AC_PATH_PROG([BREW],brew,)
46-
if test x$BREW != x; then
41+
AC_CHECK_PROG([BREW], brew, brew)
42+
if test x$BREW = xbrew; then
4743
# These Homebrew packages may be keg-only, meaning that they won't be found
4844
# in expected paths because they may conflict with system files. Ask
4945
# Homebrew where each one is located, then adjust paths accordingly.
@@ -58,10 +54,10 @@ case $host_os in
5854
VALGRIND_CPPFLAGS="-I$valgrind_prefix/include"
5955
fi
6056
else
61-
AC_PATH_PROG([PORT],port,)
57+
AC_CHECK_PROG([PORT], port, port)
6258
# If homebrew isn't installed and macports is, add the macports default paths
6359
# as a last resort.
64-
if test x$PORT != x; then
60+
if test x$PORT = xport; then
6561
CPPFLAGS="$CPPFLAGS -isystem /opt/local/include"
6662
LDFLAGS="$LDFLAGS -L/opt/local/lib"
6763
fi
@@ -70,35 +66,41 @@ case $host_os in
7066
;;
7167
esac
7268

73-
CFLAGS="-W $CFLAGS"
74-
75-
warn_CFLAGS="-std=c89 -pedantic -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -Wundef -Wno-unused-function -Wno-long-long -Wno-overlength-strings"
76-
saved_CFLAGS="$CFLAGS"
77-
CFLAGS="$warn_CFLAGS $CFLAGS"
78-
AC_MSG_CHECKING([if ${CC} supports ${warn_CFLAGS}])
79-
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
80-
[ AC_MSG_RESULT([yes]) ],
81-
[ AC_MSG_RESULT([no])
82-
CFLAGS="$saved_CFLAGS"
83-
])
84-
85-
saved_CFLAGS="$CFLAGS"
86-
CFLAGS="-Wconditional-uninitialized $CFLAGS"
87-
AC_MSG_CHECKING([if ${CC} supports -Wconditional-uninitialized])
88-
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
89-
[ AC_MSG_RESULT([yes]) ],
90-
[ AC_MSG_RESULT([no])
91-
CFLAGS="$saved_CFLAGS"
92-
])
93-
94-
saved_CFLAGS="$CFLAGS"
95-
CFLAGS="-fvisibility=hidden $CFLAGS"
96-
AC_MSG_CHECKING([if ${CC} supports -fvisibility=hidden])
97-
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
98-
[ AC_MSG_RESULT([yes]) ],
99-
[ AC_MSG_RESULT([no])
100-
CFLAGS="$saved_CFLAGS"
101-
])
69+
# Try if some desirable compiler flags are supported and append them to SECP_CFLAGS.
70+
#
71+
# These are our own flags, so we append them to our own SECP_CFLAGS variable (instead of CFLAGS) as
72+
# recommended in the automake manual (Section "Flag Variables Ordering"). CFLAGS belongs to the user
73+
# and we are not supposed to touch it. In the Makefile, we will need to ensure that SECP_CFLAGS
74+
# is prepended to CFLAGS when invoking the compiler so that the user always has the last word (flag).
75+
#
76+
# Another advantage of not touching CFLAGS is that the contents of CFLAGS will be picked up by
77+
# libtool for compiling helper executables. For example, when compiling for Windows, libtool will
78+
# generate entire wrapper executables (instead of simple wrapper scripts as on Unix) to ensure
79+
# proper operation of uninstalled programs linked by libtool against the uninstalled shared library.
80+
# These executables are compiled from C source file for which our flags may not be appropriate,
81+
# e.g., -std=c89 flag has lead to undesirable warnings in the past.
82+
#
83+
# TODO We should analogously not touch CPPFLAGS and LDFLAGS but currently there are no issues.
84+
AC_DEFUN([SECP_TRY_APPEND_DEFAULT_CFLAGS], [
85+
# Try to append -Werror=unknown-warning-option to CFLAGS temporarily. Otherwise clang will
86+
# not error out if it gets unknown warning flags and the checks here will always succeed
87+
# no matter if clang knows the flag or not.
88+
SECP_TRY_APPEND_DEFAULT_CFLAGS_saved_CFLAGS="$CFLAGS"
89+
SECP_TRY_APPEND_CFLAGS([-Werror=unknown-warning-option], CFLAGS)
90+
91+
SECP_TRY_APPEND_CFLAGS([-std=c89 -pedantic -Wno-long-long -Wnested-externs -Wshadow -Wstrict-prototypes -Wundef], $1) # GCC >= 3.0, -Wlong-long is implied by -pedantic.
92+
SECP_TRY_APPEND_CFLAGS([-Wno-overlength-strings], $1) # GCC >= 4.2, -Woverlength-strings is implied by -pedantic.
93+
SECP_TRY_APPEND_CFLAGS([-Wall], $1) # GCC >= 2.95 and probably many other compilers
94+
SECP_TRY_APPEND_CFLAGS([-Wno-unused-function], $1) # GCC >= 3.0, -Wunused-function is implied by -Wall.
95+
SECP_TRY_APPEND_CFLAGS([-Wextra], $1) # GCC >= 3.4, this is the newer name of -W, which we don't use because older GCCs will warn about unused functions.
96+
SECP_TRY_APPEND_CFLAGS([-Wcast-align], $1) # GCC >= 2.95
97+
SECP_TRY_APPEND_CFLAGS([-Wcast-align=strict], $1) # GCC >= 8.0
98+
SECP_TRY_APPEND_CFLAGS([-Wconditional-uninitialized], $1) # Clang >= 3.0 only
99+
SECP_TRY_APPEND_CFLAGS([-fvisibility=hidden], $1) # GCC >= 4.0
100+
101+
CFLAGS="$SECP_TRY_APPEND_DEFAULT_CFLAGS_saved_CFLAGS"
102+
])
103+
SECP_TRY_APPEND_DEFAULT_CFLAGS(SECP_CFLAGS)
102104

103105
###
104106
### Define config arguments
@@ -213,10 +215,14 @@ AM_CONDITIONAL([VALGRIND_ENABLED],[test "$enable_valgrind" = "yes"])
213215

214216
if test x"$enable_coverage" = x"yes"; then
215217
AC_DEFINE(COVERAGE, 1, [Define this symbol to compile out all VERIFY code])
216-
CFLAGS="-O0 --coverage $CFLAGS"
218+
SECP_CFLAGS="-O0 --coverage $SECP_CFLAGS"
217219
LDFLAGS="--coverage $LDFLAGS"
218220
else
219-
CFLAGS="-O2 $CFLAGS"
221+
# Most likely the CFLAGS already contain -O2 because that is autoconf's default.
222+
# We still add it here because passing it twice is not an issue, and handling
223+
# this case would just add unnecessary complexity (see #896).
224+
SECP_CFLAGS="-O2 $SECP_CFLAGS"
225+
SECP_CFLAGS_FOR_BUILD="-O2 $SECP_CFLAGS_FOR_BUILD"
220226
fi
221227

222228
if test x"$req_asm" = x"auto"; then
@@ -351,6 +357,9 @@ if test x"$enable_valgrind" = x"yes"; then
351357
SECP_INCLUDES="$SECP_INCLUDES $VALGRIND_CPPFLAGS"
352358
fi
353359

360+
# Add -Werror and similar flags passed from the outside (for testing, e.g., in CI)
361+
SECP_CFLAGS="$SECP_CFLAGS $WERROR_CFLAGS"
362+
354363
# Handle static precomputation (after everything which modifies CFLAGS and friends)
355364
if test x"$use_ecmult_static_precomputation" != x"no"; then
356365
if test x"$cross_compiling" = x"no"; then
@@ -360,8 +369,9 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
360369
fi
361370
# If we're not cross-compiling, simply use the same compiler for building the static precompation code.
362371
CC_FOR_BUILD="$CC"
363-
CFLAGS_FOR_BUILD="$CFLAGS"
364372
CPPFLAGS_FOR_BUILD="$CPPFLAGS"
373+
SECP_CFLAGS_FOR_BUILD="$SECP_CFLAGS"
374+
CFLAGS_FOR_BUILD="$CFLAGS"
365375
LDFLAGS_FOR_BUILD="$LDFLAGS"
366376
else
367377
AX_PROG_CC_FOR_BUILD
@@ -371,42 +381,32 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
371381
cross_compiling=no
372382
SAVE_CC="$CC"
373383
CC="$CC_FOR_BUILD"
374-
SAVE_CFLAGS="$CFLAGS"
375-
CFLAGS="$CFLAGS_FOR_BUILD"
376384
SAVE_CPPFLAGS="$CPPFLAGS"
377385
CPPFLAGS="$CPPFLAGS_FOR_BUILD"
386+
SAVE_CFLAGS="$CFLAGS"
387+
CFLAGS="$CFLAGS_FOR_BUILD"
378388
SAVE_LDFLAGS="$LDFLAGS"
379389
LDFLAGS="$LDFLAGS_FOR_BUILD"
380390

381-
warn_CFLAGS_FOR_BUILD="-Wall -Wextra -Wno-unused-function"
382-
saved_CFLAGS="$CFLAGS"
383-
CFLAGS="$warn_CFLAGS_FOR_BUILD $CFLAGS"
384-
AC_MSG_CHECKING([if native ${CC_FOR_BUILD} supports ${warn_CFLAGS_FOR_BUILD}])
385-
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[char foo;]])],
386-
[ AC_MSG_RESULT([yes]) ],
387-
[ AC_MSG_RESULT([no])
388-
CFLAGS="$saved_CFLAGS"
389-
])
391+
SECP_TRY_APPEND_DEFAULT_CFLAGS(SECP_CFLAGS_FOR_BUILD)
390392

391393
AC_MSG_CHECKING([for working native compiler: ${CC_FOR_BUILD}])
392394
AC_RUN_IFELSE(
393395
[AC_LANG_PROGRAM([], [])],
394396
[working_native_cc=yes],
395397
[working_native_cc=no],[:])
396398

397-
CFLAGS_FOR_BUILD="$CFLAGS"
398-
399399
# Restore the environment
400400
cross_compiling=$save_cross_compiling
401401
CC="$SAVE_CC"
402-
CFLAGS="$SAVE_CFLAGS"
403402
CPPFLAGS="$SAVE_CPPFLAGS"
403+
CFLAGS="$SAVE_CFLAGS"
404404
LDFLAGS="$SAVE_LDFLAGS"
405405

406406
if test x"$working_native_cc" = x"no"; then
407407
AC_MSG_RESULT([no])
408408
set_precomp=no
409-
m4_define([please_set_for_build], [Please set CC_FOR_BUILD, CFLAGS_FOR_BUILD, CPPFLAGS_FOR_BUILD, and/or LDFLAGS_FOR_BUILD.])
409+
m4_define([please_set_for_build], [Please set CC_FOR_BUILD, CPPFLAGS_FOR_BUILD, CFLAGS_FOR_BUILD, and/or LDFLAGS_FOR_BUILD.])
410410
if test x"$use_ecmult_static_precomputation" = x"yes"; then
411411
AC_MSG_ERROR([native compiler ${CC_FOR_BUILD} does not produce working binaries. please_set_for_build])
412412
else
@@ -419,8 +419,9 @@ if test x"$use_ecmult_static_precomputation" != x"no"; then
419419
fi
420420

421421
AC_SUBST(CC_FOR_BUILD)
422-
AC_SUBST(CFLAGS_FOR_BUILD)
423422
AC_SUBST(CPPFLAGS_FOR_BUILD)
423+
AC_SUBST(SECP_CFLAGS_FOR_BUILD)
424+
AC_SUBST(CFLAGS_FOR_BUILD)
424425
AC_SUBST(LDFLAGS_FOR_BUILD)
425426
else
426427
set_precomp=no
@@ -490,6 +491,7 @@ AC_SUBST(SECP_INCLUDES)
490491
AC_SUBST(SECP_LIBS)
491492
AC_SUBST(SECP_TEST_LIBS)
492493
AC_SUBST(SECP_TEST_INCLUDES)
494+
AC_SUBST(SECP_CFLAGS)
493495
AM_CONDITIONAL([ENABLE_COVERAGE], [test x"$enable_coverage" = x"yes"])
494496
AM_CONDITIONAL([USE_TESTS], [test x"$use_tests" != x"no"])
495497
AM_CONDITIONAL([USE_EXHAUSTIVE_TESTS], [test x"$use_exhaustive_tests" != x"no"])
@@ -532,13 +534,15 @@ fi
532534
echo
533535
echo " valgrind = $enable_valgrind"
534536
echo " CC = $CC"
535-
echo " CFLAGS = $CFLAGS"
536537
echo " CPPFLAGS = $CPPFLAGS"
538+
echo " SECP_CFLAGS = $SECP_CFLAGS"
539+
echo " CFLAGS = $CFLAGS"
537540
echo " LDFLAGS = $LDFLAGS"
538541
echo
539542
if test x"$set_precomp" = x"yes"; then
540543
echo " CC_FOR_BUILD = $CC_FOR_BUILD"
541-
echo " CFLAGS_FOR_BUILD = $CFLAGS_FOR_BUILD"
542544
echo " CPPFLAGS_FOR_BUILD = $CPPFLAGS_FOR_BUILD"
545+
echo " SECP_CFLAGS_FOR_BUILD = $SECP_CFLAGS_FOR_BUILD"
546+
echo " CFLAGS_FOR_BUILD = $CFLAGS_FOR_BUILD"
543547
echo " LDFLAGS_FOR_BUILD = $LDFLAGS_FOR_BUILD"
544548
fi

contrib/lax_der_parsing.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
120120
/* Copy R value */
121121
if (rlen > 32) {
122122
overflow = 1;
123-
} else {
123+
} else if (rlen) {
124124
memcpy(tmpsig + 32 - rlen, input + rpos, rlen);
125125
}
126126

@@ -132,7 +132,7 @@ int ecdsa_signature_parse_der_lax(const secp256k1_context* ctx, secp256k1_ecdsa_
132132
/* Copy S value */
133133
if (slen > 32) {
134134
overflow = 1;
135-
} else {
135+
} else if (slen) {
136136
memcpy(tmpsig + 64 - slen, input + spos, slen);
137137
}
138138

contrib/lax_der_privatekey_parsing.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int ec_privkey_import_der(const secp256k1_context* ctx, unsigned char *out32, co
4444
if (end < privkey+2 || privkey[0] != 0x04 || privkey[1] > 0x20 || end < privkey+2+privkey[1]) {
4545
return 0;
4646
}
47-
memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
47+
if (privkey[1]) memcpy(out32 + 32 - privkey[1], privkey + 2, privkey[1]);
4848
if (!secp256k1_ec_seckey_verify(ctx, out32)) {
4949
memset(out32, 0, 32);
5050
return 0;

0 commit comments

Comments
 (0)