Commit 607095de authored by Niels Möller's avatar Niels Möller

(yarrow256_seed): New function.

(yarrow_fast_reseed): Create new seed file contents.

Rev: src/nettle/yarrow256.c:1.11
parent 6b499209
...@@ -28,10 +28,14 @@ ...@@ -28,10 +28,14 @@
#include "macros.h" #include "macros.h"
#include <assert.h> #include <assert.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
/* #define YARROW_DEBUG */ #ifndef YARROW_DEBUG
#ifdef YARROW_DEBUG #define YARROW_DEBUG 0
#endif
#if YARROW_DEBUG
#include <stdio.h> #include <stdio.h>
#endif #endif
...@@ -60,6 +64,14 @@ ...@@ -60,6 +64,14 @@
* overflows. */ * overflows. */
#define YARROW_MAX_ENTROPY 0x100000 #define YARROW_MAX_ENTROPY 0x100000
/* Forward declarations */
static void
yarrow_fast_reseed(struct yarrow256_ctx *ctx);
static void
yarrow_gate(struct yarrow256_ctx *ctx);
void void
yarrow256_init(struct yarrow256_ctx *ctx, yarrow256_init(struct yarrow256_ctx *ctx,
unsigned n, unsigned n,
...@@ -83,6 +95,23 @@ yarrow256_init(struct yarrow256_ctx *ctx, ...@@ -83,6 +95,23 @@ yarrow256_init(struct yarrow256_ctx *ctx,
} }
} }
void
yarrow256_seed(struct yarrow256_ctx *ctx,
unsigned length,
uint8_t *seed_file)
{
/* FIXME: Perhaps it's better to use assert ? */
if (!length)
return;
sha256_update(&ctx->pools[YARROW_FAST], length, seed_file);
yarrow_fast_reseed(ctx);
ctx->seeded = 1;
}
/* FIXME: Generalize so that it generates a few more blocks at a
* time. */
static void static void
yarrow_generate_block(struct yarrow256_ctx *ctx, yarrow_generate_block(struct yarrow256_ctx *ctx,
uint8_t *block) uint8_t *block)
...@@ -142,7 +171,7 @@ yarrow_fast_reseed(struct yarrow256_ctx *ctx) ...@@ -142,7 +171,7 @@ yarrow_fast_reseed(struct yarrow256_ctx *ctx)
uint8_t digest[SHA256_DIGEST_SIZE]; uint8_t digest[SHA256_DIGEST_SIZE];
unsigned i; unsigned i;
#ifdef YARROW_DEBUG #if YARROW_DEBUG
fprintf(stderr, "yarrow_fast_reseed\n"); fprintf(stderr, "yarrow_fast_reseed\n");
#endif #endif
...@@ -173,6 +202,13 @@ yarrow_fast_reseed(struct yarrow256_ctx *ctx) ...@@ -173,6 +202,13 @@ yarrow_fast_reseed(struct yarrow256_ctx *ctx)
/* Reset estimates. */ /* Reset estimates. */
for (i = 0; i<ctx->nsources; i++) for (i = 0; i<ctx->nsources; i++)
ctx->sources[i].estimate[YARROW_FAST] = 0; ctx->sources[i].estimate[YARROW_FAST] = 0;
/* New seed file. */
/* FIXME: Extract this into a function of its own. */
for (i = 0; i < sizeof(ctx->seed_file); i+= AES_BLOCK_SIZE)
yarrow_generate_block(ctx, ctx->seed_file + i);
yarrow_gate(ctx);
} }
static void static void
...@@ -181,7 +217,7 @@ yarrow_slow_reseed(struct yarrow256_ctx *ctx) ...@@ -181,7 +217,7 @@ yarrow_slow_reseed(struct yarrow256_ctx *ctx)
uint8_t digest[SHA256_DIGEST_SIZE]; uint8_t digest[SHA256_DIGEST_SIZE];
unsigned i; unsigned i;
#ifdef YARROW_DEBUG #if YARROW_DEBUG
fprintf(stderr, "yarrow_slow_reseed\n"); fprintf(stderr, "yarrow_slow_reseed\n");
#endif #endif
...@@ -201,7 +237,7 @@ yarrow_slow_reseed(struct yarrow256_ctx *ctx) ...@@ -201,7 +237,7 @@ yarrow_slow_reseed(struct yarrow256_ctx *ctx)
ctx->sources[i].estimate[YARROW_SLOW] = 0; ctx->sources[i].estimate[YARROW_SLOW] = 0;
} }
void int
yarrow256_update(struct yarrow256_ctx *ctx, yarrow256_update(struct yarrow256_ctx *ctx,
unsigned source_index, unsigned entropy, unsigned source_index, unsigned entropy,
unsigned length, const uint8_t *data) unsigned length, const uint8_t *data)
...@@ -213,7 +249,7 @@ yarrow256_update(struct yarrow256_ctx *ctx, ...@@ -213,7 +249,7 @@ yarrow256_update(struct yarrow256_ctx *ctx,
if (!length) if (!length)
/* Nothing happens */ /* Nothing happens */
return; return 0;
source = &ctx->sources[source_index]; source = &ctx->sources[source_index];
...@@ -252,16 +288,20 @@ yarrow256_update(struct yarrow256_ctx *ctx, ...@@ -252,16 +288,20 @@ yarrow256_update(struct yarrow256_ctx *ctx,
switch(current) switch(current)
{ {
case YARROW_FAST: case YARROW_FAST:
if (source->estimate[YARROW_FAST] >= YARROW_FAST_THRESHOLD) #if YARROW_DEBUG
yarrow_fast_reseed(ctx);
#ifdef YARROW_DEBUG
fprintf(stderr, fprintf(stderr,
"yarrow256_update: source_index = %d,\n" "yarrow256_update: source_index = %d,\n"
" fast pool estimate = %d\n", " fast pool estimate = %d\n",
source_index, source->estimate[YARROW_FAST]); source_index, source->estimate[YARROW_FAST]);
#endif #endif
break; if (source->estimate[YARROW_FAST] >= YARROW_FAST_THRESHOLD)
{
yarrow_fast_reseed(ctx);
return 1;
}
else
return 0;
case YARROW_SLOW: case YARROW_SLOW:
{ {
/* FIXME: This is somewhat inefficient. It would be better to /* FIXME: This is somewhat inefficient. It would be better to
...@@ -272,7 +312,7 @@ yarrow256_update(struct yarrow256_ctx *ctx, ...@@ -272,7 +312,7 @@ yarrow256_update(struct yarrow256_ctx *ctx,
if (ctx->sources[i].estimate[YARROW_SLOW] >= YARROW_SLOW_THRESHOLD) if (ctx->sources[i].estimate[YARROW_SLOW] >= YARROW_SLOW_THRESHOLD)
k++; k++;
#ifdef YARROW_DEBUG #if YARROW_DEBUG
fprintf(stderr, fprintf(stderr,
"yarrow256_update: source_index = %d,\n" "yarrow256_update: source_index = %d,\n"
" slow pool estimate = %d,\n" " slow pool estimate = %d,\n"
...@@ -284,8 +324,14 @@ yarrow256_update(struct yarrow256_ctx *ctx, ...@@ -284,8 +324,14 @@ yarrow256_update(struct yarrow256_ctx *ctx,
{ {
yarrow_slow_reseed(ctx); yarrow_slow_reseed(ctx);
ctx->seeded = 1; ctx->seeded = 1;
return 1;
} }
else
return 0;
} }
default:
abort();
} }
} }
......
Markdown is supported
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