mbed TLS v2.16.5

ctr_drbg.h
Go to the documentation of this file.
1
40 /*
41 * Copyright (C) 2006-2019, Arm Limited (or its affiliates), All Rights Reserved
42 * SPDX-License-Identifier: Apache-2.0
43 *
44 * Licensed under the Apache License, Version 2.0 (the "License"); you may
45 * not use this file except in compliance with the License.
46 * You may obtain a copy of the License at
47 *
48 * http://www.apache.org/licenses/LICENSE-2.0
49 *
50 * Unless required by applicable law or agreed to in writing, software
51 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
52 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53 * See the License for the specific language governing permissions and
54 * limitations under the License.
55 *
56 * This file is part of Mbed TLS (https://tls.mbed.org)
57 */
58
59 #ifndef MBEDTLS_CTR_DRBG_H
60 #define MBEDTLS_CTR_DRBG_H
61
62 #if !defined(MBEDTLS_CONFIG_FILE)
63 #include "config.h"
64 #else
65 #include MBEDTLS_CONFIG_FILE
66 #endif
67
68 #include "aes.h"
69
70 #if defined(MBEDTLS_THREADING_C)
71 #include "threading.h"
72 #endif
73
74 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
75 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
76 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
77 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
79 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
81 #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
82 #define MBEDTLS_CTR_DRBG_KEYSIZE 16
83
88 #else
89 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
90
95 #endif
96
97 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
98 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
113 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
114 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
115
118 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
119
120 #else /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
121
126 #if !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
127
130 #endif /* !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) */
131 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
132 #endif /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
133 #endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
134
135 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
136 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
137
138 #endif
139
140 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
141 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
142
143 #endif
144
145 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
146 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
147
148 #endif
149
150 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
151 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
152
153 #endif
154
155 /* \} name SECTION: Module settings */
156
157 #define MBEDTLS_CTR_DRBG_PR_OFF 0
158
159 #define MBEDTLS_CTR_DRBG_PR_ON 1
160
162 #ifdef __cplusplus
163 extern "C" {
164 #endif
165
169 typedef struct mbedtls_ctr_drbg_context
170 {
171 unsigned char counter[16];
172 int reseed_counter;
173 int prediction_resistance;
177 size_t entropy_len;
179 int reseed_interval;
181 mbedtls_aes_context aes_ctx;
183 /*
184 * Callbacks (Entropy)
185 */
186 int (*f_entropy)(void *, unsigned char *, size_t);
189 void *p_entropy;
191 #if defined(MBEDTLS_THREADING_C)
192 mbedtls_threading_mutex_t mutex;
193 #endif
194 }
195 mbedtls_ctr_drbg_context;
196
204 void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
205
240 #if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
241
249 #endif
250
276 int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
277 int (*f_entropy)(void *, unsigned char *, size_t),
278 void *p_entropy,
279 const unsigned char *custom,
280 size_t len );
281
287 void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
288
302 void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
303 int resistance );
304
327 void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
328 size_t len );
329
342 void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
343 int interval );
344
360 int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
361 const unsigned char *additional, size_t len );
362
378 int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
379 const unsigned char *additional,
380 size_t add_len );
381
408 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
409 unsigned char *output, size_t output_len,
410 const unsigned char *additional, size_t add_len );
411
428 int mbedtls_ctr_drbg_random( void *p_rng,
429 unsigned char *output, size_t output_len );
430
431
432 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
433 #if defined(MBEDTLS_DEPRECATED_WARNING)
434 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
435 #else
436 #define MBEDTLS_DEPRECATED
437 #endif
438
453 MBEDTLS_DEPRECATED void mbedtls_ctr_drbg_update(
454 mbedtls_ctr_drbg_context *ctx,
455 const unsigned char *additional,
456 size_t add_len );
457 #undef MBEDTLS_DEPRECATED
458 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
459
460 #if defined(MBEDTLS_FS_IO)
461
472 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
473
488 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
489 #endif /* MBEDTLS_FS_IO */
490
491 #if defined(MBEDTLS_SELF_TEST)
492
499 int mbedtls_ctr_drbg_self_test( int verbose );
500
501 #endif /* MBEDTLS_SELF_TEST */
502
503 /* Internal functions (do not call directly) */
504 int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
505 int (*)(void *, unsigned char *, size_t), void *,
506 const unsigned char *, size_t, size_t );
507
508 #ifdef __cplusplus
509 }
510 #endif
511
512 #endif /* ctr_drbg.h */
mbedtls_ctr_drbg_self_test
int mbedtls_ctr_drbg_self_test(int verbose)
The CTR_DRBG checkup routine.
mbedtls_threading_mutex_t
Definition: threading.h:48
mbedtls_ctr_drbg_reseed
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_ctr_drbg_seed
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.
mbedtls_ctr_drbg_context::counter
unsigned char counter[16]
Definition: ctr_drbg.h:171
mbedtls_ctr_drbg_context::reseed_interval
int reseed_interval
Definition: ctr_drbg.h:179
mbedtls_ctr_drbg_update
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.
mbedtls_ctr_drbg_set_entropy_len
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.
mbedtls_ctr_drbg_context
The CTR_DRBG context structure.
Definition: ctr_drbg.h:169
mbedtls_ctr_drbg_update_ret
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.
aes.h
This file contains AES definitions and functions.
MBEDTLS_DEPRECATED
#define MBEDTLS_DEPRECATED
Definition: ctr_drbg.h:434
mbedtls_ctr_drbg_random
int mbedtls_ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_len)
This function uses CTR_DRBG to generate random data.
mbedtls_ctr_drbg_write_seed_file
int mbedtls_ctr_drbg_write_seed_file(mbedtls_ctr_drbg_context *ctx, const char *path)
This function writes a seed file.
mbedtls_ctr_drbg_context::mutex
mbedtls_threading_mutex_t mutex
Definition: ctr_drbg.h:192
mbedtls_aes_context
The AES context-type definition.
Definition: aes.h:86
mbedtls_ctr_drbg_random_with_add
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.
mbedtls_ctr_drbg_context::entropy_len
size_t entropy_len
Definition: ctr_drbg.h:177
mbedtls_ctr_drbg_free
void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx)
This function clears CTR_CRBG context data.
mbedtls_ctr_drbg_context::prediction_resistance
int prediction_resistance
Definition: ctr_drbg.h:173
mbedtls_ctr_drbg_context
struct mbedtls_ctr_drbg_context mbedtls_ctr_drbg_context
The CTR_DRBG context structure.
mbedtls_ctr_drbg_init
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...
threading.h
Threading abstraction layer.
mbedtls_ctr_drbg_set_reseed_interval
void mbedtls_ctr_drbg_set_reseed_interval(mbedtls_ctr_drbg_context *ctx, int interval)
This function sets the reseed interval.
mbedtls_ctr_drbg_update_seed_file
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.
mbedtls_ctr_drbg_set_prediction_resistance
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.
mbedtls_ctr_drbg_seed_entropy_len
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)
config.h
Configuration options (set of defines)
mbedtls_ctr_drbg_context::reseed_counter
int reseed_counter
Definition: ctr_drbg.h:172
mbedtls_ctr_drbg_context::aes_ctx
mbedtls_aes_context aes_ctx
Definition: ctr_drbg.h:181
mbedtls_ctr_drbg_context::f_entropy
int(* f_entropy)(void *, unsigned char *, size_t)
Definition: ctr_drbg.h:186
mbedtls_ctr_drbg_context::p_entropy
void * p_entropy
Definition: ctr_drbg.h:189