Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file sha2.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 | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, 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 | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #ifndef POLARSSL_SHA2_H |
| 28 | #define POLARSSL_SHA2_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 30 | #include "config.h" |
| 31 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 32 | #include <string.h> |
| 33 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 34 | #ifdef _MSC_VER |
| 35 | #include <basetsd.h> |
| 36 | typedef UINT32 uint32_t; |
| 37 | #else |
| 38 | #include <inttypes.h> |
| 39 | #endif |
| 40 | |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 41 | #define POLARSSL_ERR_SHA2_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */ |
| 42 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 43 | #if !defined(POLARSSL_SHA2_ALT) |
| 44 | // Regular implementation |
| 45 | // |
| 46 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame^] | 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 51 | /** |
| 52 | * \brief SHA-256 context structure |
| 53 | */ |
| 54 | typedef struct |
| 55 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 56 | uint32_t total[2]; /*!< number of bytes processed */ |
| 57 | uint32_t state[8]; /*!< intermediate digest state */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 | unsigned char buffer[64]; /*!< data block being processed */ |
| 59 | |
| 60 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
| 61 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
| 62 | int is224; /*!< 0 => SHA-256, else SHA-224 */ |
| 63 | } |
| 64 | sha2_context; |
| 65 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | /** |
| 67 | * \brief SHA-256 context setup |
| 68 | * |
| 69 | * \param ctx context to be initialized |
| 70 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 71 | */ |
| 72 | void sha2_starts( sha2_context *ctx, int is224 ); |
| 73 | |
| 74 | /** |
| 75 | * \brief SHA-256 process buffer |
| 76 | * |
| 77 | * \param ctx SHA-256 context |
| 78 | * \param input buffer holding the data |
| 79 | * \param ilen length of the input data |
| 80 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 81 | void sha2_update( sha2_context *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 | |
| 83 | /** |
| 84 | * \brief SHA-256 final digest |
| 85 | * |
| 86 | * \param ctx SHA-256 context |
| 87 | * \param output SHA-224/256 checksum result |
| 88 | */ |
| 89 | void sha2_finish( sha2_context *ctx, unsigned char output[32] ); |
| 90 | |
Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 91 | /* Internal use */ |
| 92 | void sha2_process( sha2_context *ctx, const unsigned char data[64] ); |
| 93 | |
| 94 | #ifdef __cplusplus |
| 95 | } |
| 96 | #endif |
| 97 | |
| 98 | #else /* POLARSSL_SHA2_ALT */ |
| 99 | #include "sha2_alt.h" |
| 100 | #endif /* POLARSSL_SHA2_ALT */ |
| 101 | |
| 102 | #ifdef __cplusplus |
| 103 | extern "C" { |
| 104 | #endif |
| 105 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | /** |
| 107 | * \brief Output = SHA-256( input buffer ) |
| 108 | * |
| 109 | * \param input buffer holding the data |
| 110 | * \param ilen length of the input data |
| 111 | * \param output SHA-224/256 checksum result |
| 112 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 113 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 114 | void sha2( const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 115 | unsigned char output[32], int is224 ); |
| 116 | |
| 117 | /** |
| 118 | * \brief Output = SHA-256( file contents ) |
| 119 | * |
| 120 | * \param path input file name |
| 121 | * \param output SHA-224/256 checksum result |
| 122 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 123 | * |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 124 | * \return 0 if successful, or POLARSSL_ERR_SHA2_FILE_IO_ERROR |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 126 | int sha2_file( const char *path, unsigned char output[32], int is224 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * \brief SHA-256 HMAC context setup |
| 130 | * |
| 131 | * \param ctx HMAC context to be initialized |
| 132 | * \param key HMAC secret key |
| 133 | * \param keylen length of the HMAC key |
| 134 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 135 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 136 | void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keylen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | int is224 ); |
| 138 | |
| 139 | /** |
| 140 | * \brief SHA-256 HMAC process buffer |
| 141 | * |
| 142 | * \param ctx HMAC context |
| 143 | * \param input buffer holding the data |
| 144 | * \param ilen length of the input data |
| 145 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 146 | void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * \brief SHA-256 HMAC final digest |
| 150 | * |
| 151 | * \param ctx HMAC context |
| 152 | * \param output SHA-224/256 HMAC checksum result |
| 153 | */ |
| 154 | void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] ); |
| 155 | |
| 156 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 157 | * \brief SHA-256 HMAC context reset |
| 158 | * |
| 159 | * \param ctx HMAC context to be reset |
| 160 | */ |
| 161 | void sha2_hmac_reset( sha2_context *ctx ); |
| 162 | |
| 163 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 164 | * \brief Output = HMAC-SHA-256( hmac key, input buffer ) |
| 165 | * |
| 166 | * \param key HMAC secret key |
| 167 | * \param keylen length of the HMAC key |
| 168 | * \param input buffer holding the data |
| 169 | * \param ilen length of the input data |
| 170 | * \param output HMAC-SHA-224/256 result |
| 171 | * \param is224 0 = use SHA256, 1 = use SHA224 |
| 172 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 173 | void sha2_hmac( const unsigned char *key, size_t keylen, |
| 174 | const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 175 | unsigned char output[32], int is224 ); |
| 176 | |
| 177 | /** |
| 178 | * \brief Checkup routine |
| 179 | * |
| 180 | * \return 0 if successful, or 1 if the test failed |
| 181 | */ |
| 182 | int sha2_self_test( int verbose ); |
| 183 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 184 | #ifdef __cplusplus |
| 185 | } |
| 186 | #endif |
| 187 | |
| 188 | #endif /* sha2.h */ |