4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef TROPICSSL_SHA4_H
36 #define TROPICSSL_SHA4_H
38 #if defined(_MSC_VER) || defined(__WATCOMC__)
39 #define UL64(x) x##ui64
42 #define UL64(x) x##ULL
43 #define int64 long long
47 * \brief SHA-512 context structure
50 unsigned int64 total
[2]; /*!< number of bytes processed */
51 unsigned int64 state
[8]; /*!< intermediate digest state */
52 unsigned char buffer
[128]; /*!< data block being processed */
54 unsigned char ipad
[128]; /*!< HMAC: inner padding */
55 unsigned char opad
[128]; /*!< HMAC: outer padding */
56 int is384
; /*!< 0 => SHA-512, else SHA-384 */
64 * \brief SHA-512 context setup
66 * \param ctx context to be initialized
67 * \param is384 0 = use SHA512, 1 = use SHA384
69 void sha4_starts(sha4_context
* ctx
, int is384
);
72 * \brief SHA-512 process buffer
74 * \param ctx SHA-512 context
75 * \param input buffer holding the data
76 * \param ilen length of the input data
78 void sha4_update(sha4_context
* ctx
, const unsigned char *input
, int ilen
);
81 * \brief SHA-512 final digest
83 * \param ctx SHA-512 context
84 * \param output SHA-384/512 checksum result
86 void sha4_finish(sha4_context
* ctx
, unsigned char output
[64]);
89 * \brief Output = SHA-512( input buffer )
91 * \param input buffer holding the data
92 * \param ilen length of the input data
93 * \param output SHA-384/512 checksum result
94 * \param is384 0 = use SHA512, 1 = use SHA384
96 void sha4(const unsigned char *input
, int ilen
,
97 unsigned char output
[64], int is384
);
100 * \brief Output = SHA-512( file contents )
102 * \param path input file name
103 * \param output SHA-384/512 checksum result
104 * \param is384 0 = use SHA512, 1 = use SHA384
106 * \return 0 if successful, 1 if fopen failed,
107 * or 2 if fread failed
109 int sha4_file(const char *path
, unsigned char output
[64], int is384
);
112 * \brief SHA-512 HMAC context setup
114 * \param ctx HMAC context to be initialized
115 * \param is384 0 = use SHA512, 1 = use SHA384
116 * \param key HMAC secret key
117 * \param keylen length of the HMAC key
119 void sha4_hmac_starts(sha4_context
* ctx
, const unsigned char *key
,
120 int keylen
, int is384
);
123 * \brief SHA-512 HMAC process buffer
125 * \param ctx HMAC context
126 * \param input buffer holding the data
127 * \param ilen length of the input data
129 void sha4_hmac_update(sha4_context
* ctx
, const unsigned char *input
,
133 * \brief SHA-512 HMAC final digest
135 * \param ctx HMAC context
136 * \param output SHA-384/512 HMAC checksum result
138 void sha4_hmac_finish(sha4_context
* ctx
, unsigned char output
[64]);
141 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
143 * \param key HMAC secret key
144 * \param keylen length of the HMAC key
145 * \param input buffer holding the data
146 * \param ilen length of the input data
147 * \param output HMAC-SHA-384/512 result
148 * \param is384 0 = use SHA512, 1 = use SHA384
150 void sha4_hmac(const unsigned char *key
, int keylen
,
151 const unsigned char *input
, int ilen
,
152 unsigned char output
[64], int is384
);
155 * \brief Checkup routine
157 * \return 0 if successful, or 1 if the test failed
159 int sha4_self_test(int verbose
);