1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #7421 from OTAkeys/pr/dev_urandom

devfs: add /dev/urandom and /dev/hwrng
This commit is contained in:
Sebastian Meiling 2019-06-26 21:33:32 +02:00 committed by GitHub
commit 4f5ce88ba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 179 additions and 0 deletions

View File

@ -880,6 +880,15 @@ endif
ifneq (,$(filter periph_gpio_irq,$(USEMODULE)))
FEATURES_REQUIRED += periph_gpio
endif
ifneq (,$(filter devfs_hwrng,$(USEMODULE)))
FEATURES_REQUIRED += periph_hwrng
endif
ifneq (,$(filter devfs_random,$(USEMODULE)))
USEMODULE += random
endif
# always select gpio (until explicit dependencies are sorted out)
FEATURES_OPTIONAL += periph_gpio

View File

@ -9,6 +9,7 @@ PSEUDOMODULES += cord_ep_standalone
PSEUDOMODULES += core_%
PSEUDOMODULES += cortexm_fpu
PSEUDOMODULES += cpu_check_address
PSEUDOMODULES += devfs_%
PSEUDOMODULES += ecc_%
PSEUDOMODULES += emb6_router
PSEUDOMODULES += event_%

View File

@ -21,6 +21,7 @@
#include "vfs.h"
#include "fs/devfs.h"
#include "random-vfs.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
@ -30,8 +31,30 @@ static vfs_mount_t _devfs_auto_init_mount = {
.mount_point = "/dev",
};
#ifdef MODULE_DEVFS_HWRNG
static devfs_t hwrng_devfs = {
.path = "/hwrng",
.f_op = &hwrng_vfs_ops,
};
#endif
#ifdef MODULE_DEVFS_RANDOM
static devfs_t random_devfs = {
.path = "/urandom",
.f_op = &random_vfs_ops,
};
#endif
void auto_init_devfs(void)
{
DEBUG("auto_init_devfs: mounting /dev\n");
vfs_mount(&_devfs_auto_init_mount);
#ifdef MODULE_DEVFS_HWRNG
devfs_register(&hwrng_devfs);
#endif
#ifdef MODULE_DEVFS_RANDOM
devfs_register(&random_devfs);
#endif
}

61
sys/fs/devfs/random-vfs.c Normal file
View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2017 OTA keys S.A.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
*/
/**
* @ingroup sys_fs_devfs
* @{
*
* @file
* @brief Random backends for devfs implementation
*
* @author Vincent Dupont <vincent@otakeys.com>
*
* @}
*/
#include "vfs.h"
#ifdef MODULE_DEVFS_HWRNG
#include "periph/hwrng.h"
static ssize_t hwrng_vfs_read(vfs_file_t *filp, void *dest, size_t nbytes);
const vfs_file_ops_t hwrng_vfs_ops = {
.read = hwrng_vfs_read,
};
static ssize_t hwrng_vfs_read(vfs_file_t *filp, void *dest, size_t nbytes)
{
(void)filp;
hwrng_read(dest, nbytes);
return nbytes;
}
#endif /* MODULE_PERIPH_HWRNG */
#ifdef MODULE_DEVFS_RANDOM
#include "random.h"
static ssize_t random_vfs_read(vfs_file_t *filp, void *dest, size_t nbytes);
const vfs_file_ops_t random_vfs_ops = {
.read = random_vfs_read,
};
static ssize_t random_vfs_read(vfs_file_t *filp, void *dest, size_t nbytes)
{
(void)filp;
random_bytes(dest, nbytes);
return nbytes;
}
#endif /* MODULE_RANDOM */

48
sys/fs/devfs/random-vfs.h Normal file
View File

@ -0,0 +1,48 @@
/*
* Copyright (C) 2017 OTA keys S.A.
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*
*/
/**
* @ingroup sys_fs_devfs
* @{
*
* @file
* @brief Random backends for devfs
*
* @author Vincent Dupont <vincent@otakeys.com>
*/
#ifndef RANDOM_VFS_H
#define RANDOM_VFS_H
#include "vfs.h"
#ifdef __cplusplus
extern "C" {
#endif
#if defined(MODULE_PERIPH_HWRNG) || defined(DOXYGEN)
/**
* @brief hwrng driver for vfs
*/
extern const vfs_file_ops_t hwrng_vfs_ops;
#endif
#if defined(MODULE_RANDOM) || defined(DOXYGEN)
/**
* @brief urandom driver for vfs
*/
extern const vfs_file_ops_t random_vfs_ops;
#endif
#ifdef __cplusplus
}
#endif
#endif /* RANDOM_VFS_H */
/** @} */

View File

@ -1,2 +1,4 @@
USEMODULE += vfs
USEMODULE += devfs
USEMODULE += devfs_random
USEMODULE += devfs_hwrng

View File

@ -18,8 +18,10 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include "fs/devfs.h"
#include "random.h"
#include "embUnit/embUnit.h"
@ -141,11 +143,39 @@ static void test_devfs_mount_open(void)
TEST_ASSERT_EQUAL_INT(0, res);
}
static void test_devfs_urandom(void)
{
const uint8_t zeroes[8] = { 0 };
int res;
int fd = vfs_open("/dev/urandom", O_RDONLY, 0);
TEST_ASSERT(fd >= 0);
uint8_t buf[8] = { 0 };
res = vfs_read(fd, buf, sizeof(buf));
TEST_ASSERT_EQUAL_INT(sizeof(buf), res);
TEST_ASSERT(memcmp(zeroes, buf, sizeof(buf)));
}
static void test_devfs_hwrng(void)
{
const uint8_t zeroes[8] = { 0 };
int res;
int fd = vfs_open("/dev/hwrng", O_RDONLY, 0);
TEST_ASSERT(fd >= 0);
uint8_t buf[8] = { 0 };
res = vfs_read(fd, buf, sizeof(buf));
TEST_ASSERT_EQUAL_INT(sizeof(buf), res);
TEST_ASSERT(memcmp(zeroes, buf, sizeof(buf)));
}
Test *tests_devfs_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_devfs_register),
new_TestFixture(test_devfs_mount_open),
new_TestFixture(test_devfs_urandom),
new_TestFixture(test_devfs_hwrng),
};
EMB_UNIT_TESTCALLER(devfs_tests, NULL, NULL, fixtures);
@ -155,6 +185,11 @@ Test *tests_devfs_tests(void)
void tests_devfs(void)
{
extern void auto_init_devfs(void);
auto_init_devfs();
random_init(1);
TESTS_RUN(tests_devfs_tests());
}
/** @} */