mbed TLS v2.7.15
ctr_drbg.h
Go to the documentation of this file.
1 
34 /*
35  * Copyright (C) 2006-2019, Arm Limited (or its affiliates), All Rights Reserved
36  * SPDX-License-Identifier: Apache-2.0
37  *
38  * Licensed under the Apache License, Version 2.0 (the "License"); you may
39  * not use this file except in compliance with the License.
40  * You may obtain a copy of the License at
41  *
42  * http://www.apache.org/licenses/LICENSE-2.0
43  *
44  * Unless required by applicable law or agreed to in writing, software
45  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
46  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
47  * See the License for the specific language governing permissions and
48  * limitations under the License.
49  *
50  * This file is part of Mbed TLS (https://tls.mbed.org)
51  */
52 
53 #ifndef MBEDTLS_CTR_DRBG_H
54 #define MBEDTLS_CTR_DRBG_H
55 
56 #if !defined(MBEDTLS_CONFIG_FILE)
57 #include "config.h"
58 #else
59 #include MBEDTLS_CONFIG_FILE
60 #endif
61 
62 #include "aes.h"
63 
64 #if defined(MBEDTLS_THREADING_C)
65 #include "threading.h"
66 #endif
67 
68 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
69 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
70 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
71 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
73 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
74 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
75 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
76 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
91 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
92 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
93 
96 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
97 
98 #else /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
99 
107 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
108 #endif /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
109 #endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
110 
111 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
112 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
113 
114 #endif
115 
116 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
117 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
118 
119 #endif
120 
121 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
122 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
123 
124 #endif
125 
126 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
127 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
128 
129 #endif
130 
131 /* \} name SECTION: Module settings */
132 
133 #define MBEDTLS_CTR_DRBG_PR_OFF 0
134 
135 #define MBEDTLS_CTR_DRBG_PR_ON 1
136 
138 #ifdef __cplusplus
139 extern "C" {
140 #endif
141 
145 typedef struct
146 {
147  unsigned char counter[16];
153  size_t entropy_len;
159  /*
160  * Callbacks (Entropy)
161  */
162  int (*f_entropy)(void *, unsigned char *, size_t);
165  void *p_entropy;
167 #if defined(MBEDTLS_THREADING_C)
168  mbedtls_threading_mutex_t mutex;
169 #endif
170 }
172 
181 
216 #if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
217 
224 #endif
225 
252  int (*f_entropy)(void *, unsigned char *, size_t),
253  void *p_entropy,
254  const unsigned char *custom,
255  size_t len );
256 
263 
278  int resistance );
279 
295  size_t len );
296 
310  int interval );
311 
328  const unsigned char *additional, size_t len );
329 
346  const unsigned char *additional,
347  size_t add_len );
348 
367  const unsigned char *additional,
368  size_t add_len );
369 
396 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
397  unsigned char *output, size_t output_len,
398  const unsigned char *additional, size_t add_len );
399 
416 int mbedtls_ctr_drbg_random( void *p_rng,
417  unsigned char *output, size_t output_len );
418 
419 #if defined(MBEDTLS_FS_IO)
420 
431 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
432 
447 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
448 #endif /* MBEDTLS_FS_IO */
449 
455 int mbedtls_ctr_drbg_self_test( int verbose );
456 
457 /* Internal functions (do not call directly) */
459  int (*)(void *, unsigned char *, size_t), void *,
460  const unsigned char *, size_t, size_t );
461 
462 #ifdef __cplusplus
463 }
464 #endif
465 
466 #endif /* ctr_drbg.h */
The CTR_DRBG context structure.
Definition: ctr_drbg.h:145
void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx)
This function clears CTR_CRBG context data.
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_seed_entropy_len(mbedtls_ctr_drbg_context *, int(*)(void *, unsigned char *, size_t), void *, const unsigned char *, size_t, size_t)
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...
Configuration options (set of defines)
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.
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_prediction_resistance(mbedtls_ctr_drbg_context *ctx, int resistance)
This function turns prediction resistance on or off. The default value is off.
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...
Threading abstraction layer.
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_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...
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.
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 Advanced Encryption Standard (AES) specifies a FIPS-approved cryptographic algorithm that can be ...
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.
int mbedtls_ctr_drbg_self_test(int verbose)
The CTR_DRBG checkup routine.
mbedtls_aes_context aes_ctx
Definition: ctr_drbg.h:157
The AES context-type definition.
Definition: aes.h:73