mirror of
https://github.com/genodelabs/genode.git
synced 2024-12-24 15:56:41 +00:00
libports: add vfs_jitterentropy library
This file system library uses the the jitterentropy library to provide a rudimentary '/dev/random' device. Fixes #1239.
This commit is contained in:
parent
2f46930824
commit
1a170e9caf
9
repos/libports/lib/mk/vfs_jitterentropy.mk
Normal file
9
repos/libports/lib/mk/vfs_jitterentropy.mk
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
SRC_CC = vfs.cc
|
||||||
|
|
||||||
|
INC_DIR += $(REP_DIR)/src/lib/vfs/jitterentropy
|
||||||
|
|
||||||
|
LIBS += libc jitterentropy
|
||||||
|
|
||||||
|
vpath %.cc $(REP_DIR)/src/lib/vfs/jitterentropy
|
||||||
|
|
||||||
|
SHARED_LIB = yes
|
36
repos/libports/src/lib/vfs/jitterentropy/vfs.cc
Normal file
36
repos/libports/src/lib/vfs/jitterentropy/vfs.cc
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
/*
|
||||||
|
* \brief Jitterentropy based random file system
|
||||||
|
* \author Josef Soentgen
|
||||||
|
* \date 2014-08-19
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2014 Genode Labs GmbH
|
||||||
|
*
|
||||||
|
* This file is part of the Genode OS framework, which is distributed
|
||||||
|
* under the terms of the GNU General Public License version 2.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Genode includes */
|
||||||
|
#include <libc-plugin/vfs.h>
|
||||||
|
|
||||||
|
/* local includes */
|
||||||
|
#include <vfs_jitterentropy.h>
|
||||||
|
|
||||||
|
|
||||||
|
struct Jitterentropy_factory : Libc::File_system_factory
|
||||||
|
{
|
||||||
|
Jitterentropy_factory() : File_system_factory("jitterentropy") { }
|
||||||
|
|
||||||
|
Vfs::File_system *create(Genode::Xml_node node)
|
||||||
|
{
|
||||||
|
return new (Genode::env()->heap()) Jitterentropy_file_system(node);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
extern "C" Libc::File_system_factory *Libc_file_system_factory(void)
|
||||||
|
{
|
||||||
|
static Jitterentropy_factory factory;
|
||||||
|
return &factory;
|
||||||
|
}
|
101
repos/libports/src/lib/vfs/jitterentropy/vfs_jitterentropy.h
Normal file
101
repos/libports/src/lib/vfs/jitterentropy/vfs_jitterentropy.h
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
* \brief Jitterentropy based random file system
|
||||||
|
* \author Josef Soentgen
|
||||||
|
* \date 2014-08-19
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2014 Genode Labs GmbH
|
||||||
|
*
|
||||||
|
* This file is part of the Genode OS framework, which is distributed
|
||||||
|
* under the terms of the GNU General Public License version 2.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _JITTERENTROPY_FILE_SYSTEM_H_
|
||||||
|
#define _JITTERENTROPY_FILE_SYSTEM_H_
|
||||||
|
|
||||||
|
/* Genode includes */
|
||||||
|
#include <util/xml_node.h>
|
||||||
|
#include <vfs/single_file_system.h>
|
||||||
|
|
||||||
|
/* jitterentropy includes */
|
||||||
|
extern "C" {
|
||||||
|
#include <jitterentropy.h>
|
||||||
|
}
|
||||||
|
|
||||||
|
class Jitterentropy_file_system : public Vfs::Single_file_system
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
struct rand_data *_ec_stir;
|
||||||
|
bool _initialized;
|
||||||
|
|
||||||
|
bool _init_jitterentropy()
|
||||||
|
{
|
||||||
|
int err = jent_entropy_init();
|
||||||
|
if (err) {
|
||||||
|
PERR("jitterentropy library could not be initialized!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* use the default behaviour as specified in jitterentropy(3) */
|
||||||
|
_ec_stir = jent_entropy_collector_alloc(0, 0);
|
||||||
|
if (!_ec_stir) {
|
||||||
|
PERR("jitterentropy could not allocate entropy collector!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
Jitterentropy_file_system(Genode::Xml_node config)
|
||||||
|
:
|
||||||
|
Single_file_system(NODE_TYPE_CHAR_DEVICE, name(), config),
|
||||||
|
_ec_stir(0),
|
||||||
|
_initialized(_init_jitterentropy())
|
||||||
|
{ }
|
||||||
|
|
||||||
|
~Jitterentropy_file_system()
|
||||||
|
{
|
||||||
|
if (_initialized)
|
||||||
|
jent_entropy_collector_free(_ec_stir);
|
||||||
|
}
|
||||||
|
|
||||||
|
static char const *name() { return "jitterentropy"; }
|
||||||
|
|
||||||
|
|
||||||
|
/********************************
|
||||||
|
** File I/O service interface **
|
||||||
|
********************************/
|
||||||
|
|
||||||
|
Write_result write(Vfs::Vfs_handle *, char const *, Genode::size_t count,
|
||||||
|
Genode::size_t &count_out) override
|
||||||
|
{
|
||||||
|
return WRITE_ERR_IO;
|
||||||
|
}
|
||||||
|
|
||||||
|
Read_result read(Vfs::Vfs_handle *vfs_handle, char *dst, Genode::size_t count,
|
||||||
|
Genode::size_t &out_count) override
|
||||||
|
{
|
||||||
|
if (!_initialized)
|
||||||
|
return READ_ERR_IO;
|
||||||
|
|
||||||
|
enum { MAX_BUF_LEN = 256 };
|
||||||
|
char buf[MAX_BUF_LEN];
|
||||||
|
|
||||||
|
size_t len = count > MAX_BUF_LEN ? MAX_BUF_LEN : count;
|
||||||
|
|
||||||
|
if (jent_read_entropy(_ec_stir, buf, len) < 0)
|
||||||
|
return READ_ERR_IO;
|
||||||
|
|
||||||
|
Genode::memcpy(dst, buf, len);
|
||||||
|
|
||||||
|
out_count = len;
|
||||||
|
|
||||||
|
return READ_OK;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* _JITTERENTROPY_FILE_SYSTEM_H_ */
|
Loading…
Reference in New Issue
Block a user