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