blob: a322b483edc8ccff858fe982acae70336d5d073c [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é-Gonnardfe34a5f2014-01-30 15:06:40 +010088 * HMAC_DRBG initialisation
89 */
90int hmac_drbg_init( hmac_drbg_context *ctx,
91 const md_info_t * md_info,
92 int (*f_entropy)(void *, unsigned char *, size_t),
93 void *p_entropy,
94 const unsigned char *custom,
95 size_t len )
96{
97 int ret;
98 unsigned char seed[HMAC_DRBG_MAX_SEED_INPUT];
99 size_t seedlen, init_entropy_len;
100
101 memset( ctx, 0, sizeof( hmac_drbg_context ) );
102
103 if( ( ret = md_init_ctx( &ctx->md_ctx, md_info ) ) != 0 )
104 return( ret );
105
106 /*
107 * See SP800-57 5.6.1 (p. 65-66) for the security strength provided by
108 * each hash function, then according to SP800-90A rev1 10.1 table 2,
109 * min_entropy_len (in bits) is security_strength.
110 */
111 ctx->entropy_len = md_info->size <= 20 ? 16 : /* 160-bits hash -> 128 */
112 md_info->size <= 28 ? 24 : /* 224-bits hash -> 192 */
113 32; /* better (256+) -> 256 */
114
115 ctx->f_entropy = f_entropy;
116 ctx->p_entropy = p_entropy;
117
118 /*
119 * For initialisation, use more entropy to emulate a nonce
120 */
121 init_entropy_len = ctx->entropy_len * 3 / 2;
122
123 if( init_entropy_len + len > HMAC_DRBG_MAX_SEED_INPUT )
124 return( POLARSSL_ERR_HMAC_DRBG_INPUT_TOO_BIG );
125
126 memset( seed, 0, HMAC_DRBG_MAX_SEED_INPUT );
127
128 /*
129 * Gather init_entropy_len bytes of entropy for initial seed
130 */
131 if( 0 != ctx->f_entropy( ctx->p_entropy, seed,
132 init_entropy_len ) )
133 {
134 return( POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED );
135 }
136
137 seedlen = init_entropy_len;
138
139 /*
140 * Add additional data
141 */
142 if( custom != NULL && len != 0 )
143 {
144 memcpy( seed + seedlen, custom, len );
145 seedlen += len;
146 }
147
148 /*
149 * Set initial state and update it with initialisation data
150 */
151 memset( ctx->V, 0x01, md_info->size );
152 /* ctx->K is already 0 */
153
154 hmac_drbg_update( ctx, seed, seedlen );
155
156 return( 0 );
157}
158
159/*
Manuel Pégourié-Gonnard4e669c62014-01-30 18:06:08 +0100160 * Set entropy length grabbed for reseeds
161 */
162void hmac_drbg_set_entropy_len( hmac_drbg_context *ctx, size_t len )
163{
164 ctx->entropy_len = len;
165}
166
167/*
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100168 * HMAC_DRBG random function with optional additional data (10.1.2.5)
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100169 */
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100170int hmac_drbg_random_with_add( void *p_rng,
171 unsigned char *output, size_t out_len,
172 const unsigned char *additional, size_t add_len )
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100173{
174 hmac_drbg_context *ctx = (hmac_drbg_context *) p_rng;
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100175 size_t md_len = md_get_size( ctx->md_ctx.md_info );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100176 size_t left = out_len;
177 unsigned char *out = output;
178
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100179 /* 1. Check reseed counter (TODO) */
180
181 /* 2. Use additional data if any */
182 if( additional != NULL && add_len != 0 )
183 hmac_drbg_update( ctx, additional, add_len );
184
185 /* 3, 4, 5. Generate bytes */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100186 while( left != 0 )
187 {
188 size_t use_len = left > md_len ? md_len : left;
189
190 md_hmac_starts( &ctx->md_ctx, ctx->K, md_len );
191 md_hmac_update( &ctx->md_ctx, ctx->V, md_len );
192 md_hmac_finish( &ctx->md_ctx, ctx->V );
193
194 memcpy( out, ctx->V, use_len );
195 out += use_len;
196 left -= use_len;
197 }
198
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100199 /* 6. Update */
200 hmac_drbg_update( ctx, additional, add_len );
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100201
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100202 /* 7. Update reseed counter (TODO) */
203
204 /* 8. Done */
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100205 return( 0 );
206}
207
208/*
Manuel Pégourié-Gonnard8208d162014-01-30 12:19:26 +0100209 * HMAC_DRBG random function
210 */
211int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len )
212{
213 return( hmac_drbg_random_with_add( p_rng, output, out_len, NULL, 0 ) );
214}
215
216/*
Manuel Pégourié-Gonnard490bdf32014-01-27 14:03:10 +0100217 * Free an HMAC_DRBG context
218 */
219void hmac_drbg_free( hmac_drbg_context *ctx )
220{
221 if( ctx == NULL )
222 return;
223
224 md_free_ctx( &ctx->md_ctx );
225
226 memset( ctx, 0, sizeof( hmac_drbg_context ) );
227}
228
229
230#if defined(POLARSSL_SELF_TEST)
231
232#include <stdio.h>
233
234/*
235 * Checkup routine
236 */
237int hmac_drbg_self_test( int verbose )
238{
239
240 if( verbose != 0 )
241 printf( "\n" );
242
243 return( 0 );
244}
245#endif /* POLARSSL_SELF_TEST */
246
247#endif /* POLARSSL_HMAC_DRBG_C */