Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 2 | * \file sha256.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 4 | * \brief SHA-224 and SHA-256 cryptographic hash function |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 5 | * |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 6 | * Copyright (C) 2006-2014, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 11 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 12 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 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. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | */ |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 27 | #ifndef POLARSSL_SHA256_H |
| 28 | #define POLARSSL_SHA256_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 31 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else |
| 33 | #include POLARSSL_CONFIG_FILE |
| 34 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 35 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 38 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 39 | #include <basetsd.h> |
| 40 | typedef UINT32 uint32_t; |
| 41 | #else |
| 42 | #include <inttypes.h> |
| 43 | #endif |
| 44 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 45 | #define POLARSSL_ERR_SHA256_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */ |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 46 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 47 | #if !defined(POLARSSL_SHA256_ALT) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 48 | // Regular implementation |
| 49 | // |
| 50 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 51 | #ifdef __cplusplus |
| 52 | extern "C" { |
| 53 | #endif |
| 54 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 55 | /** |
| 56 | * \brief SHA-256 context structure |
| 57 | */ |
| 58 | typedef struct |
| 59 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 60 | uint32_t total[2]; /*!< number of bytes processed */ |
| 61 | uint32_t state[8]; /*!< intermediate digest state */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | unsigned char buffer[64]; /*!< data block being processed */ |
| 63 | |
| 64 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
| 65 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
| 66 | int is224; /*!< 0 => SHA-256, else SHA-224 */ |
| 67 | } |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 68 | sha256_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 70 | /** |
| 71 | * \brief SHA-256 context setup |
| 72 | * |
| 73 | * \param ctx context to be initialized |
| 74 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 75 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 76 | void sha256_starts( sha256_context *ctx, int is224 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * \brief SHA-256 process buffer |
| 80 | * |
| 81 | * \param ctx SHA-256 context |
| 82 | * \param input buffer holding the data |
| 83 | * \param ilen length of the input data |
| 84 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 85 | void sha256_update( sha256_context *ctx, const unsigned char *input, |
| 86 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * \brief SHA-256 final digest |
| 90 | * |
| 91 | * \param ctx SHA-256 context |
| 92 | * \param output SHA-224/256 checksum result |
| 93 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 94 | void sha256_finish( sha256_context *ctx, unsigned char output[32] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 96 | /* Internal use */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 97 | void sha256_process( sha256_context *ctx, const unsigned char data[64] ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 98 | |
| 99 | #ifdef __cplusplus |
| 100 | } |
| 101 | #endif |
| 102 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 103 | #else /* POLARSSL_SHA256_ALT */ |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 104 | #include "sha256_alt.h" |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 105 | #endif /* POLARSSL_SHA256_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 106 | |
| 107 | #ifdef __cplusplus |
| 108 | extern "C" { |
| 109 | #endif |
| 110 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | /** |
| 112 | * \brief Output = SHA-256( input buffer ) |
| 113 | * |
| 114 | * \param input buffer holding the data |
| 115 | * \param ilen length of the input data |
| 116 | * \param output SHA-224/256 checksum result |
| 117 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 118 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 119 | void sha256( const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | unsigned char output[32], int is224 ); |
| 121 | |
| 122 | /** |
| 123 | * \brief Output = SHA-256( file contents ) |
| 124 | * |
| 125 | * \param path input file name |
| 126 | * \param output SHA-224/256 checksum result |
| 127 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 128 | * |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 129 | * \return 0 if successful, or POLARSSL_ERR_SHA256_FILE_IO_ERROR |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 131 | int sha256_file( const char *path, unsigned char output[32], int is224 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * \brief SHA-256 HMAC context setup |
| 135 | * |
| 136 | * \param ctx HMAC context to be initialized |
| 137 | * \param key HMAC secret key |
| 138 | * \param keylen length of the HMAC key |
| 139 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 140 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 141 | void sha256_hmac_starts( sha256_context *ctx, const unsigned char *key, |
| 142 | size_t keylen, int is224 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * \brief SHA-256 HMAC process buffer |
| 146 | * |
| 147 | * \param ctx HMAC context |
| 148 | * \param input buffer holding the data |
| 149 | * \param ilen length of the input data |
| 150 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame^] | 151 | void sha256_hmac_update( sha256_context *ctx, const unsigned char *input, |
| 152 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * \brief SHA-256 HMAC final digest |
| 156 | * |
| 157 | * \param ctx HMAC context |
| 158 | * \param output SHA-224/256 HMAC checksum result |
| 159 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 160 | void sha256_hmac_finish( sha256_context *ctx, unsigned char output[32] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 161 | |
| 162 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 163 | * \brief SHA-256 HMAC context reset |
| 164 | * |
| 165 | * \param ctx HMAC context to be reset |
| 166 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 167 | void sha256_hmac_reset( sha256_context *ctx ); |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 168 | |
| 169 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 170 | * \brief Output = HMAC-SHA-256( hmac key, input buffer ) |
| 171 | * |
| 172 | * \param key HMAC secret key |
| 173 | * \param keylen length of the HMAC key |
| 174 | * \param input buffer holding the data |
| 175 | * \param ilen length of the input data |
| 176 | * \param output HMAC-SHA-224/256 result |
| 177 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 178 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 179 | void sha256_hmac( const unsigned char *key, size_t keylen, |
| 180 | const unsigned char *input, size_t ilen, |
| 181 | unsigned char output[32], int is224 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * \brief Checkup routine |
| 185 | * |
| 186 | * \return 0 if successful, or 1 if the test failed |
| 187 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 188 | int sha256_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 189 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 190 | #ifdef __cplusplus |
| 191 | } |
| 192 | #endif |
| 193 | |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 194 | #endif /* sha256.h */ |