mbed TLS v2.7.16
ctr_drbg.h
Go to the documentation of this file.
1 
34 /*
35  * Copyright (C) 2006-2019, Arm Limited (or its affiliates), All Rights Reserved
36  * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
37  *
38  * This file is provided under the Apache License 2.0, or the
39  * GNU General Public License v2.0 or later.
40  *
41  * **********
42  * Apache License 2.0:
43  *
44  * Licensed under the Apache License, Version 2.0 (the "License"); you may
45  * not use this file except in compliance with the License.
46  * You may obtain a copy of the License at
47  *
48  * http://www.apache.org/licenses/LICENSE-2.0
49  *
50  * Unless required by applicable law or agreed to in writing, software
51  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
52  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53  * See the License for the specific language governing permissions and
54  * limitations under the License.
55  *
56  * **********
57  *
58  * **********
59  * GNU General Public License v2.0 or later:
60  *
61  * This program is free software; you can redistribute it and/or modify
62  * it under the terms of the GNU General Public License as published by
63  * the Free Software Foundation; either version 2 of the License, or
64  * (at your option) any later version.
65  *
66  * This program is distributed in the hope that it will be useful,
67  * but WITHOUT ANY WARRANTY; without even the implied warranty of
68  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
69  * GNU General Public License for more details.
70  *
71  * You should have received a copy of the GNU General Public License along
72  * with this program; if not, write to the Free Software Foundation, Inc.,
73  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
74  *
75  * **********
76  *
77  * This file is part of Mbed TLS (https://tls.mbed.org)
78  */
79 
80 #ifndef MBEDTLS_CTR_DRBG_H
81 #define MBEDTLS_CTR_DRBG_H
82 
83 #if !defined(MBEDTLS_CONFIG_FILE)
84 #include "config.h"
85 #else
86 #include MBEDTLS_CONFIG_FILE
87 #endif
88 
89 #include "aes.h"
90 
91 #if defined(MBEDTLS_THREADING_C)
92 #include "threading.h"
93 #endif
94 
95 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
96 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
97 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
98 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
100 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
101 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
102 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
103 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
118 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
119 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
120 
123 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
124 
125 #else /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
126 
134 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
135 #endif /* defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256) */
136 #endif /* !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN) */
137 
138 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
139 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
140 
141 #endif
142 
143 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
144 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
145 
146 #endif
147 
148 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
149 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
150 
151 #endif
152 
153 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
154 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
155 
156 #endif
157 
158 /* \} name SECTION: Module settings */
159 
160 #define MBEDTLS_CTR_DRBG_PR_OFF 0
161 
162 #define MBEDTLS_CTR_DRBG_PR_ON 1
163 
165 #ifdef __cplusplus
166 extern "C" {
167 #endif
168 
172 typedef struct
173 {
174  unsigned char counter[16];
180  size_t entropy_len;
186  /*
187  * Callbacks (Entropy)
188  */
189  int (*f_entropy)(void *, unsigned char *, size_t);
192  void *p_entropy;
194 #if defined(MBEDTLS_THREADING_C)
196 #endif
197 }
199 
208 
243 #if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
244 
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 
322  size_t len );
323 
337  int interval );
338 
355  const unsigned char *additional, size_t len );
356 
373  const unsigned char *additional,
374  size_t add_len );
375 
394  const unsigned char *additional,
395  size_t add_len );
396 
423 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
424  unsigned char *output, size_t output_len,
425  const unsigned char *additional, size_t add_len );
426 
443 int mbedtls_ctr_drbg_random( void *p_rng,
444  unsigned char *output, size_t output_len );
445 
446 #if defined(MBEDTLS_FS_IO)
447 
458 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
459 
474 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
475 #endif /* MBEDTLS_FS_IO */
476 
482 int mbedtls_ctr_drbg_self_test( int verbose );
483 
484 /* Internal functions (do not call directly) */
486  int (*)(void *, unsigned char *, size_t), void *,
487  const unsigned char *, size_t, size_t );
488 
489 #ifdef __cplusplus
490 }
491 #endif
492 
493 #endif /* ctr_drbg.h */
The CTR_DRBG context structure.
Definition: ctr_drbg.h:172
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...
mbedtls_threading_mutex_t mutex
Definition: ctr_drbg.h:195
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.
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:184
The AES context-type definition.
Definition: aes.h:101