mbed TLS v2.24.0
ctr_drbg.h
Go to the documentation of this file.
1 
24 /*
25  * Copyright The Mbed TLS Contributors
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 
41 #ifndef MBEDTLS_CTR_DRBG_H
42 #define MBEDTLS_CTR_DRBG_H
43 
44 #if !defined(MBEDTLS_CONFIG_FILE)
45 #include "mbedtls/config.h"
46 #else
47 #include MBEDTLS_CONFIG_FILE
48 #endif
49 
50 #include "mbedtls/aes.h"
51 
52 #if defined(MBEDTLS_THREADING_C)
53 #include "mbedtls/threading.h"
54 #endif
55 
56 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
57 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
58 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
59 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
61 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
63 #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
64 #define MBEDTLS_CTR_DRBG_KEYSIZE 16
65 
70 #else
71 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
72 
77 #endif
78 
79 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
80 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
95 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
96 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
97 
100 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
101 
102 #else /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
103 
108 #if !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
109 
112 #endif /* !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) */
113 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
114 #endif /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
115 #endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
116 
117 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
118 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
119 
120 #endif
121 
122 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
123 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
124 
125 #endif
126 
127 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
128 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
129 
130 #endif
131 
132 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
133 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
134 
135 #endif
136 
137 /* \} name SECTION: Module settings */
138 
139 #define MBEDTLS_CTR_DRBG_PR_OFF 0
140 
141 #define MBEDTLS_CTR_DRBG_PR_ON 1
142 
144 #ifdef __cplusplus
145 extern "C" {
146 #endif
147 
148 #if MBEDTLS_CTR_DRBG_ENTROPY_LEN >= MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
149 
155 #define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN 0
156 #else
157 
163 #define MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN ( MBEDTLS_CTR_DRBG_ENTROPY_LEN + 1 ) / 2
164 #endif
165 
170 {
171  unsigned char counter[16];
186  size_t entropy_len;
192  mbedtls_aes_context aes_ctx;
194  /*
195  * Callbacks (Entropy)
196  */
197  int (*f_entropy)(void *, unsigned char *, size_t);
200  void *p_entropy;
202 #if defined(MBEDTLS_THREADING_C)
204 #endif
205 }
207 
216 
250 #if MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN == 0
251 
255 #else
256 
261 #endif
262 
306  int (*f_entropy)(void *, unsigned char *, size_t),
307  void *p_entropy,
308  const unsigned char *custom,
309  size_t len );
310 
317 
332  int resistance );
333 
359  size_t len );
360 
381  size_t len );
382 
396  int interval );
397 
414  const unsigned char *additional, size_t len );
415 
432  const unsigned char *additional,
433  size_t add_len );
434 
461 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
462  unsigned char *output, size_t output_len,
463  const unsigned char *additional, size_t add_len );
464 
481 int mbedtls_ctr_drbg_random( void *p_rng,
482  unsigned char *output, size_t output_len );
483 
484 
485 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
486 #if defined(MBEDTLS_DEPRECATED_WARNING)
487 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
488 #else
489 #define MBEDTLS_DEPRECATED
490 #endif
491 
506 MBEDTLS_DEPRECATED void mbedtls_ctr_drbg_update(
508  const unsigned char *additional,
509  size_t add_len );
510 #undef MBEDTLS_DEPRECATED
511 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
512 
513 #if defined(MBEDTLS_FS_IO)
514 
525 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
526 
541 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
542 #endif /* MBEDTLS_FS_IO */
543 
544 #if defined(MBEDTLS_SELF_TEST)
545 
552 int mbedtls_ctr_drbg_self_test( int verbose );
553 
554 #endif /* MBEDTLS_SELF_TEST */
555 
556 #ifdef __cplusplus
557 }
558 #endif
559 
560 #endif /* ctr_drbg.h */
The CTR_DRBG context structure.
Definition: ctr_drbg.h:169
void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx)
This function clears CTR_CRBG context data.
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:203
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.
#define MBEDTLS_DEPRECATED
Definition: pkcs11.h:60
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.
This file contains AES definitions and functions.
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.
int(* f_entropy)(void *, unsigned char *, size_t)
Definition: ctr_drbg.h:197
struct mbedtls_ctr_drbg_context mbedtls_ctr_drbg_context
The CTR_DRBG context structure.
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.
unsigned char counter[16]
Definition: ctr_drbg.h:171
mbedtls_aes_context aes_ctx
Definition: ctr_drbg.h:192