2024-04-12 19:34:46 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2024 Google LLC
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <zephyr/drivers/entropy.h>
|
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
#include <zephyr/posix/unistd.h>
|
|
|
|
|
2024-05-23 07:44:17 +08:00
|
|
|
#define ENTROPY_NODE DEVICE_DT_GET_OR_NULL(DT_CHOSEN(zephyr_entropy))
|
2024-04-12 19:34:46 +08:00
|
|
|
|
|
|
|
int getentropy(void *buffer, size_t length)
|
|
|
|
{
|
2024-05-23 07:44:17 +08:00
|
|
|
const struct device *const entropy = ENTROPY_NODE;
|
2024-04-12 19:34:46 +08:00
|
|
|
|
|
|
|
if (!buffer) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 256) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-05-23 07:44:17 +08:00
|
|
|
if (entropy == NULL || !device_is_ready(entropy)) {
|
2024-04-12 19:34:46 +08:00
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* getentropy() uses size_t to represent buffer size, but Zephyr uses
|
|
|
|
* uint16_t. The length check above allows us to safely cast without
|
|
|
|
* overflow.
|
|
|
|
*/
|
|
|
|
if (entropy_get_entropy(entropy, buffer, (uint16_t)length)) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|