xdrbg.h
Go to the documentation of this file.
1 /**
2  * @file xdrbg.h
3  * @brief XDRBG 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 _XDRBG_H
32 #define _XDRBG_H
33 
34 //Dependencies
35 #include "core/crypto.h"
36 #include "xof/xof_algorithms.h"
37 
38 //Maximum length of the additional data
39 #define XDRBG_MAX_ALPHA_LEN 84
40 //Maximum size of the internal state
41 #define XDRBG_MAX_V_SIZE 64
42 
43 //Common interface for PRNG algorithms
44 #define XDRBG_PRNG_ALGO (&xdrbgPrngAlgo)
45 
46 //Encode function
47 #define XDRBG_ENCODE(alphaLen, n) ((n * 85) + alphaLen)
48 
49 //C++ guard
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 
55 /**
56  * @brief XDRBG PRNG context
57  **/
58 
59 typedef struct
60 {
61  OsMutex mutex; ///<Mutex preventing simultaneous access to the PRNG state
62  const XofAlgo *xofAlgo; ///<XOF algorithm
63  XofContext xofContext; ///<XOF context
64  size_t securityStrength; ///<Security strength
65  size_t maxOutputLen; ///<Maximum output length
66  uint8_t v[XDRBG_MAX_V_SIZE]; ///<Internal state V
67  uint64_t reseedCounter; ///<Reseed counter
68 } XdrbgContext;
69 
70 
71 //XDRBG related constants
72 extern const PrngAlgo xdrbgPrngAlgo;
73 
74 //XDRBG related functions
75 error_t xdrbgInit(XdrbgContext *context, const XofAlgo *xofAlgo);
76 
77 error_t xdrbgSeed(XdrbgContext *context, const uint8_t *seed, size_t length);
78 
79 error_t xdrbgSeedEx(XdrbgContext *context, const uint8_t *seed, size_t seedLen,
80  const uint8_t *alpha, size_t alphaLen);
81 
82 error_t xdrbgReseed(XdrbgContext *context, const uint8_t *seed, size_t length);
83 
84 error_t xdrbgReseedEx(XdrbgContext *context, const uint8_t *seed,
85  size_t seedLen, const uint8_t *alpha, size_t alphaLen);
86 
87 error_t xdrbgGenerate(XdrbgContext *context, uint8_t *output, size_t length);
88 
89 error_t xdrbgGenerateEx(XdrbgContext *context, const uint8_t *alpha,
90  size_t alphaLen, uint8_t *output, size_t outputLen);
91 
92 void xdrbgDeinit(XdrbgContext *context);
93 
94 //C++ guard
95 #ifdef __cplusplus
96 }
97 #endif
98 
99 #endif
error_t xdrbgGenerateEx(XdrbgContext *context, const uint8_t *alpha, size_t alphaLen, uint8_t *output, size_t outputLen)
Generate pseudorandom data (with additional input)
Definition: xdrbg.c:316
#define PrngAlgo
Definition: crypto.h:1008
XDRBG PRNG context.
Definition: xdrbg.h:60
XofContext xofContext
XOF context.
Definition: xdrbg.h:63
error_t
Error codes.
Definition: error.h:43
size_t maxOutputLen
Maximum output length.
Definition: xdrbg.h:65
Collection of XOF algorithms.
#define XDRBG_MAX_V_SIZE
Definition: xdrbg.h:41
General definitions for cryptographic algorithms.
Generic XOF algorithm context.
error_t xdrbgGenerate(XdrbgContext *context, uint8_t *output, size_t length)
Generate pseudorandom data.
Definition: xdrbg.c:299
uint8_t length
Definition: tcp.h:375
void xdrbgDeinit(XdrbgContext *context)
Release PRNG context.
Definition: xdrbg.c:393
error_t xdrbgSeed(XdrbgContext *context, const uint8_t *seed, size_t length)
Seed the PRNG state.
Definition: xdrbg.c:130
Mutex object.
Common interface for XOF algorithms.
Definition: crypto.h:1146
OsMutex mutex
Mutex preventing simultaneous access to the PRNG state.
Definition: xdrbg.h:61
error_t xdrbgSeedEx(XdrbgContext *context, const uint8_t *seed, size_t seedLen, const uint8_t *alpha, size_t alphaLen)
Seed the PRNG state (with nonce and personalization string)
Definition: xdrbg.c:147
const PrngAlgo xdrbgPrngAlgo
Definition: xdrbg.c:43
error_t xdrbgReseedEx(XdrbgContext *context, const uint8_t *seed, size_t seedLen, const uint8_t *alpha, size_t alphaLen)
Reseed the PRNG state (with additional input)
Definition: xdrbg.c:229
size_t securityStrength
Security strength.
Definition: xdrbg.h:64
error_t xdrbgReseed(XdrbgContext *context, const uint8_t *seed, size_t length)
Reseed the PRNG state.
Definition: xdrbg.c:212
uint64_t reseedCounter
Reseed counter.
Definition: xdrbg.h:67
error_t xdrbgInit(XdrbgContext *context, const XofAlgo *xofAlgo)
Initialize PRNG context.
Definition: xdrbg.c:62
const XofAlgo * xofAlgo
XOF algorithm.
Definition: xdrbg.h:62