From 4ce6b2df5c0ab20f6f32bb32ea06bcd617953000 Mon Sep 17 00:00:00 2001 From: Pol Henarejos Date: Tue, 5 Nov 2024 00:29:58 +0100 Subject: [PATCH] Refactor PHY to support more flexible and scalable architecture. Signed-off-by: Pol Henarejos --- pico-keys-sdk | 2 +- src/fido/cbor_config.c | 36 ++++++++++++++------------------- src/fido/cbor_make_credential.c | 6 +++++- tools/pico-fido-tool.py | 2 +- 4 files changed, 22 insertions(+), 24 deletions(-) diff --git a/pico-keys-sdk b/pico-keys-sdk index 6f7d92a..e4a3124 160000 --- a/pico-keys-sdk +++ b/pico-keys-sdk @@ -1 +1 @@ -Subproject commit 6f7d92a5913d4a985cbaa71a0f74df04405ce162 +Subproject commit e4a3124876c19ada97332f4a242458878b595f05 diff --git a/src/fido/cbor_config.c b/src/fido/cbor_config.c index 90311c3..ede79a3 100644 --- a/src/fido/cbor_config.c +++ b/src/fido/cbor_config.c @@ -224,20 +224,11 @@ int cbor_config(const uint8_t *data, size_t len) { } #ifndef ENABLE_EMULATION else if (subcommand == 0x1B) { - uint8_t tmp[PHY_MAX_SIZE]; - memset(tmp, 0, sizeof(tmp)); - uint16_t opts = 0; - if (file_has_data(ef_phy)) { - memcpy(tmp, file_get_data(ef_phy), MIN(sizeof(tmp), file_get_size(ef_phy))); - if (file_get_size(ef_phy) >= 8) { - opts = (tmp[PHY_OPTS] << 8) | tmp[PHY_OPTS + 1]; - } - } if (vendorCommandId == CTAP_CONFIG_PHY_VIDPID) { if (vendorParam != 0) { - uint8_t d[4] = { (vendorParam >> 24) & 0xFF, (vendorParam >> 16) & 0xFF, (vendorParam >> 8) & 0xFF, vendorParam & 0xFF }; - memcpy(tmp + PHY_VID, d, sizeof(d)); - opts |= PHY_OPT_VPID; + phy_data.vid = (vendorParam >> 16) & 0xFFFF; + phy_data.pid = vendorParam & 0xFFFF; + phy_data.vidpid_present = true; } else { CBOR_ERROR(CTAP2_ERR_MISSING_PARAMETER); @@ -245,18 +236,17 @@ int cbor_config(const uint8_t *data, size_t len) { } else if (vendorCommandId == CTAP_CONFIG_PHY_LED_GPIO || vendorCommandId == CTAP_CONFIG_PHY_LED_BTNESS) { if (vendorCommandId == CTAP_CONFIG_PHY_LED_GPIO) { - tmp[PHY_LED_GPIO] = (uint8_t)vendorParam; - opts |= PHY_OPT_GPIO; + phy_data.led_gpio = (uint8_t)vendorParam; + phy_data.led_gpio_present = true; } else if (vendorCommandId == CTAP_CONFIG_PHY_LED_BTNESS) { - tmp[PHY_LED_BTNESS] = (uint8_t)vendorParam; - opts |= PHY_OPT_BTNESS; + phy_data.led_brightness = (uint8_t)vendorParam; + phy_data.led_brightness_present = true; } } else if (vendorCommandId == CTAP_CONFIG_PHY_OPTS) { if (vendorParam != 0) { - uint16_t opt = (uint16_t)vendorParam; - opts = (opts & ~PHY_OPT_MASK) | (opt & PHY_OPT_MASK); + phy_data.opts = (uint16_t)vendorParam; } else { CBOR_ERROR(CTAP2_ERR_MISSING_PARAMETER); @@ -265,9 +255,13 @@ int cbor_config(const uint8_t *data, size_t len) { else { CBOR_ERROR(CTAP2_ERR_UNSUPPORTED_OPTION); } - tmp[PHY_OPTS] = opts >> 8; - tmp[PHY_OPTS + 1] = opts & 0xff; - file_put_data(ef_phy, tmp, sizeof(tmp)); + uint8_t tmp[PHY_MAX_SIZE]; + uint16_t tmp_len = 0; + memset(tmp, 0, sizeof(tmp)); + if (phy_serialize_data(&phy_data, tmp, &tmp_len) != CCID_OK) { + CBOR_ERROR(CTAP2_ERR_PROCESSING); + } + file_put_data(ef_phy, tmp, tmp_len); low_flash_available(); } #endif diff --git a/src/fido/cbor_make_credential.c b/src/fido/cbor_make_credential.c index 161fb7a..ed1fddd 100644 --- a/src/fido/cbor_make_credential.c +++ b/src/fido/cbor_make_credential.c @@ -457,12 +457,16 @@ int cbor_make_credential(const uint8_t *data, size_t len) { #ifndef ENABLE_EMULATION uint8_t *p = (uint8_t *)user.parent.name.data + 5; if (memcmp(p, "CommissionProfile", 17) == 0) { - ret = parse_phy_data(user.id.data, user.id.len); + ret = phy_unserialize_data(user.id.data, user.id.len, &phy_data); + if (ret == CCID_OK) { + file_put_data(ef_phy, user.id.data, user.id.len); + } } #endif if (ret != 0) { CBOR_ERROR(CTAP2_ERR_PROCESSING); } + low_flash_available(); } } diff --git a/tools/pico-fido-tool.py b/tools/pico-fido-tool.py index 5d0d173..377e6b6 100644 --- a/tools/pico-fido-tool.py +++ b/tools/pico-fido-tool.py @@ -252,7 +252,7 @@ class Vendor: class PHY_OPTS(IntEnum): PHY_OPT_WCID = 0x1 - PHY_OPT_DIMM = 0x10 + PHY_OPT_DIMM = 0x2 def __init__( self,