tizen 2.4 release
[external/nettle.git] / yarrow.h
1 /* yarrow.h
2  *
3  * The yarrow pseudo-randomness generator.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2001 Niels Möller
9  *  
10  * The nettle library is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or (at your
13  * option) any later version.
14  * 
15  * The nettle library is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
18  * License for more details.
19  * 
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with the nettle library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23  * MA 02111-1301, USA.
24  */
25  
26 #ifndef NETTLE_YARROW_H_INCLUDED
27 #define NETTLE_YARROW_H_INCLUDED
28
29 #include "aes.h"
30 #include "sha2.h"
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /* Name mangling */
37 #define yarrow256_init nettle_yarrow256_init
38 #define yarrow256_seed nettle_yarrow256_seed
39 #define yarrow256_update nettle_yarrow256_update
40 #define yarrow256_random nettle_yarrow256_random
41 #define yarrow256_is_seeded nettle_yarrow256_is_seeded
42 #define yarrow256_needed_sources nettle_yarrow256_needed_sources
43 #define yarrow256_fast_reseed nettle_yarrow256_fast_reseed
44 #define yarrow256_slow_reseed nettle_yarrow256_slow_reseed
45 #define yarrow_key_event_init nettle_yarrow_key_event_init
46 #define yarrow_key_event_estimate nettle_yarrow_key_event_estimate
47
48 /* Obsolete alias for backwards compatibility. Will be deleted in some
49    later version. */
50 #define yarrow256_force_reseed yarrow256_slow_reseed
51   
52 enum yarrow_pool_id { YARROW_FAST = 0, YARROW_SLOW = 1 };
53
54 struct yarrow_source
55 {
56   /* Indexed by yarrow_pool_id */
57   uint32_t estimate[2];
58   
59   /* The pool next sample should go to. */
60   enum yarrow_pool_id next;
61 };
62
63
64 #define YARROW256_SEED_FILE_SIZE (2 * AES_BLOCK_SIZE)
65
66 /* Yarrow-256, based on SHA-256 and AES-256 */
67 struct yarrow256_ctx
68 {
69   /* Indexed by yarrow_pool_id */
70   struct sha256_ctx pools[2];
71
72   int seeded;
73
74   /* The current key and counter block */
75   struct aes_ctx key;
76   uint8_t counter[AES_BLOCK_SIZE];
77
78   /* The entropy sources */
79   unsigned nsources;
80   struct yarrow_source *sources;
81 };
82
83 void
84 yarrow256_init(struct yarrow256_ctx *ctx,
85                unsigned nsources,
86                struct yarrow_source *sources);
87
88 void
89 yarrow256_seed(struct yarrow256_ctx *ctx,
90                unsigned length,
91                const uint8_t *seed_file);
92
93 /* Returns 1 on reseed */
94 int
95 yarrow256_update(struct yarrow256_ctx *ctx,
96                  unsigned source, unsigned entropy,
97                  unsigned length, const uint8_t *data);
98
99 void
100 yarrow256_random(struct yarrow256_ctx *ctx, unsigned length, uint8_t *dst);
101
102 int
103 yarrow256_is_seeded(struct yarrow256_ctx *ctx);
104
105 unsigned
106 yarrow256_needed_sources(struct yarrow256_ctx *ctx);
107
108 void
109 yarrow256_fast_reseed(struct yarrow256_ctx *ctx);
110
111 void
112 yarrow256_slow_reseed(struct yarrow256_ctx *ctx);
113
114
115 /* Key event estimator */
116 #define YARROW_KEY_EVENT_BUFFER 16
117
118 struct yarrow_key_event_ctx
119 {
120   /* Counter for initial priming of the state */
121   unsigned index;
122   unsigned chars[YARROW_KEY_EVENT_BUFFER];
123   unsigned previous;
124 };
125
126 void
127 yarrow_key_event_init(struct yarrow_key_event_ctx *ctx);
128
129 unsigned
130 yarrow_key_event_estimate(struct yarrow_key_event_ctx *ctx,
131                           unsigned key, unsigned time);
132   
133 #ifdef __cplusplus
134 }
135 #endif
136
137 #endif /* NETTLE_YARROW_H_INCLUDED */