feat(protocomm): Migrate to PSA api interface

This commit is contained in:
Aditya Patwardhan
2025-12-18 21:18:58 +08:00
committed by Ashish Sharma
parent 1d92b530cd
commit 77d2a9e627
5 changed files with 248 additions and 184 deletions
@@ -1054,53 +1054,64 @@ int crypto_ecdsa_get_sign(unsigned char *hash,
return 0; return 0;
} }
// TODO: Add a test case for this function and then migrate to PSA
int crypto_ec_key_verify_signature_r_s(struct crypto_ec_key *csign, int crypto_ec_key_verify_signature_r_s(struct crypto_ec_key *csign,
const unsigned char *hash, int hlen, const unsigned char *hash, int hlen,
const u8 *r, size_t r_len, const u8 *r, size_t r_len,
const u8 *s, size_t s_len) const u8 *s, size_t s_len)
{ {
printf("crypto_ec_key_verify_signature_r_s\n"); psa_status_t status = psa_crypto_init();
mbedtls_ecp_keypair *ecp_kp = mbedtls_pk_ec(*(mbedtls_pk_context *)csign); if (status != PSA_SUCCESS) {
if (!ecp_kp) {
return -1; return -1;
} }
struct crypto_bignum *rb = NULL, *sb = NULL; psa_key_id_t *key_id = (psa_key_id_t *)csign;
rb = crypto_bignum_init_set(r, r_len);
sb = crypto_bignum_init_set(s, s_len);
mbedtls_ecp_group *ecp_kp_grp = &ecp_kp->MBEDTLS_PRIVATE(grp); u8 *sig = os_zalloc(r_len + s_len);
mbedtls_ecp_point *ecp_kp_q = &ecp_kp->MBEDTLS_PRIVATE(Q); if (!sig) {
int ret = mbedtls_ecdsa_verify(ecp_kp_grp, hash, hlen, return -1;
ecp_kp_q, (mbedtls_mpi *)rb, (mbedtls_mpi *)sb);
if (ret != 0) {
wpa_printf(MSG_ERROR, "ecdsa verification failed");
crypto_bignum_deinit(rb, 0);
crypto_bignum_deinit(sb, 0);
return ret;
} }
return ret; os_memcpy(sig, r, r_len);
os_memcpy(sig + r_len, s, s_len);
status = psa_verify_hash(*key_id, PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_SHA_256), hash, hlen, sig, r_len + s_len);
if (status != PSA_SUCCESS) {
printf("psa_verify_hash failed with %d\n", status);
os_free(sig);
return -1;
}
os_free(sig);
return 0;
} }
void crypto_ec_key_debug_print(struct crypto_ec_key *key, const char *title) void crypto_ec_key_debug_print(struct crypto_ec_key *key, const char *title)
{ {
#ifdef DEBUG_PRINT #ifdef DEBUG_PRINT
// mbedtls_pk_context *pkey = (mbedtls_pk_context *)key; psa_key_id_t *pkey = (psa_key_id_t *)key;
// mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(*pkey);
// u8 x[32], y[32], d[32]; unsigned char pub[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
// wpa_printf(MSG_INFO, "curve: %s", size_t pub_len = 0;
// mbedtls_ecp_curve_info_from_grp_id(ecp->MBEDTLS_PRIVATE(grp).id)->name); psa_export_public_key(*pkey, pub, sizeof(pub), &pub_len);
// int len = mbedtls_mpi_size((mbedtls_mpi *)crypto_ec_get_prime((struct crypto_ec *)crypto_ec_get_group_from_key(key)));
wpa_hexdump(MSG_INFO, "public key:", pub, pub_len);
wpa_printf(MSG_INFO, "public key len: %d", pub_len);
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
psa_get_key_attributes(*pkey, &key_attributes);
psa_ecc_family_t ecc_family = psa_get_key_type(&key_attributes);
size_t bits = psa_get_key_bits(&key_attributes);
int ret = mbedtls_ecc_group_from_psa(ecc_family, bits);
if (ret == 0) {
wpa_printf(MSG_ERROR, "Unsupported ECC group");
}
wpa_printf(MSG_INFO, "curve: %s", mbedtls_ecp_curve_info_from_grp_id(ret)->name);
wpa_printf(MSG_INFO, "bits: %d", bits);
wpa_printf(MSG_INFO, "psa_ecc_family: %d", ecc_family);
// wpa_printf(MSG_INFO, "prime len is %d", len);
// crypto_ec_point_to_bin((struct crypto_ec *)crypto_ec_get_group_from_key(key), crypto_ec_key_get_public_key(key), x, y);
// crypto_bignum_to_bin(crypto_ec_key_get_private_key(key),
// d, len, len);
// wpa_hexdump(MSG_INFO, "Q_x:", x, 32);
// wpa_hexdump(MSG_INFO, "Q_y:", y, 32);
// wpa_hexdump(MSG_INFO, "d: ", d, 32);
#endif #endif
} }
@@ -1282,7 +1293,7 @@ int crypto_ec_write_pub_key(struct crypto_ec_key *key, unsigned char **key_buf)
psa_key_id_t *pkey = (psa_key_id_t *)key; psa_key_id_t *pkey = (psa_key_id_t *)key;
mbedtls_pk_context *pk = os_malloc(sizeof(mbedtls_pk_context)); mbedtls_pk_context *pk = os_zalloc(sizeof(mbedtls_pk_context));
mbedtls_pk_init(pk); mbedtls_pk_init(pk);
int ret = mbedtls_pk_copy_public_from_psa(*pkey, pk); int ret = mbedtls_pk_copy_public_from_psa(*pkey, pk);
@@ -1290,9 +1301,14 @@ int crypto_ec_write_pub_key(struct crypto_ec_key *key, unsigned char **key_buf)
wpa_printf(MSG_ERROR, "Failed to copy public key from psa key"); wpa_printf(MSG_ERROR, "Failed to copy public key from psa key");
return 0; return 0;
} }
unsigned char output_buf[1600] = {0}; unsigned char *output_buf = os_zalloc(1600);
if (!output_buf) {
wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__);
return 0;
}
int len = crypto_pk_write_formatted_pubkey_der(pk, output_buf, 1600, 1); int len = crypto_pk_write_formatted_pubkey_der(pk, output_buf, 1600, 1);
if (len <= 0) { if (len <= 0) {
os_free(output_buf);
return 0; return 0;
} }
@@ -1302,9 +1318,11 @@ int crypto_ec_write_pub_key(struct crypto_ec_key *key, unsigned char **key_buf)
*key_buf = os_malloc(len); *key_buf = os_malloc(len);
if (!*key_buf) { if (!*key_buf) {
wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__); wpa_printf(MSG_ERROR, "%s: memory allocation failed", __func__);
os_free(output_buf);
return 0; return 0;
} }
os_memcpy(*key_buf, output_buf + 1600 - len, len); os_memcpy(*key_buf, output_buf + 1600 - len, len);
os_free(output_buf);
return len; return len;
} }
@@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2015-2025 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@@ -185,15 +185,6 @@ int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
u8 *out, size_t *outlen) u8 *out, size_t *outlen)
{ {
int ret = 0; int ret = 0;
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
psa_key_id_t key_id = 0;
if (!pkey) {
wpa_printf(MSG_ERROR, "failed to allocate memory");
ret = -1;
goto cleanup;
}
psa_status_t status = psa_crypto_init(); psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
@@ -202,16 +193,22 @@ int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
goto cleanup; goto cleanup;
} }
ret = mbedtls_pk_get_psa_attributes(pkey, PSA_KEY_USAGE_ENCRYPT, &attributes); mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
if (ret != 0) {
wpa_printf(MSG_ERROR, "failed to get key attributes"); // Load the key into PSA
psa_key_id_t key_id = 0;
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
status = mbedtls_pk_get_psa_attributes(pkey, PSA_KEY_USAGE_ENCRYPT, &key_attributes);
if (status != PSA_SUCCESS) {
wpa_printf(MSG_ERROR, "Failed to get key attributes, returned %d", (int) status);
ret = -1; ret = -1;
goto cleanup; goto cleanup;
} }
ret = mbedtls_pk_import_into_psa(pkey, &attributes, &key_id); ret = mbedtls_pk_import_into_psa(pkey, &key_attributes, &key_id);
if (ret != 0) { if (ret != 0) {
wpa_printf(MSG_ERROR, "failed to import key into PSA"); wpa_printf(MSG_ERROR, "Failed to import key, returned %d", ret);
ret = -1; ret = -1;
goto cleanup; goto cleanup;
} }
@@ -220,6 +217,7 @@ int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT, in, inlen, NULL, 0, out, *outlen, &output_len); status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT, in, inlen, NULL, 0, out, *outlen, &output_len);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
wpa_printf(MSG_ERROR, "Failed to encrypt data, returned %d", (int) status); wpa_printf(MSG_ERROR, "Failed to encrypt data, returned %d", (int) status);
printf("Failed to encrypt data, returned %d\n", (int) status);
ret = -1; ret = -1;
goto cleanup; goto cleanup;
} }
@@ -227,10 +225,8 @@ int crypto_public_key_encrypt_pkcs1_v15(struct crypto_public_key *key,
*outlen = output_len; *outlen = output_len;
cleanup: cleanup:
psa_reset_key_attributes(&key_attributes);
if (key_id) { if (key_id) {
printf("Destroying key\n");
psa_reset_key_attributes(&attributes);
psa_destroy_key(key_id); psa_destroy_key(key_id);
} }
return ret; return ret;
@@ -241,51 +237,44 @@ int crypto_private_key_decrypt_pkcs1_v15(struct crypto_private_key *key,
u8 *out, size_t *outlen) u8 *out, size_t *outlen)
{ {
int ret = 0; int ret = 0;
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
psa_key_id_t key_id = 0;
if (!pkey) {
wpa_printf(MSG_ERROR, "failed to allocate memory");
ret = -1;
goto cleanup;
}
psa_status_t status = psa_crypto_init(); psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
wpa_printf(MSG_ERROR, "Failed to initialize PSA crypto, returned %d", (int) status); wpa_printf(MSG_ERROR, "Failed to initialize PSA crypto, returned %d", (int) status);
return -1; return -1;
} }
ret = mbedtls_pk_get_psa_attributes(pkey, PSA_KEY_USAGE_DECRYPT, &attributes); mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
if (ret != 0) {
wpa_printf(MSG_ERROR, "failed to get key attributes"); // Load the key into PSA
psa_key_id_t key_id = 0;
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
status = mbedtls_pk_get_psa_attributes(pkey, PSA_KEY_USAGE_DECRYPT, &key_attributes);
if (status != PSA_SUCCESS) {
wpa_printf(MSG_ERROR, "Failed to get key attributes, returned %d", (int) status);
ret = -1; ret = -1;
goto cleanup; goto cleanup;
} }
ret = mbedtls_pk_import_into_psa(pkey, &attributes, &key_id); ret = mbedtls_pk_import_into_psa(pkey, &key_attributes, &key_id);
if (ret != 0) { if (ret != 0) {
wpa_printf(MSG_ERROR, "failed to import key into PSA"); wpa_printf(MSG_ERROR, "Failed to import key, returned %d", ret);
ret = -1; ret = -1;
goto cleanup; goto cleanup;
} }
size_t output_len = 0; size_t output_len = 0;
size_t heap_free_before = esp_get_free_heap_size();
status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT, in, inlen, NULL, 0, out, *outlen, &output_len); status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT, in, inlen, NULL, 0, out, *outlen, &output_len);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
printf("Failed to decrypt data, returned %d", (int) status); printf("Failed to decrypt data, returned %d", (int) status);
ret = -1; ret = -1;
goto cleanup; goto cleanup;
} }
size_t heap_free_after = esp_get_free_heap_size();
printf("Heap free before: %d, Heap free after: %d, used: %d\n", heap_free_before, heap_free_after, heap_free_before - heap_free_after);
*outlen = output_len; *outlen = output_len;
cleanup: cleanup:
psa_reset_key_attributes(&key_attributes);
if (key_id) { if (key_id) {
psa_reset_key_attributes(&attributes);
psa_destroy_key(key_id); psa_destroy_key(key_id);
} }
return ret; return ret;
@@ -296,15 +285,6 @@ int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
u8 *out, size_t *outlen) u8 *out, size_t *outlen)
{ {
int ret = 0; int ret = 0;
mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
psa_key_id_t key_id = 0;
if (!pkey) {
wpa_printf(MSG_ERROR, "failed to allocate memory");
ret = -1;
goto cleanup;
}
psa_status_t status = psa_crypto_init(); psa_status_t status = psa_crypto_init();
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
@@ -312,26 +292,31 @@ int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
return -1; return -1;
} }
ret = mbedtls_pk_get_psa_attributes(pkey, PSA_KEY_USAGE_SIGN_HASH , &attributes); mbedtls_pk_context *pkey = (mbedtls_pk_context *)key;
if (ret != 0) {
wpa_printf(MSG_ERROR, "failed to get key attributes"); // Load the key into PSA
psa_key_id_t key_id = 0;
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
status = mbedtls_pk_get_psa_attributes(pkey, PSA_KEY_USAGE_SIGN_HASH, &key_attributes);
if (status != PSA_SUCCESS) {
wpa_printf(MSG_ERROR, "Failed to get key attributes, returned %d", (int) status);
ret = -1; ret = -1;
goto cleanup; goto cleanup;
} }
ret = mbedtls_pk_import_into_psa(pkey, &attributes, &key_id); ret = mbedtls_pk_import_into_psa(pkey, &key_attributes, &key_id);
if (ret != 0) { if (ret != 0) {
wpa_printf(MSG_ERROR, "failed to import key into PSA"); wpa_printf(MSG_ERROR, "Failed to import key, returned %d", ret);
ret = -1; ret = -1;
goto cleanup; goto cleanup;
} }
printf("Hash ID: %d\n", (mbedtls_pk_rsa(*pkey))->MBEDTLS_PRIVATE(hash_id));
size_t output_len = 0; size_t output_len = 0;
status = psa_sign_hash(key_id, PSA_ALG_RSA_PKCS1V15_SIGN_RAW, in, inlen, out, *outlen, &output_len); status = psa_sign_hash(key_id, PSA_ALG_RSA_PKCS1V15_SIGN_RAW, in, inlen, out, *outlen, &output_len);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
wpa_printf(MSG_ERROR, "Failed to sign data, returned %d", (int) status); wpa_printf(MSG_ERROR, "Failed to sign data, returned %d", (int) status);
printf("Failed to sign data, returned %d\n", (int) status);
ret = -1; ret = -1;
goto cleanup; goto cleanup;
} }
@@ -339,8 +324,8 @@ int crypto_private_key_sign_pkcs1(struct crypto_private_key *key,
*outlen = output_len; *outlen = output_len;
cleanup: cleanup:
psa_reset_key_attributes(&key_attributes);
if (key_id) { if (key_id) {
psa_reset_key_attributes(&attributes);
psa_destroy_key(key_id); psa_destroy_key(key_id);
} }
return ret; return ret;
@@ -46,14 +46,13 @@
struct crypto_hash { struct crypto_hash {
union { union {
psa_hash_operation_t *hash_operation; psa_hash_operation_t *hash_operation;
psa_mac_operation_t *mac_operation; psa_mac_operation_t *mac_operation;
} u; } u;
int is_mac; int is_mac;
psa_key_id_t key_id; psa_key_id_t key_id;
}; };
static int digest_vector(psa_algorithm_t alg, size_t num_elem, static int digest_vector(psa_algorithm_t alg, size_t num_elem,
const u8 *addr[], const size_t *len, u8 *mac) const u8 *addr[], const size_t *len, u8 *mac)
{ {
@@ -144,38 +143,38 @@ struct crypto_hash * crypto_hash_init(enum crypto_hash_alg alg, const u8 *key,
psa_algorithm_t psa_alg; psa_algorithm_t psa_alg;
int is_hmac = 0; int is_hmac = 0;
switch(alg) { switch (alg) {
case CRYPTO_HASH_ALG_MD5: case CRYPTO_HASH_ALG_MD5:
case CRYPTO_HASH_ALG_HMAC_MD5: case CRYPTO_HASH_ALG_HMAC_MD5:
psa_alg = PSA_ALG_MD5; psa_alg = PSA_ALG_MD5;
break; break;
case CRYPTO_HASH_ALG_SHA1: case CRYPTO_HASH_ALG_SHA1:
case CRYPTO_HASH_ALG_HMAC_SHA1: case CRYPTO_HASH_ALG_HMAC_SHA1:
psa_alg = PSA_ALG_SHA_1; psa_alg = PSA_ALG_SHA_1;
break; break;
case CRYPTO_HASH_ALG_SHA256: case CRYPTO_HASH_ALG_SHA256:
case CRYPTO_HASH_ALG_HMAC_SHA256: case CRYPTO_HASH_ALG_HMAC_SHA256:
psa_alg = PSA_ALG_SHA_256; psa_alg = PSA_ALG_SHA_256;
break; break;
case CRYPTO_HASH_ALG_SHA384: case CRYPTO_HASH_ALG_SHA384:
psa_alg = PSA_ALG_SHA_384; psa_alg = PSA_ALG_SHA_384;
break; break;
case CRYPTO_HASH_ALG_SHA512: case CRYPTO_HASH_ALG_SHA512:
psa_alg = PSA_ALG_SHA_512; psa_alg = PSA_ALG_SHA_512;
break; break;
default: default:
os_free(ctx); os_free(ctx);
return NULL; return NULL;
} }
switch(alg) { switch (alg) {
case CRYPTO_HASH_ALG_HMAC_MD5: case CRYPTO_HASH_ALG_HMAC_MD5:
case CRYPTO_HASH_ALG_HMAC_SHA1: case CRYPTO_HASH_ALG_HMAC_SHA1:
case CRYPTO_HASH_ALG_HMAC_SHA256: case CRYPTO_HASH_ALG_HMAC_SHA256:
is_hmac = 1; is_hmac = 1;
break; break;
default: default:
break; break;
} }
// If is_hmac is set, then it is HMAC mode // If is_hmac is set, then it is HMAC mode
@@ -251,7 +250,7 @@ void crypto_hash_update(struct crypto_hash *crypto_ctx, const u8 *data, size_t l
} else { } else {
status = psa_hash_update(crypto_ctx->u.hash_operation, data, len); status = psa_hash_update(crypto_ctx->u.hash_operation, data, len);
} }
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
wpa_printf(MSG_ERROR, "%s: psa_hash_update failed", __func__); wpa_printf(MSG_ERROR, "%s: psa_hash_update failed", __func__);
} }
@@ -573,7 +572,7 @@ int aes_128_cbc_encrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
psa_key_id_t key_id; psa_key_id_t key_id;
status = psa_crypto_init(); status = psa_crypto_init();
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
wpa_printf(MSG_ERROR, "%s: psa_crypto_init failed", __func__); wpa_printf(MSG_ERROR, "%s: psa_crypto_init failed", __func__);
return -1; return -1;
@@ -797,7 +796,6 @@ struct crypto_cipher *crypto_cipher_init(enum crypto_cipher_alg alg,
#endif /* CONFIG_MBEDTLS_CIPHER_MODE_WITH_PADDING */ #endif /* CONFIG_MBEDTLS_CIPHER_MODE_WITH_PADDING */
return ctx; return ctx;
cleanup: cleanup:
os_free(ctx); os_free(ctx);
return NULL; return NULL;
@@ -1047,7 +1045,7 @@ int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
psa_set_key_algorithm(&attributes, PSA_ALG_ECB_NO_PADDING); psa_set_key_algorithm(&attributes, PSA_ALG_ECB_NO_PADDING);
psa_set_key_type(&attributes, PSA_KEY_TYPE_DES); psa_set_key_type(&attributes, PSA_KEY_TYPE_DES);
psa_set_key_bits(&attributes, 64); psa_set_key_bits(&attributes, 128);
status = psa_import_key(&attributes, key, 8, &key_id); status = psa_import_key(&attributes, key, 8, &key_id);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
@@ -1135,7 +1133,7 @@ int aes_ccm_ae(const u8 *key, size_t key_len, const u8 *nonce,
} }
size_t output_length = 0; size_t output_length = 0;
size_t tag_len = 0; size_t tag_len = 0;
status = psa_aead_update_ad(&operation, aad, aad_len); status = psa_aead_update_ad(&operation, aad, aad_len);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
@@ -1159,7 +1157,7 @@ int aes_ccm_ae(const u8 *key, size_t key_len, const u8 *nonce,
psa_destroy_key(key_id); psa_destroy_key(key_id);
return 0; return 0;
} }
int aes_ccm_ad(const u8 *key, size_t key_len, const u8 *nonce, int aes_ccm_ad(const u8 *key, size_t key_len, const u8 *nonce,
@@ -412,7 +412,7 @@ int des_encrypt(const u8 *clear, const u8 *key, u8 *cypher)
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
psa_set_key_algorithm(&attributes, PSA_ALG_ECB_NO_PADDING); psa_set_key_algorithm(&attributes, PSA_ALG_ECB_NO_PADDING);
psa_set_key_type(&attributes, PSA_KEY_TYPE_DES); psa_set_key_type(&attributes, PSA_KEY_TYPE_DES);
psa_set_key_bits(&attributes, 64); psa_set_key_bits(&attributes, 128);
status = psa_import_key(&attributes, key, 8, &key_id); status = psa_import_key(&attributes, key, 8, &key_id);
if (status != PSA_SUCCESS) { if (status != PSA_SUCCESS) {
@@ -745,6 +745,9 @@ TEST_CASE("Test crypto lib ecdsa apis", "[wpa_crypto]")
ret = crypto_ec_key_verify_signature(eckey, expected_data, 64, data, 64); ret = crypto_ec_key_verify_signature(eckey, expected_data, 64, data, 64);
TEST_ASSERT(ret == 0); TEST_ASSERT(ret == 0);
ret = crypto_ec_key_verify_signature_r_s(eckey, expected_data, 64, data, 32, data + 32, 32);
TEST_ASSERT(ret == 0);
// Negative test case // Negative test case
expected_data[0] = 0x5A; expected_data[0] = 0x5A;
ret = crypto_ec_key_verify_signature(eckey, expected_data, 64, data, 64); ret = crypto_ec_key_verify_signature(eckey, expected_data, 64, data, 64);
@@ -934,48 +937,119 @@ TEST_CASE("Test crypto lib rsa apis", "[wpa_crypto]")
{ {
set_leak_threshold(1); set_leak_threshold(1);
{ {
const char *rsa_key = unsigned char rsa_der[] = {
"-----BEGIN RSA PRIVATE KEY-----\n" 0x30, 0x82, 0x04, 0xbd, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
"MIIEowIBAAKCAQEAojDGyuzNRtqWDS3no8PPA0LFCme4V8qnpKaJnHjp/YxMEcny\n" 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
"W3eWPBTtL0WUvSRHBY7PgiVTKWKN/4zuRq77XnLxZQuRGYQeLcpLmsyZPArnlfym\n" 0x04, 0xa7, 0x30, 0x82, 0x04, 0xa3, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01,
"jZRsXPsnPscmbKt5SAxCrFKXKIwZ76rR7he/2OsxQ7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 0x58,
0x0a, 0x69, 0x35, 0x9c, 0x78, 0x39, 0x92, 0x01, 0xa9, 0x99, 0xf9, 0xc4,
0xf6, 0x47, 0x9c, 0x1e, 0xa1, 0x02, 0x81, 0x81, 0x00, 0xae, 0xf4, 0x94,
0x50, 0xe0, 0x58, 0x7e, 0xed, 0x9b, 0xa9, 0x26, 0x99, 0x24, 0x92, 0x0e,
0x4f, 0x63, 0x93, 0x80, 0x87, 0x89, 0x78, 0x16, 0xa2, 0x05, 0x46, 0xa2,
0x0d, 0x3e, 0x44, 0x0b, 0x4a, 0x7f, 0x82, 0x5a, 0x15, 0x4e, 0x93, 0xcf,
0x95, 0x18, 0x7a, 0x3a, 0xa8, 0xfe, 0xc5, 0xd5, 0x9b, 0xb1, 0x82, 0x00,
0xab, 0x69, 0x43, 0xa8, 0x8a, 0xef, 0xd6, 0x4c, 0x98, 0x53, 0x95, 0x1b,
0x87, 0xc6, 0xc7, 0x75, 0x9d, 0xae, 0x30, 0x26, 0x08, 0xbb, 0x29, 0x88,
0xed, 0xc5, 0x78, 0x9d, 0x9d, 0x47, 0x48, 0x68, 0x73, 0xf3, 0xa4, 0x6c,
0x3b, 0xff, 0x40, 0xeb, 0x63, 0x0c, 0xa0, 0xaf, 0x5b, 0xc6, 0xf7, 0x6c,
0xb5, 0x03, 0xdf, 0xaf, 0x58, 0x87, 0xd1, 0x95, 0x0a, 0xea, 0xf9, 0x78,
0x9e, 0xac, 0x8d, 0x98, 0x1a, 0xe0, 0x3e, 0xe3, 0x69, 0x21, 0x20, 0x38,
0xdd, 0xc4, 0x34, 0x89, 0x04
};
unsigned int rsa_der_len = 1217;
mbedtls_pk_context *ctx = calloc(1, sizeof(mbedtls_pk_context)); struct crypto_private_key *ctx = crypto_private_key_import(rsa_der, rsa_der_len, NULL);
mbedtls_pk_init(ctx); TEST_ASSERT_NOT_NULL(ctx);
// Read the above key into mbedtls_pk_context
int ret = mbedtls_pk_parse_key(ctx, (const unsigned char *)rsa_key, strlen(rsa_key) + 1, NULL, 0, NULL, MBEDTLS_PSA_RANDOM_STATE);
TEST_ASSERT(ret == 0);
uint8_t data[32] = {[0 ... 31] = 0xA5}; uint8_t data[32] = {[0 ... 31] = 0xA5};
uint8_t encrypted[2048]; uint8_t encrypted[2048];
size_t encrypted_size = 2048; size_t encrypted_size = 2048;
psa_crypto_init(); int ret = crypto_public_key_encrypt_pkcs1_v15((struct crypto_public_key *)ctx, data, 32, encrypted, &encrypted_size);
ret = crypto_public_key_encrypt_pkcs1_v15((struct crypto_public_key *)ctx, data, 32, encrypted, &encrypted_size);
TEST_ASSERT(ret == 0); TEST_ASSERT(ret == 0);
uint8_t decrypted[32] = {[0 ... 31] = 0}; uint8_t decrypted[32] = {[0 ... 31] = 0};
@@ -984,8 +1058,8 @@ TEST_CASE("Test crypto lib rsa apis", "[wpa_crypto]")
ret = crypto_private_key_decrypt_pkcs1_v15((struct crypto_private_key *)ctx, encrypted, encrypted_size, decrypted, &decrypted_size); ret = crypto_private_key_decrypt_pkcs1_v15((struct crypto_private_key *)ctx, encrypted, encrypted_size, decrypted, &decrypted_size);
TEST_ASSERT(ret == 0); TEST_ASSERT(ret == 0);
TEST_ASSERT(!memcmp(data, decrypted, 32)); TEST_ASSERT(!memcmp(data, decrypted, 32));
mbedtls_pk_free(ctx);
free(ctx); crypto_private_key_free(ctx);
} }
{ {
@@ -1019,26 +1093,16 @@ TEST_CASE("Test crypto lib rsa apis", "[wpa_crypto]")
"QOtjDKCvW8b3bLUD369Yh9GVCur5eJ6sjZga4D7jaSEgON3ENIkE\n" "QOtjDKCvW8b3bLUD369Yh9GVCur5eJ6sjZga4D7jaSEgON3ENIkE\n"
"-----END RSA PRIVATE KEY-----\n"; "-----END RSA PRIVATE KEY-----\n";
mbedtls_pk_context *ctx = calloc(1, sizeof(mbedtls_pk_context)); struct crypto_private_key *ctx = crypto_private_key_import((uint8_t *)rsa_key, strlen(rsa_key) + 1, NULL);
mbedtls_pk_init(ctx); TEST_ASSERT_NOT_NULL(ctx);
// Read the above key into mbedtls_pk_context
int ret = mbedtls_pk_parse_key(ctx, (const unsigned char *)rsa_key, strlen(rsa_key) + 1, NULL, 0, NULL, MBEDTLS_PSA_RANDOM_STATE);
TEST_ASSERT(ret == 0);
uint8_t data[32] = {[0 ... 31] = 0xA5}; uint8_t data[32] = {[0 ... 31] = 0xA5};
uint8_t signature[2048]; uint8_t signature[2048];
size_t signature_size = 2048; size_t signature_size = 2048;
ret = crypto_private_key_sign_pkcs1((struct crypto_private_key *)ctx, data, 32, signature, &signature_size); int ret = crypto_private_key_sign_pkcs1((struct crypto_private_key *)ctx, data, 32, signature, &signature_size);
TEST_ASSERT(ret == 0); TEST_ASSERT(ret == 0);
// printf("Signature size: %d\n", signature_size);
// for (int i = 0; i < signature_size; i++) {
// printf("%02X", signature[i]);
// }
// printf("\n");
// 700CE7B382057B3DA95734BFF2371A6435E9B226BFE2BB97922529A3331F1DEE57CA02341EE7448A5605813C8124BD64EBC21623EAC7CA8DECB61B615676BA0CBCE3A0B51369E4D69C8C7628AC55952D1553951722C05A0F79F9AC1C17061781532B8E2577529C480F96B93ED73D4079C865D71758ABB6EC4B51C4ED0ED8D47DF82C9B8701E072D5B9786CC835A52F508F2BCC2A762DD8C4BB9C02B44591954EDEF38655A2E551C6BAA0AFA803D583147856980D4EF3A1053A32EB997B3DEF46C86E7BB59F83F7D6FE38E825B60BF42652DF87AA4F19689BFBB6CEF07789A4B3AAD270A4FF9D942083BA08D0D97BD0D707B57424C652850627A505D23E1D0B2E // 700CE7B382057B3DA95734BFF2371A6435E9B226BFE2BB97922529A3331F1DEE57CA02341EE7448A5605813C8124BD64EBC21623EAC7CA8DECB61B615676BA0CBCE3A0B51369E4D69C8C7628AC55952D1553951722C05A0F79F9AC1C17061781532B8E2577529C480F96B93ED73D4079C865D71758ABB6EC4B51C4ED0ED8D47DF82C9B8701E072D5B9786CC835A52F508F2BCC2A762DD8C4BB9C02B44591954EDEF38655A2E551C6BAA0AFA803D583147856980D4EF3A1053A32EB997B3DEF46C86E7BB59F83F7D6FE38E825B60BF42652DF87AA4F19689BFBB6CEF07789A4B3AAD270A4FF9D942083BA08D0D97BD0D707B57424C652850627A505D23E1D0B2E
uint8_t expected_signature[256] = { uint8_t expected_signature[256] = {
0x70, 0x0C, 0xE7, 0xB3, 0x82, 0x05, 0x7B, 0x3D, 0xA9, 0x57, 0x70, 0x0C, 0xE7, 0xB3, 0x82, 0x05, 0x7B, 0x3D, 0xA9, 0x57,
@@ -1077,8 +1141,7 @@ TEST_CASE("Test crypto lib rsa apis", "[wpa_crypto]")
} }
} }
mbedtls_pk_free(ctx); crypto_private_key_free(ctx);
free(ctx);
} }
} }