mbed TLS v2.16.7
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: Apache-2.0 OR GPL-2.0-or-later
43  *
44  * This file is provided under the Apache License 2.0, or the
45  * GNU General Public License v2.0 or later.
46  *
47  * **********
48  * Apache License 2.0:
49  *
50  * Licensed under the Apache License, Version 2.0 (the "License"); you may
51  * not use this file except in compliance with the License.
52  * You may obtain a copy of the License at
53  *
54  * http://www.apache.org/licenses/LICENSE-2.0
55  *
56  * Unless required by applicable law or agreed to in writing, software
57  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
58  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
59  * See the License for the specific language governing permissions and
60  * limitations under the License.
61  *
62  * **********
63  *
64  * **********
65  * GNU General Public License v2.0 or later:
66  *
67  * This program is free software; you can redistribute it and/or modify
68  * it under the terms of the GNU General Public License as published by
69  * the Free Software Foundation; either version 2 of the License, or
70  * (at your option) any later version.
71  *
72  * This program is distributed in the hope that it will be useful,
73  * but WITHOUT ANY WARRANTY; without even the implied warranty of
74  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75  * GNU General Public License for more details.
76  *
77  * You should have received a copy of the GNU General Public License along
78  * with this program; if not, write to the Free Software Foundation, Inc.,
79  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
80  *
81  * **********
82  *
83  * This file is part of Mbed TLS (https://tls.mbed.org)
84  */
85 
86 #ifndef MBEDTLS_CTR_DRBG_H
87 #define MBEDTLS_CTR_DRBG_H
88 
89 #if !defined(MBEDTLS_CONFIG_FILE)
90 #include "config.h"
91 #else
92 #include MBEDTLS_CONFIG_FILE
93 #endif
94 
95 #include "aes.h"
96 
97 #if defined(MBEDTLS_THREADING_C)
98 #include "threading.h"
99 #endif
100 
101 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
102 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
103 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
104 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
106 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
108 #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
109 #define MBEDTLS_CTR_DRBG_KEYSIZE 16
110 
115 #else
116 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
117 
122 #endif
123 
124 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
125 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
140 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
141 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
142 
145 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
146 
147 #else /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
148 
153 #if !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
154 
157 #endif /* !defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY) */
158 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
159 #endif /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
160 #endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
161 
162 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
163 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
164 
165 #endif
166 
167 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
168 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
169 
170 #endif
171 
172 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
173 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
174 
175 #endif
176 
177 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
178 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
179 
180 #endif
181 
182 /* \} name SECTION: Module settings */
183 
184 #define MBEDTLS_CTR_DRBG_PR_OFF 0
185 
186 #define MBEDTLS_CTR_DRBG_PR_ON 1
187 
189 #ifdef __cplusplus
190 extern "C" {
191 #endif
192 
197 {
198  unsigned char counter[16];
204  size_t entropy_len;
210  /*
211  * Callbacks (Entropy)
212  */
213  int (*f_entropy)(void *, unsigned char *, size_t);
216  void *p_entropy;
218 #if defined(MBEDTLS_THREADING_C)
220 #endif
221 }
223 
232 
267 #if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
268 
276 #endif
277 
304  int (*f_entropy)(void *, unsigned char *, size_t),
305  void *p_entropy,
306  const unsigned char *custom,
307  size_t len );
308 
315 
330  int resistance );
331 
355  size_t len );
356 
370  int interval );
371 
388  const unsigned char *additional, size_t len );
389 
406  const unsigned char *additional,
407  size_t add_len );
408 
435 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
436  unsigned char *output, size_t output_len,
437  const unsigned char *additional, size_t add_len );
438 
455 int mbedtls_ctr_drbg_random( void *p_rng,
456  unsigned char *output, size_t output_len );
457 
458 
459 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
460 #if defined(MBEDTLS_DEPRECATED_WARNING)
461 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
462 #else
463 #define MBEDTLS_DEPRECATED
464 #endif
465 
482  const unsigned char *additional,
483  size_t add_len );
484 #undef MBEDTLS_DEPRECATED
485 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
486 
487 #if defined(MBEDTLS_FS_IO)
488 
499 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
500 
515 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
516 #endif /* MBEDTLS_FS_IO */
517 
518 #if defined(MBEDTLS_SELF_TEST)
519 
526 int mbedtls_ctr_drbg_self_test( int verbose );
527 
528 #endif /* MBEDTLS_SELF_TEST */
529 
530 /* Internal functions (do not call directly) */
532  int (*)(void *, unsigned char *, size_t), void *,
533  const unsigned char *, size_t, size_t );
534 
535 #ifdef __cplusplus
536 }
537 #endif
538 
539 #endif /* ctr_drbg.h */
int(* f_entropy)(void *, unsigned char *, size_t)
Definition: ctr_drbg.h:213
The CTR_DRBG context structure.
Definition: ctr_drbg.h:196
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...
mbedtls_threading_mutex_t mutex
Definition: ctr_drbg.h:219
#define MBEDTLS_DEPRECATED
Definition: ctr_drbg.h:463
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.
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:198
mbedtls_aes_context aes_ctx
Definition: ctr_drbg.h:208
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:114