Skip to content

Commit 9d8505b

Browse files
committed
Use cx_crc32_hw()
Ledger have fixed their buggy implementation of CRC32 so we can start using it again
1 parent 156cf78 commit 9d8505b

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
### Fixed
1313
- Fixed failing Ledger rule enforcer check
14+
- Use `cx_crc32_hw()`
15+
- Ledger have fixed their buggy implementation of so we can start using it again
1416

1517
## [1.6.0] - 2024-01-14
1618
### Added

src/ux_common/onboarding_seed_sskr.c

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,13 @@
1010

1111
// Return the CRC-32 checksum of the input buffer in network byte order (big endian).
1212
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
13-
#define crc32_nbo(...) crc32(__VA_ARGS__)
13+
#define crc32_nbo(...) cx_crc32_hw(__VA_ARGS__)
1414
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
15-
#define crc32_nbo(...) os_swap_u32(crc32(__VA_ARGS__))
15+
#define crc32_nbo(...) os_swap_u32(cx_crc32_hw(__VA_ARGS__))
1616
#else
1717
#error "What kind of system is this?"
1818
#endif
1919

20-
// NOTE:
21-
// The implementation of cx_crc32_hw() on Ledger devices is buggy and produces incorrect CRC32
22-
// checks. Ledger are fixing cx_crc32_hw() on each device either through SDK or OS updates.
23-
// The following function is a temporary workaround that can be removed once cx_crc32_hw()
24-
// works on all Ledger devices
25-
26-
uint32_t crc32(const uint8_t *data, size_t len) {
27-
uint32_t crc = ~0;
28-
const uint8_t *end = data + len;
29-
30-
while (data < end) {
31-
crc ^= *data++;
32-
for (uint8_t i = 0; i < 8; i++) {
33-
uint32_t mask = ~((crc & 1) - 1);
34-
crc = (crc >> 1) ^ (0xEDB88320 & mask);
35-
}
36-
}
37-
return ~crc;
38-
}
39-
4020
unsigned int bolos_ux_sskr_size_get(uint8_t bip39_onboarding_kind,
4121
uint8_t groups_threshold,
4222
unsigned int *group_descriptor,
@@ -280,7 +260,7 @@ unsigned int bolos_ux_sskr_hex_check(unsigned char *mnemonic_hex,
280260

281261
for (unsigned int i = 0; i < sskr_shares_count; i++) {
282262
checksum = crc32_nbo(mnemonic_hex + i * (mnemonic_length / sskr_shares_count),
283-
(mnemonic_length / sskr_shares_count) - checksum_len);
263+
(mnemonic_length / sskr_shares_count) - checksum_len);
284264
// First 8 bytes of all shares in group should be same
285265
// Test checksum
286266
if ((os_secure_memcmp(cbor, mnemonic_hex + i * mnemonic_length / sskr_shares_count, 3) !=

tests/unit/lib/testutils.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,17 @@ char os_secure_memcmp(const void *src1, const void *src2, size_t length)
2424
}
2525
return xoracc;
2626
}
27+
28+
uint32_t cx_crc32_hw(const uint8_t *data, size_t len) {
29+
uint32_t crc = ~0;
30+
const uint8_t *end = data + len;
31+
32+
while (data < end) {
33+
crc ^= *data++;
34+
for (uint8_t i = 0; i < 8; i++) {
35+
uint32_t mask = ~((crc & 1) - 1);
36+
crc = (crc >> 1) ^ (0xEDB88320 & mask);
37+
}
38+
}
39+
return ~crc;
40+
}

0 commit comments

Comments
 (0)