Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 1 | /* |
| 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é-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 27 | * The NIST SP 800-90A DRBGs are described in the following publication. |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 28 | * http://csrc.nist.gov/publications/nistpubs/800-90A/SP800-90A.pdf |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 29 | * References below are based on rev. 1 (January 2012). |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 30 | */ |
| 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é-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 39 | * HMAC_DRBG update, using optional additional data (10.1.2.2) |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 40 | */ |
| 41 | void 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é-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 50 | /* Step 1 or 4 */ |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 51 | 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é-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 58 | /* Step 2 or 5 */ |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 59 | 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 | /* |
| 66 | * Simplified HMAC_DRBG initialisation. |
| 67 | */ |
| 68 | int hmac_drbg_init( hmac_drbg_context *ctx, |
| 69 | const md_info_t * md_info, |
| 70 | const unsigned char *data, size_t data_len ) |
| 71 | { |
| 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é-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 88 | * HMAC_DRBG random function with optional additional data (10.1.2.5) |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 89 | */ |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 90 | int hmac_drbg_random_with_add( void *p_rng, |
| 91 | unsigned char *output, size_t out_len, |
| 92 | const unsigned char *additional, size_t add_len ) |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 93 | { |
| 94 | hmac_drbg_context *ctx = (hmac_drbg_context *) p_rng; |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 95 | size_t md_len = md_get_size( ctx->md_ctx.md_info ); |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 96 | size_t left = out_len; |
| 97 | unsigned char *out = output; |
| 98 | |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 99 | /* 1. Check reseed counter (TODO) */ |
| 100 | |
| 101 | /* 2. Use additional data if any */ |
| 102 | if( additional != NULL && add_len != 0 ) |
| 103 | hmac_drbg_update( ctx, additional, add_len ); |
| 104 | |
| 105 | /* 3, 4, 5. Generate bytes */ |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 106 | while( left != 0 ) |
| 107 | { |
| 108 | size_t use_len = left > md_len ? md_len : left; |
| 109 | |
| 110 | md_hmac_starts( &ctx->md_ctx, ctx->K, md_len ); |
| 111 | md_hmac_update( &ctx->md_ctx, ctx->V, md_len ); |
| 112 | md_hmac_finish( &ctx->md_ctx, ctx->V ); |
| 113 | |
| 114 | memcpy( out, ctx->V, use_len ); |
| 115 | out += use_len; |
| 116 | left -= use_len; |
| 117 | } |
| 118 | |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 119 | /* 6. Update */ |
| 120 | hmac_drbg_update( ctx, additional, add_len ); |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 121 | |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 122 | /* 7. Update reseed counter (TODO) */ |
| 123 | |
| 124 | /* 8. Done */ |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 125 | return( 0 ); |
| 126 | } |
| 127 | |
| 128 | /* |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 129 | * HMAC_DRBG random function |
| 130 | */ |
| 131 | int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len ) |
| 132 | { |
| 133 | return( hmac_drbg_random_with_add( p_rng, output, out_len, NULL, 0 ) ); |
| 134 | } |
| 135 | |
| 136 | /* |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 137 | * Free an HMAC_DRBG context |
| 138 | */ |
| 139 | void hmac_drbg_free( hmac_drbg_context *ctx ) |
| 140 | { |
| 141 | if( ctx == NULL ) |
| 142 | return; |
| 143 | |
| 144 | md_free_ctx( &ctx->md_ctx ); |
| 145 | |
| 146 | memset( ctx, 0, sizeof( hmac_drbg_context ) ); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | #if defined(POLARSSL_SELF_TEST) |
| 151 | |
| 152 | #include <stdio.h> |
| 153 | |
| 154 | /* |
| 155 | * Checkup routine |
| 156 | */ |
| 157 | int hmac_drbg_self_test( int verbose ) |
| 158 | { |
| 159 | |
| 160 | if( verbose != 0 ) |
| 161 | printf( "\n" ); |
| 162 | |
| 163 | return( 0 ); |
| 164 | } |
| 165 | #endif /* POLARSSL_SELF_TEST */ |
| 166 | |
| 167 | #endif /* POLARSSL_HMAC_DRBG_C */ |