Skip to content

Commit 978e810

Browse files
authored
Bixin dev:update secp256k1 submodule && fix pin issues (#342)
* chore(vendor): replace secp256k1-zkp with secp256k1 (v0.3.0) * chore(vendor): update secp256k1 submodule * fix(legacy):fix pin related issues
1 parent f959b10 commit 978e810

File tree

14 files changed

+71
-31
lines changed

14 files changed

+71
-31
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
ignore = untracked
88
[submodule "vendor/secp256k1-zkp"]
99
path = vendor/secp256k1-zkp
10-
url = https://github.com/ElementsProject/secp256k1-zkp.git
10+
url = https://github.com/bitcoin-core/secp256k1.git
1111
[submodule "common/defs/ethereum/tokens"]
1212
path = common/defs/ethereum/tokens
1313
url = https://github.com/ethereum-lists/tokens.git

crypto/zkp_bip340.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ int zkp_bip340_get_public_key(const uint8_t *private_key_bytes,
101101
// private_key_bytes has 32 bytes
102102
// digest has 32 bytes
103103
// signature_bytes has 64 bytes
104-
// auxiliary_data has 32 bytes or is NULL
104+
// auxiliary_data has 32 bytes or is NULL (32 zero bytes are used)
105105
// returns 0 on success
106106
int zkp_bip340_sign_digest(const uint8_t *private_key_bytes,
107107
const uint8_t *digest, uint8_t *signature_bytes,
@@ -136,8 +136,8 @@ int zkp_bip340_sign_digest(const uint8_t *private_key_bytes,
136136
}
137137

138138
if (result == 0) {
139-
if (secp256k1_schnorrsig_sign(context_writable, signature_bytes, digest,
140-
&keypair, auxiliary_data) != 1) {
139+
if (secp256k1_schnorrsig_sign32(context_writable, signature_bytes, digest,
140+
&keypair, auxiliary_data) != 1) {
141141
result = -1;
142142
}
143143
}
@@ -184,6 +184,27 @@ int zkp_bip340_verify_digest(const uint8_t *public_key_bytes,
184184
return result;
185185
}
186186

187+
// BIP340 Schnorr public key verification
188+
// public_key_bytes has 32 bytes
189+
// returns 0 if verification succeeded
190+
int zkp_bip340_verify_publickey(const uint8_t *public_key_bytes) {
191+
int result = 0;
192+
193+
secp256k1_xonly_pubkey xonly_pubkey = {0};
194+
const secp256k1_context *context_read_only = zkp_context_get_read_only();
195+
196+
if (result == 0) {
197+
if (secp256k1_xonly_pubkey_parse(context_read_only, &xonly_pubkey,
198+
public_key_bytes) != 1) {
199+
result = 1;
200+
}
201+
}
202+
203+
memzero(&xonly_pubkey, sizeof(xonly_pubkey));
204+
205+
return result;
206+
}
207+
187208
// BIP340 Schnorr public key tweak
188209
// internal_public_key has 32 bytes
189210
// root_hash has 32 bytes or is empty (NULL)

crypto/zkp_bip340.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ int zkp_bip340_sign_digest(const uint8_t *private_key_bytes,
1111
int zkp_bip340_verify_digest(const uint8_t *public_key_bytes,
1212
const uint8_t *signature_bytes,
1313
const uint8_t *digest);
14+
int zkp_bip340_verify_publickey(const uint8_t *public_key_bytes);
1415
int zkp_bip340_tweak_public_key(const uint8_t *internal_public_key,
1516
const uint8_t *root_hash,
1617
uint8_t *output_public_key);

crypto/zkp_context.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ int secp256k1_context_writable_randomize(secp256k1_context *context_writable) {
5151
bool zkp_context_is_initialized(void) { return context != NULL; }
5252

5353
// returns 0 on success
54-
int zkp_context_init() {
54+
int zkp_context_init(void) {
5555
assert(context == NULL);
5656

5757
const unsigned int context_flags =
@@ -78,22 +78,23 @@ int zkp_context_init() {
7878
return 0;
7979
}
8080

81-
void zkp_context_destroy() {
81+
void zkp_context_destroy(void) {
8282
assert(context != NULL);
8383

8484
secp256k1_context_preallocated_destroy(context);
8585
memzero(context_buffer, sizeof(context_buffer));
8686
atomic_flag_clear(&locked);
87+
context = NULL;
8788
}
8889

89-
const secp256k1_context *zkp_context_get_read_only() {
90+
const secp256k1_context *zkp_context_get_read_only(void) {
9091
assert(context != NULL);
9192

9293
return context;
9394
}
9495

9596
// returns NULL if context cannot be acquired
96-
secp256k1_context *zkp_context_acquire_writable() {
97+
secp256k1_context *zkp_context_acquire_writable(void) {
9798
assert(context != NULL);
9899

99100
// We don't expect the context to be used by multiple threads
@@ -104,7 +105,7 @@ secp256k1_context *zkp_context_acquire_writable() {
104105
return context;
105106
}
106107

107-
void zkp_context_release_writable() {
108+
void zkp_context_release_writable(void) {
108109
assert(context != NULL);
109110

110111
atomic_flag_clear(&locked);

crypto/zkp_context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "vendor/secp256k1-zkp/include/secp256k1_preallocated.h"
77

88
int secp256k1_context_writable_randomize(secp256k1_context *context);
9+
bool zkp_context_is_initialized(void);
910
int zkp_context_init(void);
1011
void zkp_context_destroy(void);
1112
const secp256k1_context *zkp_context_get_read_only(void);

legacy/Makefile.include

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,15 @@ $(NAME).elf: $(OBJS) $(LDSCRIPT) $(LIBDEPS)
232232
@printf " LD $@\n"
233233
$(Q)$(LD) -o $(NAME).elf $(OBJS) $(LDLIBS) $(LDFLAGS)
234234

235-
$(ZKP_PATH)/src/ecmult_static_context.h: $(ZKP_PATH)/src/gen_context.c
236-
@printf " GEN $@\n"
237-
$(Q)$(CC_FOR_BUILD) $(ZKP_CFLAGS) $(ZKP_PATH)/src/gen_context.c -o $(ZKP_PATH)/gen_context
238-
$(Q)cd $(ZKP_PATH) && ./gen_context
235+
precomputed_ecmult.o:
236+
@printf " CC $@\n"
237+
$(Q)$(CC) $(CFLAGS) -Wno-unused-function $(ZKP_CFLAGS) -c $(ZKP_PATH)/src/precomputed_ecmult.c -o precomputed_ecmult.o
238+
239+
precomputed_ecmult_gen.o:
240+
@printf " CC $@\n"
241+
$(Q)$(CC) $(CFLAGS) -Wno-unused-function $(ZKP_CFLAGS) -c $(ZKP_PATH)/src/precomputed_ecmult_gen.c -o precomputed_ecmult_gen.o
239242

240-
secp256k1-zkp.o: $(ZKP_PATH)/src/ecmult_static_context.h
243+
secp256k1-zkp.o:
241244
@printf " CC $@\n"
242245
$(Q)$(CC) $(CFLAGS) -Wno-unused-function $(ZKP_CFLAGS) -I$(ZKP_PATH) -I$(ZKP_PATH)/src -c $(ZKP_PATH)/src/secp256k1.c -o secp256k1-zkp.o
243246

legacy/common.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ uint8_t HW_ENTROPY_DATA[HW_ENTROPY_LEN];
3232

3333
uint8_t ui_language = 0;
3434

35+
uint8_t cpu_mode = 0;
36+
3537
bool g_bSelectSEFlag = false;
3638
bool g_bIsBixinAPP = false;
3739
uint32_t g_uiFastPayFlag = 0;

legacy/common.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ extern uint8_t HW_ENTROPY_DATA[HW_ENTROPY_LEN];
3232
#define WORK_MODE_USB 0x20
3333
#define WORK_MODE_NFC 0x30
3434

35+
typedef enum {
36+
PRIVILEGED = 0,
37+
UNPRIVILEGED,
38+
} CPU_MODE;
39+
40+
extern uint8_t cpu_mode;
41+
3542
extern uint8_t ui_language;
3643

3744
extern bool g_bSelectSEFlag;

legacy/firmware/Makefile

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,33 @@ else
99
NAME=classic.$(FIRMWARE_BUILD_VERSION)-Alpha-$(FIRMWARE_BUILD_DATE)-$(BUILD_ID)
1010
endif
1111

12-
SECP256K1_ZKP ?= 0
12+
SECP256K1_ZKP ?= 1
1313

1414

1515
ifeq ($(SECP256K1_ZKP),1)
16+
CFLAGS += -DSECP256K1_ZKP
1617
CFLAGS += -DUSE_SECP256K1_ZKP
1718
CFLAGS += -DUSE_SECP256K1_ZKP_ECDSA
1819
ifeq ($(EMULATOR),1)
1920
CFLAGS += -DSECP256K1_CONTEXT_SIZE=208
2021
else
21-
CFLAGS += -DSECP256K1_CONTEXT_SIZE=184
22+
CFLAGS += -DSECP256K1_CONTEXT_SIZE=180
2223
OBJS += field_10x26_arm.o
2324
endif
2425
ZKP_CFLAGS = \
2526
-DUSE_ASM_ARM \
26-
-DUSE_NUM_NONE \
27-
-DUSE_FIELD_INV_BUILTIN \
28-
-DUSE_SCALAR_INV_BUILTIN \
2927
-DUSE_EXTERNAL_ASM \
30-
-DUSE_FIELD_10X26 \
31-
-DUSE_SCALAR_8X32 \
32-
-DUSE_ECMULT_STATIC_PRECOMPUTATION \
3328
-DUSE_EXTERNAL_DEFAULT_CALLBACKS \
34-
-DECMULT_GEN_PREC_BITS=4 \
29+
-DECMULT_GEN_PREC_BITS=2 \
3530
-DECMULT_WINDOW_SIZE=8 \
3631
-DENABLE_MODULE_GENERATOR \
3732
-DENABLE_MODULE_RECOVERY \
3833
-DENABLE_MODULE_SCHNORRSIG \
3934
-DENABLE_MODULE_EXTRAKEYS
4035

4136
OBJS += secp256k1-zkp.o
37+
OBJS += precomputed_ecmult.o
38+
OBJS += precomputed_ecmult_gen.o
4239
OBJS += ../vendor/trezor-crypto/zkp_bip340.o
4340
OBJS += ../vendor/trezor-crypto/zkp_context.o
4441
OBJS += ../vendor/trezor-crypto/zkp_ecdsa.o
@@ -236,7 +233,7 @@ DEBUG_LOG ?= 0
236233
ifeq ($(EMULATOR),1)
237234
CFLAGS += --warn-no-unused-parameter
238235
endif
239-
CFLAGS += -fstack-protector-all
236+
CFLAGS += -fstack-protector-strong
240237
CFLAGS += -Wno-sequence-point
241238
CFLAGS += -I../vendor/nanopb -Iprotob -DPB_FIELD_16BIT=1 -DPB_ENCODE_ARRAYS_UNPACKED=1 -DPB_VALIDATE_UTF8=1
242239
CFLAGS += -DDEBUG_LINK=$(DEBUG_LINK)

legacy/firmware/config.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,12 @@ void config_init(void) {
488488
g_bSelectSEFlag = false;
489489
}
490490

491+
uint32_t fails = config_getPinFails();
492+
493+
if (fails >= 10) {
494+
config_wipe();
495+
}
496+
491497
// Auto-unlock storage if no PIN is set.
492498
if (storage_is_unlocked() == secfalse && storage_has_pin() == secfalse) {
493499
storage_unlock(PIN_EMPTY, PIN_EMPTY_LEN, NULL);

0 commit comments

Comments
 (0)