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