Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cx_crc_hw syscall support #510

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
57 changes: 47 additions & 10 deletions src/bolos/cx_crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

#include "cx.h"

#define CX_CRC32_INIT 0xFFFFFFFF

#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 +38,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 +96,51 @@ 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;
return crc ^ CX_CRC32_INIT;
}

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

return crc;
}

static uint32_t reverse_32_bits(uint32_t value)
{
uint32_t reverse_val = 0;

for (uint8_t i = 0; i < 32; i++) {
if ((value & (1 << i))) {
reverse_val |= 1 << ((32 - 1) - i);
}
}
return reverse_val;
}

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

switch (crc_type) {
case CRC_TYPE_CRC16_CCITT_FALSE:
crc = sys_cx_crc16_update(crc_state, buf, len);
break;
case CRC_TYPE_CRC32:
crc = sys_cx_crc32_update(reverse_32_bits(crc_state), buf, len);
break;
default:
break;
}
return crc;
}
12 changes: 9 additions & 3 deletions src/bolos/cx_crc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

#include <stddef.h>

unsigned short sys_cx_crc16_update(unsigned short crc, const void *buf,
size_t len);
typedef enum {
CRC_TYPE_CRC16_CCITT_FALSE = 0,
CRC_TYPE_CRC32 = 1,
} crc_type_t;

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(crc_type_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)",
crc_type_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
Loading