mbed TLS v2.16.1
ctr_drbg.h
Go to the documentation of this file.
1 
17 /*
18  * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
19  * SPDX-License-Identifier: GPL-2.0
20  *
21  * This program is free software; you can redistribute it and/or modify
22  * it under the terms of the GNU General Public License as published by
23  * the Free Software Foundation; either version 2 of the License, or
24  * (at your option) any later version.
25  *
26  * This program is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29  * GNU General Public License for more details.
30  *
31  * You should have received a copy of the GNU General Public License along
32  * with this program; if not, write to the Free Software Foundation, Inc.,
33  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34  *
35  * This file is part of Mbed TLS (https://tls.mbed.org)
36  */
37 
38 #ifndef MBEDTLS_CTR_DRBG_H
39 #define MBEDTLS_CTR_DRBG_H
40 
41 #if !defined(MBEDTLS_CONFIG_FILE)
42 #include "config.h"
43 #else
44 #include MBEDTLS_CONFIG_FILE
45 #endif
46 
47 #include "aes.h"
48 
49 #if defined(MBEDTLS_THREADING_C)
50 #include "threading.h"
51 #endif
52 
53 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
54 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
55 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
56 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
58 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
60 #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
61 #define MBEDTLS_CTR_DRBG_KEYSIZE 16
62 #else
63 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
64 #endif
65 
66 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
67 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
78 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
79 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
80 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
81 
85 #else
86 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
87 
91 #endif
92 #endif
93 
94 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
95 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
96 
97 #endif
98 
99 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
100 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
101 
102 #endif
103 
104 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
105 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
106 
107 #endif
108 
109 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
110 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
111 
112 #endif
113 
114 /* \} name SECTION: Module settings */
115 
116 #define MBEDTLS_CTR_DRBG_PR_OFF 0
117 
118 #define MBEDTLS_CTR_DRBG_PR_ON 1
119 
121 #ifdef __cplusplus
122 extern "C" {
123 #endif
124 
129 {
130  unsigned char counter[16];
136  size_t entropy_len;
142  /*
143  * Callbacks (Entropy)
144  */
145  int (*f_entropy)(void *, unsigned char *, size_t);
148  void *p_entropy;
150 #if defined(MBEDTLS_THREADING_C)
151  mbedtls_threading_mutex_t mutex;
152 #endif
153 }
155 
164 
185  int (*f_entropy)(void *, unsigned char *, size_t),
186  void *p_entropy,
187  const unsigned char *custom,
188  size_t len );
189 
196 
210  int resistance );
211 
221  size_t len );
222 
231  int interval );
232 
245  const unsigned char *additional, size_t len );
246 
262  const unsigned char *additional,
263  size_t add_len );
264 
282 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
283  unsigned char *output, size_t output_len,
284  const unsigned char *additional, size_t add_len );
285 
300 int mbedtls_ctr_drbg_random( void *p_rng,
301  unsigned char *output, size_t output_len );
302 
303 
304 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
305 #if defined(MBEDTLS_DEPRECATED_WARNING)
306 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
307 #else
308 #define MBEDTLS_DEPRECATED
309 #endif
310 
327  const unsigned char *additional,
328  size_t add_len );
329 #undef MBEDTLS_DEPRECATED
330 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
331 
332 #if defined(MBEDTLS_FS_IO)
333 
344 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
345 
358 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
359 #endif /* MBEDTLS_FS_IO */
360 
361 #if defined(MBEDTLS_SELF_TEST)
362 
369 int mbedtls_ctr_drbg_self_test( int verbose );
370 
371 #endif /* MBEDTLS_SELF_TEST */
372 
373 /* Internal functions (do not call directly) */
375  int (*)(void *, unsigned char *, size_t), void *,
376  const unsigned char *, size_t, size_t );
377 
378 #ifdef __cplusplus
379 }
380 #endif
381 
382 #endif /* ctr_drbg.h */