hmac_drbg.h
Go to the documentation of this file.
1 /**
2  * @file hmac_drbg.h
3  * @brief HMAC_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 _HMAC_DRBG_H
32 #define _HMAC_DRBG_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "mac/hmac.h"
37 
38 //Maximum number of requests between reseeds
39 #define HMAC_DRBG_MAX_RESEED_INTERVAL 281474976710656ULL
40 
41 //Common interface for PRNG algorithms
42 #define HMAC_DRBG_PRNG_ALGO (&hmacDrbgPrngAlgo)
43 
44 //C++ guard
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 
50 /**
51  * @brief HMAC_DRBG PRNG context
52  **/
53 
54 typedef struct
55 {
56  OsMutex mutex; ///<Mutex preventing simultaneous access to the PRNG state
57  const HashAlgo *hashAlgo; ///<Hash function
58  HmacContext hmacContext; ///<HMAC context
59  size_t securityStrength; ///<Security strength
60  uint8_t v[MAX_HASH_DIGEST_SIZE]; ///<Value V
61  uint8_t k[MAX_HASH_DIGEST_SIZE]; ///<Key
62  uint64_t reseedCounter; ///<Reseed counter
64 
65 
66 //HMAC_DRBG related constants
67 extern const PrngAlgo hmacDrbgPrngAlgo;
68 
69 //HMAC_DRBG related functions
70 error_t hmacDrbgInit(HmacDrbgContext *context, const HashAlgo *hashAlgo);
71 
72 error_t hmacDrbgSeed(HmacDrbgContext *context, const uint8_t *seed,
73  size_t length);
74 
75 error_t hmacDrbgSeedEx(HmacDrbgContext *context, const uint8_t *entropyInput,
76  size_t entropyInputLen, const uint8_t *nonce, size_t nonceLen,
77  const uint8_t *personalizationString, size_t personalizationStringLen);
78 
79 error_t hmacDrbgReseed(HmacDrbgContext *context, const uint8_t *seed,
80  size_t length);
81 
82 error_t hmacDrbgReseedEx(HmacDrbgContext *context, const uint8_t *entropyInput,
83  size_t entropyInputLen, const uint8_t *additionalInput,
84  size_t additionalInputLen);
85 
86 error_t hmacDrbgGenerate(HmacDrbgContext *context, uint8_t *output,
87  size_t length);
88 
90  const uint8_t *additionalInput, size_t additionalInputLen, uint8_t *output,
91  size_t outputLen);
92 
93 void hmacDrbgDeinit(HmacDrbgContext *context);
94 
95 void hmacDrbgUpdate(HmacDrbgContext *context, const DataChunk *providedData,
96  uint_t providedDataLen);
97 
98 //C++ guard
99 #ifdef __cplusplus
100 }
101 #endif
102 
103 #endif
HMAC algorithm context.
Definition: hmac.h:59
OsMutex mutex
Mutex preventing simultaneous access to the PRNG state.
Definition: hmac_drbg.h:56
#define PrngAlgo
Definition: crypto.h:1008
size_t securityStrength
Security strength.
Definition: hmac_drbg.h:59
error_t hmacDrbgSeed(HmacDrbgContext *context, const uint8_t *seed, size_t length)
Seed the PRNG state.
Definition: hmac_drbg.c:109
HMAC_DRBG PRNG context.
Definition: hmac_drbg.h:55
#define MAX_HASH_DIGEST_SIZE
HmacContext hmacContext
HMAC context.
Definition: hmac_drbg.h:58
error_t
Error codes.
Definition: error.h:43
error_t hmacDrbgGenerateEx(HmacDrbgContext *context, const uint8_t *additionalInput, size_t additionalInputLen, uint8_t *output, size_t outputLen)
Generate pseudorandom data (with additional input)
Definition: hmac_drbg.c:306
General definitions for cryptographic algorithms.
uint8_t length
Definition: tcp.h:375
error_t hmacDrbgGenerate(HmacDrbgContext *context, uint8_t *output, size_t length)
Generate pseudorandom data.
Definition: hmac_drbg.c:287
Mutex object.
Data chunk descriptor.
Definition: crypto.h:1052
uint64_t reseedCounter
Reseed counter.
Definition: hmac_drbg.h:62
void hmacDrbgUpdate(HmacDrbgContext *context, const DataChunk *providedData, uint_t providedDataLen)
Update internal state.
Definition: hmac_drbg.c:422
error_t hmacDrbgReseed(HmacDrbgContext *context, const uint8_t *seed, size_t length)
Reseed the PRNG state.
Definition: hmac_drbg.c:209
error_t hmacDrbgInit(HmacDrbgContext *context, const HashAlgo *hashAlgo)
Initialize PRNG context.
Definition: hmac_drbg.c:62
error_t hmacDrbgSeedEx(HmacDrbgContext *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: hmac_drbg.c:131
Common interface for hash algorithms.
Definition: crypto.h:1124
error_t hmacDrbgReseedEx(HmacDrbgContext *context, const uint8_t *entropyInput, size_t entropyInputLen, const uint8_t *additionalInput, size_t additionalInputLen)
Reseed the PRNG state (with additional input)
Definition: hmac_drbg.c:228
const HashAlgo * hashAlgo
Hash function.
Definition: hmac_drbg.h:57
unsigned int uint_t
Definition: compiler_port.h:57
const PrngAlgo hmacDrbgPrngAlgo
Definition: hmac_drbg.c:43
uint8_t nonce[]
Definition: ntp_common.h:239
HMAC (Keyed-Hashing for Message Authentication)
void hmacDrbgDeinit(HmacDrbgContext *context)
Release PRNG context.
Definition: hmac_drbg.c:401