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