From 8e68e6cae997b92c35c8ac1f30cbcf32d500401b Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 23 Dec 2024 21:00:38 +0100 Subject: [PATCH] Use BE/LE functions for packing uint16. Signed-off-by: Pol Henarejos --- src/asn1.c | 3 +-- src/eac.c | 7 +++---- src/fs/file.c | 16 ++++++++-------- src/fs/phy.c | 4 ++-- src/usb/hid/hid.c | 3 +-- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/asn1.c b/src/asn1.c index ad52ee4..6f8ea61 100644 --- a/src/asn1.c +++ b/src/asn1.c @@ -73,8 +73,7 @@ uint8_t format_tlv_len(uint16_t len, uint8_t *out) { } if (out) { *out++ = 0x82; - *out++ = (len >> 8) & 0xff; - *out++ = len & 0xff; + put_uint16_t_be(len, out); } return 3; } diff --git a/src/eac.c b/src/eac.c index 824eb96..4630e89 100644 --- a/src/eac.c +++ b/src/eac.c @@ -181,16 +181,15 @@ int sm_wrap() { else { memmove(res_APDU + 4, res_APDU, res_APDU_size); res_APDU[1] = 0x82; - res_APDU[2] = (uint8_t)(res_APDU_size >> 8); - res_APDU[3] = (uint8_t)(res_APDU_size & 0xff); + put_uint16_t_be(res_APDU_size, res_APDU + 2); res_APDU_size += 4; } res_APDU[0] = 0x87; } res_APDU[res_APDU_size++] = 0x99; res_APDU[res_APDU_size++] = 2; - res_APDU[res_APDU_size++] = apdu.sw >> 8; - res_APDU[res_APDU_size++] = apdu.sw & 0xff; + put_uint16_t_be(apdu.sw, res_APDU + res_APDU_size); + res_APDU_size += 2; memcpy(input + input_len, res_APDU, res_APDU_size); input_len += res_APDU_size; input[input_len++] = 0x80; diff --git a/src/fs/file.c b/src/fs/file.c index 27e362d..2643147 100644 --- a/src/fs/file.c +++ b/src/fs/file.c @@ -56,13 +56,13 @@ void process_fci(const file_t *pe, int fmd) { if (pe->data) { if ((pe->type & FILE_DATA_FUNC) == FILE_DATA_FUNC) { uint16_t len = (uint16_t)((int (*)(const file_t *, int))(pe->data))(pe, 0); - res_APDU[res_APDU_size++] = (len >> 8) & 0xff; - res_APDU[res_APDU_size++] = len & 0xff; + put_uint16_t_be(len, res_APDU + res_APDU_size); + res_APDU_size += 2; } else { uint16_t v = file_get_size(pe); - res_APDU[res_APDU_size++] = v >> 8; - res_APDU[res_APDU_size++] = v & 0xff; + put_uint16_t_be(v, res_APDU + res_APDU_size); + res_APDU_size += 2; } } else { @@ -493,8 +493,8 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) { uint8_t *f = fdata + meta_offset; *f++ = fid & 0xff; f += format_tlv_len(len + 2, f); - *f++ = fid >> 8; - *f++ = fid & 0xff; + put_uint16_t_be(fid, f); + f += 2; memcpy(f, data, len); r = file_put_data(ef, fdata, ef_size); free(fdata); @@ -509,8 +509,8 @@ int meta_add(uint16_t fid, const uint8_t *data, uint16_t len) { uint8_t *f = fdata + ef_size; *f++ = fid & 0x1f; f += format_tlv_len(len + 2, f); - *f++ = fid >> 8; - *f++ = fid & 0xff; + put_uint16_t_be(fid, f); + f += 2; memcpy(f, data, len); r = file_put_data(ef, fdata, ef_size + (uint16_t)asn1_len_tag(fid & 0x1f, len + 2)); free(fdata); diff --git a/src/fs/phy.c b/src/fs/phy.c index 173a72a..2bcb272 100644 --- a/src/fs/phy.c +++ b/src/fs/phy.c @@ -43,8 +43,8 @@ int phy_serialize_data(const phy_data_t *phy, uint8_t *data, uint16_t *len) { *p++ = phy->led_brightness; } *p++ = PHY_OPTS; - *p++ = phy->opts >> 8; - *p++ = phy->opts & 0xff; + put_uint16_t_be(phy->opts, p); + p += 2; if (phy->up_btn_present) { *p++ = PHY_UP_BTN; *p++ = phy->up_btn; diff --git a/src/usb/hid/hid.c b/src/usb/hid/hid.c index e877fad..e3e88f2 100644 --- a/src/usb/hid/hid.c +++ b/src/usb/hid/hid.c @@ -558,8 +558,7 @@ void driver_exec_finished_hid(uint16_t size_next) { else { if (is_nitrokey) { memmove(apdu.rdata + 2, apdu.rdata, size_next - 2); - apdu.rdata[0] = apdu.sw >> 8; - apdu.rdata[1] = apdu.sw & 0xff; + put_uint16_t_be(apdu.sw, apdu.rdata); } driver_exec_finished_cont_hid(ITF_HID_CTAP, size_next, 7); }