Mbed TLS v3.5.2
Loading...
Searching...
No Matches
ctr_drbg.h
Go to the documentation of this file.
1
24/*
25 * Copyright The Mbed TLS Contributors
26 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
27 */
28
29#ifndef MBEDTLS_CTR_DRBG_H
30#define MBEDTLS_CTR_DRBG_H
32
33#include "mbedtls/build_info.h"
34
35#include "mbedtls/aes.h"
36#include "entropy.h"
37
38#if defined(MBEDTLS_THREADING_C)
39#include "mbedtls/threading.h"
40#endif
41
43#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
45#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
47#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
49#define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
50
51#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
53#if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
54#define MBEDTLS_CTR_DRBG_KEYSIZE 16
60#else
61#define MBEDTLS_CTR_DRBG_KEYSIZE 32
67#endif
68
69#define MBEDTLS_CTR_DRBG_KEYBITS (MBEDTLS_CTR_DRBG_KEYSIZE * 8)
70#define MBEDTLS_CTR_DRBG_SEEDLEN (MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE)
85#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
86#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
89#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
90
91#else /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
92
95#if !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
99#endif /* !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) */
100#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
101#endif /* MBEDTLS_ENTROPY_SHA512_ACCUMULATOR */
102#endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
103
104#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
105#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
107#endif
108
109#if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
110#define MBEDTLS_CTR_DRBG_MAX_INPUT 256
112#endif
113
114#if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
115#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
117#endif
118
119#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
120#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
122#endif
123
126#define MBEDTLS_CTR_DRBG_PR_OFF 0
128#define MBEDTLS_CTR_DRBG_PR_ON 1
131#ifdef __cplusplus
132extern "C" {
133#endif
134
135#if MBEDTLS_CTR_DRBG_ENTROPY_LEN >= MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
142#define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN 0
143#else
150#define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN (MBEDTLS_CTR_DRBG_ENTROPY_LEN + 1) / 2
151#endif
152
157 unsigned char MBEDTLS_PRIVATE(counter)[16];
158 int MBEDTLS_PRIVATE(reseed_counter);
168 int MBEDTLS_PRIVATE(prediction_resistance);
172 size_t MBEDTLS_PRIVATE(entropy_len);
174 int MBEDTLS_PRIVATE(reseed_interval);
180 /*
181 * Callbacks (Entropy)
182 */
183 int(*MBEDTLS_PRIVATE(f_entropy))(void *, unsigned char *, size_t);
186 void *MBEDTLS_PRIVATE(p_entropy);
188#if defined(MBEDTLS_THREADING_C)
189 /* Invariant: the mutex is initialized if and only if f_entropy != NULL.
190 * This means that the mutex is initialized during the initial seeding
191 * in mbedtls_ctr_drbg_seed() and freed in mbedtls_ctr_drbg_free().
192 *
193 * Note that this invariant may change without notice. Do not rely on it
194 * and do not access the mutex directly in application code.
195 */
197#endif
198}
200
214
248#if MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN == 0
253#else
259#endif
260#if defined(MBEDTLS_THREADING_C)
268#endif /* MBEDTLS_THREADING_C */
315 int (*f_entropy)(void *, unsigned char *, size_t),
316 void *p_entropy,
317 const unsigned char *custom,
318 size_t len);
319
327
342 int resistance);
343
369 size_t len);
370
391 size_t len);
392
406 int interval);
407
429 const unsigned char *additional, size_t len);
430
452 const unsigned char *additional,
453 size_t add_len);
454
487 unsigned char *output, size_t output_len,
488 const unsigned char *additional, size_t add_len);
489
496#if defined(MBEDTLS_THREADING_C)
503#endif /* MBEDTLS_THREADING_C */
515 unsigned char *output, size_t output_len);
516
517#if defined(MBEDTLS_FS_IO)
530
546#endif /* MBEDTLS_FS_IO */
547
548#if defined(MBEDTLS_SELF_TEST)
549
557
558#endif /* MBEDTLS_SELF_TEST */
559
560#ifdef __cplusplus
561}
562#endif
563
564#endif /* ctr_drbg.h */
This file contains AES definitions and functions.
int mbedtls_ctr_drbg_set_nonce_len(mbedtls_ctr_drbg_context *ctx, size_t len)
This function sets the amount of entropy grabbed as a nonce for the initial seeding.
int mbedtls_ctr_drbg_random_with_add(void *p_rng, unsigned char *output, size_t output_len, const unsigned char *additional, size_t add_len)
This function updates a CTR_DRBG instance with additional data and uses it to generate random data.
void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx)
This function resets CTR_DRBG context to the state immediately after initial call of mbedtls_ctr_drbg...
int mbedtls_ctr_drbg_update_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path)
This function reads and updates a seed file. The seed is added to this instance.
int mbedtls_ctr_drbg_reseed(mbedtls_ctr_drbg_context *ctx, const unsigned char *additional, size_t len)
This function reseeds the CTR_DRBG context, that is extracts data from the entropy source.
struct mbedtls_ctr_drbg_context mbedtls_ctr_drbg_context
The CTR_DRBG context structure.
int mbedtls_ctr_drbg_self_test(int verbose)
The CTR_DRBG checkup routine.
void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *ctx)
This function initializes the CTR_DRBG context, and prepares it for mbedtls_ctr_drbg_seed() or mbedtl...
int mbedtls_ctr_drbg_update(mbedtls_ctr_drbg_context *ctx, const unsigned char *additional, size_t add_len)
This function updates the state of the CTR_DRBG context.
int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context *ctx, int(*f_entropy)(void *, unsigned char *, size_t), void *p_entropy, const unsigned char *custom, size_t len)
This function seeds and sets up the CTR_DRBG entropy source for future reseeds.
void mbedtls_ctr_drbg_set_prediction_resistance(mbedtls_ctr_drbg_context *ctx, int resistance)
This function turns prediction resistance on or off. The default value is off.
void mbedtls_ctr_drbg_set_reseed_interval(mbedtls_ctr_drbg_context *ctx, int interval)
This function sets the reseed interval.
void mbedtls_ctr_drbg_set_entropy_len(mbedtls_ctr_drbg_context *ctx, size_t len)
This function sets the amount of entropy grabbed on each seed or reseed.
int mbedtls_ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_len)
This function uses CTR_DRBG to generate random data.
int mbedtls_ctr_drbg_write_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path)
This function writes a seed file.
Entropy accumulator implementation.
Build-time configuration info.
Macro wrapper for struct's members.
#define MBEDTLS_PRIVATE(member)
The AES context-type definition.
Definition aes.h:63
The CTR_DRBG context structure.
Definition ctr_drbg.h:156
Threading abstraction layer.