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 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame^] | 8 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 9 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | */ |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 24 | #ifndef POLARSSL_SHA256_H |
| 25 | #define POLARSSL_SHA256_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 27 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 28 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
| 30 | #include POLARSSL_CONFIG_FILE |
| 31 | #endif |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 32 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 33 | #include <string.h> |
| 34 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 35 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 36 | #include <basetsd.h> |
| 37 | typedef UINT32 uint32_t; |
| 38 | #else |
| 39 | #include <inttypes.h> |
| 40 | #endif |
| 41 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 42 | #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] | 43 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 44 | #if !defined(POLARSSL_SHA256_ALT) |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 45 | // Regular implementation |
| 46 | // |
| 47 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 48 | #ifdef __cplusplus |
| 49 | extern "C" { |
| 50 | #endif |
| 51 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | /** |
| 53 | * \brief SHA-256 context structure |
| 54 | */ |
| 55 | typedef struct |
| 56 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 57 | uint32_t total[2]; /*!< number of bytes processed */ |
| 58 | uint32_t state[8]; /*!< intermediate digest state */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | unsigned char buffer[64]; /*!< data block being processed */ |
| 60 | |
| 61 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
| 62 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
| 63 | int is224; /*!< 0 => SHA-256, else SHA-224 */ |
| 64 | } |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 65 | sha256_context; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 67 | /** |
Paul Bakker | 5b4af39 | 2014-06-26 12:09:34 +0200 | [diff] [blame] | 68 | * \brief Initialize SHA-256 context |
| 69 | * |
| 70 | * \param ctx SHA-256 context to be initialized |
| 71 | */ |
| 72 | void sha256_init( sha256_context *ctx ); |
| 73 | |
| 74 | /** |
| 75 | * \brief Clear SHA-256 context |
| 76 | * |
| 77 | * \param ctx SHA-256 context to be cleared |
| 78 | */ |
| 79 | void sha256_free( sha256_context *ctx ); |
| 80 | |
| 81 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 | * \brief SHA-256 context setup |
| 83 | * |
| 84 | * \param ctx context to be initialized |
| 85 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 86 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 87 | void sha256_starts( sha256_context *ctx, int is224 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * \brief SHA-256 process buffer |
| 91 | * |
| 92 | * \param ctx SHA-256 context |
| 93 | * \param input buffer holding the data |
| 94 | * \param ilen length of the input data |
| 95 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 96 | void sha256_update( sha256_context *ctx, const unsigned char *input, |
| 97 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * \brief SHA-256 final digest |
| 101 | * |
| 102 | * \param ctx SHA-256 context |
| 103 | * \param output SHA-224/256 checksum result |
| 104 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 105 | void sha256_finish( sha256_context *ctx, unsigned char output[32] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 107 | /* Internal use */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 108 | void sha256_process( sha256_context *ctx, const unsigned char data[64] ); |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 109 | |
| 110 | #ifdef __cplusplus |
| 111 | } |
| 112 | #endif |
| 113 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 114 | #else /* POLARSSL_SHA256_ALT */ |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 115 | #include "sha256_alt.h" |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 116 | #endif /* POLARSSL_SHA256_ALT */ |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 117 | |
| 118 | #ifdef __cplusplus |
| 119 | extern "C" { |
| 120 | #endif |
| 121 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | /** |
| 123 | * \brief Output = SHA-256( input buffer ) |
| 124 | * |
| 125 | * \param input buffer holding the data |
| 126 | * \param ilen length of the input data |
| 127 | * \param output SHA-224/256 checksum result |
| 128 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 129 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 130 | void sha256( const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | unsigned char output[32], int is224 ); |
| 132 | |
| 133 | /** |
| 134 | * \brief Output = SHA-256( file contents ) |
| 135 | * |
| 136 | * \param path input file name |
| 137 | * \param output SHA-224/256 checksum result |
| 138 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 139 | * |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 140 | * \return 0 if successful, or POLARSSL_ERR_SHA256_FILE_IO_ERROR |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 142 | int sha256_file( const char *path, unsigned char output[32], int is224 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * \brief SHA-256 HMAC context setup |
| 146 | * |
| 147 | * \param ctx HMAC context to be initialized |
| 148 | * \param key HMAC secret key |
| 149 | * \param keylen length of the HMAC key |
| 150 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 151 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 152 | void sha256_hmac_starts( sha256_context *ctx, const unsigned char *key, |
| 153 | size_t keylen, int is224 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * \brief SHA-256 HMAC process buffer |
| 157 | * |
| 158 | * \param ctx HMAC context |
| 159 | * \param input buffer holding the data |
| 160 | * \param ilen length of the input data |
| 161 | */ |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 162 | void sha256_hmac_update( sha256_context *ctx, const unsigned char *input, |
| 163 | size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * \brief SHA-256 HMAC final digest |
| 167 | * |
| 168 | * \param ctx HMAC context |
| 169 | * \param output SHA-224/256 HMAC checksum result |
| 170 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 171 | void sha256_hmac_finish( sha256_context *ctx, unsigned char output[32] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 172 | |
| 173 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 174 | * \brief SHA-256 HMAC context reset |
| 175 | * |
| 176 | * \param ctx HMAC context to be reset |
| 177 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 178 | void sha256_hmac_reset( sha256_context *ctx ); |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 179 | |
| 180 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | * \brief Output = HMAC-SHA-256( hmac key, input buffer ) |
| 182 | * |
| 183 | * \param key HMAC secret key |
| 184 | * \param keylen length of the HMAC key |
| 185 | * \param input buffer holding the data |
| 186 | * \param ilen length of the input data |
| 187 | * \param output HMAC-SHA-224/256 result |
| 188 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 189 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 190 | void sha256_hmac( const unsigned char *key, size_t keylen, |
| 191 | const unsigned char *input, size_t ilen, |
| 192 | unsigned char output[32], int is224 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 | |
| 194 | /** |
| 195 | * \brief Checkup routine |
| 196 | * |
| 197 | * \return 0 if successful, or 1 if the test failed |
| 198 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 199 | int sha256_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 200 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | #ifdef __cplusplus |
| 202 | } |
| 203 | #endif |
| 204 | |
Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 205 | #endif /* sha256.h */ |