mbed TLS v2.16.9
ctr_drbg.h
Go to the documentation of this file.
1
40/*
41 * Copyright The Mbed TLS Contributors
42 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
43 *
44 * This file is provided under the Apache License 2.0, or the
45 * GNU General Public License v2.0 or later.
46 *
47 * **********
48 * Apache License 2.0:
49 *
50 * Licensed under the Apache License, Version 2.0 (the "License"); you may
51 * not use this file except in compliance with the License.
52 * You may obtain a copy of the License at
53 *
54 * http://www.apache.org/licenses/LICENSE-2.0
55 *
56 * Unless required by applicable law or agreed to in writing, software
57 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
58 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
59 * See the License for the specific language governing permissions and
60 * limitations under the License.
61 *
62 * **********
63 *
64 * **********
65 * GNU General Public License v2.0 or later:
66 *
67 * This program is free software; you can redistribute it and/or modify
68 * it under the terms of the GNU General Public License as published by
69 * the Free Software Foundation; either version 2 of the License, or
70 * (at your option) any later version.
71 *
72 * This program is distributed in the hope that it will be useful,
73 * but WITHOUT ANY WARRANTY; without even the implied warranty of
74 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75 * GNU General Public License for more details.
76 *
77 * You should have received a copy of the GNU General Public License along
78 * with this program; if not, write to the Free Software Foundation, Inc.,
79 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
80 *
81 * **********
82 */
83
84#ifndef MBEDTLS_CTR_DRBG_H
85#define MBEDTLS_CTR_DRBG_H
86
87#if !defined(MBEDTLS_CONFIG_FILE)
88#include "config.h"
89#else
90#include MBEDTLS_CONFIG_FILE
91#endif
92
93#include "aes.h"
94
95#if defined(MBEDTLS_THREADING_C)
96#include "threading.h"
97#endif
98
99#define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
100#define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
101#define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
102#define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
104#define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
106#if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
107#define MBEDTLS_CTR_DRBG_KEYSIZE 16
113#else
114#define MBEDTLS_CTR_DRBG_KEYSIZE 32
120#endif
121
122#define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
123#define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
138#if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
139#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
143#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
144
145#else /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
146
151#if !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
155#endif /* !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) */
156#define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
157#endif /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
158#endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
159
160#if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
161#define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
163#endif
164
165#if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
166#define MBEDTLS_CTR_DRBG_MAX_INPUT 256
168#endif
169
170#if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
171#define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
173#endif
174
175#if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
176#define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
178#endif
179
180/* \} name SECTION: Module settings */
181
182#define MBEDTLS_CTR_DRBG_PR_OFF 0
184#define MBEDTLS_CTR_DRBG_PR_ON 1
187#ifdef __cplusplus
188extern "C" {
189#endif
190
195{
196 unsigned char counter[16];
202 size_t entropy_len;
208 /*
209 * Callbacks (Entropy)
210 */
211 int (*f_entropy)(void *, unsigned char *, size_t);
214 void *p_entropy;
216#if defined(MBEDTLS_THREADING_C)
218#endif
219}
221
235
270#if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
279#endif
307 int (*f_entropy)(void *, unsigned char *, size_t),
308 void *p_entropy,
309 const unsigned char *custom,
310 size_t len );
311
319
334 int resistance );
335
359 size_t len );
360
374 int interval );
375
392 const unsigned char *additional, size_t len );
393
410 const unsigned char *additional,
411 size_t add_len );
412
440 unsigned char *output, size_t output_len,
441 const unsigned char *additional, size_t add_len );
442
459int mbedtls_ctr_drbg_random( void *p_rng,
460 unsigned char *output, size_t output_len );
461
462
463#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
464#if defined(MBEDTLS_DEPRECATED_WARNING)
465#define MBEDTLS_DEPRECATED __attribute__((deprecated))
466#else
467#define MBEDTLS_DEPRECATED
468#endif
486 const unsigned char *additional,
487 size_t add_len );
488#undef MBEDTLS_DEPRECATED
489#endif /* !MBEDTLS_DEPRECATED_REMOVED */
490
491#if defined(MBEDTLS_FS_IO)
504
520#endif /* MBEDTLS_FS_IO */
521
522#if defined(MBEDTLS_SELF_TEST)
523
530int mbedtls_ctr_drbg_self_test( int verbose );
531
532#endif /* MBEDTLS_SELF_TEST */
533
534/* Internal functions (do not call directly) */
536 int (*)(void *, unsigned char *, size_t), void *,
537 const unsigned char *, size_t, size_t );
538
539#ifdef __cplusplus
540}
541#endif
542
543#endif /* ctr_drbg.h */
This file contains AES definitions and functions.
Configuration options (set of defines)
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.
MBEDTLS_DEPRECATED void 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_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_seed_entropy_len(mbedtls_ctr_drbg_context *, int(*)(void *, unsigned char *, size_t), void *, const unsigned char *, size_t, size_t)
int mbedtls_ctr_drbg_update_ret(mbedtls_ctr_drbg_context *ctx, const unsigned char *additional, size_t add_len)
This function updates the state of the CTR_DRBG context.
#define MBEDTLS_DEPRECATED
Definition ctr_drbg.h:465
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.
The AES context-type definition.
Definition aes.h:113
The CTR_DRBG context structure.
Definition ctr_drbg.h:195
unsigned char counter[16]
Definition ctr_drbg.h:196
mbedtls_threading_mutex_t mutex
Definition ctr_drbg.h:217
int(* f_entropy)(void *, unsigned char *, size_t)
Definition ctr_drbg.h:211
mbedtls_aes_context aes_ctx
Definition ctr_drbg.h:206
Threading abstraction layer.