blob: a7f84437674056d730b8f415f9b31d9e7462b6de [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file sha4.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
Paul Bakker785a9ee2009-01-25 14:15:10 +00006 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
8 * 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 */
67void sha4_update( sha4_context *ctx, unsigned char *input, int ilen );
68
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 */
85void sha4( unsigned char *input, int ilen,
86 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 */
98int sha4_file( char *path, unsigned char output[64], int is384 );
99
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 */
108void sha4_hmac_starts( sha4_context *ctx, unsigned char *key, int keylen,
109 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 */
118void sha4_hmac_update( sha4_context *ctx, unsigned char *input, int ilen );
119
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/**
129 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
130 *
131 * \param key HMAC secret key
132 * \param keylen length of the HMAC key
133 * \param input buffer holding the data
134 * \param ilen length of the input data
135 * \param output HMAC-SHA-384/512 result
136 * \param is384 0 = use SHA512, 1 = use SHA384
137 */
138void sha4_hmac( unsigned char *key, int keylen,
139 unsigned char *input, int ilen,
140 unsigned char output[64], int is384 );
141
142/**
143 * \brief Checkup routine
144 *
145 * \return 0 if successful, or 1 if the test failed
146 */
147int sha4_self_test( int verbose );
148
149#ifdef __cplusplus
150}
151#endif
152
153#endif /* sha4.h */