yarrow.h
Go to the documentation of this file.
1 /**
2  * @file yarrow.h
3  * @brief Yarrow PRNG
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneSSL Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _YARROW_H
30 #define _YARROW_H
31 
32 //Dependencies
33 #include "core/crypto.h"
34 #include "cipher/aes.h"
35 #include "hash/sha256.h"
36 
37 //Common interface for PRNG algorithms
38 #define YARROW_PRNG_ALGO (&yarrowPrngAlgo)
39 
40 //Pool identifiers
41 #define YARROW_FAST_POOL_ID 0
42 #define YARROW_SLOW_POOL_ID 1
43 
44 //Yarrow PRNG parameters
45 #define YARROW_N 3
46 #define YARROW_K 2
47 #define YARROW_PG 10
48 #define YARROW_FAST_THRESHOLD 100
49 #define YARROW_SLOW_THRESHOLD 160
50 
51 //C++ guard
52 #ifdef __cplusplus
53  extern "C" {
54 #endif
55 
56 
57 /**
58  * @brief Yarrow PRNG context
59  **/
60 
61 typedef struct
62 {
63  OsMutex mutex; //Mutex to prevent simultaneous access to the PRNG state
64  bool_t ready; //This flag tells whether the PRNG has been properly seeded
65  uint_t currentPool[YARROW_N]; //Current pool identifier
66  Sha256Context fastPool; //Fast pool
67  size_t fastPoolEntropy[YARROW_N]; //Entropy estimation (fast pool)
68  Sha256Context slowPool; //Slow pool
69  size_t slowPoolEntropy[YARROW_N]; //Entropy estimation (slow pool)
70  AesContext cipherContext; //Cipher context
71  uint8_t key[32]; //Current key
72  uint8_t counter[16]; //Counter block
73  size_t blockCount; //Number of blocks that have been generated
75 
76 
77 //Yarrow related constants
78 extern const PrngAlgo yarrowPrngAlgo;
79 
80 //Yarrow related functions
82 void yarrowRelease(YarrowContext *context);
83 
84 error_t yarrowSeed(YarrowContext *context, const uint8_t *input, size_t length);
85 
87  const uint8_t *input, size_t length, size_t entropy);
88 
89 error_t yarrowRead(YarrowContext *context, uint8_t *output, size_t length);
90 
91 void yarrowGenerateBlock(YarrowContext *context, uint8_t *output);
92 void yarrowFastReseed(YarrowContext *context);
93 void yarrowSlowReseed(YarrowContext *context);
94 
95 //C++ guard
96 #ifdef __cplusplus
97  }
98 #endif
99 
100 #endif
error_t yarrowSeed(YarrowContext *context, const uint8_t *input, size_t length)
Seed the PRNG state.
Definition: yarrow.c:106
AES algorithm context.
Definition: aes.h:50
size_t blockCount
Definition: yarrow.h:73
void yarrowSlowReseed(YarrowContext *context)
Reseed from the slow pool.
Definition: yarrow.c:316
void yarrowRelease(YarrowContext *context)
Release PRNG context.
Definition: yarrow.c:88
error_t yarrowAddEntropy(YarrowContext *context, uint_t source, const uint8_t *input, size_t length, size_t entropy)
Add entropy to the PRNG state.
Definition: yarrow.c:132
General definitions for cryptographic algorithms.
error_t yarrowInit(YarrowContext *context)
Initialize PRNG context.
Definition: yarrow.c:59
error_t yarrowRead(YarrowContext *context, uint8_t *output, size_t length)
Read random data.
Definition: yarrow.c:204
const PrngAlgo yarrowPrngAlgo
Definition: yarrow.c:41
bool_t ready
Definition: yarrow.h:64
Sha256Context slowPool
Definition: yarrow.h:68
AES (Advanced Encryption Standard)
Yarrow PRNG context.
Definition: yarrow.h:61
#define YARROW_N
Definition: yarrow.h:45
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
Common interface for pseudo-random number generators.
Definition: crypto.h:1091
Mutex object.
SHA-256 algorithm context.
Definition: sha256.h:54
Sha256Context fastPool
Definition: yarrow.h:66
AesContext cipherContext
Definition: yarrow.h:70
void yarrowGenerateBlock(YarrowContext *context, uint8_t *output)
Generate a random block of data.
Definition: yarrow.c:261
uint8_t length
Definition: dtls_misc.h:140
SHA-256 (Secure Hash Algorithm 256)
int bool_t
Definition: compiler_port.h:47
void yarrowFastReseed(YarrowContext *context)
Reseed from the fast pool.
Definition: yarrow.c:283
OsMutex mutex
Definition: yarrow.h:63