2019-07-24 03:16:24 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019, NXP
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <init.h>
|
|
|
|
#include <device.h>
|
2020-01-25 19:34:55 +08:00
|
|
|
#include <drivers/entropy.h>
|
2019-07-24 03:16:24 +08:00
|
|
|
#include <kernel.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_MBEDTLS)
|
|
|
|
#if !defined(CONFIG_MBEDTLS_CFG_FILE)
|
|
|
|
#include "mbedtls/config.h"
|
|
|
|
#else
|
|
|
|
#include CONFIG_MBEDTLS_CFG_FILE
|
|
|
|
#endif /* CONFIG_MBEDTLS_CFG_FILE */
|
|
|
|
#include <mbedtls/ctr_drbg.h>
|
|
|
|
|
|
|
|
#elif defined(CONFIG_TINYCRYPT)
|
|
|
|
|
|
|
|
#include <tinycrypt/ctr_prng.h>
|
|
|
|
#include <tinycrypt/aes.h>
|
|
|
|
#include <tinycrypt/constants.h>
|
|
|
|
|
|
|
|
#endif /* CONFIG_MBEDTLS */
|
|
|
|
|
|
|
|
static K_SEM_DEFINE(state_sem, 1, 1);
|
|
|
|
|
2022-02-26 01:43:41 +08:00
|
|
|
static const struct device *entropy_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_entropy));
|
2019-07-24 03:16:24 +08:00
|
|
|
static const unsigned char drbg_seed[] = CONFIG_CS_CTR_DRBG_PERSONALIZATION;
|
|
|
|
|
|
|
|
#if defined(CONFIG_MBEDTLS)
|
|
|
|
|
|
|
|
static mbedtls_ctr_drbg_context ctr_ctx;
|
|
|
|
|
|
|
|
static int ctr_drbg_entropy_func(void *ctx, unsigned char *buf, size_t len)
|
|
|
|
{
|
2022-02-26 01:43:41 +08:00
|
|
|
return entropy_get_entropy(entropy_dev, (void *)buf, len);
|
2019-07-24 03:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(CONFIG_TINYCRYPT)
|
|
|
|
|
|
|
|
static TCCtrPrng_t ctr_ctx;
|
|
|
|
|
|
|
|
#endif /* CONFIG_MBEDTLS */
|
|
|
|
|
|
|
|
|
|
|
|
static int ctr_drbg_initialize(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2022-02-26 01:43:41 +08:00
|
|
|
if (!device_is_ready(entropy_dev)) {
|
|
|
|
__ASSERT(0, "Entropy device %s not ready", entropy_dev->name);
|
|
|
|
return -ENODEV;
|
2019-07-24 03:16:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONFIG_MBEDTLS)
|
|
|
|
|
|
|
|
mbedtls_ctr_drbg_init(&ctr_ctx);
|
|
|
|
|
|
|
|
ret = mbedtls_ctr_drbg_seed(&ctr_ctx,
|
|
|
|
ctr_drbg_entropy_func,
|
2020-09-04 10:36:13 +08:00
|
|
|
NULL,
|
2019-07-24 03:16:24 +08:00
|
|
|
drbg_seed,
|
|
|
|
sizeof(drbg_seed));
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_ctr_drbg_free(&ctr_ctx);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(CONFIG_TINYCRYPT)
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t entropy[TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE];
|
2019-07-24 03:16:24 +08:00
|
|
|
|
2022-02-26 01:43:41 +08:00
|
|
|
ret = entropy_get_entropy(entropy_dev, (void *)&entropy,
|
2020-01-27 14:23:34 +08:00
|
|
|
sizeof(entropy));
|
|
|
|
if (ret != 0) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
2019-07-24 03:16:24 +08:00
|
|
|
|
|
|
|
ret = tc_ctr_prng_init(&ctr_ctx,
|
|
|
|
(uint8_t *)&entropy,
|
|
|
|
sizeof(entropy),
|
|
|
|
(uint8_t *)drbg_seed,
|
|
|
|
sizeof(drbg_seed));
|
|
|
|
|
|
|
|
if (ret == TC_CRYPTO_FAIL) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-05 02:00:45 +08:00
|
|
|
int z_impl_sys_csrand_get(void *dst, uint32_t outlen)
|
2019-07-24 03:16:24 +08:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned int key = irq_lock();
|
|
|
|
|
2022-02-26 01:43:41 +08:00
|
|
|
if (unlikely(!entropy_dev)) {
|
2019-07-24 03:16:24 +08:00
|
|
|
ret = ctr_drbg_initialize();
|
|
|
|
if (ret != 0) {
|
2020-11-12 02:42:51 +08:00
|
|
|
ret = -EIO;
|
|
|
|
goto end;
|
2019-07-24 03:16:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONFIG_MBEDTLS)
|
|
|
|
|
|
|
|
ret = mbedtls_ctr_drbg_random(&ctr_ctx, (unsigned char *)dst, outlen);
|
|
|
|
|
|
|
|
#elif defined(CONFIG_TINYCRYPT)
|
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t entropy[TC_AES_KEY_SIZE + TC_AES_BLOCK_SIZE];
|
2019-07-24 03:16:24 +08:00
|
|
|
|
|
|
|
ret = tc_ctr_prng_generate(&ctr_ctx, 0, 0, (uint8_t *)dst, outlen);
|
|
|
|
|
|
|
|
if (ret == TC_CRYPTO_SUCCESS) {
|
|
|
|
ret = 0;
|
|
|
|
} else if (ret == TC_CTR_PRNG_RESEED_REQ) {
|
|
|
|
|
2022-02-26 01:43:41 +08:00
|
|
|
ret = entropy_get_entropy(entropy_dev,
|
2019-07-24 03:16:24 +08:00
|
|
|
(void *)&entropy, sizeof(entropy));
|
2020-11-12 02:38:38 +08:00
|
|
|
if (ret != 0) {
|
|
|
|
ret = -EIO;
|
|
|
|
goto end;
|
|
|
|
}
|
2019-07-24 03:16:24 +08:00
|
|
|
|
|
|
|
ret = tc_ctr_prng_reseed(&ctr_ctx,
|
|
|
|
entropy,
|
|
|
|
sizeof(entropy),
|
|
|
|
drbg_seed,
|
|
|
|
sizeof(drbg_seed));
|
|
|
|
|
|
|
|
ret = tc_ctr_prng_generate(&ctr_ctx, 0, 0,
|
|
|
|
(uint8_t *)dst, outlen);
|
|
|
|
|
|
|
|
ret = (ret == TC_CRYPTO_SUCCESS) ? 0 : -EIO;
|
|
|
|
} else {
|
|
|
|
ret = -EIO;
|
|
|
|
}
|
|
|
|
#endif
|
2020-11-12 02:42:51 +08:00
|
|
|
end:
|
2019-07-24 03:16:24 +08:00
|
|
|
irq_unlock(key);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|