aboutsummaryrefslogtreecommitdiff
path: root/components/service/crypto/provider/mbedcrypto/trng_adapter/platform/platform_trng_adapter.c
blob: 1b9f1d2c29999db2f79b02e58a0224466040ef63 (plain)
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
/*
 * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
#include <mbedtls/entropy.h>
#include <platform/interface/trng.h>
#include <service/crypto/provider/mbedcrypto/trng_adapter/trng_adapter.h>
#include <config/interface/platform_config.h>
#include <stddef.h>

/*
 * An mbed tls compatibile hardware entropy source that adapts the mbed tls hardware poll
 * function to a platform trng driver.  The actual realization of the driver
 * will depend on the platform selected at build-time.
 */
static struct platform_trng_driver driver = {0};

int trng_adapter_init(int instance)
{
    int status;
    struct device_region *device_region;

    device_region = platform_config_device_query("trng", instance);
    status = platform_trng_create(&driver, device_region);
    platform_config_device_query_free(device_region);

    return status;
}

void trng_adapter_deinit()
{
    platform_trng_destroy(&driver);

    driver.iface = NULL;
    driver.context = NULL;
}

int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen)
{
    int status = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
    *olen = 0;

    if (driver.iface) {

        status = driver.iface->poll(driver.context, output, len, olen);
    }

    return status;
}