blob: 8a4f3f09c7c85c2c0f817374e9a3cc86db628aa7 [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é-Gonnard4e669c62014-01-30 18:06:08 +0100174 * Set entropy length grabbed for reseeds
175 */
176void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx, size_t len )
177{
178 ctx->entropy_len = len;
179}
180
181/*
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100182 * HMAC_DRBG random function with optional additional data (10.1.2.5)
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100183 */
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100184int hmac_drbg_random_with_add( void *p_rng,
185 unsigned char *output, size_t out_len,
186 const unsigned char *additional, size_t add_len )
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100187{
188 hmac_drbg_context *ctx = (hmac_drbg_context *) p_rng;
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100189 size_t md_len = md_get_size( ctx->md_ctx.md_info );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100190 size_t left = out_len;
191 unsigned char *out = output;
192
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100193 /* 1. Check reseed counter (TODO) */
194
195 /* 2. Use additional data if any */
196 if( additional != NULL && add_len != 0 )
197 hmac_drbg_update( ctx, additional, add_len );
198
199 /* 3, 4, 5. Generate bytes */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100200 while( left != 0 )
201 {
202 size_t use_len = left > md_len ? md_len : left;
203
204 md_hmac_starts( &ctx->md_ctx, ctx->K, md_len );
205 md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
206 md_hmac_finish( &ctx->md_ctx, ctx->V );
207
208 memcpy( out, ctx->V, use_len );
209 out += use_len;
210 left -= use_len;
211 }
212
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100213 /* 6. Update */
214 hmac_drbg_update( ctx, additional, add_len );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100215
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100216 /* 7. Update reseed counter (TODO) */
217
218 /* 8. Done */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100219 return( 0 );
220}
221
222/*
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100223 * HMAC_DRBG random function
224 */
225int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len )
226{
227 return( hmac_drbg_random_with_add( p_rng, output, out_len, NULL, 0 ) );
228}
229
230/*
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100231 * Free an HMAC_DRBG context
232 */
233void hmac_drbg_free( hmac_drbg_context *ctx )
234{
235 if( ctx == NULL )
236 return;
237
238 md_free_ctx( &ctx->md_ctx );
239
240 memset( ctx, 0, sizeof( hmac_drbg_context ) );
241}
242
243
244#if defined(POLARSSL_SELF_TEST)
245
246#include <stdio.h>
247
248/*
249 * Checkup routine
250 */
251int hmac_drbg_self_test( int verbose )
252{
253
254 if( verbose != 0 )
255 printf( "\n" );
256
257 return( 0 );
258}
259#endif /* POLARSSL_SELF_TEST */
260
261#endif /* POLARSSL_HMAC_DRBG_C */