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 | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2010, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame^] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame^] | 8 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 24 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 25 | #ifndef POLARSSL_SHA4_H |
| 26 | #define POLARSSL_SHA4_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
| 28 | #if defined(_MSC_VER) || defined(__WATCOMC__) |
| 29 | #define UL64(x) x##ui64 |
| 30 | #define int64 __int64 |
| 31 | #else |
| 32 | #define UL64(x) x##ULL |
| 33 | #define int64 long long |
| 34 | #endif |
| 35 | |
| 36 | /** |
| 37 | * \brief SHA-512 context structure |
| 38 | */ |
| 39 | typedef struct |
| 40 | { |
| 41 | unsigned int64 total[2]; /*!< number of bytes processed */ |
| 42 | unsigned int64 state[8]; /*!< intermediate digest state */ |
| 43 | unsigned char buffer[128]; /*!< data block being processed */ |
| 44 | |
| 45 | unsigned char ipad[128]; /*!< HMAC: inner padding */ |
| 46 | unsigned char opad[128]; /*!< HMAC: outer padding */ |
| 47 | int is384; /*!< 0 => SHA-512, else SHA-384 */ |
| 48 | } |
| 49 | sha4_context; |
| 50 | |
| 51 | #ifdef __cplusplus |
| 52 | extern "C" { |
| 53 | #endif |
| 54 | |
| 55 | /** |
| 56 | * \brief SHA-512 context setup |
| 57 | * |
| 58 | * \param ctx context to be initialized |
| 59 | * \param is384 0 = use SHA512, 1 = use SHA384 |
| 60 | */ |
| 61 | void sha4_starts( sha4_context *ctx, int is384 ); |
| 62 | |
| 63 | /** |
| 64 | * \brief SHA-512 process buffer |
| 65 | * |
| 66 | * \param ctx SHA-512 context |
| 67 | * \param input buffer holding the data |
| 68 | * \param ilen length of the input data |
| 69 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 70 | void sha4_update( sha4_context *ctx, const unsigned char *input, int ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * \brief SHA-512 final digest |
| 74 | * |
| 75 | * \param ctx SHA-512 context |
| 76 | * \param output SHA-384/512 checksum result |
| 77 | */ |
| 78 | void sha4_finish( sha4_context *ctx, unsigned char output[64] ); |
| 79 | |
| 80 | /** |
| 81 | * \brief Output = SHA-512( input buffer ) |
| 82 | * |
| 83 | * \param input buffer holding the data |
| 84 | * \param ilen length of the input data |
| 85 | * \param output SHA-384/512 checksum result |
| 86 | * \param is384 0 = use SHA512, 1 = use SHA384 |
| 87 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 88 | void sha4( const unsigned char *input, int ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 89 | unsigned char output[64], int is384 ); |
| 90 | |
| 91 | /** |
| 92 | * \brief Output = SHA-512( file contents ) |
| 93 | * |
| 94 | * \param path input file name |
| 95 | * \param output SHA-384/512 checksum result |
| 96 | * \param is384 0 = use SHA512, 1 = use SHA384 |
| 97 | * |
| 98 | * \return 0 if successful, 1 if fopen failed, |
| 99 | * or 2 if fread failed |
| 100 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 101 | int sha4_file( const char *path, unsigned char output[64], int is384 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * \brief SHA-512 HMAC context setup |
| 105 | * |
| 106 | * \param ctx HMAC context to be initialized |
| 107 | * \param is384 0 = use SHA512, 1 = use SHA384 |
| 108 | * \param key HMAC secret key |
| 109 | * \param keylen length of the HMAC key |
| 110 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 111 | void sha4_hmac_starts( sha4_context *ctx, const unsigned char *key, int keylen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 112 | int is384 ); |
| 113 | |
| 114 | /** |
| 115 | * \brief SHA-512 HMAC process buffer |
| 116 | * |
| 117 | * \param ctx HMAC context |
| 118 | * \param input buffer holding the data |
| 119 | * \param ilen length of the input data |
| 120 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 121 | void sha4_hmac_update( sha4_context *ctx, const unsigned char *input, int ilen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 122 | |
| 123 | /** |
| 124 | * \brief SHA-512 HMAC final digest |
| 125 | * |
| 126 | * \param ctx HMAC context |
| 127 | * \param output SHA-384/512 HMAC checksum result |
| 128 | */ |
| 129 | void sha4_hmac_finish( sha4_context *ctx, unsigned char output[64] ); |
| 130 | |
| 131 | /** |
Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 132 | * \brief SHA-512 HMAC context reset |
| 133 | * |
| 134 | * \param ctx HMAC context to be reset |
| 135 | */ |
| 136 | void sha4_hmac_reset( sha4_context *ctx ); |
| 137 | |
| 138 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 139 | * \brief Output = HMAC-SHA-512( hmac key, input buffer ) |
| 140 | * |
| 141 | * \param key HMAC secret key |
| 142 | * \param keylen length of the HMAC key |
| 143 | * \param input buffer holding the data |
| 144 | * \param ilen length of the input data |
| 145 | * \param output HMAC-SHA-384/512 result |
| 146 | * \param is384 0 = use SHA512, 1 = use SHA384 |
| 147 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 148 | void sha4_hmac( const unsigned char *key, int keylen, |
| 149 | const unsigned char *input, int ilen, |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 150 | unsigned char output[64], int is384 ); |
| 151 | |
| 152 | /** |
| 153 | * \brief Checkup routine |
| 154 | * |
| 155 | * \return 0 if successful, or 1 if the test failed |
| 156 | */ |
| 157 | int sha4_self_test( int verbose ); |
| 158 | |
| 159 | #ifdef __cplusplus |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | #endif /* sha4.h */ |