blob: 096bff8bcb231a6117e4b9d9c6e186265ca823b1 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/**
2 * \file entropy.h
3 *
4 * \brief Entropy accumulator implementation
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker6083fd22011-12-03 21:45:14 +00009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#ifndef MBEDTLS_ENTROPY_H
11#define MBEDTLS_ENTROPY_H
Paul Bakker6083fd22011-12-03 21:45:14 +000012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010014#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020015#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020017#endif
Paul Bakker28c7e7f2011-12-15 19:49:30 +000018
Rich Evans00ab4702015-02-06 13:43:58 +000019#include <stddef.h>
20
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010022#include "mbedtls/sha512.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#define MBEDTLS_ENTROPY_SHA512_ACCUMULATOR
Paul Bakkerfb08fd22013-08-27 15:06:26 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if defined(MBEDTLS_SHA256_C)
26#define MBEDTLS_ENTROPY_SHA256_ACCUMULATOR
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010027#include "mbedtls/sha256.h"
Paul Bakkerfb08fd22013-08-27 15:06:26 +020028#endif
29#endif
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_THREADING_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010032#include "mbedtls/threading.h"
Paul Bakkerf4e7dc52013-09-28 15:23:57 +020033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_HAVEGE_C)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/havege.h"
Paul Bakker28c7e7f2011-12-15 19:49:30 +000037#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000038
Gilles Peskinea3974432021-07-26 18:48:10 +020039/** Critical entropy source failure. */
40#define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED -0x003C
41/** No more sources can be added. */
42#define MBEDTLS_ERR_ENTROPY_MAX_SOURCES -0x003E
43/** No sources have been added to poll. */
44#define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040
45/** No strong sources have been added to poll. */
46#define MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE -0x003D
47/** Read/write error in file. */
48#define MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR -0x003F
Paul Bakker6083fd22011-12-03 21:45:14 +000049
Paul Bakker088c5c52014-04-25 11:11:10 +020050/**
51 * \name SECTION: Module settings
52 *
53 * The configuration options you can set for this module are in this section.
54 * Either change them in config.h or define them on the compiler command line.
55 * \{
56 */
57
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if !defined(MBEDTLS_ENTROPY_MAX_SOURCES)
59#define MBEDTLS_ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
Paul Bakker088c5c52014-04-25 11:11:10 +020060#endif
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if !defined(MBEDTLS_ENTROPY_MAX_GATHER)
63#define MBEDTLS_ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
Paul Bakker088c5c52014-04-25 11:11:10 +020064#endif
65
Andrzej Kurek73afe272022-01-24 10:31:06 -050066/** \} name SECTION: Module settings */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
69#define MBEDTLS_ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020070#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#define MBEDTLS_ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
Paul Bakkerfb08fd22013-08-27 15:06:26 +020072#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#define MBEDTLS_ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
75#define MBEDTLS_ENTROPY_SOURCE_MANUAL MBEDTLS_ENTROPY_MAX_SOURCES
Paul Bakker6083fd22011-12-03 21:45:14 +000076
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +020077#define MBEDTLS_ENTROPY_SOURCE_STRONG 1 /**< Entropy source is strong */
78#define MBEDTLS_ENTROPY_SOURCE_WEAK 0 /**< Entropy source is weak */
79
Paul Bakker6083fd22011-12-03 21:45:14 +000080#ifdef __cplusplus
81extern "C" {
82#endif
83
84/**
85 * \brief Entropy poll callback pointer
86 *
87 * \param data Callback-specific data pointer
88 * \param output Data to fill
89 * \param len Maximum size to provide
90 * \param olen The actual amount of bytes put into the buffer (Can be 0)
91 *
92 * \return 0 if no critical failures occurred,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED otherwise
Paul Bakker6083fd22011-12-03 21:45:14 +000094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095typedef int (*mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 size_t *olen);
Paul Bakker6083fd22011-12-03 21:45:14 +000097
98/**
Paul Bakkerbd4a9d02011-12-10 17:02:19 +000099 * \brief Entropy source state
100 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101typedef struct mbedtls_entropy_source_state {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_entropy_f_source_ptr f_source; /**< The entropy source callback */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 void *p_source; /**< The callback data pointer */
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200104 size_t size; /**< Amount received in bytes */
105 size_t threshold; /**< Minimum bytes required before release */
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200106 int strong; /**< Is the source strong? */
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000107}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108mbedtls_entropy_source_state;
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000109
110/**
Paul Bakker6083fd22011-12-03 21:45:14 +0000111 * \brief Entropy context structure
112 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113typedef struct mbedtls_entropy_context {
Gilles Peskineb1583212021-02-22 21:26:54 +0100114 int accumulator_started; /* 0 after init.
115 * 1 after the first update.
116 * -1 after free. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
118 mbedtls_sha512_context accumulator;
Tom Cosgrove58efe612021-11-15 09:59:53 +0000119#elif defined(MBEDTLS_ENTROPY_SHA256_ACCUMULATOR)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 mbedtls_sha256_context accumulator;
Paul Bakkerfb08fd22013-08-27 15:06:26 +0200121#endif
Gilles Peskineb1583212021-02-22 21:26:54 +0100122 int source_count; /* Number of entries used in source. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_entropy_source_state source[MBEDTLS_ENTROPY_MAX_SOURCES];
124#if defined(MBEDTLS_HAVEGE_C)
125 mbedtls_havege_state havege_data;
Paul Bakker28c7e7f2011-12-15 19:49:30 +0000126#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#if defined(MBEDTLS_THREADING_C)
128 mbedtls_threading_mutex_t mutex; /*!< mutex */
Paul Bakkerf4e7dc52013-09-28 15:23:57 +0200129#endif
Paul Bakker96029232016-06-01 15:25:50 +0100130#if defined(MBEDTLS_ENTROPY_NV_SEED)
131 int initial_entropy_run;
132#endif
Paul Bakker6083fd22011-12-03 21:45:14 +0000133}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134mbedtls_entropy_context;
Paul Bakker6083fd22011-12-03 21:45:14 +0000135
136/**
137 * \brief Initialize the context
138 *
139 * \param ctx Entropy context to initialize
140 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141void mbedtls_entropy_init(mbedtls_entropy_context *ctx);
Paul Bakker6083fd22011-12-03 21:45:14 +0000142
143/**
Paul Bakker1ffefac2013-09-28 15:23:03 +0200144 * \brief Free the data in the context
145 *
146 * \param ctx Entropy context to free
147 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148void mbedtls_entropy_free(mbedtls_entropy_context *ctx);
Paul Bakker1ffefac2013-09-28 15:23:03 +0200149
150/**
Paul Bakker6083fd22011-12-03 21:45:14 +0000151 * \brief Adds an entropy source to poll
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000153 *
154 * \param ctx Entropy context
155 * \param f_source Entropy function
156 * \param p_source Function data
Paul Bakkerbd4a9d02011-12-10 17:02:19 +0000157 * \param threshold Minimum required from source before entropy is released
Manuel Pégourié-Gonnardbf82ff02015-06-19 09:40:51 +0200158 * ( with mbedtls_entropy_func() ) (in bytes)
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200159 * \param strong MBEDTLS_ENTROPY_SOURCE_STRONG or
Darryl Green11999bb2018-03-13 15:22:58 +0000160 * MBEDTLS_ENTROPY_SOURCE_WEAK.
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200161 * At least one strong source needs to be added.
162 * Weaker sources (such as the cycle counter) can be used as
163 * a complement.
Paul Bakker6083fd22011-12-03 21:45:14 +0000164 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 * \return 0 if successful or MBEDTLS_ERR_ENTROPY_MAX_SOURCES
Paul Bakker6083fd22011-12-03 21:45:14 +0000166 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx,
168 mbedtls_entropy_f_source_ptr f_source, void *p_source,
169 size_t threshold, int strong);
Paul Bakker6083fd22011-12-03 21:45:14 +0000170
171/**
172 * \brief Trigger an extra gather poll for the accumulator
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000174 *
175 * \param ctx Entropy context
176 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Paul Bakker6083fd22011-12-03 21:45:14 +0000178 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179int mbedtls_entropy_gather(mbedtls_entropy_context *ctx);
Paul Bakker6083fd22011-12-03 21:45:14 +0000180
181/**
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200182 * \brief Retrieve entropy from the accumulator
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 * (Maximum length: MBEDTLS_ENTROPY_BLOCK_SIZE)
184 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker6083fd22011-12-03 21:45:14 +0000185 *
186 * \param data Entropy context
187 * \param output Buffer to fill
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 * \param len Number of bytes desired, must be at most MBEDTLS_ENTROPY_BLOCK_SIZE
Paul Bakker6083fd22011-12-03 21:45:14 +0000189 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 * \return 0 if successful, or MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Paul Bakker6083fd22011-12-03 21:45:14 +0000191 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192int mbedtls_entropy_func(void *data, unsigned char *output, size_t len);
Paul Bakker6083fd22011-12-03 21:45:14 +0000193
194/**
195 * \brief Add data to the accumulator manually
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 * (Thread-safe if MBEDTLS_THREADING_C is enabled)
Paul Bakker9af723c2014-05-01 13:03:14 +0200197 *
Paul Bakker6083fd22011-12-03 21:45:14 +0000198 * \param ctx Entropy context
199 * \param data Data to add
200 * \param len Length of data
201 *
202 * \return 0 if successful
203 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
205 const unsigned char *data, size_t len);
Paul Bakker6083fd22011-12-03 21:45:14 +0000206
Paul Bakker7da30712016-06-01 11:30:54 +0100207#if defined(MBEDTLS_ENTROPY_NV_SEED)
208/**
209 * \brief Trigger an update of the seed file in NV by using the
210 * current entropy pool.
211 *
212 * \param ctx Entropy context
213 *
214 * \return 0 if successful
215 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx);
Paul Bakker7da30712016-06-01 11:30:54 +0100217#endif /* MBEDTLS_ENTROPY_NV_SEED */
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#if defined(MBEDTLS_FS_IO)
Paul Bakker66ff70d2014-03-26 11:54:05 +0100220/**
221 * \brief Write a seed file
222 *
223 * \param ctx Entropy context
224 * \param path Name of the file
225 *
226 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error, or
228 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Paul Bakker66ff70d2014-03-26 11:54:05 +0100229 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path);
Paul Bakker66ff70d2014-03-26 11:54:05 +0100231
232/**
233 * \brief Read and update a seed file. Seed is added to this
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 * instance. No more than MBEDTLS_ENTROPY_MAX_SEED_SIZE bytes are
Paul Bakker66ff70d2014-03-26 11:54:05 +0100235 * read from the seed file. The rest is ignored.
236 *
237 * \param ctx Entropy context
238 * \param path Name of the file
239 *
240 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 * MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR on file error,
242 * MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Paul Bakker66ff70d2014-03-26 11:54:05 +0100243 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#endif /* MBEDTLS_FS_IO */
Paul Bakker66ff70d2014-03-26 11:54:05 +0100246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_SELF_TEST)
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200248/**
249 * \brief Checkup routine
250 *
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100251 * This module self-test also calls the entropy self-test,
252 * mbedtls_entropy_source_self_test();
253 *
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200254 * \return 0 if successful, or 1 if a test failed
255 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256int mbedtls_entropy_self_test(int verbose);
Andres AGb34e42e2016-08-22 11:08:50 +0100257
Andres AGe7723ec2016-08-25 10:18:50 +0100258#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
Andres AGb34e42e2016-08-22 11:08:50 +0100259/**
260 * \brief Checkup routine
261 *
Andres Amaya Garciaa928e672016-09-13 13:30:02 +0100262 * Verifies the integrity of the hardware entropy source
263 * provided by the function 'mbedtls_hardware_poll()'.
264 *
265 * Note this is the only hardware entropy source that is known
266 * at link time, and other entropy sources configured
267 * dynamically at runtime by the function
268 * mbedtls_entropy_add_source() will not be tested.
269 *
Andres AGb34e42e2016-08-22 11:08:50 +0100270 * \return 0 if successful, or 1 if a test failed
271 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272int mbedtls_entropy_source_self_test(int verbose);
Andres AGe7723ec2016-08-25 10:18:50 +0100273#endif /* MBEDTLS_ENTROPY_HARDWARE_ALT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#endif /* MBEDTLS_SELF_TEST */
Manuel Pégourié-Gonnard4dd73922014-05-30 10:34:15 +0200275
Paul Bakker6083fd22011-12-03 21:45:14 +0000276#ifdef __cplusplus
277}
278#endif
279
280#endif /* entropy.h */