blob: f52eeaa5637d081cc8520c5a2594954ec0243ea3 [file] [log] [blame]
julhal012c18fbf2021-02-01 08:29:28 +00001/*
2 * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6#include <platform/interface/trng.h>
7#include <platform/interface/device_region.h>
8#include <tztrng.h>
9#include <tztrng_defs.h>
10#include <stdlib.h>
11#include <limits.h>
12
13/*
14 * A platform trng driver that uses the tz-trng driver to provide a
15 * hardware entropy source.
16 */
17struct tztrng_instance
18{
19 struct device_region trng_device_region;
20};
21
22
23static int trng_poll(void *context, unsigned char *output, size_t nbyte, size_t *len)
24{
25 struct tztrng_instance *this_instance = (struct tztrng_instance*)context;
26 int status = 0;
27
28 *len = 0;
29
30 if (nbyte >= sizeof(unsigned char)) {
31
32 if (this_instance) {
33
34 status = CC_TrngGetSource((unsigned long)this_instance->trng_device_region.base_addr,
35 output, len, nbyte * CHAR_BIT);
36 }
37 else {
38 /* No context for TRNG instance */
39 /* status = LLF_RND_STATE_PTR_INVALID_ERROR; @todo mbedcrypto segfaults when an error is returned */
40 *len = sizeof(unsigned char);
41 }
42 }
43
44 return status;
45}
46
47int platform_trng_create(struct platform_trng_driver *driver,
48 const struct device_region *device_region)
49{
50 static const struct platform_trng_iface iface = { .poll = trng_poll };
51
52 /*
53 * Default to leaving the driver in a safe but inoperable state.
54 */
55 driver->iface = &iface;
56 driver->context = NULL;
57
58 if (device_region) {
59
60 /*
61 * A device region has been provided, possibly from an external configuation.
62 * Check that it's a sensible size to defend against a bogus configuration.
63 */
64 struct tztrng_instance *new_instance = malloc(sizeof(struct tztrng_instance));
65
66 if (new_instance) {
67
68 new_instance->trng_device_region = *device_region;
69 driver->context = new_instance;
70 }
71 }
72
73 return 0;
74}
75
76void platform_trng_destroy(struct platform_trng_driver *driver)
77{
78 free(driver->context);
79}