hmac_drbg.h
Go to the documentation of this file.
/*
* Copyright The Mbed TLS Contributors
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MBEDTLS_HMAC_DRBG_H
#define MBEDTLS_HMAC_DRBG_H
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "mbedtls/md.h"
#if defined(MBEDTLS_THREADING_C)
#include "mbedtls/threading.h"
#endif
/*
* Error codes
*/
#define MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0003
#define MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0005
#define MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR -0x0007
#define MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0009
#if !defined(MBEDTLS_HMAC_DRBG_RESEED_INTERVAL)
#define MBEDTLS_HMAC_DRBG_RESEED_INTERVAL 10000
#endif
#if !defined(MBEDTLS_HMAC_DRBG_MAX_INPUT)
#define MBEDTLS_HMAC_DRBG_MAX_INPUT 256
#endif
#if !defined(MBEDTLS_HMAC_DRBG_MAX_REQUEST)
#define MBEDTLS_HMAC_DRBG_MAX_REQUEST 1024
#endif
#if !defined(MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT)
#define MBEDTLS_HMAC_DRBG_MAX_SEED_INPUT 384
#endif
/* \} name SECTION: Module settings */
#define MBEDTLS_HMAC_DRBG_PR_OFF 0
#define MBEDTLS_HMAC_DRBG_PR_ON 1
#ifdef __cplusplus
extern "C" {
#endif
typedef struct mbedtls_hmac_drbg_context
{
/* Working state: the key K is not stored explicitly,
* but is implied by the HMAC context */
mbedtls_md_context_t md_ctx;
unsigned char V[MBEDTLS_MD_MAX_SIZE];
int reseed_counter;
/* Administrative state */
size_t entropy_len;
int prediction_resistance;
int reseed_interval;
/* Callbacks */
int (*f_entropy)(void *, unsigned char *, size_t);
void *p_entropy;
#if defined(MBEDTLS_THREADING_C)
/* Invariant: the mutex is initialized if and only if
* md_ctx->md_info != NULL. This means that the mutex is initialized
* during the initial seeding in mbedtls_hmac_drbg_seed() or
* mbedtls_hmac_drbg_seed_buf() and freed in mbedtls_ctr_drbg_free().
*
* Note that this invariant may change without notice. Do not rely on it
* and do not access the mutex directly in application code.
*/
mbedtls_threading_mutex_t mutex;
#endif
} mbedtls_hmac_drbg_context;
void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx );
#if defined(MBEDTLS_THREADING_C)
#endif /* MBEDTLS_THREADING_C */
int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx,
const mbedtls_md_info_t * md_info,
int (*f_entropy)(void *, unsigned char *, size_t),
void *p_entropy,
const unsigned char *custom,
size_t len );
#if defined(MBEDTLS_THREADING_C)
#endif /* MBEDTLS_THREADING_C */
int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context *ctx,
const mbedtls_md_info_t * md_info,
const unsigned char *data, size_t data_len );
void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx,
int resistance );
void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx,
size_t len );
void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context *ctx,
int interval );
int mbedtls_hmac_drbg_update_ret( mbedtls_hmac_drbg_context *ctx,
const unsigned char *additional, size_t add_len );
int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx,
const unsigned char *additional, size_t len );
int mbedtls_hmac_drbg_random_with_add( void *p_rng,
unsigned char *output, size_t output_len,
const unsigned char *additional,
size_t add_len );
#if defined(MBEDTLS_THREADING_C)
#endif /* MBEDTLS_THREADING_C */
int mbedtls_hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx );
#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
#if defined(MBEDTLS_DEPRECATED_WARNING)
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
#else
#define MBEDTLS_DEPRECATED
#endif
MBEDTLS_DEPRECATED void mbedtls_hmac_drbg_update(
mbedtls_hmac_drbg_context *ctx,
const unsigned char *additional, size_t add_len );
#undef MBEDTLS_DEPRECATED
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
#if defined(MBEDTLS_FS_IO)
int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path );
#endif /* MBEDTLS_FS_IO */
#if defined(MBEDTLS_SELF_TEST)
int mbedtls_hmac_drbg_self_test( int verbose );
#endif
#ifdef __cplusplus
}
#endif
#endif /* hmac_drbg.h */
config.h
Configuration options (set of defines)
mbedtls_hmac_drbg_update_seed_file
int mbedtls_hmac_drbg_update_seed_file(mbedtls_hmac_drbg_context *ctx, const char *path)
This function reads and updates a seed file. The seed is added to this instance.
mbedtls_hmac_drbg_set_prediction_resistance
void mbedtls_hmac_drbg_set_prediction_resistance(mbedtls_hmac_drbg_context *ctx, int resistance)
This function turns prediction resistance on or off. The default value is off.
mbedtls_hmac_drbg_seed
int mbedtls_hmac_drbg_seed(mbedtls_hmac_drbg_context *ctx, const mbedtls_md_info_t *md_info, int(*f_entropy)(void *, unsigned char *, size_t), void *p_entropy, const unsigned char *custom, size_t len)
HMAC_DRBG initial seeding.
mbedtls_hmac_drbg_reseed
int mbedtls_hmac_drbg_reseed(mbedtls_hmac_drbg_context *ctx, const unsigned char *additional, size_t len)
This function reseeds the HMAC_DRBG context, that is extracts data from the entropy source.
mbedtls_hmac_drbg_init
void mbedtls_hmac_drbg_init(mbedtls_hmac_drbg_context *ctx)
HMAC_DRBG context initialization.
mbedtls_hmac_drbg_set_reseed_interval
void mbedtls_hmac_drbg_set_reseed_interval(mbedtls_hmac_drbg_context *ctx, int interval)
Set the reseed interval.
mbedtls_hmac_drbg_self_test
int mbedtls_hmac_drbg_self_test(int verbose)
The HMAC_DRBG Checkup routine.
mbedtls_hmac_drbg_free
void mbedtls_hmac_drbg_free(mbedtls_hmac_drbg_context *ctx)
This function resets HMAC_DRBG context to the state immediately after initial call of mbedtls_hmac_dr...
mbedtls_hmac_drbg_seed_buf
int mbedtls_hmac_drbg_seed_buf(mbedtls_hmac_drbg_context *ctx, const mbedtls_md_info_t *md_info, const unsigned char *data, size_t data_len)
Initilisation of simpified HMAC_DRBG (never reseeds).
mbedtls_hmac_drbg_set_entropy_len
void mbedtls_hmac_drbg_set_entropy_len(mbedtls_hmac_drbg_context *ctx, size_t len)
This function sets the amount of entropy grabbed on each seed or reseed.
mbedtls_hmac_drbg_write_seed_file
int mbedtls_hmac_drbg_write_seed_file(mbedtls_hmac_drbg_context *ctx, const char *path)
This function writes a seed file.
mbedtls_hmac_drbg_random_with_add
int mbedtls_hmac_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 an HMAC_DRBG instance with additional data and uses it to generate random data.
mbedtls_hmac_drbg_random
int mbedtls_hmac_drbg_random(void *p_rng, unsigned char *output, size_t out_len)
This function uses HMAC_DRBG to generate random data.
MBEDTLS_DEPRECATED
#define MBEDTLS_DEPRECATED
Definition: hmac_drbg.h:406
mbedtls_hmac_drbg_update
MBEDTLS_DEPRECATED void mbedtls_hmac_drbg_update(mbedtls_hmac_drbg_context *ctx, const unsigned char *additional, size_t add_len)
This function updates the state of the HMAC_DRBG context.
mbedtls_hmac_drbg_update_ret
int mbedtls_hmac_drbg_update_ret(mbedtls_hmac_drbg_context *ctx, const unsigned char *additional, size_t add_len)
This function updates the state of the HMAC_DRBG context.
mbedtls_hmac_drbg_context
struct mbedtls_hmac_drbg_context mbedtls_hmac_drbg_context
md.h
This file contains the generic message-digest wrapper.
mbedtls_md_info_t
struct mbedtls_md_info_t mbedtls_md_info_t
Definition: md.h:90
MBEDTLS_MD_MAX_SIZE
#define MBEDTLS_MD_MAX_SIZE
Definition: md.h:76
mbedtls_hmac_drbg_context
Definition: hmac_drbg.h:90
mbedtls_hmac_drbg_context::entropy_len
size_t entropy_len
Definition: hmac_drbg.h:98
mbedtls_hmac_drbg_context::p_entropy
void * p_entropy
Definition: hmac_drbg.h:105
mbedtls_hmac_drbg_context::prediction_resistance
int prediction_resistance
Definition: hmac_drbg.h:99
mbedtls_hmac_drbg_context::reseed_counter
int reseed_counter
Definition: hmac_drbg.h:95
mbedtls_hmac_drbg_context::V
unsigned char V[MBEDTLS_MD_MAX_SIZE]
Definition: hmac_drbg.h:94
mbedtls_hmac_drbg_context::mutex
mbedtls_threading_mutex_t mutex
Definition: hmac_drbg.h:116
mbedtls_hmac_drbg_context::reseed_interval
int reseed_interval
Definition: hmac_drbg.h:101
mbedtls_hmac_drbg_context::f_entropy
int(* f_entropy)(void *, unsigned char *, size_t)
Definition: hmac_drbg.h:104
mbedtls_hmac_drbg_context::md_ctx
mbedtls_md_context_t md_ctx
Definition: hmac_drbg.h:93
mbedtls_md_context_t
Definition: md.h:96
mbedtls_threading_mutex_t
Definition: threading.h:50
threading.h
Threading abstraction layer.