| 1 | /** |
|---|
| 2 | * \file sha4.h |
|---|
| 3 | * |
|---|
| 4 | * \brief SHA-384 and SHA-512 cryptographic hash function |
|---|
| 5 | * |
|---|
| 6 | * Copyright (C) 2006-2010, Brainspark B.V. |
|---|
| 7 | * |
|---|
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
|---|
| 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
|---|
| 10 | * |
|---|
| 11 | * All rights reserved. |
|---|
| 12 | * |
|---|
| 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. |
|---|
| 26 | */ |
|---|
| 27 | #ifndef POLARSSL_SHA4_H |
|---|
| 28 | #define POLARSSL_SHA4_H |
|---|
| 29 | |
|---|
| 30 | #include <string.h> |
|---|
| 31 | |
|---|
| 32 | #define POLARSSL_ERR_SHA4_FILE_IO_ERROR -0x007A /**< Read/write error in file. */ |
|---|
| 33 | |
|---|
| 34 | #if defined(_MSC_VER) || defined(__WATCOMC__) |
|---|
| 35 | #define UL64(x) x##ui64 |
|---|
| 36 | #define long64 __int64 |
|---|
| 37 | #else |
|---|
| 38 | #define UL64(x) x##ULL |
|---|
| 39 | #define long64 long long |
|---|
| 40 | #endif |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * \brief SHA-512 context structure |
|---|
| 44 | */ |
|---|
| 45 | typedef struct |
|---|
| 46 | { |
|---|
| 47 | unsigned long64 total[2]; /*!< number of bytes processed */ |
|---|
| 48 | unsigned long64 state[8]; /*!< intermediate digest state */ |
|---|
| 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 | */ |
|---|
| 76 | void sha4_update( sha4_context *ctx, const unsigned char *input, size_t ilen ); |
|---|
| 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 | */ |
|---|
| 94 | void sha4( const unsigned char *input, size_t ilen, |
|---|
| 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 | * |
|---|
| 104 | * \return 0 if successful, or POLARSSL_ERR_SHA4_FILE_IO_ERROR |
|---|
| 105 | */ |
|---|
| 106 | int sha4_file( const char *path, unsigned char output[64], int is384 ); |
|---|
| 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 | */ |
|---|
| 116 | void sha4_hmac_starts( sha4_context *ctx, const unsigned char *key, size_t keylen, |
|---|
| 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 | */ |
|---|
| 126 | void sha4_hmac_update( sha4_context *ctx, const unsigned char *input, size_t ilen ); |
|---|
| 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 | /** |
|---|
| 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 | /** |
|---|
| 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 | */ |
|---|
| 153 | void sha4_hmac( const unsigned char *key, size_t keylen, |
|---|
| 154 | const unsigned char *input, size_t ilen, |
|---|
| 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 | |
|---|
| 164 | #ifdef __cplusplus |
|---|
| 165 | } |
|---|
| 166 | #endif |
|---|
| 167 | |
|---|
| 168 | #endif /* sha4.h */ |
|---|