OPTIGA Trust M  1.1.0
C++ library for Optiga Trust M Chip Security Controller
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: Apache-2.0
20  *
21  * Licensed under the Apache License, Version 2.0 (the "License"); you may
22  * not use this file except in compliance with the License.
23  * You may obtain a copy of the License at
24  *
25  * http://www.apache.org/licenses/LICENSE-2.0
26  *
27  * Unless required by applicable law or agreed to in writing, software
28  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30  * See the License for the specific language governing permissions and
31  * limitations under the License.
32  *
33  * This file is part of Mbed TLS (https://tls.mbed.org)
34  */
35 
36 #ifndef MBEDTLS_CTR_DRBG_H
37 #define MBEDTLS_CTR_DRBG_H
38 
39 #include "aes.h"
40 
41 #if defined(MBEDTLS_THREADING_C)
42 #include "threading.h"
43 #endif
44 
45 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
46 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
47 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
48 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
50 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
52 #if defined(MBEDTLS_CTR_DRBG_USE_128_BIT_KEY)
53 #define MBEDTLS_CTR_DRBG_KEYSIZE 16
54 #else
55 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
56 #endif
57 
58 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
59 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
70 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
71 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
72 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
73 
77 #else
78 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
79 
83 #endif
84 #endif
85 
86 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
87 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
88 
89 #endif
90 
91 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
92 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
93 
94 #endif
95 
96 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
97 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
98 
99 #endif
100 
101 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
102 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
103 
104 #endif
105 
106 /* \} name SECTION: Module settings */
107 
108 #define MBEDTLS_CTR_DRBG_PR_OFF 0
109 
110 #define MBEDTLS_CTR_DRBG_PR_ON 1
111 
113 #ifdef __cplusplus
114 extern "C" {
115 #endif
116 
121 {
122  unsigned char counter[16];
128  size_t entropy_len;
134  /*
135  * Callbacks (Entropy)
136  */
137  int (*f_entropy)(void *, unsigned char *, size_t);
140  void *p_entropy;
142 #if defined(MBEDTLS_THREADING_C)
143  mbedtls_threading_mutex_t mutex;
144 #endif
145 }
147 
156 
177  int (*f_entropy)(void *, unsigned char *, size_t),
178  void *p_entropy,
179  const unsigned char *custom,
180  size_t len );
181 
188 
202  int resistance );
203 
213  size_t len );
214 
223  int interval );
224 
237  const unsigned char *additional, size_t len );
238 
254  const unsigned char *additional,
255  size_t add_len );
256 
274 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
275  unsigned char *output, size_t output_len,
276  const unsigned char *additional, size_t add_len );
277 
292 int mbedtls_ctr_drbg_random( void *p_rng,
293  unsigned char *output, size_t output_len );
294 
295 
296 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
297 #if defined(MBEDTLS_DEPRECATED_WARNING)
298 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
299 #else
300 #define MBEDTLS_DEPRECATED
301 #endif
302 
319  const unsigned char *additional,
320  size_t add_len );
321 #undef MBEDTLS_DEPRECATED
322 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
323 
324 #if defined(MBEDTLS_FS_IO)
325 
336 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
337 
350 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
351 #endif /* MBEDTLS_FS_IO */
352 
359 int mbedtls_ctr_drbg_self_test( int verbose );
360 
361 /* Internal functions (do not call directly) */
363  int (*)(void *, unsigned char *, size_t), void *,
364  const unsigned char *, size_t, size_t );
365 
366 #ifdef __cplusplus
367 }
368 #endif
369 
370 #endif /* ctr_drbg.h */
int(* f_entropy)(void *, unsigned char *, size_t)
Definition: ctr_drbg.h:137
The CTR_DRBG context structure.
Definition: ctr_drbg.h:120
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)
int prediction_resistance
Definition: ctr_drbg.h:124
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:300
int mbedtls_ctr_drbg_random(void *p_rng, unsigned char *output, size_t output_len)
This function uses CTR_DRBG to generate random data.
void mbedtls_ctr_drbg_set_reseed_interval(mbedtls_ctr_drbg_context *ctx, int interval)
This function sets the reseed interval. The default value is MBEDTLS_CTR_DRBG_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_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 default value is MBEDTLS...
int reseed_interval
Definition: ctr_drbg.h:130
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.
size_t entropy_len
Definition: ctr_drbg.h:128
unsigned char counter[16]
Definition: ctr_drbg.h:122
mbedtls_aes_context aes_ctx
Definition: ctr_drbg.h:132
int reseed_counter
Definition: ctr_drbg.h:123
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:86
void * p_entropy
Definition: ctr_drbg.h:140