diff options
Diffstat (limited to 'components/service/crypto/provider/mbedcrypto/entropy_adapter/linux/linux_entropy_adapter.c')
-rw-r--r-- | components/service/crypto/provider/mbedcrypto/entropy_adapter/linux/linux_entropy_adapter.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/components/service/crypto/provider/mbedcrypto/entropy_adapter/linux/linux_entropy_adapter.c b/components/service/crypto/provider/mbedcrypto/entropy_adapter/linux/linux_entropy_adapter.c new file mode 100644 index 000000000..83899b162 --- /dev/null +++ b/components/service/crypto/provider/mbedcrypto/entropy_adapter/linux/linux_entropy_adapter.c @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ +#include <mbedtls/entropy.h> +#include <mbedtls/entropy_poll.h> +#include <service/crypto/provider/mbedcrypto/entropy_adapter/entropy_adapter.h> +#include <errno.h> +#include <sys/syscall.h> +#include <unistd.h> + +/* + * An mbed tls compatibile hardware entropy source that adapts the mbed tls hardware poll + * function to the Linux getrandom system call. + */ + +int entropy_adapter_init(void *config) +{ + (void)config; + return 0; +} + +void entropy_adapter_deinit(void) +{ + +} + +int mbedtls_hardware_poll(void *data, unsigned char *output, size_t len, size_t *olen) +{ + int status = MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; + *olen = 0; + + int num_output = syscall(SYS_getrandom, output, len, 0); + + if (num_output >= 0) { + + *olen = num_output; + status = 0; + } + + return status; +} |