hash_drbg.h
Go to the documentation of this file.
1 /**
2  * @file hash_drbg.h
3  * @brief Hash_DRBG pseudorandom number generator
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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.5.4
29  **/
30 
31 #ifndef _HASH_DRBG_H
32 #define _HASH_DRBG_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "hash/hash_algorithms.h"
37 
38 //Maximum seed length
39 #define HASH_DRBG_MAX_SEED_LEN 111
40 //Maximum number of requests between reseeds
41 #define HASH_DRBG_MAX_RESEED_INTERVAL 281474976710656ULL
42 
43 //Common interface for PRNG algorithms
44 #define HASH_DRBG_PRNG_ALGO (&hashDrbgPrngAlgo)
45 
46 //C++ guard
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50 
51 
52 /**
53  * @brief Hash_DRBG PRNG context
54  **/
55 
56 typedef struct
57 {
58  OsMutex mutex; ///<Mutex preventing simultaneous access to the PRNG state
59  const HashAlgo *hashAlgo; ///<Hash function
60  HashContext hashContext; ///<Hash context
61  size_t securityStrength; ///<Security strength
62  size_t seedLen; ///<Seed length
63  uint8_t v[HASH_DRBG_MAX_SEED_LEN]; ///<Value V
64  uint8_t c[HASH_DRBG_MAX_SEED_LEN]; ///<Constant C
65  uint64_t reseedCounter; ///<Reseed counter
67 
68 
69 //Hash_DRBG related constants
70 extern const PrngAlgo hashDrbgPrngAlgo;
71 
72 //Hash_DRBG related functions
73 error_t hashDrbgInit(HashDrbgContext *context, const HashAlgo *hashAlgo);
74 
75 error_t hashDrbgSeed(HashDrbgContext *context, const uint8_t *seed,
76  size_t length);
77 
78 error_t hashDrbgSeedEx(HashDrbgContext *context, const uint8_t *entropyInput,
79  size_t entropyInputLen, const uint8_t *nonce, size_t nonceLen,
80  const uint8_t *personalizationString, size_t personalizationStringLen);
81 
82 error_t hashDrbgReseed(HashDrbgContext *context, const uint8_t *seed,
83  size_t length);
84 
85 error_t hashDrbgReseedEx(HashDrbgContext *context, const uint8_t *entropyInput,
86  size_t entropyInputLen, const uint8_t *additionalInput,
87  size_t additionalInputLen);
88 
89 error_t hashDrbgGenerate(HashDrbgContext *context, uint8_t *output,
90  size_t length);
91 
93  const uint8_t *additionalInput, size_t additionalInputLen, uint8_t *output,
94  size_t outputLen);
95 
96 void hashDrbgDeinit(HashDrbgContext *context);
97 
98 void hashDf(HashDrbgContext *context, const DataChunk *input, uint_t inputLen,
99  uint8_t *output, size_t outputLen);
100 
101 void hashGen(HashDrbgContext *context, uint8_t *output, size_t outputLen);
102 
103 void hashDrbgAdd(uint8_t *a, size_t aLen, const uint8_t *b, size_t bLen);
104 void hashDrbgInc(uint8_t *a, size_t aLen);
105 
106 //C++ guard
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif
Generic hash algorithm context.
uint8_t b
Definition: nbns_common.h:122
HashContext hashContext
Hash context.
Definition: hash_drbg.h:60
uint8_t a
Definition: ndp.h:411
#define PrngAlgo
Definition: crypto.h:1008
const PrngAlgo hashDrbgPrngAlgo
Definition: hash_drbg.c:43
uint64_t reseedCounter
Reseed counter.
Definition: hash_drbg.h:65
void hashDrbgAdd(uint8_t *a, size_t aLen, const uint8_t *b, size_t bLen)
Add blocks.
Definition: hash_drbg.c:565
error_t hashDrbgGenerateEx(HashDrbgContext *context, const uint8_t *additionalInput, size_t additionalInputLen, uint8_t *output, size_t outputLen)
Generate pseudorandom data (with additional input)
Definition: hash_drbg.c:346
OsMutex mutex
Mutex preventing simultaneous access to the PRNG state.
Definition: hash_drbg.h:58
void hashDrbgDeinit(HashDrbgContext *context)
Release PRNG context.
Definition: hash_drbg.c:440
error_t hashDrbgReseed(HashDrbgContext *context, const uint8_t *seed, size_t length)
Reseed the PRNG state.
Definition: hash_drbg.c:225
error_t
Error codes.
Definition: error.h:43
error_t hashDrbgInit(HashDrbgContext *context, const HashAlgo *hashAlgo)
Initialize PRNG context.
Definition: hash_drbg.c:62
void hashGen(HashDrbgContext *context, uint8_t *output, size_t outputLen)
Hash generation sub function.
Definition: hash_drbg.c:517
error_t hashDrbgGenerate(HashDrbgContext *context, uint8_t *output, size_t length)
Generate pseudorandom data.
Definition: hash_drbg.c:327
const HashAlgo * hashAlgo
Hash function.
Definition: hash_drbg.h:59
General definitions for cryptographic algorithms.
#define HASH_DRBG_MAX_SEED_LEN
Definition: hash_drbg.h:39
error_t hashDrbgReseedEx(HashDrbgContext *context, const uint8_t *entropyInput, size_t entropyInputLen, const uint8_t *additionalInput, size_t additionalInputLen)
Reseed the PRNG state (with additional input)
Definition: hash_drbg.c:244
uint8_t length
Definition: tcp.h:375
size_t seedLen
Seed length.
Definition: hash_drbg.h:62
size_t securityStrength
Security strength.
Definition: hash_drbg.h:61
Collection of hash algorithms.
Mutex object.
Data chunk descriptor.
Definition: crypto.h:1052
error_t hashDrbgSeedEx(HashDrbgContext *context, const uint8_t *entropyInput, size_t entropyInputLen, const uint8_t *nonce, size_t nonceLen, const uint8_t *personalizationString, size_t personalizationStringLen)
Seed the PRNG state (with nonce and personalization string)
Definition: hash_drbg.c:142
void hashDrbgInc(uint8_t *a, size_t aLen)
Increment block.
Definition: hash_drbg.c:588
error_t hashDrbgSeed(HashDrbgContext *context, const uint8_t *seed, size_t length)
Seed the PRNG state.
Definition: hash_drbg.c:120
Hash_DRBG PRNG context.
Definition: hash_drbg.h:57
Common interface for hash algorithms.
Definition: crypto.h:1124
unsigned int uint_t
Definition: compiler_port.h:57
uint8_t nonce[]
Definition: ntp_common.h:239
void hashDf(HashDrbgContext *context, const DataChunk *input, uint_t inputLen, uint8_t *output, size_t outputLen)
Hash derivation function.
Definition: hash_drbg.c:463
uint8_t c
Definition: ndp.h:514