Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file sha4.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-384 and SHA-512 cryptographic hash function |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 5 | * |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2010, 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_SHA4_H |
| 28 | #define POLARSSL_SHA4_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | #if defined(_MSC_VER) || defined(__WATCOMC__) |
| 33 | #define UL64(x) x##ui64 |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 34 | typedef unsigned __int64 uint64_t; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | #else |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 36 | #include <inttypes.h> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 | #define UL64(x) x##ULL |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 40 | #define POLARSSL_ERR_SHA4_FILE_IO_ERROR -0x007A /**< Read/write error in file. */ |
| 41 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 42 | /** |
| 43 | * \brief SHA-512 context structure |
| 44 | */ |
| 45 | typedef struct |
| 46 | { |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 47 | uint64_t total[2]; /*!< number of bytes processed */ |
| 48 | uint64_t state[8]; /*!< intermediate digest state */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 49 | unsigned char buffer[128]; /*!< data block being processed */ |
| 50 | |
| 51 | unsigned char ipad[128]; /*!< HMAC: inner padding */ |
| 52 | unsigned char opad[128]; /*!< HMAC: outer padding */ |
| 53 | int is384; /*!< 0 => SHA-512, else SHA-384 */ |
| 54 | } |
| 55 | sha4_context; |
| 56 | |
| 57 | #ifdef __cplusplus |
| 58 | extern "C" { |
| 59 | #endif |
| 60 | |
| 61 | /** |
| 62 | * \brief SHA-512 context setup |
| 63 | * |
| 64 | * \param ctx context to be initialized |
| 65 | * \param is384 0 = use SHA512, 1 = use SHA384 |
| 66 | */ |
| 67 | void sha4_starts( sha4_context *ctx, int is384 ); |
| 68 | |
| 69 | /** |
| 70 | * \brief SHA-512 process buffer |
| 71 | * |
| 72 | * \param ctx SHA-512 context |
| 73 | * \param input buffer holding the data |
| 74 | * \param ilen length of the input data |
| 75 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 76 | void sha4_update( sha4_context *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * \brief SHA-512 final digest |
| 80 | * |
| 81 | * \param ctx SHA-512 context |
| 82 | * \param output SHA-384/512 checksum result |
| 83 | */ |
| 84 | void sha4_finish( sha4_context *ctx, unsigned char output[64] ); |
| 85 | |
| 86 | /** |
| 87 | * \brief Output = SHA-512( input buffer ) |
| 88 | * |
| 89 | * \param input buffer holding the data |
| 90 | * \param ilen length of the input data |
| 91 | * \param output SHA-384/512 checksum result |
| 92 | * \param is384 0 = use SHA512, 1 = use SHA384 |
| 93 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 94 | void sha4( const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 95 | unsigned char output[64], int is384 ); |
| 96 | |
| 97 | /** |
| 98 | * \brief Output = SHA-512( file contents ) |
| 99 | * |
| 100 | * \param path input file name |
| 101 | * \param output SHA-384/512 checksum result |
| 102 | * \param is384 0 = use SHA512, 1 = use SHA384 |
| 103 | * |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 104 | * \return 0 if successful, or POLARSSL_ERR_SHA4_FILE_IO_ERROR |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 106 | int sha4_file( const char *path, unsigned char output[64], int is384 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * \brief SHA-512 HMAC context setup |
| 110 | * |
| 111 | * \param ctx HMAC context to be initialized |
| 112 | * \param is384 0 = use SHA512, 1 = use SHA384 |
| 113 | * \param key HMAC secret key |
| 114 | * \param keylen length of the HMAC key |
| 115 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 116 | void sha4_hmac_starts( sha4_context *ctx, const unsigned char *key, size_t keylen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | int is384 ); |
| 118 | |
| 119 | /** |
| 120 | * \brief SHA-512 HMAC process buffer |
| 121 | * |
| 122 | * \param ctx HMAC context |
| 123 | * \param input buffer holding the data |
| 124 | * \param ilen length of the input data |
| 125 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 126 | void sha4_hmac_update( sha4_context *ctx, const unsigned char *input, size_t ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * \brief SHA-512 HMAC final digest |
| 130 | * |
| 131 | * \param ctx HMAC context |
| 132 | * \param output SHA-384/512 HMAC checksum result |
| 133 | */ |
| 134 | void sha4_hmac_finish( sha4_context *ctx, unsigned char output[64] ); |
| 135 | |
| 136 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 137 | * \brief SHA-512 HMAC context reset |
| 138 | * |
| 139 | * \param ctx HMAC context to be reset |
| 140 | */ |
| 141 | void sha4_hmac_reset( sha4_context *ctx ); |
| 142 | |
| 143 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | * \brief Output = HMAC-SHA-512( hmac key, input buffer ) |
| 145 | * |
| 146 | * \param key HMAC secret key |
| 147 | * \param keylen length of the HMAC key |
| 148 | * \param input buffer holding the data |
| 149 | * \param ilen length of the input data |
| 150 | * \param output HMAC-SHA-384/512 result |
| 151 | * \param is384 0 = use SHA512, 1 = use SHA384 |
| 152 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 153 | void sha4_hmac( const unsigned char *key, size_t keylen, |
| 154 | const unsigned char *input, size_t ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 155 | unsigned char output[64], int is384 ); |
| 156 | |
| 157 | /** |
| 158 | * \brief Checkup routine |
| 159 | * |
| 160 | * \return 0 if successful, or 1 if the test failed |
| 161 | */ |
| 162 | int sha4_self_test( int verbose ); |
| 163 | |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame^] | 164 | /* Internal use */ |
| 165 | void sha4_process( sha4_context *ctx, const unsigned char data[128] ); |
| 166 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 167 | #ifdef __cplusplus |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | #endif /* sha4.h */ |