blob: ebf2d45510100fa07c9aadf98ed26b830abb209c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file sha4.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief SHA-384/512 cryptographic hash function
5 *
Paul Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * 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.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_SHA4_H
28#define POLARSSL_SHA4_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
30#if defined(_MSC_VER) || defined(__WATCOMC__)
31 #define UL64(x) x##ui64
32 #define int64 __int64
33#else
34 #define UL64(x) x##ULL
35 #define int64 long long
36#endif
37
38/**
39 * \brief SHA-512 context structure
40 */
41typedef struct
42{
43 unsigned int64 total[2]; /*!< number of bytes processed */
44 unsigned int64 state[8]; /*!< intermediate digest state */
45 unsigned char buffer[128]; /*!< data block being processed */
46
47 unsigned char ipad[128]; /*!< HMAC: inner padding */
48 unsigned char opad[128]; /*!< HMAC: outer padding */
49 int is384; /*!< 0 => SHA-512, else SHA-384 */
50}
51sha4_context;
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57/**
58 * \brief SHA-512 context setup
59 *
60 * \param ctx context to be initialized
61 * \param is384 0 = use SHA512, 1 = use SHA384
62 */
63void sha4_starts( sha4_context *ctx, int is384 );
64
65/**
66 * \brief SHA-512 process buffer
67 *
68 * \param ctx SHA-512 context
69 * \param input buffer holding the data
70 * \param ilen length of the input data
71 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000072void sha4_update( sha4_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000073
74/**
75 * \brief SHA-512 final digest
76 *
77 * \param ctx SHA-512 context
78 * \param output SHA-384/512 checksum result
79 */
80void sha4_finish( sha4_context *ctx, unsigned char output[64] );
81
82/**
83 * \brief Output = SHA-512( input buffer )
84 *
85 * \param input buffer holding the data
86 * \param ilen length of the input data
87 * \param output SHA-384/512 checksum result
88 * \param is384 0 = use SHA512, 1 = use SHA384
89 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000090void sha4( const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +000091 unsigned char output[64], int is384 );
92
93/**
94 * \brief Output = SHA-512( file contents )
95 *
96 * \param path input file name
97 * \param output SHA-384/512 checksum result
98 * \param is384 0 = use SHA512, 1 = use SHA384
99 *
100 * \return 0 if successful, 1 if fopen failed,
101 * or 2 if fread failed
102 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000103int sha4_file( const char *path, unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105/**
106 * \brief SHA-512 HMAC context setup
107 *
108 * \param ctx HMAC context to be initialized
109 * \param is384 0 = use SHA512, 1 = use SHA384
110 * \param key HMAC secret key
111 * \param keylen length of the HMAC key
112 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000113void sha4_hmac_starts( sha4_context *ctx, const unsigned char *key, int keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 int is384 );
115
116/**
117 * \brief SHA-512 HMAC process buffer
118 *
119 * \param ctx HMAC context
120 * \param input buffer holding the data
121 * \param ilen length of the input data
122 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000123void sha4_hmac_update( sha4_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125/**
126 * \brief SHA-512 HMAC final digest
127 *
128 * \param ctx HMAC context
129 * \param output SHA-384/512 HMAC checksum result
130 */
131void sha4_hmac_finish( sha4_context *ctx, unsigned char output[64] );
132
133/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000134 * \brief SHA-512 HMAC context reset
135 *
136 * \param ctx HMAC context to be reset
137 */
138void sha4_hmac_reset( sha4_context *ctx );
139
140/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
142 *
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
149 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000150void sha4_hmac( const unsigned char *key, int keylen,
151 const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 unsigned char output[64], int is384 );
153
154/**
155 * \brief Checkup routine
156 *
157 * \return 0 if successful, or 1 if the test failed
158 */
159int sha4_self_test( int verbose );
160
161#ifdef __cplusplus
162}
163#endif
164
165#endif /* sha4.h */