yarrow.h 3.49 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2 3 4 5 6 7
/* yarrow.h
 *
 * The yarrow pseudo-randomness generator.
 */

/* nettle, low-level cryptographics library
 *
Niels Möller's avatar
Niels Möller committed
8
 * Copyright (C) 2001 Niels Möller
Niels Möller's avatar
Niels Möller committed
9 10 11 12 13 14 15 16 17 18 19 20 21
 *  
 * The nettle library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 * 
 * The nettle library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with the nettle library; see the file COPYING.LIB.  If not, write to
22 23
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02111-1301, USA.
Niels Möller's avatar
Niels Möller committed
24 25
 */
 
Niels Möller's avatar
Niels Möller committed
26 27
#ifndef NETTLE_YARROW_H_INCLUDED
#define NETTLE_YARROW_H_INCLUDED
Niels Möller's avatar
Niels Möller committed
28

29
#include "aes.h"
30
#include "sha2.h"
Niels Möller's avatar
Niels Möller committed
31

Niels Möller's avatar
Niels Möller committed
32 33 34 35
#ifdef __cplusplus
extern "C" {
#endif

36 37 38 39 40 41 42
/* Name mangling */
#define yarrow256_init nettle_yarrow256_init
#define yarrow256_seed nettle_yarrow256_seed
#define yarrow256_update nettle_yarrow256_update
#define yarrow256_random nettle_yarrow256_random
#define yarrow256_is_seeded nettle_yarrow256_is_seeded
#define yarrow256_needed_sources nettle_yarrow256_needed_sources
43 44
#define yarrow256_fast_reseed nettle_yarrow256_fast_reseed
#define yarrow256_slow_reseed nettle_yarrow256_slow_reseed
45 46 47
#define yarrow_key_event_init nettle_yarrow_key_event_init
#define yarrow_key_event_estimate nettle_yarrow_key_event_estimate

48 49 50 51
/* Obsolete alias for backwards compatibility. Will be deleted in some
   later version. */
#define yarrow256_force_reseed yarrow256_slow_reseed
  
Niels Möller's avatar
Niels Möller committed
52 53 54 55
enum yarrow_pool_id { YARROW_FAST = 0, YARROW_SLOW = 1 };

struct yarrow_source
{
56 57
  /* Indexed by yarrow_pool_id */
  uint32_t estimate[2];
Niels Möller's avatar
Niels Möller committed
58 59 60 61 62
  
  /* The pool next sample should go to. */
  enum yarrow_pool_id next;
};

Niels Möller's avatar
Niels Möller committed
63

64
#define YARROW256_SEED_FILE_SIZE (2 * AES_BLOCK_SIZE)
Niels Möller's avatar
Niels Möller committed
65

66 67 68 69 70 71 72 73
/* Yarrow-256, based on SHA-256 and AES-256 */
struct yarrow256_ctx
{
  /* Indexed by yarrow_pool_id */
  struct sha256_ctx pools[2];

  int seeded;

74
  /* The current key and counter block */
75 76 77 78 79 80 81 82 83 84
  struct aes_ctx key;
  uint8_t counter[AES_BLOCK_SIZE];

  /* The entropy sources */
  unsigned nsources;
  struct yarrow_source *sources;
};

void
yarrow256_init(struct yarrow256_ctx *ctx,
85
	       unsigned nsources,
86 87 88
	       struct yarrow_source *sources);

void
89 90
yarrow256_seed(struct yarrow256_ctx *ctx,
	       unsigned length,
91
	       const uint8_t *seed_file);
92 93 94

/* Returns 1 on reseed */
int
95 96 97 98 99 100 101 102
yarrow256_update(struct yarrow256_ctx *ctx,
		 unsigned source, unsigned entropy,
		 unsigned length, const uint8_t *data);

void
yarrow256_random(struct yarrow256_ctx *ctx, unsigned length, uint8_t *dst);

int
103
yarrow256_is_seeded(struct yarrow256_ctx *ctx);
104

105 106
unsigned
yarrow256_needed_sources(struct yarrow256_ctx *ctx);
107

108
void
109 110 111 112
yarrow256_fast_reseed(struct yarrow256_ctx *ctx);

void
yarrow256_slow_reseed(struct yarrow256_ctx *ctx);
113 114


115
/* Key event estimator */
116 117
#define YARROW_KEY_EVENT_BUFFER 16

118 119 120 121
struct yarrow_key_event_ctx
{
  /* Counter for initial priming of the state */
  unsigned index;
122
  unsigned chars[YARROW_KEY_EVENT_BUFFER];
123 124
  unsigned previous;
};
Niels Möller's avatar
Niels Möller committed
125

126 127 128 129 130 131 132
void
yarrow_key_event_init(struct yarrow_key_event_ctx *ctx);

unsigned
yarrow_key_event_estimate(struct yarrow_key_event_ctx *ctx,
			  unsigned key, unsigned time);
  
Niels Möller's avatar
Niels Möller committed
133 134 135 136
#ifdef __cplusplus
}
#endif

Niels Möller's avatar
Niels Möller committed
137
#endif /* NETTLE_YARROW_H_INCLUDED */