Commit e2b8f5d5 authored by Philipp Schafft's avatar Philipp Schafft 🦁 Committed by Philipp Schafft
Browse files

Update: Made public PRNG function take a flags argument

parent 7809d576
......@@ -30,20 +30,22 @@ extern "C" {
typedef uint64_t igloo_prng_flags_t;
#define igloo_PRNG_FLAG_NONE ((igloo_prng_flags_t)0)
/* Change options */
igloo_error_t igloo_prng_configure(igloo_ro_t instance, igloo_prng_flags_t addflags, igloo_prng_flags_t removeflags, ssize_t overcommitment);
/* Run a reseed now, useful to be run at times of idle */
igloo_error_t igloo_prng_auto_reseed(igloo_ro_t instance);
igloo_error_t igloo_prng_auto_reseed(igloo_ro_t instance, igloo_prng_flags_t flags);
/* write data to the PRNG to seed it */
igloo_error_t igloo_prng_write(igloo_ro_t instance, const void *buffer, size_t len, ssize_t bits);
igloo_error_t igloo_prng_write(igloo_ro_t instance, const void *buffer, size_t len, ssize_t bits, igloo_prng_flags_t flags);
/* read pseudo random bytes from the PRNG */
ssize_t igloo_prng_read(igloo_ro_t instance, void *buffer, size_t len);
ssize_t igloo_prng_read(igloo_ro_t instance, void *buffer, size_t len, igloo_prng_flags_t flags);
/* write len pseudo random bytes to a file. If len is -1 a default value is used. */
igloo_error_t igloo_prng_write_file(igloo_ro_t instance, const char *filename, ssize_t len);
igloo_error_t igloo_prng_write_file(igloo_ro_t instance, const char *filename, ssize_t len, igloo_prng_flags_t flags);
/* read at max len bytes from the file and see the PRNG with it. if len is -1 all of the file is read. */
igloo_error_t igloo_prng_read_file(igloo_ro_t instance, const char *filename, ssize_t len, ssize_t bits);
igloo_error_t igloo_prng_read_file(igloo_ro_t instance, const char *filename, ssize_t len, ssize_t bits, igloo_prng_flags_t flags);
#ifdef __cplusplus
}
......
......@@ -300,13 +300,15 @@ igloo_error_t igloo_prng_auto_reseed_unlocked(igloo_prng_state_t *self, const vo
return igloo_ERROR_NONE;
}
igloo_error_t igloo_prng_auto_reseed(igloo_ro_t instance)
igloo_error_t igloo_prng_auto_reseed(igloo_ro_t instance, igloo_prng_flags_t flags)
{
igloo_prng_state_t *self;
size_t instancelen;
igloo_error_t error = get_inited_state(&self, &instancelen, instance);
const void *instanceraw = igloo_ro_to_type(instance, igloo_ro_stub_t);
(void)flags;
if (error != igloo_ERROR_NONE)
return error;
......@@ -317,7 +319,7 @@ igloo_error_t igloo_prng_auto_reseed(igloo_ro_t instance)
return error;
}
igloo_error_t igloo_prng_write(igloo_ro_t instance, const void *buffer, size_t len, ssize_t bits)
igloo_error_t igloo_prng_write(igloo_ro_t instance, const void *buffer, size_t len, ssize_t bits, igloo_prng_flags_t flags)
{
igloo_prng_state_t *self;
size_t instancelen;
......@@ -325,6 +327,8 @@ igloo_error_t igloo_prng_write(igloo_ro_t instance, const void *buffer, size_t l
const void *instanceraw;
igloo_prng_buffer_extra_t extra;
(void)flags;
if (bits > (ssize_t)(len*8))
return igloo_ERROR_INVAL;
......@@ -376,7 +380,7 @@ static igloo_error_t igloo_prng_read__inner(igloo_prng_state_t *self, const void
return digest_instance_extra_once(instanceraw, instancelen, instance, extra, NULL, 0, self->last_result);
}
ssize_t igloo_prng_read(igloo_ro_t instance, void *buffer, size_t len)
ssize_t igloo_prng_read(igloo_ro_t instance, void *buffer, size_t len, igloo_prng_flags_t flags)
{
igloo_prng_state_t *self;
size_t instancelen;
......@@ -386,6 +390,8 @@ ssize_t igloo_prng_read(igloo_ro_t instance, void *buffer, size_t len)
size_t bitswant;
igloo_prng_buffer_extra_t extra;
(void)flags;
if (error != igloo_ERROR_NONE)
return -1;
......@@ -434,7 +440,7 @@ ssize_t igloo_prng_read(igloo_ro_t instance, void *buffer, size_t len)
}
/* write len pseudo random bytes to a file. If len is -1 a default value is used. */
igloo_error_t igloo_prng_write_file(igloo_ro_t instance, const char *filename, ssize_t len);
igloo_error_t igloo_prng_write_file(igloo_ro_t instance, const char *filename, ssize_t len, igloo_prng_flags_t flags);
/* read at max len bytes from the file and see the PRNG with it. if len is -1 all of the file is read. */
igloo_error_t igloo_prng_read_file(igloo_ro_t instance, const char *filename, ssize_t len, ssize_t bits);
igloo_error_t igloo_prng_read_file(igloo_ro_t instance, const char *filename, ssize_t len, ssize_t bits, igloo_prng_flags_t flags);
......@@ -47,15 +47,15 @@ int main (void) {
}
dump();
igloo_tap_test_success("igloo_prng_auto_reseed", igloo_prng_auto_reseed(g_instance));
igloo_tap_test_success("igloo_prng_auto_reseed", igloo_prng_auto_reseed(g_instance, igloo_PRNG_FLAG_NONE));
dump();
memset(buf, 0, sizeof(buf));
igloo_tap_test_success("igloo_prng_write", igloo_prng_write(g_instance, buf, sizeof(buf), sizeof(buf)*8));
igloo_tap_test_success("igloo_prng_write", igloo_prng_write(g_instance, buf, sizeof(buf), sizeof(buf)*8, igloo_PRNG_FLAG_NONE));
for (iter = 0; iter < 16; iter++) {
dump();
res = igloo_prng_read(g_instance, buf, sizeof(buf));
res = igloo_prng_read(g_instance, buf, sizeof(buf), igloo_PRNG_FLAG_NONE);
igloo_tap_test("igloo_prng_read", res == (ssize_t)sizeof(buf));
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment