blob: 328ba9272e4ecbe03b26a07285b4003378c08014 [file] [log] [blame]
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +01001/*
2 * HMAC_DRBG implementation (NIST SP 800-90)
3 *
4 * Copyright (C) 2014, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26/*
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +010027 * The NIST SP 800-90A DRBGs are described in the following publication.
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010028 * http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +010029 * References below are based on rev. 1 (January 2012).
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010030 */
31
32#include "polarssl/config.h"
33
34#if defined(POLARSSL_HMAC_DRBG_C)
35
36#include "polarssl/hmac_drbg.h"
37
38/*
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +010039 * HMAC_DRBG update, using optional additional data (10.1.2.2)
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010040 */
41void hmac_drbg_update( hmac_drbg_context *ctx,
42 const unsigned char *additional, size_t add_len )
43{
44 size_t md_len = ctx->md_ctx.md_info->size;
45 unsigned char rounds = ( additional != NULL && add_len != 0 ) ? 2 : 1;
46 unsigned char sep[1];
47
48 for( sep[0] = 0; sep[0] < rounds; sep[0]++ )
49 {
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +010050 /* Step 1 or 4 */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010051 md_hmac_starts( &ctx->md_ctx, ctx->K, md_len );
52 md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
53 md_hmac_update( &ctx->md_ctx, sep, 1 );
54 if( rounds == 2 )
55 md_hmac_update( &ctx->md_ctx, additional, add_len );
56 md_hmac_finish( &ctx->md_ctx, ctx->K );
57
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +010058 /* Step 2 or 5 */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010059 md_hmac_starts( &ctx->md_ctx, ctx->K, md_len );
60 md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
61 md_hmac_finish( &ctx->md_ctx, ctx->V );
62 }
63}
64
65/*
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010066 * Simplified HMAC_DRBG initialisation (for use with deterministic ECDSA)
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010067 */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +010068int hmac_drbg_init_buf( hmac_drbg_context *ctx,
69 const md_info_t * md_info,
70 const unsigned char *data, size_t data_len )
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +010071{
72 int ret;
73
74 memset( ctx, 0, sizeof( hmac_drbg_context ) );
75
76 if( ( ret = md_init_ctx( &ctx->md_ctx, md_info ) ) != 0 )
77 return( ret );
78
79 memset( ctx->V, 0x01, md_info->size );
80 /* ctx->K is already 0 */
81
82 hmac_drbg_update( ctx, data, data_len );
83
84 return( 0 );
85}
86
87/*
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +010088 * HMAC_DRBG reseeding (10.1.2.4)
89 */
90int hmac_drbg_reseed( hmac_drbg_context *ctx,
91 const unsigned char *additional, size_t len )
92{
93 unsigned char seed[HMAC_DRBG_MAX_SEED_INPUT];
94 size_t seedlen;
95
96 if( ctx->entropy_len + len > HMAC_DRBG_MAX_SEED_INPUT )
97 return( POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG );
98
99 memset( seed, 0, HMAC_DRBG_MAX_SEED_INPUT );
100
101 /* 1a. Gather entropy_len bytes of entropy for the seed */
102 if( ctx->f_entropy( ctx->p_entropy, seed, ctx->entropy_len ) != 0 )
103 return( POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED );
104
105 seedlen = ctx->entropy_len;
106
107 /* 1b. Append additional data if any */
108 if( additional != NULL && len != 0 )
109 {
110 memcpy( seed + seedlen, additional, len );
111 seedlen += len;
112 }
113
114 /* 2. Update state */
115 hmac_drbg_update( ctx, seed, seedlen );
116
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100117 /* 3. Reset reseed_counter */
118 ctx->reseed_counter = 1;
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100119
120 /* 4. Done */
121 return( 0 );
122}
123
124/*
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100125 * HMAC_DRBG initialisation
126 */
127int hmac_drbg_init( hmac_drbg_context *ctx,
128 const md_info_t * md_info,
129 int (*f_entropy)(void *, unsigned char *, size_t),
130 void *p_entropy,
131 const unsigned char *custom,
132 size_t len )
133{
134 int ret;
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100135 size_t entropy_len;
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100136
137 memset( ctx, 0, sizeof( hmac_drbg_context ) );
138
139 if( ( ret = md_init_ctx( &ctx->md_ctx, md_info ) ) != 0 )
140 return( ret );
141
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100142 /* Set initial working state */
143 memset( ctx->V, 0x01, md_info->size );
144 /* ctx->K is already 0 */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100145
146 ctx->f_entropy = f_entropy;
147 ctx->p_entropy = p_entropy;
148
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100149 ctx->reseed_interval = HMAC_DRBG_RESEED_INTERVAL;
150
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100151 /*
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100152 * See SP800-57 5.6.1 (p. 65-66) for the security strength provided by
153 * each hash function, then according to SP800-90A rev1 10.1 table 2,
154 * min_entropy_len (in bits) is security_strength.
155 *
156 * (This also matches the sizes used in the NIST test vectors.)
157 */
158 entropy_len = md_info->size <= 20 ? 16 : /* 160-bits hash -> 128 bits */
159 md_info->size <= 28 ? 24 : /* 224-bits hash -> 192 bits */
160 32; /* better (256+) -> 256 bits */
161
162 /*
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100163 * For initialisation, use more entropy to emulate a nonce
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100164 * (Again, matches test vectors.)
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100165 */
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100166 ctx->entropy_len = entropy_len * 3 / 2;
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100167
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100168 if( ( ret = hmac_drbg_reseed( ctx, custom, len ) ) != 0 )
169 return( ret );
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100170
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100171 ctx->entropy_len = entropy_len;
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100172
173 return( 0 );
174}
175
176/*
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100177 * Set prediction resistance
178 */
179void hmac_drbg_set_prediction_resistance( hmac_drbg_context *ctx,
180 int resistance )
181{
182 ctx->prediction_resistance = resistance;
183}
184
185/*
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100186 * Set entropy length grabbed for reseeds
187 */
188void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx, size_t len )
189{
190 ctx->entropy_len = len;
191}
192
193/*
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100194 * Set reseed interval
195 */
196void hmac_drbg_set_reseed_interval( hmac_drbg_context *ctx, int interval )
197{
198 ctx->reseed_interval = interval;
199}
200
201/*
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100202 * HMAC_DRBG random function with optional additional data (10.1.2.5)
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100203 */
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100204int hmac_drbg_random_with_add( void *p_rng,
205 unsigned char *output, size_t out_len,
206 const unsigned char *additional, size_t add_len )
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100207{
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100208 int ret;
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100209 hmac_drbg_context *ctx = (hmac_drbg_context *) p_rng;
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100210 size_t md_len = md_get_size( ctx->md_ctx.md_info );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100211 size_t left = out_len;
212 unsigned char *out = output;
213
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100214 /* 1. Check reseed counter and PR */
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100215 if( ctx->f_entropy != NULL &&
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100216 ( ctx->prediction_resistance == HMAC_DRBG_PR_ON ||
217 ctx->reseed_counter > ctx->reseed_interval ) )
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100218 {
219 if( ( ret = hmac_drbg_reseed( ctx, additional, add_len ) ) != 0 )
220 return( ret );
221 }
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100222
223 /* 2. Use additional data if any */
224 if( additional != NULL && add_len != 0 )
225 hmac_drbg_update( ctx, additional, add_len );
226
227 /* 3, 4, 5. Generate bytes */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100228 while( left != 0 )
229 {
230 size_t use_len = left > md_len ? md_len : left;
231
232 md_hmac_starts( &ctx->md_ctx, ctx->K, md_len );
233 md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
234 md_hmac_finish( &ctx->md_ctx, ctx->V );
235
236 memcpy( out, ctx->V, use_len );
237 out += use_len;
238 left -= use_len;
239 }
240
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100241 /* 6. Update */
242 hmac_drbg_update( ctx, additional, add_len );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100243
Manuel Pégourié-Gonnard658dbed2014-01-30 19:03:45 +0100244 /* 7. Update reseed counter */
245 ctx->reseed_counter++;
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100246
247 /* 8. Done */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100248 return( 0 );
249}
250
251/*
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100252 * HMAC_DRBG random function
253 */
254int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len )
255{
256 return( hmac_drbg_random_with_add( p_rng, output, out_len, NULL, 0 ) );
257}
258
259/*
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100260 * Free an HMAC_DRBG context
261 */
262void hmac_drbg_free( hmac_drbg_context *ctx )
263{
264 if( ctx == NULL )
265 return;
266
267 md_free_ctx( &ctx->md_ctx );
268
269 memset( ctx, 0, sizeof( hmac_drbg_context ) );
270}
271
272
273#if defined(POLARSSL_SELF_TEST)
274
275#include <stdio.h>
276
277/*
278 * Checkup routine
279 */
280int hmac_drbg_self_test( int verbose )
281{
282
283 if( verbose != 0 )
284 printf( "\n" );
285
286 return( 0 );
287}
288#endif /* POLARSSL_SELF_TEST */
289
290#endif /* POLARSSL_HMAC_DRBG_C */