-
Notifications
You must be signed in to change notification settings - Fork 8k
net: ip: ipv6: replace legacy crypto with PSA API #97346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ LOG_MODULE_REGISTER(net_ipv6, CONFIG_NET_IPV6_LOG_LEVEL); | |
|
||
#if defined(CONFIG_NET_IPV6_IID_STABLE) | ||
#include <zephyr/random/random.h> | ||
#include <mbedtls/md.h> | ||
#include <psa/crypto.h> | ||
#endif /* CONFIG_NET_IPV6_IID_STABLE */ | ||
|
||
#include <zephyr/net/net_core.h> | ||
|
@@ -875,10 +875,12 @@ static int gen_stable_iid(uint8_t if_index, | |
size_t stable_iid_len) | ||
{ | ||
#if defined(CONFIG_NET_IPV6_IID_STABLE) | ||
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256); | ||
mbedtls_md_context_t ctx; | ||
psa_key_id_t key_id; | ||
psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; | ||
psa_mac_operation_t mac_op = PSA_MAC_OPERATION_INIT; | ||
psa_status_t status; | ||
uint8_t digest[32]; | ||
int ret; | ||
size_t digest_len; | ||
static bool once; | ||
static uint8_t secret_key[16]; /* Min 128 bits, RFC 7217 ch 5 */ | ||
struct { | ||
|
@@ -909,28 +911,30 @@ static int gen_stable_iid(uint8_t if_index, | |
once = true; | ||
} | ||
|
||
mbedtls_md_init(&ctx); | ||
ret = mbedtls_md_setup(&ctx, md_info, true); | ||
if (ret != 0) { | ||
NET_DBG("Cannot %s hmac (%d)", "setup", ret); | ||
psa_set_key_type(&key_attr, PSA_KEY_TYPE_HMAC); | ||
psa_set_key_algorithm(&key_attr, PSA_ALG_HMAC(PSA_ALG_SHA_256)); | ||
psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_SIGN_MESSAGE); | ||
status = psa_import_key(&key_attr, secret_key, sizeof(secret_key), &key_id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering if instead of importing the key every time we could just have the key ID as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When should we then destroy that key? The drawback I see in doing what you are suggesting is that this key will occupy a slot in PSA core until is destroyed or the device is reset. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (status != PSA_SUCCESS) { | ||
NET_DBG("Cannot %s hmac (%d)", "import key", status); | ||
goto err; | ||
rlubos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
ret = mbedtls_md_hmac_starts(&ctx, secret_key, sizeof(secret_key)); | ||
if (ret != 0) { | ||
NET_DBG("Cannot %s hmac (%d)", "start", ret); | ||
status = psa_mac_sign_setup(&mac_op, key_id, PSA_ALG_HMAC(PSA_ALG_SHA_256)); | ||
if (status != PSA_SUCCESS) { | ||
NET_DBG("Cannot %s hmac (%d)", "setup", status); | ||
goto err; | ||
} | ||
|
||
ret = mbedtls_md_hmac_update(&ctx, (uint8_t *)&buf, sizeof(buf)); | ||
if (ret != 0) { | ||
NET_DBG("Cannot %s hmac (%d)", "update", ret); | ||
status = psa_mac_update(&mac_op, (uint8_t *)&buf, sizeof(buf)); | ||
if (status != PSA_SUCCESS) { | ||
NET_DBG("Cannot %s hmac (%d)", "update", status); | ||
goto err; | ||
} | ||
|
||
ret = mbedtls_md_hmac_finish(&ctx, digest); | ||
if (ret != 0) { | ||
NET_DBG("Cannot %s hmac (%d)", "finish", ret); | ||
status = psa_mac_sign_finish(&mac_op, digest, sizeof(digest), &digest_len); | ||
if (status != PSA_SUCCESS) { | ||
NET_DBG("Cannot %s hmac (%d)", "finish", status); | ||
goto err; | ||
} | ||
|
||
|
@@ -940,14 +944,14 @@ static int gen_stable_iid(uint8_t if_index, | |
if (unlikely(check_reserved(stable_iid, stable_iid_len))) { | ||
LOG_HEXDUMP_DBG(stable_iid, stable_iid_len, | ||
"Generated IID is reserved"); | ||
ret = -EINVAL; | ||
goto err; | ||
} | ||
|
||
err: | ||
mbedtls_md_free(&ctx); | ||
psa_mac_abort(&mac_op); | ||
psa_destroy_key(key_id); | ||
|
||
return ret; | ||
return (status == PSA_SUCCESS) ? 0 : -EIO; | ||
#else | ||
return -ENOTSUP; | ||
#endif | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -316,7 +316,7 @@ manifest: | |
revision: b03edc8e6282a963cd312cd0b409eb5ce263ea75 | ||
path: modules/lib/gui/lvgl | ||
- name: mbedtls | ||
revision: 85440ef5fffa95d0e9971e9163719189cf34d979 | ||
revision: pull/76/head | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to know what you're pulling in the commit title (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
path: modules/crypto/mbedtls | ||
groups: | ||
- crypto | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know #96415 is not merged yet, but are you planning to update this (and similar occurrences)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, #96415 will likely be merged before this one. Once this will be done I will update this PR accordingly. I didn't do this for now because I don't want spurious failures in the CI.