From da04fbb8242724594fff557ad5ba18ba11e60103 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Thu, 30 Mar 2023 23:44:37 +0200 Subject: [PATCH] Add crc check. Signed-off-by: Pol Henarejos --- pico-hsm-sdk | 2 +- src/fido/otp.c | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pico-hsm-sdk b/pico-hsm-sdk index 3947b59..38fb801 160000 --- a/pico-hsm-sdk +++ b/pico-hsm-sdk @@ -1 +1 @@ -Subproject commit 3947b59881d42ef75257908001f538e3becdd596 +Subproject commit 38fb8018e68011af14c1320f89ad27cfc89d62ab diff --git a/src/fido/otp.c b/src/fido/otp.c index c3033ed..2cc2f80 100644 --- a/src/fido/otp.c +++ b/src/fido/otp.c @@ -208,7 +208,13 @@ int otp_unload() { return CCID_OK; } +static bool scanned = false; +extern void scan_all(); uint16_t otp_status() { + if (scanned == false) { + scan_all(); + scanned = true; + } res_APDU_size = 0; res_APDU[1] = PICO_FIDO_VERSION_MAJOR; res_APDU[2] = PICO_FIDO_VERSION_MINOR; @@ -223,21 +229,22 @@ uint16_t otp_status() { return SW_OK(); } +bool check_crc(const otp_config_t *data) { + uint16_t crc = calculate_crc((const uint8_t *)data, otp_config_size); + return crc == 0xF0B8; +} + int cmd_otp() { uint8_t p1 = P1(apdu), p2 = P2(apdu); if (p2 != 0x00) { return SW_INCORRECT_P1P2(); } if (p1 == 0x01 || p1 == 0x03) { // Configure slot - /*if (apdu.nc != otp_config_size + ACC_CODE_SIZE) { - return SW_WRONG_LENGTH(); - }*/ otp_config_t *odata = (otp_config_t *)apdu.data; - if (odata->rfu[0] != 0 || odata->rfu[1] != 0) { + if (odata->rfu[0] != 0 || odata->rfu[1] != 0 || check_crc(odata) == false) { return SW_WRONG_DATA(); } file_t *ef = file_new(p1 == 0x01 ? EF_OTP_SLOT1 : EF_OTP_SLOT2); - printf("has data %d\n",file_has_data(ef)); if (file_has_data(ef)) { otp_config_t *otpc = (otp_config_t *) file_get_data(ef); if (memcmp(otpc->acc_code, apdu.data + otp_config_size, ACC_CODE_SIZE) != 0) {