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