From 1f805b1df2401d8bf2a91e501a0c55c45cd30c06 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Mon, 23 Dec 2024 21:25:46 +0100 Subject: [PATCH] Use more uint16 funcs. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- src/fido/cbor_get_assertion.c | 2 +- src/fido/cbor_make_credential.c | 5 ++--- src/fido/cbor_vendor.c | 2 +- src/fido/fido.c | 2 +- src/fido/management.c | 2 +- src/fido/oath.c | 14 +++--------- src/fido/otp.c | 39 +++++++++++---------------------- 8 files changed, 23 insertions(+), 45 deletions(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index d530ea6..f8cb36c 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit d530ea69797a3c91063ab0411840c0be384d70d1 +Subproject commit f8cb36c2cf5de7f0e8b7cd4a497160e86de50107 diff --git a/src/fido/cbor_get_assertion.c b/src/fido/cbor_get_assertion.c index c829807..b2a28dd 100644 --- a/src/fido/cbor_get_assertion.c +++ b/src/fido/cbor_get_assertion.c @@ -519,7 +519,7 @@ int cbor_get_assertion(const uint8_t *data, size_t len, bool next) { uint8_t *pa = aut_data; memcpy(pa, rp_id_hash, 32); pa += 32; *pa++ = flags; - put_uint32_t_be(ctr, pa); pa += 4; + pa += put_uint32_t_be(ctr, pa); memcpy(pa, ext, ext_len); pa += ext_len; if ((size_t)(pa - aut_data) != aut_data_len) { CBOR_ERROR(CTAP1_ERR_OTHER); diff --git a/src/fido/cbor_make_credential.c b/src/fido/cbor_make_credential.c index 9083521..bb62795 100644 --- a/src/fido/cbor_make_credential.c +++ b/src/fido/cbor_make_credential.c @@ -409,10 +409,9 @@ int cbor_make_credential(const uint8_t *data, size_t len) { uint8_t *pa = aut_data; memcpy(pa, rp_id_hash, 32); pa += 32; *pa++ = flags; - put_uint32_t_be(ctr, pa); pa += 4; + pa += put_uint32_t_be(ctr, pa); memcpy(pa, aaguid, 16); pa += 16; - *pa++ = ((uint16_t)cred_id_len >> 8) & 0xFF; - *pa++ = (uint16_t)cred_id_len & 0xFF; + pa += put_uint16_t_be(cred_id_len, pa); memcpy(pa, cred_id, cred_id_len); pa += (uint16_t)cred_id_len; memcpy(pa, cbor_buf, rs); pa += (uint16_t)rs; memcpy(pa, ext, ext_len); pa += (uint16_t)ext_len; diff --git a/src/fido/cbor_vendor.c b/src/fido/cbor_vendor.c index 4eb5a04..e8ff439 100644 --- a/src/fido/cbor_vendor.c +++ b/src/fido/cbor_vendor.c @@ -255,7 +255,7 @@ int cbor_vendor_generic(uint8_t cmd, const uint8_t *data, size_t len) { uint16_t opts = 0; if (file_has_data(ef_phy)) { uint8_t *data = file_get_data(ef_phy); - opts = (data[PHY_OPTS] << 8) | data[PHY_OPTS+1]; + opts = get_uint16_t_be(data + PHY_OPTS); } CBOR_CHECK(cbor_encoder_create_map(&encoder, &mapEncoder, 1)); CBOR_CHECK(cbor_encode_uint(&mapEncoder, 0x01)); diff --git a/src/fido/fido.c b/src/fido/fido.c index d63fa46..6ef148f 100644 --- a/src/fido/fido.c +++ b/src/fido/fido.c @@ -411,7 +411,7 @@ bool check_user_presence() { uint32_t get_sign_counter() { uint8_t *caddr = file_get_data(ef_counter); - return (*caddr) | (*(caddr + 1) << 8) | (*(caddr + 2) << 16) | (*(caddr + 3) << 24); + return get_uint32_t_le(caddr); } uint8_t get_opts() { diff --git a/src/fido/management.c b/src/fido/management.c index b75ec3d..d833d57 100644 --- a/src/fido/management.c +++ b/src/fido/management.c @@ -65,7 +65,7 @@ bool cap_supported(uint16_t cap) { if (tag == TAG_USB_ENABLED) { uint16_t ecaps = tag_data[0]; if (tag_len == 2) { - ecaps = (tag_data[0] << 8) | tag_data[1]; + ecaps = get_uint16_t_be(tag_data); } return ecaps & cap; } diff --git a/src/fido/oath.c b/src/fido/oath.c index dac79c5..477d373 100644 --- a/src/fido/oath.c +++ b/src/fido/oath.c @@ -395,15 +395,7 @@ int cmd_calculate() { return SW_EXEC_ERROR(); } if ((key.data[0] & OATH_TYPE_MASK) == OATH_TYPE_HOTP) { - uint64_t v = - ((uint64_t) chal.data[0] << 56) | - ((uint64_t) chal.data[1] << 48) | - ((uint64_t) chal.data[2] << 40) | - ((uint64_t) chal.data[3] << 32) | - ((uint64_t) chal.data[4] << 24) | - ((uint64_t) chal.data[5] << 16) | - ((uint64_t) chal.data[6] << 8) | - (uint64_t) chal.data[7]; + uint64_t v = get_uint64_t_be(chal.data); size_t ef_size = file_get_size(ef); v++; uint8_t *tmp = (uint8_t *) calloc(1, ef_size); @@ -570,14 +562,14 @@ int cmd_verify_hotp() { return SW_INCORRECT_PARAMS(); } if (asn1_find_tag(&ctxi, TAG_RESPONSE, &code) == true) { - code_int = (code.data[0] << 24) | (code.data[1] << 16) | (code.data[2] << 8) | code.data[3]; + code_int = get_uint32_t_be(code.data); } int ret = calculate_oath(0x01, key.data, key.len, chal.data, chal.len); if (ret != PICOKEY_OK) { return SW_EXEC_ERROR(); } - uint32_t res_int = (res_APDU[2] << 24) | (res_APDU[3] << 16) | (res_APDU[4] << 8) | res_APDU[5]; + uint32_t res_int = get_uint32_t_be(res_APDU + 2); if (res_APDU[1] == 6) { res_int %= (uint32_t) 1e6; } diff --git a/src/fido/otp.c b/src/fido/otp.c index d6442fd..7337699 100644 --- a/src/fido/otp.c +++ b/src/fido/otp.c @@ -169,12 +169,11 @@ void init_otp() { otp_config_t *otp_config = (otp_config_t *) data; if (file_has_data(ef) && !(otp_config->tkt_flags & OATH_HOTP) && !(otp_config->cfg_flags & SHORT_TICKET || otp_config->cfg_flags & STATIC_TICKET)) { - uint16_t counter = (data[otp_config_size] << 8) | data[otp_config_size + 1]; + uint16_t counter = get_uint16_t_be(data + otp_config_size); if (++counter <= 0x7fff) { uint8_t new_data[otp_config_size + 8]; memcpy(new_data, data, sizeof(new_data)); - new_data[otp_config_size] = counter >> 8; - new_data[otp_config_size + 1] = counter & 0xff; + put_uint16_t_be(counter, new_data + otp_config_size); file_put_data(ef, new_data, sizeof(new_data)); } } @@ -228,16 +227,10 @@ int otp_button_pressed(uint8_t slot) { memcpy(tmp_key + 2, otp_config->aes_key, KEY_SIZE); uint64_t imf = 0; const uint8_t *p = data + otp_config_size; - imf |= (uint64_t) *p++ << 56; - imf |= (uint64_t) *p++ << 48; - imf |= (uint64_t) *p++ << 40; - imf |= (uint64_t) *p++ << 32; - imf |= *p++ << 24; - imf |= *p++ << 16; - imf |= *p++ << 8; - imf |= *p++; + imf = get_uint64_t_be(p); + p += 8; if (imf == 0) { - imf = ((otp_config->uid[4] << 8) | otp_config->uid[5]) << 4; + imf = get_uint16_t_be(otp_config->uid + 4); } uint8_t chal[8]; put_uint64_t_be(imf, chal); @@ -245,8 +238,7 @@ int otp_button_pressed(uint8_t slot) { int ret = calculate_oath(1, tmp_key, sizeof(tmp_key), chal, sizeof(chal)); if (ret == PICOKEY_OK) { uint32_t base = otp_config->cfg_flags & OATH_HOTP8 ? 1e8 : 1e6; - uint32_t number = - (res_APDU[2] << 24) | (res_APDU[3] << 16) | (res_APDU[4] << 8) | res_APDU[5]; + uint32_t number = get_uint16_t_be(res_APDU + 2); number %= base; char number_str[9]; if (otp_config->cfg_flags & OATH_HOTP8) { @@ -283,7 +275,7 @@ int otp_button_pressed(uint8_t slot) { else { uint8_t otpk[22], *po = otpk; bool update_counter = false; - uint16_t counter = (data[otp_config_size] << 8) | data[otp_config_size + 1], crc = 0; + uint16_t counter = get_uint16_t_be(data + otp_config_size), crc = 0; uint32_t ts = board_millis() / 1000; if (counter == 0) { update_counter = true; @@ -293,9 +285,8 @@ int otp_button_pressed(uint8_t slot) { po += 6; memcpy(po, otp_config->uid, UID_SIZE); po += UID_SIZE; - *po++ = counter & 0xff; - *po++ = counter >> 8; - ts >>= 3; + po += put_uint16_t_le(counter, po); + ts >>= 1; *po++ = ts & 0xff; *po++ = ts >> 8; *po++ = ts >> 16; @@ -303,8 +294,7 @@ int otp_button_pressed(uint8_t slot) { random_gen(NULL, po, 2); po += 2; crc = calculate_crc(otpk + 6, 14); - *po++ = ~crc & 0xff; - *po++ = ~crc >> 8; + po += put_uint16_t_le(~crc, po); mbedtls_aes_context ctx; mbedtls_aes_init(&ctx); mbedtls_aes_setkey_enc(&ctx, otp_config->aes_key, 128); @@ -325,8 +315,7 @@ int otp_button_pressed(uint8_t slot) { if (update_counter == true) { uint8_t new_data[otp_config_size + 8]; memcpy(new_data, data, sizeof(new_data)); - new_data[otp_config_size] = counter >> 8; - new_data[otp_config_size + 1] = counter & 0xff; + put_uint16_t_be(counter, new_data + otp_config_size); file_put_data(ef, new_data, sizeof(new_data)); low_flash_available(); } @@ -531,9 +520,7 @@ extern uint16_t *get_send_buffer_size(uint8_t itf); int otp_send_frame(uint8_t *frame, size_t frame_len) { uint16_t crc = calculate_crc(frame, frame_len); - frame[frame_len] = ~crc & 0xff; - frame[frame_len + 1] = ~crc >> 8; - frame_len += 2; + frame_len += put_uint16_t_le(~crc, frame + frame_len); *get_send_buffer_size(ITF_KEYBOARD) = frame_len; otp_exp_seq = (frame_len / 7); if (frame_len % 7) { @@ -566,7 +553,7 @@ int otp_hid_set_report_cb(uint8_t itf, memcpy(otp_frame_rx + rseq * 7, buffer, 7); if (rseq == 9) { DEBUG_DATA(otp_frame_rx, sizeof(otp_frame_rx)); - uint16_t residual_crc = calculate_crc(otp_frame_rx, 64), rcrc = (otp_frame_rx[66] << 8 | otp_frame_rx[65]); + uint16_t residual_crc = calculate_crc(otp_frame_rx, 64), rcrc = get_uint16_t_le(otp_frame_rx + 65); uint8_t slot_id = otp_frame_rx[64]; if (residual_crc == rcrc) { uint8_t hdr[5];