1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT zephyr_psa_crypto_rng
8 
9 #include <zephyr/drivers/entropy.h>
10 #include <psa/crypto.h>
11 
12 /* API implementation: PSA Crypto initialization */
entropy_psa_crypto_rng_init(const struct device * dev)13 static int entropy_psa_crypto_rng_init(const struct device *dev)
14 {
15 	psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
16 
17 	ARG_UNUSED(dev);
18 
19 	status = psa_crypto_init();
20 	if (status != PSA_SUCCESS) {
21 		return -EIO;
22 	}
23 
24 	return 0;
25 }
26 
27 /* API implementation: get_entropy */
entropy_psa_crypto_rng_get_entropy(const struct device * dev,uint8_t * buffer,uint16_t length)28 static int entropy_psa_crypto_rng_get_entropy(const struct device *dev,
29 					      uint8_t *buffer, uint16_t length)
30 {
31 	psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
32 
33 	ARG_UNUSED(dev);
34 
35 	status = psa_generate_random(buffer, length);
36 	if (status != PSA_SUCCESS) {
37 		return -EIO;
38 	}
39 
40 	return 0;
41 }
42 
43 /* Entropy driver APIs structure */
44 static DEVICE_API(entropy, entropy_psa_crypto_rng_api) = {
45 	.get_entropy = entropy_psa_crypto_rng_get_entropy,
46 };
47 
48 /* Entropy driver registration */
49 DEVICE_DT_INST_DEFINE(0, entropy_psa_crypto_rng_init, NULL, NULL, NULL,
50 		      PRE_KERNEL_1, CONFIG_ENTROPY_INIT_PRIORITY,
51 		      &entropy_psa_crypto_rng_api);
52