|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic |
| 5 | + */ |
| 6 | + |
| 7 | +#include <init.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <string.h> |
| 11 | +#include <assert.h> |
| 12 | + |
| 13 | +#include <zephyr.h> |
| 14 | +#include <drivers/entropy.h> |
| 15 | + |
| 16 | +#if CONFIG_ENTROPY_CC310 |
| 17 | + |
| 18 | +#if defined(CONFIG_SPM) |
| 19 | +#include "secure_services.h" |
| 20 | +#else |
| 21 | +#include "nrf_cc310_platform_entropy.h" |
| 22 | +#endif |
| 23 | + |
| 24 | +static int entropy_cc310_rng_get_entropy(struct device *dev, u8_t *buffer, |
| 25 | + u16_t length) { |
| 26 | + int res = -EINVAL; |
| 27 | + size_t olen; |
| 28 | + |
| 29 | + __ASSERT_NO_MSG(dev != NULL); |
| 30 | + __ASSERT_NO_MSG(buffer != NULL); |
| 31 | + |
| 32 | +#if defined(CONFIG_SPM) |
| 33 | + /** This is a call from a non-secure app that enables secure services, |
| 34 | + * in which case entropy is gathered by calling through SPM |
| 35 | + */ |
| 36 | + res = spm_request_random_number(buffer, length, &olen); |
| 37 | + if (olen != length) { |
| 38 | + return -EINVAL; |
| 39 | + } |
| 40 | + |
| 41 | +#else |
| 42 | + /** This is a call from a secure app, in which case entropy is gathered |
| 43 | + * using CC310 HW using the nrf_cc310_platform library. |
| 44 | + */ |
| 45 | + res = nrf_cc310_platform_entropy_get(buffer, length, &olen); |
| 46 | + if (olen != length) { |
| 47 | + return -EINVAL; |
| 48 | + } |
| 49 | +#endif |
| 50 | + |
| 51 | + return res; |
| 52 | +} |
| 53 | + |
| 54 | +static int entropy_cc310_rng_init(struct device *dev) { |
| 55 | + /* No initialization is required */ |
| 56 | + (void)dev; |
| 57 | + |
| 58 | + return 0; |
| 59 | +} |
| 60 | + |
| 61 | +static const struct entropy_driver_api entropy_cc310_rng_api = { |
| 62 | + .get_entropy = entropy_cc310_rng_get_entropy |
| 63 | +}; |
| 64 | + |
| 65 | +DEVICE_AND_API_INIT(entropy_cc310_rng, CONFIG_ENTROPY_NAME, |
| 66 | + &entropy_cc310_rng_init, |
| 67 | + NULL, |
| 68 | + NULL, |
| 69 | + POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, |
| 70 | + &entropy_cc310_rng_api); |
| 71 | + |
| 72 | +#endif /* CONFIG_ENTROPY_CC310 */ |
0 commit comments