Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:02:16

0001 /* yarrow.h
0002 
0003    The yarrow pseudo-randomness generator.
0004 
0005    Copyright (C) 2001 Niels Möller
0006 
0007    This file is part of GNU Nettle.
0008 
0009    GNU Nettle is free software: you can redistribute it and/or
0010    modify it under the terms of either:
0011 
0012      * the GNU Lesser General Public License as published by the Free
0013        Software Foundation; either version 3 of the License, or (at your
0014        option) any later version.
0015 
0016    or
0017 
0018      * the GNU General Public License as published by the Free
0019        Software Foundation; either version 2 of the License, or (at your
0020        option) any later version.
0021 
0022    or both in parallel, as here.
0023 
0024    GNU Nettle is distributed in the hope that it will be useful,
0025    but WITHOUT ANY WARRANTY; without even the implied warranty of
0026    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0027    General Public License for more details.
0028 
0029    You should have received copies of the GNU General Public License and
0030    the GNU Lesser General Public License along with this program.  If
0031    not, see http://www.gnu.org/licenses/.
0032 */
0033  
0034 #ifndef NETTLE_YARROW_H_INCLUDED
0035 #define NETTLE_YARROW_H_INCLUDED
0036 
0037 #include "aes.h"
0038 #include "sha2.h"
0039 
0040 #ifdef __cplusplus
0041 extern "C" {
0042 #endif
0043 
0044 /* Name mangling */
0045 #define yarrow256_init nettle_yarrow256_init
0046 #define yarrow256_seed nettle_yarrow256_seed
0047 #define yarrow256_update nettle_yarrow256_update
0048 #define yarrow256_random nettle_yarrow256_random
0049 #define yarrow256_is_seeded nettle_yarrow256_is_seeded
0050 #define yarrow256_needed_sources nettle_yarrow256_needed_sources
0051 #define yarrow256_fast_reseed nettle_yarrow256_fast_reseed
0052 #define yarrow256_slow_reseed nettle_yarrow256_slow_reseed
0053 #define yarrow_key_event_init nettle_yarrow_key_event_init
0054 #define yarrow_key_event_estimate nettle_yarrow_key_event_estimate
0055 
0056 /* Obsolete alias for backwards compatibility. Will be deleted in some
0057    later version. */
0058 #define yarrow256_force_reseed yarrow256_slow_reseed
0059   
0060 enum yarrow_pool_id { YARROW_FAST = 0, YARROW_SLOW = 1 };
0061 
0062 struct yarrow_source
0063 {
0064   /* Indexed by yarrow_pool_id */
0065   uint32_t estimate[2];
0066   
0067   /* The pool next sample should go to. */
0068   enum yarrow_pool_id next;
0069 };
0070 
0071 
0072 #define YARROW256_SEED_FILE_SIZE (2 * AES_BLOCK_SIZE)
0073 
0074 /* Yarrow-256, based on SHA-256 and AES-256 */
0075 struct yarrow256_ctx
0076 {
0077   /* Indexed by yarrow_pool_id */
0078   struct sha256_ctx pools[2];
0079 
0080   int seeded;
0081 
0082   /* The current key and counter block */
0083   struct aes256_ctx key;
0084   uint8_t counter[AES_BLOCK_SIZE];
0085 
0086   /* The entropy sources */
0087   unsigned nsources;
0088   struct yarrow_source *sources;
0089 };
0090 
0091 void
0092 yarrow256_init(struct yarrow256_ctx *ctx,
0093            unsigned nsources,
0094            struct yarrow_source *sources);
0095 
0096 void
0097 yarrow256_seed(struct yarrow256_ctx *ctx,
0098            size_t length,
0099            const uint8_t *seed_file);
0100 
0101 /* Returns 1 on reseed */
0102 int
0103 yarrow256_update(struct yarrow256_ctx *ctx,
0104          unsigned source, unsigned entropy,
0105          size_t length, const uint8_t *data);
0106 
0107 void
0108 yarrow256_random(struct yarrow256_ctx *ctx, size_t length, uint8_t *dst);
0109 
0110 int
0111 yarrow256_is_seeded(struct yarrow256_ctx *ctx);
0112 
0113 unsigned
0114 yarrow256_needed_sources(struct yarrow256_ctx *ctx);
0115 
0116 void
0117 yarrow256_fast_reseed(struct yarrow256_ctx *ctx);
0118 
0119 void
0120 yarrow256_slow_reseed(struct yarrow256_ctx *ctx);
0121 
0122 
0123 /* Key event estimator */
0124 #define YARROW_KEY_EVENT_BUFFER 16
0125 
0126 struct yarrow_key_event_ctx
0127 {
0128   /* Counter for initial priming of the state */
0129   unsigned index;
0130   unsigned chars[YARROW_KEY_EVENT_BUFFER];
0131   unsigned previous;
0132 };
0133 
0134 void
0135 yarrow_key_event_init(struct yarrow_key_event_ctx *ctx);
0136 
0137 unsigned
0138 yarrow_key_event_estimate(struct yarrow_key_event_ctx *ctx,
0139               unsigned key, unsigned time);
0140   
0141 #ifdef __cplusplus
0142 }
0143 #endif
0144 
0145 #endif /* NETTLE_YARROW_H_INCLUDED */