zephyr/subsys/bluetooth/crypto/bt_crypto_psa.c
Valerio Setti 9032f8d791 bt-crypto: add option to use PSA APIs instead of TinyCrypt
This commit adds CONFIG_BT_USE_PSA_API to allow the end
user to prefer PSA APIs over TinyCrypt for crypto operations
in bluetooth. Of course, this is possible only if
a PSA provider is available on the system, i.e.
CONFIG_PSA_CRYPTO_CLIENT is set.

This commit also extends tests/bluetooth/bt_crypto adding
a test case for PSA.

Signed-off-by: Valerio Setti <vsetti@baylibre.com>
2024-06-14 15:41:34 +02:00

47 lines
1.2 KiB
C

/* Copyright (c) 2022 Nordic Semiconductor ASA
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <string.h>
#include <zephyr/sys/byteorder.h>
#include "psa/crypto.h"
#include "common/bt_str.h"
#include "bt_crypto.h"
#define LOG_LEVEL CONFIG_BT_CRYPTO_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(bt_crypto);
int bt_crypto_aes_cmac(const uint8_t *key, const uint8_t *in, size_t len, uint8_t *out)
{
psa_key_id_t key_id;
psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
size_t out_size;
psa_status_t status, destroy_status;
psa_set_key_type(&key_attr, PSA_KEY_TYPE_AES);
psa_set_key_bits(&key_attr, 128);
psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_SIGN_MESSAGE |
PSA_KEY_USAGE_VERIFY_MESSAGE);
psa_set_key_algorithm(&key_attr, PSA_ALG_CMAC);
status = psa_import_key(&key_attr, key, 16, &key_id);
if (status != PSA_SUCCESS) {
LOG_ERR("Failed to import AES key %d", status);
return -EIO;
}
status = psa_mac_compute(key_id, PSA_ALG_CMAC, in, len, out, 16, &out_size);
destroy_status = psa_destroy_key(key_id);
if ((status != PSA_SUCCESS) || (destroy_status != PSA_SUCCESS)) {
LOG_ERR("Failed to compute MAC %d", status);
return -EIO;
}
return 0;
}