Skip to content

Commit 41e1617

Browse files
cx_crc_hw syscall support
1 parent 8f8d569 commit 41e1617

File tree

6 files changed

+64
-23
lines changed

6 files changed

+64
-23
lines changed

sdk/bolos_syscalls_unified_sdk.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@
9292
#define SYSCALL_cx_ecpoint_is_at_infinity_ID_IN 0x0200014b
9393
#define SYSCALL_cx_ecpoint_x25519_ID_IN 0x0300001b
9494
#define SYSCALL_cx_ecpoint_x448_ID_IN 0x03000060
95-
#define SYSCALL_cx_crc32_hw_ID_IN 0x02000102
95+
#define SYSCALL_cx_crc32_hw_ID_IN 0x02000102 // API levels < 18
96+
#define SYSCALL_cx_crc_hw_ID_IN 0x04000102 // API levels >= 18
9697
#define SYSCALL_cx_get_random_bytes_ID_IN 0x02000107
9798
#define SYSCALL_cx_trng_get_random_data_ID_IN 0x02000106
9899
#define SYSCALL_os_perso_erase_all_ID_IN 0x0000004b

src/bolos/cx_crc.c

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
#include "cx.h"
44

5+
#define CX_CRC32_INIT 0xFFFFFFFF
6+
57
#define cx_crc16_update sys_cx_crc16_update
68

7-
static const unsigned short cx_ccitt16[] = {
9+
static const uint16_t cx_ccitt16[] = {
810
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, 0x8108,
911
0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, 0x1231, 0x0210,
1012
0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, 0x9339, 0x8318, 0xb37b,
@@ -36,21 +38,19 @@ static const unsigned short cx_ccitt16[] = {
3638
0x2e93, 0x3eb2, 0x0ed1, 0x1ef0,
3739
};
3840

39-
unsigned short sys_cx_crc16_update(unsigned short crc, const void *buf,
40-
size_t len)
41+
uint16_t sys_cx_crc16_update(uint16_t crc, const void *buf, size_t len)
4142
{
4243
const uint8_t *p = buf;
43-
size_t i;
4444

45-
for (i = 0; i < len; i++) {
45+
for (size_t i = 0; i < len; i++) {
4646
crc = cx_ccitt16[*p++ ^ (uint16_t)(crc >> 8u)] ^ (uint16_t)(crc << 8u);
4747
}
4848
return crc;
4949
}
5050

5151
#define cx_crc32_update sys_cx_crc32_update
5252

53-
static const unsigned int cx_ccitt32[] = {
53+
static const uint32_t cx_ccitt32[] = {
5454
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
5555
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
5656
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
@@ -96,14 +96,51 @@ static const unsigned int cx_ccitt32[] = {
9696
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
9797
};
9898

99-
unsigned int sys_cx_crc32_update(unsigned int crc, const void *buf, size_t len)
99+
uint32_t sys_cx_crc32_update(uint32_t crc, const void *buf, size_t len)
100100
{
101101
const uint8_t *p = buf;
102-
size_t i;
103102

104-
for (i = 0; i < len; i++) {
103+
for (size_t i = 0; i < len; i++) {
105104
crc = cx_ccitt32[(crc ^ *p++) & 0xff] ^ (crc >> 8u);
106105
}
107106

108-
return crc ^ 0xFFFFFFFF;
107+
return crc ^ CX_CRC32_INIT;
108+
}
109+
110+
uint32_t sys_cx_crc32_hw(const void *buf, size_t len)
111+
{
112+
uint32_t crc;
113+
crc = sys_cx_crc32_update(CX_CRC32_INIT, buf, len);
114+
115+
return crc;
116+
}
117+
118+
static uint32_t reverse_32_bits(uint32_t value)
119+
{
120+
uint32_t reverse_val = 0;
121+
122+
for (uint8_t i = 0; i < 32; i++) {
123+
if ((value & (1 << i))) {
124+
reverse_val |= 1 << ((32 - 1) - i);
125+
}
126+
}
127+
return reverse_val;
128+
}
129+
130+
uint32_t sys_cx_crc_hw(crc_type_t crc_type, uint32_t crc_state, const void *buf,
131+
size_t len)
132+
{
133+
uint32_t crc = 0;
134+
135+
switch (crc_type) {
136+
case CRC_TYPE_CRC16_CCITT_FALSE:
137+
crc = sys_cx_crc16_update(crc_state, buf, len);
138+
break;
139+
case CRC_TYPE_CRC32:
140+
crc = sys_cx_crc32_update(reverse_32_bits(crc_state), buf, len);
141+
break;
142+
default:
143+
break;
144+
}
145+
return crc;
109146
}

src/bolos/cx_crc.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
#include <stddef.h>
44

5-
unsigned short sys_cx_crc16_update(unsigned short crc, const void *buf,
6-
size_t len);
5+
typedef enum {
6+
CRC_TYPE_CRC16_CCITT_FALSE = 0,
7+
CRC_TYPE_CRC32 = 1,
8+
} crc_type_t;
79

8-
unsigned int sys_cx_crc32_update(unsigned int crc, const void *buf, size_t len);
10+
uint16_t sys_cx_crc16_update(uint16_t crc, const void *buf, size_t len);
11+
uint32_t sys_cx_crc32_update(uint32_t crc, const void *buf, size_t len);
12+
uint32_t sys_cx_crc32_hw(const void *_buf, size_t len);
13+
uint32_t sys_cx_crc_hw(crc_type_t crc_type, uint32_t crc_state, const void *buf,
14+
size_t len);

src/bolos/cxlib.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,3 @@ cx_err_t sys_cx_trng_get_random_data(void *buffer, size_t len)
2525

2626
return CX_OK;
2727
}
28-
29-
uint32_t sys_cx_crc32_hw(const void *buf, size_t len)
30-
{
31-
uint32_t crc;
32-
crc = sys_cx_crc32_update(0xFFFFFFFF, buf, len);
33-
34-
return crc;
35-
}

src/bolos/cxlib.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ cx_err_t sys_cx_ecdomain_generator_bn(cx_curve_t cv, cx_ecpoint_t *P);
221221
unsigned int sys_get_api_level(void);
222222
cx_err_t sys_cx_get_random_bytes(void *buffer, size_t len);
223223
cx_err_t sys_cx_trng_get_random_data(void *buffer, size_t len);
224-
uint32_t sys_cx_crc32_hw(const void *_buf, size_t len);
225224

226225
// cx_aes_sdk2.c
227226
cx_err_t sys_cx_aes_set_key_hw(const cx_aes_key_t *key, uint32_t mode);

src/emulate_unified_sdk.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ int emulate_syscall_cx(unsigned long syscall, unsigned long *parameters,
196196
uint8_t *, buffer,
197197
size_t, len);
198198

199+
SYSCALL4(cx_crc_hw, "(0x%x, %u, %p, %u)",
200+
crc_type_t, crc_type,
201+
uint32_t, crc_state,
202+
const void *, buf,
203+
size_t, len);
204+
199205
SYSCALL2(cx_aes_set_key_hw, "(%p %u)",
200206
void *, key,
201207
uint32_t, mode);

0 commit comments

Comments
 (0)