mbed TLS v2.9.0
ctr_drbg.h
Go to the documentation of this file.
1 
14 /*
15  * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
16  * SPDX-License-Identifier: Apache-2.0
17  *
18  * Licensed under the Apache License, Version 2.0 (the "License"); you may
19  * not use this file except in compliance with the License.
20  * You may obtain a copy of the License at
21  *
22  * http://www.apache.org/licenses/LICENSE-2.0
23  *
24  * Unless required by applicable law or agreed to in writing, software
25  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27  * See the License for the specific language governing permissions and
28  * limitations under the License.
29  *
30  * This file is part of Mbed TLS (https://tls.mbed.org)
31  */
32 
33 #ifndef MBEDTLS_CTR_DRBG_H
34 #define MBEDTLS_CTR_DRBG_H
35 
36 #include "aes.h"
37 
38 #if defined(MBEDTLS_THREADING_C)
39 #include "mbedtls/threading.h"
40 #endif
41 
42 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
43 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
44 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
45 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
47 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
48 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
49 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
50 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
61 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
62 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
63 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
64 
68 #else
69 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
70 
74 #endif
75 #endif
76 
77 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
78 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
79 
80 #endif
81 
82 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
83 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
84 
85 #endif
86 
87 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
88 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
89 
90 #endif
91 
92 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
93 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
94 
95 #endif
96 
97 /* \} name SECTION: Module settings */
98 
99 #define MBEDTLS_CTR_DRBG_PR_OFF 0
100 
101 #define MBEDTLS_CTR_DRBG_PR_ON 1
102 
104 #ifdef __cplusplus
105 extern "C" {
106 #endif
107 
111 typedef struct
112 {
113  unsigned char counter[16];
119  size_t entropy_len;
125  /*
126  * Callbacks (Entropy)
127  */
128  int (*f_entropy)(void *, unsigned char *, size_t);
131  void *p_entropy;
133 #if defined(MBEDTLS_THREADING_C)
135 #endif
136 }
138 
147 
168  int (*f_entropy)(void *, unsigned char *, size_t),
169  void *p_entropy,
170  const unsigned char *custom,
171  size_t len );
172 
179 
193  int resistance );
194 
204  size_t len );
205 
214  int interval );
215 
228  const unsigned char *additional, size_t len );
229 
244  const unsigned char *additional, size_t add_len );
245 
263 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
264  unsigned char *output, size_t output_len,
265  const unsigned char *additional, size_t add_len );
266 
281 int mbedtls_ctr_drbg_random( void *p_rng,
282  unsigned char *output, size_t output_len );
283 
284 #if defined(MBEDTLS_FS_IO)
285 
296 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
297 
310 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
311 #endif /* MBEDTLS_FS_IO */
312 
319 int mbedtls_ctr_drbg_self_test( int verbose );
320 
321 /* Internal functions (do not call directly) */
323  int (*)(void *, unsigned char *, size_t), void *,
324  const unsigned char *, size_t, size_t );
325 
326 #ifdef __cplusplus
327 }
328 #endif
329 
330 #endif /* ctr_drbg.h */
The CTR_DRBG context structure.
Definition: ctr_drbg.h:111
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...
mbedtls_threading_mutex_t mutex
Definition: ctr_drbg.h:134
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. The default value is MBEDTLS_CTR_DRBG_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...
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...
This file contains AES definitions and functions.
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:123
The AES context-type definition.
Definition: aes.h:76