Skip to content

Commit

Permalink
cx_crc_hw syscall support
Browse files Browse the repository at this point in the history
  • Loading branch information
apaillier-ledger committed Jul 30, 2024
1 parent 8f8d569 commit 244b5a3
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 23 deletions.
3 changes: 2 additions & 1 deletion sdk/bolos_syscalls_unified_sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@
#define SYSCALL_cx_ecpoint_is_at_infinity_ID_IN 0x0200014b
#define SYSCALL_cx_ecpoint_x25519_ID_IN 0x0300001b
#define SYSCALL_cx_ecpoint_x448_ID_IN 0x03000060
#define SYSCALL_cx_crc32_hw_ID_IN 0x02000102
#define SYSCALL_cx_crc32_hw_ID_IN 0x02000102 // API levels < 18
#define SYSCALL_cx_crc_hw_ID_IN 0x04000102 // API levels >= 18
#define SYSCALL_cx_get_random_bytes_ID_IN 0x02000107
#define SYSCALL_cx_trng_get_random_data_ID_IN 0x02000106
#define SYSCALL_os_perso_erase_all_ID_IN 0x0000004b
Expand Down
38 changes: 29 additions & 9 deletions src/bolos/cx_crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#define cx_crc16_update sys_cx_crc16_update

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

unsigned short sys_cx_crc16_update(unsigned short crc, const void *buf,
size_t len)
uint16_t sys_cx_crc16_update(uint16_t crc, const void *buf, size_t len)
{
const uint8_t *p = buf;
size_t i;

for (i = 0; i < len; i++) {
for (size_t i = 0; i < len; i++) {
crc = cx_ccitt16[*p++ ^ (uint16_t)(crc >> 8u)] ^ (uint16_t)(crc << 8u);
}
return crc;
}

#define cx_crc32_update sys_cx_crc32_update

static const unsigned int cx_ccitt32[] = {
static const uint32_t cx_ccitt32[] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
Expand Down Expand Up @@ -96,14 +94,36 @@ static const unsigned int cx_ccitt32[] = {
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};

unsigned int sys_cx_crc32_update(unsigned int crc, const void *buf, size_t len)
uint32_t sys_cx_crc32_update(uint32_t crc, const void *buf, size_t len)
{
const uint8_t *p = buf;
size_t i;

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

return crc ^ 0xFFFFFFFF;
}

uint32_t sys_cx_crc32_hw(const void *buf, size_t len)
{
uint32_t crc;
crc = sys_cx_crc32_update(0xFFFFFFFF, buf, len);

return crc;
}

uint32_t sys_cx_crc_hw(uint8_t crc_type, uint32_t crc_state, const void *buf, size_t len)
{
uint32_t crc;

if (crc_type == 1)
{
crc = sys_cx_crc32_update(crc_state, buf, len);
}
else
{
crc = sys_cx_crc16_update(crc_state, buf, len);
}
return crc;
}
8 changes: 4 additions & 4 deletions src/bolos/cx_crc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <stddef.h>

unsigned short sys_cx_crc16_update(unsigned short crc, const void *buf,
size_t len);

unsigned int sys_cx_crc32_update(unsigned int crc, const void *buf, size_t len);
uint16_t sys_cx_crc16_update(uint16_t crc, const void *buf, size_t len);
uint32_t sys_cx_crc32_update(uint32_t crc, const void *buf, size_t len);
uint32_t sys_cx_crc32_hw(const void *_buf, size_t len);
uint32_t sys_cx_crc_hw(uint8_t crc_type, uint32_t crc_state, const void *buf, size_t len);
8 changes: 0 additions & 8 deletions src/bolos/cxlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,3 @@ cx_err_t sys_cx_trng_get_random_data(void *buffer, size_t len)

return CX_OK;
}

uint32_t sys_cx_crc32_hw(const void *buf, size_t len)
{
uint32_t crc;
crc = sys_cx_crc32_update(0xFFFFFFFF, buf, len);

return crc;
}
1 change: 0 additions & 1 deletion src/bolos/cxlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ cx_err_t sys_cx_ecdomain_generator_bn(cx_curve_t cv, cx_ecpoint_t *P);
unsigned int sys_get_api_level(void);
cx_err_t sys_cx_get_random_bytes(void *buffer, size_t len);
cx_err_t sys_cx_trng_get_random_data(void *buffer, size_t len);
uint32_t sys_cx_crc32_hw(const void *_buf, size_t len);

// cx_aes_sdk2.c
cx_err_t sys_cx_aes_set_key_hw(const cx_aes_key_t *key, uint32_t mode);
Expand Down
6 changes: 6 additions & 0 deletions src/emulate_unified_sdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ int emulate_syscall_cx(unsigned long syscall, unsigned long *parameters,
uint8_t *, buffer,
size_t, len);

SYSCALL4(cx_crc_hw, "(0x%x, %u, %p, %u)",
uint8_t, crc_type,
uint32_t, crc_state,
const void *, buf,
size_t, len);

SYSCALL2(cx_aes_set_key_hw, "(%p %u)",
void *, key,
uint32_t, mode);
Expand Down

0 comments on commit 244b5a3

Please sign in to comment.