blob: 808be614714c1adb0b9af657329428d9b8aa24c7 [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
117 /* 3. Reset reseed_counter (TODO) */
118
119 /* 4. Done */
120 return( 0 );
121}
122
123/*
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100124 * HMAC_DRBG initialisation
125 */
126int hmac_drbg_init( hmac_drbg_context *ctx,
127 const md_info_t * md_info,
128 int (*f_entropy)(void *, unsigned char *, size_t),
129 void *p_entropy,
130 const unsigned char *custom,
131 size_t len )
132{
133 int ret;
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100134 size_t entropy_len;
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100135
136 memset( ctx, 0, sizeof( hmac_drbg_context ) );
137
138 if( ( ret = md_init_ctx( &ctx->md_ctx, md_info ) ) != 0 )
139 return( ret );
140
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100141 /* Set initial working state */
142 memset( ctx->V, 0x01, md_info->size );
143 /* ctx->K is already 0 */
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100144
145 ctx->f_entropy = f_entropy;
146 ctx->p_entropy = p_entropy;
147
148 /*
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100149 * See SP800-57 5.6.1 (p. 65-66) for the security strength provided by
150 * each hash function, then according to SP800-90A rev1 10.1 table 2,
151 * min_entropy_len (in bits) is security_strength.
152 *
153 * (This also matches the sizes used in the NIST test vectors.)
154 */
155 entropy_len = md_info->size <= 20 ? 16 : /* 160-bits hash -> 128 bits */
156 md_info->size <= 28 ? 24 : /* 224-bits hash -> 192 bits */
157 32; /* better (256+) -> 256 bits */
158
159 /*
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100160 * For initialisation, use more entropy to emulate a nonce
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100161 * (Again, matches test vectors.)
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100162 */
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100163 ctx->entropy_len = entropy_len * 3 / 2;
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100164
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100165 if( ( ret = hmac_drbg_reseed( ctx, custom, len ) ) != 0 )
166 return( ret );
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100167
Manuel Pégourié-Gonnard8fc484d2014-01-30 18:28:09 +0100168 ctx->entropy_len = entropy_len;
Manuel Pégourié-Gonnardfe34a5f2014-01-30 15:06:40 +0100169
170 return( 0 );
171}
172
173/*
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100174 * Set prediction resistance
175 */
176void hmac_drbg_set_prediction_resistance( hmac_drbg_context *ctx,
177 int resistance )
178{
179 ctx->prediction_resistance = resistance;
180}
181
182/*
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100183 * Set entropy length grabbed for reseeds
184 */
185void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx, size_t len )
186{
187 ctx->entropy_len = len;
188}
189
190/*
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100191 * HMAC_DRBG random function with optional additional data (10.1.2.5)
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100192 */
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100193int hmac_drbg_random_with_add( void *p_rng,
194 unsigned char *output, size_t out_len,
195 const unsigned char *additional, size_t add_len )
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100196{
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100197 int ret;
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100198 hmac_drbg_context *ctx = (hmac_drbg_context *) p_rng;
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100199 size_t md_len = md_get_size( ctx->md_ctx.md_info );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100200 size_t left = out_len;
201 unsigned char *out = output;
202
Manuel Pégourié-Gonnardaf786ff2014-01-30 18:44:18 +0100203 /* 1. Check reseed counter (TODO) and PR */
204 if( ctx->f_entropy != NULL &&
205 ctx->prediction_resistance == HMAC_DRBG_PR_ON )
206 {
207 if( ( ret = hmac_drbg_reseed( ctx, additional, add_len ) ) != 0 )
208 return( ret );
209 }
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100210
211 /* 2. Use additional data if any */
212 if( additional != NULL && add_len != 0 )
213 hmac_drbg_update( ctx, additional, add_len );
214
215 /* 3, 4, 5. Generate bytes */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100216 while( left != 0 )
217 {
218 size_t use_len = left > md_len ? md_len : left;
219
220 md_hmac_starts( &ctx->md_ctx, ctx->K, md_len );
221 md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
222 md_hmac_finish( &ctx->md_ctx, ctx->V );
223
224 memcpy( out, ctx->V, use_len );
225 out += use_len;
226 left -= use_len;
227 }
228
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100229 /* 6. Update */
230 hmac_drbg_update( ctx, additional, add_len );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100231
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100232 /* 7. Update reseed counter (TODO) */
233
234 /* 8. Done */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100235 return( 0 );
236}
237
238/*
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100239 * HMAC_DRBG random function
240 */
241int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len )
242{
243 return( hmac_drbg_random_with_add( p_rng, output, out_len, NULL, 0 ) );
244}
245
246/*
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100247 * Free an HMAC_DRBG context
248 */
249void hmac_drbg_free( hmac_drbg_context *ctx )
250{
251 if( ctx == NULL )
252 return;
253
254 md_free_ctx( &ctx->md_ctx );
255
256 memset( ctx, 0, sizeof( hmac_drbg_context ) );
257}
258
259
260#if defined(POLARSSL_SELF_TEST)
261
262#include <stdio.h>
263
264/*
265 * Checkup routine
266 */
267int hmac_drbg_self_test( int verbose )
268{
269
270 if( verbose != 0 )
271 printf( "\n" );
272
273 return( 0 );
274}
275#endif /* POLARSSL_SELF_TEST */
276
277#endif /* POLARSSL_HMAC_DRBG_C */