mbed TLS v2.7.11
ctr_drbg.h
Go to the documentation of this file.
1 
9 /*
10  * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
11  * SPDX-License-Identifier: Apache-2.0
12  *
13  * Licensed under the Apache License, Version 2.0 (the "License"); you may
14  * not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  * http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  *
25  * This file is part of Mbed TLS (https://tls.mbed.org)
26  */
27 
28 #ifndef MBEDTLS_CTR_DRBG_H
29 #define MBEDTLS_CTR_DRBG_H
30 
31 #if !defined(MBEDTLS_CONFIG_FILE)
32 #include "config.h"
33 #else
34 #include MBEDTLS_CONFIG_FILE
35 #endif
36 
37 #include "aes.h"
38 
39 #if defined(MBEDTLS_THREADING_C)
40 #include "threading.h"
41 #endif
42 
43 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
44 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
45 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
46 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
48 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
49 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
50 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
51 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
62 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
63 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
64 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
65 
69 #else
70 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
71 
75 #endif
76 #endif
77 
78 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
79 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
80 
81 #endif
82 
83 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
84 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
85 
86 #endif
87 
88 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
89 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
90 
91 #endif
92 
93 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
94 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
95 
96 #endif
97 
98 /* \} name SECTION: Module settings */
99 
100 #define MBEDTLS_CTR_DRBG_PR_OFF 0
101 
102 #define MBEDTLS_CTR_DRBG_PR_ON 1
103 
105 #ifdef __cplusplus
106 extern "C" {
107 #endif
108 
112 typedef struct
113 {
114  unsigned char counter[16];
120  size_t entropy_len;
126  /*
127  * Callbacks (Entropy)
128  */
129  int (*f_entropy)(void *, unsigned char *, size_t);
132  void *p_entropy;
134 #if defined(MBEDTLS_THREADING_C)
135  mbedtls_threading_mutex_t mutex;
136 #endif
137 }
139 
148 
169  int (*f_entropy)(void *, unsigned char *, size_t),
170  void *p_entropy,
171  const unsigned char *custom,
172  size_t len );
173 
180 
194  int resistance );
195 
205  size_t len );
206 
215  int interval );
216 
229  const unsigned char *additional, size_t len );
230 
246  const unsigned char *additional,
247  size_t add_len );
248 
265  const unsigned char *additional,
266  size_t add_len );
267 
285 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
286  unsigned char *output, size_t output_len,
287  const unsigned char *additional, size_t add_len );
288 
303 int mbedtls_ctr_drbg_random( void *p_rng,
304  unsigned char *output, size_t output_len );
305 
306 #if defined(MBEDTLS_FS_IO)
307 
318 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
319 
332 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
333 #endif /* MBEDTLS_FS_IO */
334 
340 int mbedtls_ctr_drbg_self_test( int verbose );
341 
342 /* Internal functions (do not call directly) */
344  int (*)(void *, unsigned char *, size_t), void *,
345  const unsigned char *, size_t, size_t );
346 
347 #ifdef __cplusplus
348 }
349 #endif
350 
351 #endif /* ctr_drbg.h */
The CTR_DRBG context structure.
Definition: ctr_drbg.h:112
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. 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...
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 default value is MBEDTLS...
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:124
The AES context-type definition.
Definition: aes.h:73