mbed TLS v2.16.3: ctr_drbg.h Source File

mbed TLS v2.16.3

ctr_drbg.h
Go to the documentation of this file.
1
17 /*
18 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
19 * SPDX-License-Identifier: Apache-2.0
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may
22 * not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
32 *
33 * This file is part of Mbed TLS (https://tls.mbed.org)
34 */
35
36 #ifndef MBEDTLS_CTR_DRBG_H
37 #define MBEDTLS_CTR_DRBG_H
38
39 #if !defined(MBEDTLS_CONFIG_FILE)
40 #include "config.h"
41 #else
42 #include MBEDTLS_CONFIG_FILE
43 #endif
44
45 #include "aes.h"
46
47 #if defined(MBEDTLS_THREADING_C)
48 #include "threading.h"
49 #endif
50
51 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
52 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
53 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
54 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
56 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
58 #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
59 #define MBEDTLS_CTR_DRBG_KEYSIZE 16
60 #else
61 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
62 #endif
63
64 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
65 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
76 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
77 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
78 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
79
83 #else
84 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
85
89 #endif
90 #endif
91
92 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
93 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
94
95 #endif
96
97 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
98 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
99
100 #endif
101
102 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
103 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
104
105 #endif
106
107 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
108 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
109
110 #endif
111
112 /* \} name SECTION: Module settings */
113
114 #define MBEDTLS_CTR_DRBG_PR_OFF 0
115
116 #define MBEDTLS_CTR_DRBG_PR_ON 1
117
119 #ifdef __cplusplus
120 extern "C" {
121 #endif
122
126 typedef struct mbedtls_ctr_drbg_context
127 {
128 unsigned char counter[16];
129 int reseed_counter;
130 int prediction_resistance;
134 size_t entropy_len;
136 int reseed_interval;
138 mbedtls_aes_context aes_ctx;
140 /*
141 * Callbacks (Entropy)
142 */
143 int (*f_entropy)(void *, unsigned char *, size_t);
146 void *p_entropy;
148 #if defined(MBEDTLS_THREADING_C)
149 mbedtls_threading_mutex_t mutex;
150 #endif
151 }
152 mbedtls_ctr_drbg_context;
153
161 void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
162
182 int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
183 int (*f_entropy)(void *, unsigned char *, size_t),
184 void *p_entropy,
185 const unsigned char *custom,
186 size_t len );
187
193 void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
194
207 void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
208 int resistance );
209
218 void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
219 size_t len );
220
228 void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
229 int interval );
230
242 int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
243 const unsigned char *additional, size_t len );
244
259 int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
260 const unsigned char *additional,
261 size_t add_len );
262
280 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
281 unsigned char *output, size_t output_len,
282 const unsigned char *additional, size_t add_len );
283
298 int mbedtls_ctr_drbg_random( void *p_rng,
299 unsigned char *output, size_t output_len );
300
301
302 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
303 #if defined(MBEDTLS_DEPRECATED_WARNING)
304 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
305 #else
306 #define MBEDTLS_DEPRECATED
307 #endif
308
323 MBEDTLS_DEPRECATED void mbedtls_ctr_drbg_update(
324 mbedtls_ctr_drbg_context *ctx,
325 const unsigned char *additional,
326 size_t add_len );
327 #undef MBEDTLS_DEPRECATED
328 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
329
330 #if defined(MBEDTLS_FS_IO)
331
342 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
343
356 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
357 #endif /* MBEDTLS_FS_IO */
358
359 #if defined(MBEDTLS_SELF_TEST)
360
367 int mbedtls_ctr_drbg_self_test( int verbose );
368
369 #endif /* MBEDTLS_SELF_TEST */
370
371 /* Internal functions (do not call directly) */
372 int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
373 int (*)(void *, unsigned char *, size_t), void *,
374 const unsigned char *, size_t, size_t );
375
376 #ifdef __cplusplus
377 }
378 #endif
379
380 #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:128
mbedtls_ctr_drbg_context::reseed_interval
int reseed_interval
Definition: ctr_drbg.h:136
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. The default value is MBEDTLS...
mbedtls_ctr_drbg_context
The CTR_DRBG context structure.
Definition: ctr_drbg.h:126
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_ctr_drbg_context
struct mbedtls_ctr_drbg_context mbedtls_ctr_drbg_context
The CTR_DRBG context structure.
MBEDTLS_DEPRECATED
#define MBEDTLS_DEPRECATED
Definition: ctr_drbg.h:306
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: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:134
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:130
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. The default value is MBEDTLS_CTR_DRBG_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:129
mbedtls_ctr_drbg_context::aes_ctx
mbedtls_aes_context aes_ctx
Definition: ctr_drbg.h:138
mbedtls_ctr_drbg_context::f_entropy
int(* f_entropy)(void *, unsigned char *, size_t)
Definition: ctr_drbg.h:143
mbedtls_ctr_drbg_context::p_entropy
void * p_entropy
Definition: ctr_drbg.h:146