--- /dev/null
+/**\r
+ * \file hmac_drbg.h\r
+ *\r
+ * \brief HMAC_DRBG (NIST SP 800-90A)\r
+ */\r
+/*\r
+ * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may\r
+ * not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * This file is part of mbed TLS (https://tls.mbed.org)\r
+ */\r
+#ifndef MBEDTLS_HMAC_DRBG_H\r
+#define MBEDTLS_HMAC_DRBG_H\r
+\r
+#if !defined(MBEDTLS_CONFIG_FILE)\r
+#include "config.h"\r
+#else\r
+#include MBEDTLS_CONFIG_FILE\r
+#endif\r
+\r
+#include "md.h"\r
+\r
+#if defined(MBEDTLS_THREADING_C)\r
+#include "threading.h"\r
+#endif\r
+\r
+/*\r
+ * Error codes\r
+ */\r
+#define MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003 /**< Too many random requested in single call. */\r
+#define MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005 /**< Input too large (Entropy + additional). */\r
+#define MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007 /**< Read/write error in file. */\r
+#define MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0009 /**< The entropy source failed. */\r
+\r
+/**\r
+ * \name SECTION: Module settings\r
+ *\r
+ * The configuration options you can set for this module are in this section.\r
+ * Either change them in config.h or define them on the compiler command line.\r
+ * \{\r
+ */\r
+\r
+#if !defined(MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)\r
+#define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */\r
+#endif\r
+\r
+#if !defined(MBEDTLS_HMAC_DRBG_MAX_INPUT)\r
+#define MBEDTLS_HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */\r
+#endif\r
+\r
+#if !defined(MBEDTLS_HMAC_DRBG_MAX_REQUEST)\r
+#define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */\r
+#endif\r
+\r
+#if !defined(MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT)\r
+#define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */\r
+#endif\r
+\r
+/* \} name SECTION: Module settings */\r
+\r
+#define MBEDTLS_HMAC_DRBG_PR_OFF 0 /**< No prediction resistance */\r
+#define MBEDTLS_HMAC_DRBG_PR_ON 1 /**< Prediction resistance enabled */\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+/**\r
+ * HMAC_DRBG context.\r
+ */\r
+typedef struct mbedtls_hmac_drbg_context\r
+{\r
+ /* Working state: the key K is not stored explicitly,\r
+ * but is implied by the HMAC context */\r
+ mbedtls_md_context_t md_ctx; /*!< HMAC context (inc. K) */\r
+ unsigned char V[MBEDTLS_MD_MAX_SIZE]; /*!< V in the spec */\r
+ int reseed_counter; /*!< reseed counter */\r
+\r
+ /* Administrative state */\r
+ size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */\r
+ int prediction_resistance; /*!< enable prediction resistance (Automatic\r
+ reseed before every random generation) */\r
+ int reseed_interval; /*!< reseed interval */\r
+\r
+ /* Callbacks */\r
+ int (*f_entropy)(void *, unsigned char *, size_t); /*!< entropy function */\r
+ void *p_entropy; /*!< context for the entropy function */\r
+\r
+#if defined(MBEDTLS_THREADING_C)\r
+ mbedtls_threading_mutex_t mutex;\r
+#endif\r
+} mbedtls_hmac_drbg_context;\r
+\r
+/**\r
+ * \brief HMAC_DRBG context initialization\r
+ * Makes the context ready for mbedtls_hmac_drbg_seed(),\r
+ * mbedtls_hmac_drbg_seed_buf() or\r
+ * mbedtls_hmac_drbg_free().\r
+ *\r
+ * \param ctx HMAC_DRBG context to be initialized\r
+ */\r
+void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx );\r
+\r
+/**\r
+ * \brief HMAC_DRBG initial seeding\r
+ * Seed and setup entropy source for future reseeds.\r
+ *\r
+ * \param ctx HMAC_DRBG context to be seeded\r
+ * \param md_info MD algorithm to use for HMAC_DRBG\r
+ * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer\r
+ * length)\r
+ * \param p_entropy Entropy context\r
+ * \param custom Personalization data (Device specific identifiers)\r
+ * (Can be NULL)\r
+ * \param len Length of personalization data\r
+ *\r
+ * \note The "security strength" as defined by NIST is set to:\r
+ * 128 bits if md_alg is SHA-1,\r
+ * 192 bits if md_alg is SHA-224,\r
+ * 256 bits if md_alg is SHA-256 or higher.\r
+ * Note that SHA-256 is just as efficient as SHA-224.\r
+ *\r
+ * \return 0 if successful, or\r
+ * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or\r
+ * MBEDTLS_ERR_MD_ALLOC_FAILED, or\r
+ * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED.\r
+ */\r
+int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx,\r
+ const mbedtls_md_info_t * md_info,\r
+ int (*f_entropy)(void *, unsigned char *, size_t),\r
+ void *p_entropy,\r
+ const unsigned char *custom,\r
+ size_t len );\r
+\r
+/**\r
+ * \brief Initilisation of simpified HMAC_DRBG (never reseeds).\r
+ * (For use with deterministic ECDSA.)\r
+ *\r
+ * \param ctx HMAC_DRBG context to be initialised\r
+ * \param md_info MD algorithm to use for HMAC_DRBG\r
+ * \param data Concatenation of entropy string and additional data\r
+ * \param data_len Length of data in bytes\r
+ *\r
+ * \return 0 if successful, or\r
+ * MBEDTLS_ERR_MD_BAD_INPUT_DATA, or\r
+ * MBEDTLS_ERR_MD_ALLOC_FAILED.\r
+ */\r
+int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context *ctx,\r
+ const mbedtls_md_info_t * md_info,\r
+ const unsigned char *data, size_t data_len );\r
+\r
+/**\r
+ * \brief Enable / disable prediction resistance (Default: Off)\r
+ *\r
+ * Note: If enabled, entropy is used for ctx->entropy_len before each call!\r
+ * Only use this if you have ample supply of good entropy!\r
+ *\r
+ * \param ctx HMAC_DRBG context\r
+ * \param resistance MBEDTLS_HMAC_DRBG_PR_ON or MBEDTLS_HMAC_DRBG_PR_OFF\r
+ */\r
+void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx,\r
+ int resistance );\r
+\r
+/**\r
+ * \brief Set the amount of entropy grabbed on each reseed\r
+ * (Default: given by the security strength, which\r
+ * depends on the hash used, see \c mbedtls_hmac_drbg_init() )\r
+ *\r
+ * \param ctx HMAC_DRBG context\r
+ * \param len Amount of entropy to grab, in bytes\r
+ */\r
+void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx,\r
+ size_t len );\r
+\r
+/**\r
+ * \brief Set the reseed interval\r
+ * (Default: MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)\r
+ *\r
+ * \param ctx HMAC_DRBG context\r
+ * \param interval Reseed interval\r
+ */\r
+void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context *ctx,\r
+ int interval );\r
+\r
+/**\r
+ * \brief HMAC_DRBG update state\r
+ *\r
+ * \param ctx HMAC_DRBG context\r
+ * \param additional Additional data to update state with, or NULL\r
+ * \param add_len Length of additional data, or 0\r
+ *\r
+ * \return \c 0 on success, or an error from the underlying\r
+ * hash calculation.\r
+ *\r
+ * \note Additional data is optional, pass NULL and 0 as second\r
+ * third argument if no additional data is being used.\r
+ */\r
+int mbedtls_hmac_drbg_update_ret( mbedtls_hmac_drbg_context *ctx,\r
+ const unsigned char *additional, size_t add_len );\r
+\r
+/**\r
+ * \brief HMAC_DRBG reseeding (extracts data from entropy source)\r
+ *\r
+ * \param ctx HMAC_DRBG context\r
+ * \param additional Additional data to add to state (Can be NULL)\r
+ * \param len Length of additional data\r
+ *\r
+ * \return 0 if successful, or\r
+ * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED\r
+ */\r
+int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx,\r
+ const unsigned char *additional, size_t len );\r
+\r
+/**\r
+ * \brief HMAC_DRBG generate random with additional update input\r
+ *\r
+ * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.\r
+ *\r
+ * \param p_rng HMAC_DRBG context\r
+ * \param output Buffer to fill\r
+ * \param output_len Length of the buffer\r
+ * \param additional Additional data to update with (can be NULL)\r
+ * \param add_len Length of additional data (can be 0)\r
+ *\r
+ * \return 0 if successful, or\r
+ * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or\r
+ * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG, or\r
+ * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG.\r
+ */\r
+int mbedtls_hmac_drbg_random_with_add( void *p_rng,\r
+ unsigned char *output, size_t output_len,\r
+ const unsigned char *additional,\r
+ size_t add_len );\r
+\r
+/**\r
+ * \brief HMAC_DRBG generate random\r
+ *\r
+ * Note: Automatically reseeds if reseed_counter is reached or PR is enabled.\r
+ *\r
+ * \param p_rng HMAC_DRBG context\r
+ * \param output Buffer to fill\r
+ * \param out_len Length of the buffer\r
+ *\r
+ * \return 0 if successful, or\r
+ * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or\r
+ * MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG\r
+ */\r
+int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );\r
+\r
+/**\r
+ * \brief Free an HMAC_DRBG context\r
+ *\r
+ * \param ctx HMAC_DRBG context to free.\r
+ */\r
+void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx );\r
+\r
+#if ! defined(MBEDTLS_DEPRECATED_REMOVED)\r
+#if defined(MBEDTLS_DEPRECATED_WARNING)\r
+#define MBEDTLS_DEPRECATED __attribute__((deprecated))\r
+#else\r
+#define MBEDTLS_DEPRECATED\r
+#endif\r
+/**\r
+ * \brief HMAC_DRBG update state\r
+ *\r
+ * \deprecated Superseded by mbedtls_hmac_drbg_update_ret()\r
+ * in 2.16.0.\r
+ *\r
+ * \param ctx HMAC_DRBG context\r
+ * \param additional Additional data to update state with, or NULL\r
+ * \param add_len Length of additional data, or 0\r
+ *\r
+ * \note Additional data is optional, pass NULL and 0 as second\r
+ * third argument if no additional data is being used.\r
+ */\r
+MBEDTLS_DEPRECATED void mbedtls_hmac_drbg_update(\r
+ mbedtls_hmac_drbg_context *ctx,\r
+ const unsigned char *additional, size_t add_len );\r
+#undef MBEDTLS_DEPRECATED\r
+#endif /* !MBEDTLS_DEPRECATED_REMOVED */\r
+\r
+#if defined(MBEDTLS_FS_IO)\r
+/**\r
+ * \brief Write a seed file\r
+ *\r
+ * \param ctx HMAC_DRBG context\r
+ * \param path Name of the file\r
+ *\r
+ * \return 0 if successful, 1 on file error, or\r
+ * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED\r
+ */\r
+int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );\r
+\r
+/**\r
+ * \brief Read and update a seed file. Seed is added to this\r
+ * instance\r
+ *\r
+ * \param ctx HMAC_DRBG context\r
+ * \param path Name of the file\r
+ *\r
+ * \return 0 if successful, 1 on file error,\r
+ * MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED or\r
+ * MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG\r
+ */\r
+int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );\r
+#endif /* MBEDTLS_FS_IO */\r
+\r
+\r
+#if defined(MBEDTLS_SELF_TEST)\r
+/**\r
+ * \brief Checkup routine\r
+ *\r
+ * \return 0 if successful, or 1 if the test failed\r
+ */\r
+int mbedtls_hmac_drbg_self_test( int verbose );\r
+#endif\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+#endif /* hmac_drbg.h */\r