blob: 059b885de4d15a36e302fdda4a0dfdd6221cb457 [file] [log] [blame]
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +01001/**
2 * \file hmac_drbg.h
3 *
4 * \brief HMAC_DRBG (NIST SP 800-90A)
5 *
6 * Copyright (C) 2014, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27#ifndef POLARSSL_HMAC_DRBG_H
28#define POLARSSL_HMAC_DRBG_H
29
30#include "md.h"
31
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010032/*
33 * ! Same values as ctr_drbg.h !
34 */
35#define POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
36#define POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */
37#define POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */
38#define POLARSSL_ERR_HMAC_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */
39
40#define HMAC_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
41#define HMAC_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
42#define HMAC_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
43#define HMAC_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
44
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010045#ifdef __cplusplus
46extern "C" {
47#endif
48
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010049/**
50 * HMAC_DRBG context.
51 * TODO: reseed counter, prediction resistance flag.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010052 */
53typedef struct
54{
55 md_context_t md_ctx;
56 unsigned char V[POLARSSL_MD_MAX_SIZE];
57 unsigned char K[POLARSSL_MD_MAX_SIZE];
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010058
59 size_t entropy_len; /*!< entropy bytes grabbed on each (re)seed */
60
61 /*
62 * Callbacks (Entropy)
63 */
64 int (*f_entropy)(void *, unsigned char *, size_t);
65 void *p_entropy; /*!< context for the entropy function */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010066} hmac_drbg_context;
67
68/**
69 * \brief HMAC_DRBG initialisation
70 *
71 * \param ctx HMAC_DRBG context to be initialised
72 * \param md_info MD algorithm to use for HMAC_DRBG
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010073 * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
74 * length)
75 * \param p_entropy Entropy context
76 * \param custom Personalization data (Device specific identifiers)
77 * (Can be NULL)
78 * \param len Length of personalization data
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010079 *
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010080 * \note The "security strength" as defined by NIST is set to:
81 * 128 bits if md_alg is SHA-1,
82 * 192 bits if md_alg is SHA-224,
83 * 256 bits if md_alg is SHA-256 or higher.
84 * Note that SHA-256 is just as efficient as SHA-224.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010085 *
86 * \return 0 if successful, or
87 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010088 * POLARSSL_ERR_MD_ALLOC_FAILED, or
89 * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010090 */
91int hmac_drbg_init( hmac_drbg_context *ctx,
92 const md_info_t * md_info,
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010093 int (*f_entropy)(void *, unsigned char *, size_t),
94 void *p_entropy,
95 const unsigned char *custom,
96 size_t len );
97
98/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +010099 * \brief Initilisation of simpified HMAC_DRBG (never reseeds).
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100100 * (For use with deterministic ECDSA.)
101 *
102 * \param ctx HMAC_DRBG context to be initialised
103 * \param md_info MD algorithm to use for HMAC_DRBG
104 * \param data Concatenation of entropy string and additional data
105 * \param data_len Length of data in bytes
106 *
107 * \return 0 if successful, or
108 * POLARSSL_ERR_MD_BAD_INPUT_DATA, or
109 * POLARSSL_ERR_MD_ALLOC_FAILED.
110 */
111int hmac_drbg_init_buf( hmac_drbg_context *ctx,
112 const md_info_t * md_info,
113 const unsigned char *data, size_t data_len );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100114
115/**
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100116 * \brief Set the amount of entropy grabbed on each reseed
117 * (Default: HMAC_DRBG_ENTROPY_LEN)
118 *
119 * \param ctx HMAC_DRBG context
120 * \param len Amount of entropy to grab
121 */
122void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx,
123 size_t len );
124
125/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100126 * \brief HMAC_DRBG update state
127 *
128 * \param ctx HMAC_DRBG context
129 * \param additional Additional data to update state with, or NULL
130 * \param add_len Length of additional data, or 0
131 *
132 * \note Additional data is optional, pass NULL and 0 as second
133 * third argument if no additional data is being used.
134 */
135void hmac_drbg_update( hmac_drbg_context *ctx,
136 const unsigned char *additional, size_t add_len );
137
138/**
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100139 * \brief HMAC_DRBG reseeding (extracts data from entropy source)
140 *
141 * \param ctx HMAC_DRBG context
142 * \param additional Additional data to add to state (Can be NULL)
143 * \param len Length of additional data
144 *
145 * \return 0 if successful, or
146 * POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED
147 */
148int hmac_drbg_reseed( hmac_drbg_context *ctx,
149 const unsigned char *additional, size_t len );
150
151/**
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100152 * \brief HMAC_DRBG generate random with additional update input
153 *
154 * Note: Automatically reseeds if reseed_counter is reached.
155 *
156 * \param p_rng HMAC_DRBG context
157 * \param output Buffer to fill
158 * \param output_len Length of the buffer
159 * \param additional Additional data to update with (can be NULL)
160 * \param add_len Length of additional data (can be 0)
161 *
162 * \return 0 if successful, or
163 * TODO: POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
164 * TODO: POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG
165 */
166int hmac_drbg_random_with_add( void *p_rng,
167 unsigned char *output, size_t output_len,
168 const unsigned char *additional,
169 size_t add_len );
170
171/**
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100172 * \brief HMAC_DRBG generate random
173 *
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100174 * Note: Automatically reseeds if reseed_counter is reached.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100175 *
176 * \param p_rng HMAC_DRBG context
177 * \param output Buffer to fill
178 * \param output_len Length of the buffer
179 *
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100180 * \return 0 if successful, or
181 * TODO: POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or
182 * TODO: POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100183 */
184int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len );
185
186/**
187 * \brief Free an HMAC_DRBG context
188 *
189 * \param ctx HMAC_DRBG context to free.
190 */
191void hmac_drbg_free( hmac_drbg_context *ctx );
192
193
194#if defined(POLARSSL_SELF_TEST)
195/**
196 * \brief Checkup routine
197 *
198 * \return 0 if successful, or 1 if the test failed
199 */
200int hmac_drbg_self_test( int verbose );
201#endif
202
203#ifdef __cplusplus
204}
205#endif
206
207#endif /* hmac_drbg.h */