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-2024 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 2.4.0
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 
85 error_t yarrowSeed(YarrowContext *context, const uint8_t *input, size_t length);
86 
88  const uint8_t *input, size_t length, size_t entropy);
89 
90 error_t yarrowRead(YarrowContext *context, uint8_t *output, size_t length);
91 
92 void yarrowGenerateBlock(YarrowContext *context, uint8_t *output);
93 void yarrowFastReseed(YarrowContext *context);
94 void yarrowSlowReseed(YarrowContext *context);
95 
96 void yarrowDeinit(YarrowContext *context);
97 
98 //C++ guard
99 #ifdef __cplusplus
100 }
101 #endif
102 
103 #endif
AES (Advanced Encryption Standard)
unsigned int uint_t
Definition: compiler_port.h:50
int bool_t
Definition: compiler_port.h:53
General definitions for cryptographic algorithms.
#define PrngAlgo
Definition: crypto.h:917
error_t
Error codes.
Definition: error.h:43
SHA-256 (Secure Hash Algorithm 256)
AES algorithm context.
Definition: aes.h:58
Mutex object.
SHA-256 algorithm context.
Definition: sha256.h:62
Yarrow PRNG context.
Definition: yarrow.h:64
size_t blockCount
Definition: yarrow.h:75
Sha256Context slowPool
Definition: yarrow.h:70
Sha256Context fastPool
Definition: yarrow.h:68
bool_t ready
Definition: yarrow.h:66
AesContext cipherContext
Definition: yarrow.h:72
OsMutex mutex
Definition: yarrow.h:65
uint8_t length
Definition: tcp.h:368
error_t yarrowRead(YarrowContext *context, uint8_t *output, size_t length)
Read random data.
Definition: yarrow.c:199
#define YARROW_N
Definition: yarrow.h:47
void yarrowFastReseed(YarrowContext *context)
Reseed from the fast pool.
Definition: yarrow.c:282
error_t yarrowSeed(YarrowContext *context, const uint8_t *input, size_t length)
Seed the PRNG state.
Definition: yarrow.c:93
error_t yarrowInit(YarrowContext *context)
Initialize PRNG context.
Definition: yarrow.c:61
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:119
const PrngAlgo yarrowPrngAlgo
Definition: yarrow.c:43
void yarrowDeinit(YarrowContext *context)
Release PRNG context.
Definition: yarrow.c:370
void yarrowGenerateBlock(YarrowContext *context, uint8_t *output)
Generate a random block of data.
Definition: yarrow.c:258
void yarrowSlowReseed(YarrowContext *context)
Reseed from the slow pool.
Definition: yarrow.c:323