ctr_drbg.c File Reference

CTR_DRBG pseudorandom number generator. More...

#include "core/crypto.h"
#include "rng/ctr_drbg.h"
#include "debug.h"

Go to the source code of this file.

Macros

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL
 

Functions

error_t ctrDrbgInit (CtrDrbgContext *context, const CipherAlgo *cipherAlgo, size_t keyLen, uint_t ctrLen, bool_t df)
 Initialize PRNG context. More...
 
error_t ctrDrbgSeed (CtrDrbgContext *context, const uint8_t *seed, size_t length)
 Seed the PRNG state. More...
 
error_t ctrDrbgSeedEx (CtrDrbgContext *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) More...
 
error_t ctrDrbgReseed (CtrDrbgContext *context, const uint8_t *seed, size_t length)
 Reseed the PRNG state. More...
 
error_t ctrDrbgReseedEx (CtrDrbgContext *context, const uint8_t *entropyInput, size_t entropyInputLen, const uint8_t *additionalInput, size_t additionalInputLen)
 Reseed the PRNG state (with additional input) More...
 
error_t ctrDrbgGenerate (CtrDrbgContext *context, uint8_t *output, size_t length)
 Generate pseudorandom data. More...
 
error_t ctrDrbgGenerateEx (CtrDrbgContext *context, const uint8_t *additionalInput, size_t additionalInputLen, uint8_t *output, size_t outputLen)
 Generate pseudorandom data (with additional input) More...
 
void ctrDrbgDeinit (CtrDrbgContext *context)
 Release PRNG context. More...
 
error_t blockCipherDf (CtrDrbgContext *context, const DataFrag *inputFrags, uint_t inputNumFrags, uint8_t *output, size_t outputLen)
 Block cipher derivation function. More...
 
error_t ctrDrbgBcc (CtrDrbgContext *context, const uint8_t *key, const DataFrag *dataFrags, uint_t dataNumFrags, uint8_t *output)
 BCC function. More...
 
error_t ctrDrbgUpdate (CtrDrbgContext *context, const uint8_t *providedData, size_t providedDataLen)
 Update internal state. More...
 
error_t ctrDrbgLoadKey (CtrDrbgContext *context, const uint8_t *key)
 Load encryption key. More...
 
void ctrDrbgIncBlock (uint8_t *ctr, size_t blockLen, size_t ctrLen)
 Increment counter block. More...
 
void ctrDrbgXorBlock (uint8_t *x, const uint8_t *a, const uint8_t *b, size_t n)
 XOR operation. More...
 

Variables

const PrngAlgo ctrDrbgPrngAlgo
 

Detailed Description

CTR_DRBG pseudorandom number generator.

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneCRYPTO Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.6.6

Definition in file ctr_drbg.c.

Macro Definition Documentation

◆ TRACE_LEVEL

#define TRACE_LEVEL   CRYPTO_TRACE_LEVEL

Definition at line 32 of file ctr_drbg.c.

Function Documentation

◆ blockCipherDf()

error_t blockCipherDf ( CtrDrbgContext *  context,
const DataFrag *  inputFrags,
uint_t  inputNumFrags,
uint8_t *  output,
size_t  outputLen 
)

Block cipher derivation function.

Parameters
[in]contextPointer to the CTR_DRBG context
[in]inputFragsArray of fragments representing the input string
[in]inputNumFragsNumber of fragments representing the input string
[out]outputBuffer where to store the output value
[out]outputLenThe number of bytes to be returned
Returns
Error code

Definition at line 603 of file ctr_drbg.c.

◆ ctrDrbgBcc()

error_t ctrDrbgBcc ( CtrDrbgContext *  context,
const uint8_t *  key,
const DataFrag *  dataFrags,
uint_t  dataNumFrags,
uint8_t *  output 
)

BCC function.

Parameters
[in]contextPointer to the CTR_DRBG context
[in]keyThe key to be used for the block cipher operation
[in]dataFragsArray of fragments representing the input data
[in]dataNumFragsNumber of fragments representing the input data
[out]outputThe result to be returned from the BCC operation
Returns
Error code

Definition at line 763 of file ctr_drbg.c.

◆ ctrDrbgDeinit()

void ctrDrbgDeinit ( CtrDrbgContext *  context)

Release PRNG context.

Parameters
[in]contextPointer to the CTR_DRBG context

Definition at line 579 of file ctr_drbg.c.

◆ ctrDrbgGenerate()

error_t ctrDrbgGenerate ( CtrDrbgContext *  context,
uint8_t *  output,
size_t  length 
)

Generate pseudorandom data.

Parameters
[in]contextPointer to the CTR_DRBG context
[out]outputBuffer where to store the pseudorandom bytes
[in]lengthRequested number of bytes
Returns
Error code

Definition at line 413 of file ctr_drbg.c.

◆ ctrDrbgGenerateEx()

error_t ctrDrbgGenerateEx ( CtrDrbgContext *  context,
const uint8_t *  additionalInput,
size_t  additionalInputLen,
uint8_t *  output,
size_t  outputLen 
)

Generate pseudorandom data (with additional input)

Parameters
[in]contextPointer to the CTR_DRBG context
[in]additionalInputAdditional input string received from the consuming application
[in]additionalInputLenLength of the additional input string, in bytes
[out]outputBuffer where to store the pseudorandom bytes
[in]outputLenRequested number of bytes
Returns
Error code

Definition at line 432 of file ctr_drbg.c.

◆ ctrDrbgIncBlock()

void ctrDrbgIncBlock ( uint8_t *  ctr,
size_t  blockLen,
size_t  ctrLen 
)

Increment counter block.

Parameters
[in,out]ctrPointer to the counter block
[in]blockLenLength of the block, in bytes
[in]ctrLenSize of the specific part of the block to be incremented

Definition at line 955 of file ctr_drbg.c.

◆ ctrDrbgInit()

error_t ctrDrbgInit ( CtrDrbgContext *  context,
const CipherAlgo *  cipherAlgo,
size_t  keyLen,
uint_t  ctrLen,
bool_t  df 
)

Initialize PRNG context.

Parameters
[in]contextPointer to the CTR_DRBG context to initialize
[in]cipherAlgoApproved block cipher algorithm
[in]keyLenKey length, in bits
[in]ctrLenCounter field length, in bits
[in]dfUse key derivation function
Returns
Error code

Definition at line 68 of file ctr_drbg.c.

◆ ctrDrbgLoadKey()

error_t ctrDrbgLoadKey ( CtrDrbgContext *  context,
const uint8_t *  key 
)

Load encryption key.

Parameters
[in]contextPointer to the CTR_DRBG context
[in]keyPointer to the Encryption key to load
Returns
Error code

Definition at line 914 of file ctr_drbg.c.

◆ ctrDrbgReseed()

error_t ctrDrbgReseed ( CtrDrbgContext *  context,
const uint8_t *  seed,
size_t  length 
)

Reseed the PRNG state.

Parameters
[in]contextPointer to the CTR_DRBG context
[in]seedString of bits obtained from the randomness source
[in]lengthLength of the string, in bytes
Returns
Error code

Definition at line 288 of file ctr_drbg.c.

◆ ctrDrbgReseedEx()

error_t ctrDrbgReseedEx ( CtrDrbgContext *  context,
const uint8_t *  entropyInput,
size_t  entropyInputLen,
const uint8_t *  additionalInput,
size_t  additionalInputLen 
)

Reseed the PRNG state (with additional input)

Parameters
[in]contextPointer to the CTR_DRBG context
[in]entropyInputString of bits obtained from the randomness source
[in]entropyInputLenLength of the string, in bytes
[in]additionalInputAdditional input string received from the consuming application
[in]additionalInputLenLength of the additional input string, in bytes
Returns
Error code

Definition at line 307 of file ctr_drbg.c.

◆ ctrDrbgSeed()

error_t ctrDrbgSeed ( CtrDrbgContext *  context,
const uint8_t *  seed,
size_t  length 
)

Seed the PRNG state.

Parameters
[in]contextPointer to the CTR_DRBG context
[in]seedString of bits obtained from the randomness source
[in]lengthLength of the string, in bytes
Returns
Error code

Definition at line 147 of file ctr_drbg.c.

◆ ctrDrbgSeedEx()

error_t ctrDrbgSeedEx ( CtrDrbgContext *  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)

Parameters
[in]contextPointer to the CTR_DRBG context
[in]entropyInputString of bits obtained from the randomness source
[in]entropyInputLenLength of the string, in bytes
[in]nonceNonce
[in]nonceLenLength of the nonce, in bytes
[in]personalizationStringPersonalization string received from the consuming application
[in]personalizationStringLenLength of the personalization string, in bytes
Returns
Error code

Definition at line 169 of file ctr_drbg.c.

◆ ctrDrbgUpdate()

error_t ctrDrbgUpdate ( CtrDrbgContext *  context,
const uint8_t *  providedData,
size_t  providedDataLen 
)

Update internal state.

Parameters
[in]contextPointer to the CTR_DRBG context
[in]providedDataThe data to be used
[in]providedDataLenLength of the data, in bytes
Returns
Error code

Definition at line 848 of file ctr_drbg.c.

◆ ctrDrbgXorBlock()

void ctrDrbgXorBlock ( uint8_t *  x,
const uint8_t *  a,
const uint8_t *  b,
size_t  n 
)

XOR operation.

Parameters
[out]xBlock resulting from the XOR operation
[in]aFirst input block
[in]bSecond input block
[in]nLength of the block, in bytes

Definition at line 980 of file ctr_drbg.c.

Variable Documentation

◆ ctrDrbgPrngAlgo

const PrngAlgo ctrDrbgPrngAlgo
Initial value:

Definition at line 46 of file ctr_drbg.c.

error_t(* PrngAlgoSeed)(void *context, const uint8_t *seed, size_t length)
Definition: crypto.h:1226
error_t(* PrngAlgoReseed)(void *context, const uint8_t *seed, size_t length)
Definition: crypto.h:1229
error_t ctrDrbgGenerate(CtrDrbgContext *context, uint8_t *output, size_t length)
Generate pseudorandom data.
Definition: ctr_drbg.c:413
void(* PrngAlgoDeinit)(void *context)
Definition: crypto.h:1235
void ctrDrbgDeinit(CtrDrbgContext *context)
Release PRNG context.
Definition: ctr_drbg.c:579
error_t(* PrngAlgoGenerate)(void *context, uint8_t *output, size_t length)
Definition: crypto.h:1232
error_t ctrDrbgReseed(CtrDrbgContext *context, const uint8_t *seed, size_t length)
Reseed the PRNG state.
Definition: ctr_drbg.c:288
CTR_DRBG PRNG context.
Definition: ctr_drbg.h:59
error_t ctrDrbgSeed(CtrDrbgContext *context, const uint8_t *seed, size_t length)
Seed the PRNG state.
Definition: ctr_drbg.c:147
error_t(* PrngAlgoInit)(void *context)
Definition: crypto.h:1224