Skip to content

Commit cb70ac0

Browse files
committed
Fix issue with using 'cx_crc32_hw()' function
1 parent 8f2e598 commit cb70ac0

File tree

3 files changed

+14
-30
lines changed

3 files changed

+14
-30
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Change log
22

3-
## [1.1.1] - 2023-04-05
3+
## [1.1.1] - 2023-04-06
44
### Added
55
-
66

77
### Changed
88
-
99

1010
### Fixed
11+
- Fix issue with using 'cx_crc32_hw()' function in 'onboarding_seed_sskr.c' when testing with Speculos
1112
- Some CodeQL suggested tidy ups
1213

1314
## [1.1.0] - 2023-04-04

TODO.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99

1010
### In Progress
1111

12-
- [ ] Fix issue with using 'cx_crc32_hw()' function in 'onboarding_seed_sskr.c' when testing with Speculos
12+
- [ ] Currently hardcoded to use a k-of-n threshold of 2-of-3. Add flow to set threshold values
1313

1414
### Done ✓
1515

16+
- [x] Fix issue with using 'cx_crc32_hw()' function in 'onboarding_seed_sskr.c' when testing with Speculos
1617
- [x] Generate BIP39 mnemonic phrases from SSKR shares
1718
- [x] Add SSKR Check menu option
1819
- [x] Add flow to the Check SSKR menu

src/ux_common/onboarding_seed_sskr.c

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,12 @@
88
#include "common_bip39.h"
99
#include "bc-sskr/bc-sskr.h"
1010

11-
uint32_t crc32(const uint8_t *data, size_t len) {
12-
uint32_t crc = ~0;
13-
const uint8_t *end = data + len;
14-
15-
while (data < end) {
16-
crc ^= *data++;
17-
for (uint8_t i = 0; i < 8; i++) {
18-
uint32_t mask = ~((crc & 1) - 1);
19-
crc = (crc >> 1) ^ (0xEDB88320 & mask);
20-
}
21-
}
22-
return ~crc;
23-
}
24-
2511
// Returns the CRC-32 checksum of the input buffer in network byte order (big endian).
26-
uint32_t crc32_nbo(const uint8_t *bytes, size_t len) {
12+
uint32_t cx_crc32_hw_nbo(const uint8_t *bytes, size_t len) {
2713
#if BYTE_ORDER == BIG_ENDIAN
28-
return crc32(bytes, len);
14+
return cx_crc32_hw(bytes, len);
2915
#elif BYTE_ORDER == LITTLE_ENDIAN
30-
return os_swap_u32(crc32(bytes, len));
16+
return os_swap_u32(cx_crc32_hw(bytes, len));
3117
#else
3218
#error "What kind of system is this?"
3319
#endif
@@ -39,7 +25,7 @@ unsigned int bolos_ux_sskr_size_get(unsigned int bip39_onboarding_kind,
3925
unsigned int groups_len,
4026
unsigned int *share_len) {
4127
sskr_group_descriptor groups[groups_len];
42-
for (uint8_t i = 0; i < (uint8_t)groups_len; i++) {
28+
for (uint8_t i = 0; i < (uint8_t) groups_len; i++) {
4329
groups[i].threshold = *(group_descriptor + i * sizeof(*(group_descriptor)) / groups_len);
4430
groups[i].count = *(group_descriptor + 1 + i * sizeof(*(group_descriptor)) / groups_len);
4531
}
@@ -60,7 +46,7 @@ unsigned int bolos_ux_sskr_hex_decode(unsigned char *mnemonic_hex,
6046
sskr_share_len = mnemonic_hex[4];
6147
}
6248

63-
for (uint8_t i = 0; i < (uint8_t)sskr_shares_count; i++) {
49+
for (uint8_t i = 0; i < (uint8_t) sskr_shares_count; i++) {
6450
ptr_sskr_shares[i] =
6551
mnemonic_hex + (i * mnemonic_length / sskr_shares_count) + 4 + (sskr_share_len > 23);
6652
}
@@ -107,7 +93,7 @@ unsigned int bolos_ux_sskr_generate(unsigned int groups_threshold,
10793
unsigned int share_len_expected,
10894
unsigned int share_count_expected) {
10995
sskr_group_descriptor groups[groups_len];
110-
for (uint8_t i = 0; i < (uint8_t)groups_len; i++) {
96+
for (uint8_t i = 0; i < (uint8_t) groups_len; i++) {
11197
groups[i].threshold = *(group_descriptor + i * 2);
11298
groups[i].count = *(group_descriptor + 1 + i * 2);
11399
}
@@ -148,7 +134,7 @@ unsigned int bolos_ux_sskr_mnemonic_encode(unsigned char *input,
148134
unsigned int position = 0;
149135
unsigned int offset = 0;
150136

151-
for (uint8_t i = 0; i < (uint8_t)input_len; i++) {
137+
for (uint8_t i = 0; i < (uint8_t) input_len; i++) {
152138
offset = SSKR_MNEMONIC_LENGTH * input[i];
153139
if ((position + SSKR_MNEMONIC_LENGTH <= output_len) &&
154140
(offset <= SSKR_WORDLIST_LENGTH - SSKR_MNEMONIC_LENGTH)) {
@@ -236,11 +222,7 @@ unsigned int bolos_ux_bip39_to_sskr_convert(unsigned char *bip39_words_buffer,
236222
memcpy(cbor_share_crc_buffer + cbor_len,
237223
share_buffer + share_len * share,
238224
share_len);
239-
// TODO
240-
// During testing cx_crc32_hw() gave an incorrect CRC32 so disabling for
241-
// now and using own crc32() function instead checksum =
242-
// cx_crc32_hw_nbo(cbor_share_crc_buffer, cbor_len + share_len);
243-
checksum = crc32_nbo(cbor_share_crc_buffer, cbor_len + share_len);
225+
checksum = cx_crc32_hw_nbo(cbor_share_crc_buffer, cbor_len + share_len);
244226
memcpy(cbor_share_crc_buffer + cbor_len + share_len, &checksum, checksum_len);
245227

246228
if (bolos_ux_sskr_mnemonic_encode(
@@ -274,8 +256,8 @@ unsigned int bolos_ux_sskr_hex_check(unsigned char *mnemonic_hex,
274256
uint8_t checksum_len = sizeof(checksum);
275257

276258
for (unsigned int i = 0; i < sskr_shares_count; i++) {
277-
checksum = crc32_nbo(mnemonic_hex + i * (mnemonic_length / sskr_shares_count),
278-
(mnemonic_length / sskr_shares_count) - checksum_len);
259+
checksum = cx_crc32_hw_nbo(mnemonic_hex + i * (mnemonic_length / sskr_shares_count),
260+
(mnemonic_length / sskr_shares_count) - checksum_len);
279261
// First 8 bytes of all shares in group should be same
280262
// Test checksum
281263
if ((os_secure_memcmp(cbor, mnemonic_hex + i * mnemonic_length / sskr_shares_count, 3) !=

0 commit comments

Comments
 (0)