forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentropy_nxp_ele.c
71 lines (51 loc) · 1.52 KB
/
entropy_nxp_ele.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* entropy_nxp_ele.c - NXP ELE entropy driver */
/*
* Copyright 2025 NXP
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nxp_ele_trng
#include <zephyr/logging/log.h>
#include <zephyr/drivers/entropy.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/atomic.h>
#include <zephyr/sys/util.h>
#include <soc.h>
#include "sss_crypto.h"
struct entropy_ele_data_str {
struct k_sem sem_lock;
};
static struct entropy_ele_data_str entropy_ele_data;
static int entropy_ele_get_entropy(const struct device *dev, uint8_t *buf, uint16_t len)
{
sss_sscp_rng_t ctx;
int result = -EIO;
__ASSERT_NO_MSG(buf != NULL);
__ASSERT_NO_MSG(&entropy_ele_data == dev->data);
k_sem_take(&entropy_ele_data.sem_lock, K_FOREVER);
if (CRYPTO_InitHardware() != kStatus_Success) {
goto exit;
}
if (sss_sscp_rng_context_init(&g_sssSession, &ctx, 0u) != kStatus_SSS_Success) {
goto exit;
}
if (sss_sscp_rng_get_random(&ctx, buf, len) != kStatus_SSS_Success) {
goto exit;
}
if (sss_sscp_rng_free(&ctx) != kStatus_SSS_Success) {
goto exit;
}
result = 0;
exit:
k_sem_give(&entropy_ele_data.sem_lock);
return result;
}
static int entropy_ele_init(const struct device *dev)
{
__ASSERT_NO_MSG(&entropy_ele_data == dev->data);
k_sem_init(&entropy_ele_data.sem_lock, 1, 1);
return 0;
}
static DEVICE_API(entropy, entropy_ele_api_funcs) = {.get_entropy = entropy_ele_get_entropy};
DEVICE_DT_INST_DEFINE(0, entropy_ele_init, NULL, &entropy_ele_data, NULL, PRE_KERNEL_1,
CONFIG_ENTROPY_INIT_PRIORITY, &entropy_ele_api_funcs);