Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file hmac_drbg.h |
| 3 | * |
| 4 | * \brief HMAC_DRBG (NIST SP 800-90A) |
| 5 | * |
| 6 | * Copyright (C) 2014, Brainspark B.V. |
| 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 10 | * |
| 11 | * All rights reserved. |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | */ |
| 27 | #ifndef POLARSSL_HMAC_DRBG_H |
| 28 | #define POLARSSL_HMAC_DRBG_H |
| 29 | |
| 30 | #include "md.h" |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | /* |
| 37 | * Simplified HMAC_DRBG context. |
| 38 | * No reseed counter, no prediction resistance flag. |
| 39 | */ |
| 40 | typedef struct |
| 41 | { |
| 42 | md_context_t md_ctx; |
| 43 | unsigned char V[POLARSSL_MD_MAX_SIZE]; |
| 44 | unsigned char K[POLARSSL_MD_MAX_SIZE]; |
| 45 | } hmac_drbg_context; |
| 46 | |
| 47 | /** |
| 48 | * \brief HMAC_DRBG initialisation |
| 49 | * |
| 50 | * \param ctx HMAC_DRBG context to be initialised |
| 51 | * \param md_info MD algorithm to use for HMAC_DRBG |
| 52 | * \param data Concatenation of entropy string and additional data |
| 53 | * \param data_len Length of data in bytes |
| 54 | * |
| 55 | * \todo Use entropy callback rather than buffer. |
| 56 | * |
| 57 | * \return 0 if successful, or |
| 58 | * POLARSSL_ERR_MD_BAD_INPUT_DATA, or |
| 59 | * POLARSSL_ERR_MD_ALLOC_FAILED |
| 60 | */ |
| 61 | int hmac_drbg_init( hmac_drbg_context *ctx, |
| 62 | const md_info_t * md_info, |
| 63 | const unsigned char *data, size_t data_len ); |
| 64 | |
| 65 | /** |
| 66 | * \brief HMAC_DRBG update state |
| 67 | * |
| 68 | * \param ctx HMAC_DRBG context |
| 69 | * \param additional Additional data to update state with, or NULL |
| 70 | * \param add_len Length of additional data, or 0 |
| 71 | * |
| 72 | * \note Additional data is optional, pass NULL and 0 as second |
| 73 | * third argument if no additional data is being used. |
| 74 | */ |
| 75 | void hmac_drbg_update( hmac_drbg_context *ctx, |
| 76 | const unsigned char *additional, size_t add_len ); |
| 77 | |
| 78 | /** |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 79 | * \brief HMAC_DRBG generate random with additional update input |
| 80 | * |
| 81 | * Note: Automatically reseeds if reseed_counter is reached. |
| 82 | * |
| 83 | * \param p_rng HMAC_DRBG context |
| 84 | * \param output Buffer to fill |
| 85 | * \param output_len Length of the buffer |
| 86 | * \param additional Additional data to update with (can be NULL) |
| 87 | * \param add_len Length of additional data (can be 0) |
| 88 | * |
| 89 | * \return 0 if successful, or |
| 90 | * TODO: POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or |
| 91 | * TODO: POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG |
| 92 | */ |
| 93 | int hmac_drbg_random_with_add( void *p_rng, |
| 94 | unsigned char *output, size_t output_len, |
| 95 | const unsigned char *additional, |
| 96 | size_t add_len ); |
| 97 | |
| 98 | /** |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 99 | * \brief HMAC_DRBG generate random |
| 100 | * |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 101 | * Note: Automatically reseeds if reseed_counter is reached. |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 102 | * |
| 103 | * \param p_rng HMAC_DRBG context |
| 104 | * \param output Buffer to fill |
| 105 | * \param output_len Length of the buffer |
| 106 | * |
Manuel Pégourié-Gonnard | 8208d16 | 2014-01-30 12:19:26 +0100 | [diff] [blame^] | 107 | * \return 0 if successful, or |
| 108 | * TODO: POLARSSL_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED, or |
| 109 | * TODO: POLARSSL_ERR_HMAC_DRBG_REQUEST_TOO_BIG |
Manuel Pégourié-Gonnard | 490bdf3 | 2014-01-27 14:03:10 +0100 | [diff] [blame] | 110 | */ |
| 111 | int hmac_drbg_random( void *p_rng, unsigned char *output, size_t out_len ); |
| 112 | |
| 113 | /** |
| 114 | * \brief Free an HMAC_DRBG context |
| 115 | * |
| 116 | * \param ctx HMAC_DRBG context to free. |
| 117 | */ |
| 118 | void hmac_drbg_free( hmac_drbg_context *ctx ); |
| 119 | |
| 120 | |
| 121 | #if defined(POLARSSL_SELF_TEST) |
| 122 | /** |
| 123 | * \brief Checkup routine |
| 124 | * |
| 125 | * \return 0 if successful, or 1 if the test failed |
| 126 | */ |
| 127 | int hmac_drbg_self_test( int verbose ); |
| 128 | #endif |
| 129 | |
| 130 | #ifdef __cplusplus |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | #endif /* hmac_drbg.h */ |