mbed TLS v2.23.0: ctr_drbg.h Source File

mbed TLS v2.23.0

ctr_drbg.h
Go to the documentation of this file.
1
24 /*
25 * Copyright (C) 2006-2019, Arm Limited (or its affiliates), All Rights Reserved
26 * SPDX-License-Identifier: Apache-2.0
27 *
28 * Licensed under the Apache License, Version 2.0 (the "License"); you may
29 * not use this file except in compliance with the License.
30 * You may obtain a copy of the License at
31 *
32 * http://www.apache.org/licenses/LICENSE-2.0
33 *
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 * See the License for the specific language governing permissions and
38 * limitations under the License.
39 *
40 * This file is part of Mbed TLS (https://tls.mbed.org)
41 */
42
43 #ifndef MBEDTLS_CTR_DRBG_H
44 #define MBEDTLS_CTR_DRBG_H
45
46 #if !defined(MBEDTLS_CONFIG_FILE)
47 #include "mbedtls/config.h"
48 #else
49 #include MBEDTLS_CONFIG_FILE
50 #endif
51
52 #include "mbedtls/aes.h"
53
54 #if defined(MBEDTLS_THREADING_C)
55 #include "mbedtls/threading.h"
56 #endif
57
58 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
59 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
60 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
61 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
63 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
65 #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
66 #define MBEDTLS_CTR_DRBG_KEYSIZE 16
67
72 #else
73 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
74
79 #endif
80
81 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
82 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
97 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
98 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
99
102 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
103
104 #else /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
105
110 #if !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
111
114 #endif /* !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) */
115 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
116 #endif /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
117 #endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
118
119 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
120 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
121
122 #endif
123
124 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
125 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
126
127 #endif
128
129 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
130 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
131
132 #endif
133
134 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
135 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
136
137 #endif
138
139 /* \} name SECTION: Module settings */
140
141 #define MBEDTLS_CTR_DRBG_PR_OFF 0
142
143 #define MBEDTLS_CTR_DRBG_PR_ON 1
144
146 #ifdef __cplusplus
147 extern "C" {
148 #endif
149
150 #if MBEDTLS_CTR_DRBG_ENTROPY_LEN >= MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
151
157 #define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN 0
158 #else
159
165 #define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN ( MBEDTLS_CTR_DRBG_ENTROPY_LEN + 1 ) / 2
166 #endif
167
171 typedef struct mbedtls_ctr_drbg_context
172 {
173 unsigned char counter[16];
174 int reseed_counter;
184 int prediction_resistance;
188 size_t entropy_len;
190 int reseed_interval;
194 mbedtls_aes_context aes_ctx;
196 /*
197 * Callbacks (Entropy)
198 */
199 int (*f_entropy)(void *, unsigned char *, size_t);
202 void *p_entropy;
204 #if defined(MBEDTLS_THREADING_C)
205 mbedtls_threading_mutex_t mutex;
206 #endif
207 }
208 mbedtls_ctr_drbg_context;
209
217 void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
218
252 #if MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN == 0
253
257 #else
258
263 #endif
264
307 int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
308 int (*f_entropy)(void *, unsigned char *, size_t),
309 void *p_entropy,
310 const unsigned char *custom,
311 size_t len );
312
318 void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
319
333 void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
334 int resistance );
335
360 void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
361 size_t len );
362
382 int mbedtls_ctr_drbg_set_nonce_len( mbedtls_ctr_drbg_context *ctx,
383 size_t len );
384
397 void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
398 int interval );
399
415 int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
416 const unsigned char *additional, size_t len );
417
433 int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
434 const unsigned char *additional,
435 size_t add_len );
436
463 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
464 unsigned char *output, size_t output_len,
465 const unsigned char *additional, size_t add_len );
466
483 int mbedtls_ctr_drbg_random( void *p_rng,
484 unsigned char *output, size_t output_len );
485
486
487 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
488 #if defined(MBEDTLS_DEPRECATED_WARNING)
489 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
490 #else
491 #define MBEDTLS_DEPRECATED
492 #endif
493
508 MBEDTLS_DEPRECATED void mbedtls_ctr_drbg_update(
509 mbedtls_ctr_drbg_context *ctx,
510 const unsigned char *additional,
511 size_t add_len );
512 #undef MBEDTLS_DEPRECATED
513 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
514
515 #if defined(MBEDTLS_FS_IO)
516
527 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
528
543 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
544 #endif /* MBEDTLS_FS_IO */
545
546 #if defined(MBEDTLS_SELF_TEST)
547
554 int mbedtls_ctr_drbg_self_test( int verbose );
555
556 #endif /* MBEDTLS_SELF_TEST */
557
558 #ifdef __cplusplus
559 }
560 #endif
561
562 #endif /* ctr_drbg.h */
mbedtls_ctr_drbg_self_test
int mbedtls_ctr_drbg_self_test(int verbose)
The CTR_DRBG checkup routine.
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:173
mbedtls_ctr_drbg_context::reseed_interval
int reseed_interval
Definition: ctr_drbg.h:190
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:172
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:491
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_aes_context
The AES context-type definition.
Definition: aes.h:88
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:188
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:184
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_nonce_len
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.
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.
config.h
Configuration options (set of defines)
mbedtls_ctr_drbg_context::reseed_counter
int reseed_counter
Definition: ctr_drbg.h:174
mbedtls_ctr_drbg_context::aes_ctx
mbedtls_aes_context aes_ctx
Definition: ctr_drbg.h:194
mbedtls_ctr_drbg_context::f_entropy
int(* f_entropy)(void *, unsigned char *, size_t)
Definition: ctr_drbg.h:199
mbedtls_ctr_drbg_context::p_entropy
void * p_entropy
Definition: ctr_drbg.h:202