From 57758adc059a8152e4e3e36d0f50ca3480f7e77d Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Tue, 30 Jul 2024 15:05:47 +0200 Subject: [PATCH] cx_crc_hw syscall support --- sdk/bolos_syscalls_unified_sdk.h | 3 ++- src/bolos/cx_crc.c | 36 ++++++++++++++++++++++++-------- src/bolos/cx_crc.h | 9 ++++---- src/bolos/cxlib.c | 8 ------- src/bolos/cxlib.h | 1 - src/emulate_unified_sdk.c | 6 ++++++ 6 files changed, 40 insertions(+), 23 deletions(-) diff --git a/sdk/bolos_syscalls_unified_sdk.h b/sdk/bolos_syscalls_unified_sdk.h index 75201203..f5247b19 100644 --- a/sdk/bolos_syscalls_unified_sdk.h +++ b/sdk/bolos_syscalls_unified_sdk.h @@ -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 diff --git a/src/bolos/cx_crc.c b/src/bolos/cx_crc.c index 337ab5cc..b51e56f4 100644 --- a/src/bolos/cx_crc.c +++ b/src/bolos/cx_crc.c @@ -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, @@ -36,13 +36,11 @@ 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; @@ -50,7 +48,7 @@ unsigned short sys_cx_crc16_update(unsigned short crc, const void *buf, #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, @@ -96,14 +94,34 @@ 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; +} diff --git a/src/bolos/cx_crc.h b/src/bolos/cx_crc.h index e36931de..285995f6 100644 --- a/src/bolos/cx_crc.h +++ b/src/bolos/cx_crc.h @@ -2,7 +2,8 @@ #include -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); diff --git a/src/bolos/cxlib.c b/src/bolos/cxlib.c index c801b1d2..33a54130 100644 --- a/src/bolos/cxlib.c +++ b/src/bolos/cxlib.c @@ -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; -} diff --git a/src/bolos/cxlib.h b/src/bolos/cxlib.h index 747d2272..e0d56ef3 100644 --- a/src/bolos/cxlib.h +++ b/src/bolos/cxlib.h @@ -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); diff --git a/src/emulate_unified_sdk.c b/src/emulate_unified_sdk.c index 9ac42faa..7000e49e 100644 --- a/src/emulate_unified_sdk.c +++ b/src/emulate_unified_sdk.c @@ -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);