mbed TLS v2.16.6
ctr_drbg.h
Go to the documentation of this file.
1 
40 /*
41  * Copyright (C) 2006-2019, Arm Limited (or its affiliates), All Rights Reserved
42  * SPDX-License-Identifier: GPL-2.0
43  *
44  * This program is free software; you can redistribute it and/or modify
45  * it under the terms of the GNU General Public License as published by
46  * the Free Software Foundation; either version 2 of the License, or
47  * (at your option) any later version.
48  *
49  * This program is distributed in the hope that it will be useful,
50  * but WITHOUT ANY WARRANTY; without even the implied warranty of
51  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52  * GNU General Public License for more details.
53  *
54  * You should have received a copy of the GNU General Public License along
55  * with this program; if not, write to the Free Software Foundation, Inc.,
56  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
57  *
58  * This file is part of Mbed TLS (https://tls.mbed.org)
59  */
60 
61 #ifndef MBEDTLS_CTR_DRBG_H
62 #define MBEDTLS_CTR_DRBG_H
63 
64 #if !defined(MBEDTLS_CONFIG_FILE)
65 #include "config.h"
66 #else
67 #include MBEDTLS_CONFIG_FILE
68 #endif
69 
70 #include "aes.h"
71 
72 #if defined(MBEDTLS_THREADING_C)
73 #include "threading.h"
74 #endif
75 
76 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
77 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
78 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
79 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
81 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
83 #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
84 #define MBEDTLS_CTR_DRBG_KEYSIZE 16
85 
90 #else
91 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
92 
97 #endif
98 
99 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
100 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
115 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
116 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
117 
120 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
121 
122 #else /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
123 
128 #if !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
129 
132 #endif /* !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) */
133 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
134 #endif /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
135 #endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
136 
137 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
138 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
139 
140 #endif
141 
142 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
143 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
144 
145 #endif
146 
147 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
148 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
149 
150 #endif
151 
152 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
153 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
154 
155 #endif
156 
157 /* \} name SECTION: Module settings */
158 
159 #define MBEDTLS_CTR_DRBG_PR_OFF 0
160 
161 #define MBEDTLS_CTR_DRBG_PR_ON 1
162 
164 #ifdef __cplusplus
165 extern "C" {
166 #endif
167 
172 {
173  unsigned char counter[16];
179  size_t entropy_len;
185  /*
186  * Callbacks (Entropy)
187  */
188  int (*f_entropy)(void *, unsigned char *, size_t);
191  void *p_entropy;
193 #if defined(MBEDTLS_THREADING_C)
194  mbedtls_threading_mutex_t mutex;
195 #endif
196 }
198 
207 
242 #if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
243 
251 #endif
252 
279  int (*f_entropy)(void *, unsigned char *, size_t),
280  void *p_entropy,
281  const unsigned char *custom,
282  size_t len );
283 
290 
305  int resistance );
306 
330  size_t len );
331 
345  int interval );
346 
363  const unsigned char *additional, size_t len );
364 
381  const unsigned char *additional,
382  size_t add_len );
383 
410 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
411  unsigned char *output, size_t output_len,
412  const unsigned char *additional, size_t add_len );
413 
430 int mbedtls_ctr_drbg_random( void *p_rng,
431  unsigned char *output, size_t output_len );
432 
433 
434 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
435 #if defined(MBEDTLS_DEPRECATED_WARNING)
436 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
437 #else
438 #define MBEDTLS_DEPRECATED
439 #endif
440 
457  const unsigned char *additional,
458  size_t add_len );
459 #undef MBEDTLS_DEPRECATED
460 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
461 
462 #if defined(MBEDTLS_FS_IO)
463 
474 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
475 
490 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
491 #endif /* MBEDTLS_FS_IO */
492 
493 #if defined(MBEDTLS_SELF_TEST)
494 
501 int mbedtls_ctr_drbg_self_test( int verbose );
502 
503 #endif /* MBEDTLS_SELF_TEST */
504 
505 /* Internal functions (do not call directly) */
507  int (*)(void *, unsigned char *, size_t), void *,
508  const unsigned char *, size_t, size_t );
509 
510 #ifdef __cplusplus
511 }
512 #endif
513 
514 #endif /* ctr_drbg.h */
The CTR_DRBG context structure.
Definition: ctr_drbg.h:171
void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx)
This function clears CTR_CRBG context data.
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...
#define MBEDTLS_DEPRECATED
Definition: ctr_drbg.h:438
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.
This file contains AES definitions and functions.
int(* f_entropy)(void *, unsigned char *, size_t)
Definition: ctr_drbg.h:188
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:173
mbedtls_aes_context aes_ctx
Definition: ctr_drbg.h:183
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.
The AES context-type definition.
Definition: aes.h:88