blob: 64084b4aabf8f7d351b79eb1b9272d6571e8923a [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.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
Paul Bakker40e46942009-01-03 21:51:57 +000025#ifndef POLARSSL_SHA4_H
26#define POLARSSL_SHA4_H
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28#if defined(_MSC_VER) || defined(__WATCOMC__)
29 #define UL64(x) x##ui64
30 #define int64 __int64
31#else
32 #define UL64(x) x##ULL
33 #define int64 long long
34#endif
35
36/**
37 * \brief SHA-512 context structure
38 */
39typedef struct
40{
41 unsigned int64 total[2]; /*!< number of bytes processed */
42 unsigned int64 state[8]; /*!< intermediate digest state */
43 unsigned char buffer[128]; /*!< data block being processed */
44
45 unsigned char ipad[128]; /*!< HMAC: inner padding */
46 unsigned char opad[128]; /*!< HMAC: outer padding */
47 int is384; /*!< 0 => SHA-512, else SHA-384 */
48}
49sha4_context;
50
51#ifdef __cplusplus
52extern "C" {
53#endif
54
55/**
56 * \brief SHA-512 context setup
57 *
58 * \param ctx context to be initialized
59 * \param is384 0 = use SHA512, 1 = use SHA384
60 */
61void sha4_starts( sha4_context *ctx, int is384 );
62
63/**
64 * \brief SHA-512 process buffer
65 *
66 * \param ctx SHA-512 context
67 * \param input buffer holding the data
68 * \param ilen length of the input data
69 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000070void sha4_update( sha4_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000071
72/**
73 * \brief SHA-512 final digest
74 *
75 * \param ctx SHA-512 context
76 * \param output SHA-384/512 checksum result
77 */
78void sha4_finish( sha4_context *ctx, unsigned char output[64] );
79
80/**
81 * \brief Output = SHA-512( input buffer )
82 *
83 * \param input buffer holding the data
84 * \param ilen length of the input data
85 * \param output SHA-384/512 checksum result
86 * \param is384 0 = use SHA512, 1 = use SHA384
87 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000088void sha4( const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +000089 unsigned char output[64], int is384 );
90
91/**
92 * \brief Output = SHA-512( file contents )
93 *
94 * \param path input file name
95 * \param output SHA-384/512 checksum result
96 * \param is384 0 = use SHA512, 1 = use SHA384
97 *
98 * \return 0 if successful, 1 if fopen failed,
99 * or 2 if fread failed
100 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000101int sha4_file( const char *path, unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
103/**
104 * \brief SHA-512 HMAC context setup
105 *
106 * \param ctx HMAC context to be initialized
107 * \param is384 0 = use SHA512, 1 = use SHA384
108 * \param key HMAC secret key
109 * \param keylen length of the HMAC key
110 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000111void sha4_hmac_starts( sha4_context *ctx, const unsigned char *key, int keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 int is384 );
113
114/**
115 * \brief SHA-512 HMAC process buffer
116 *
117 * \param ctx HMAC context
118 * \param input buffer holding the data
119 * \param ilen length of the input data
120 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000121void sha4_hmac_update( sha4_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
123/**
124 * \brief SHA-512 HMAC final digest
125 *
126 * \param ctx HMAC context
127 * \param output SHA-384/512 HMAC checksum result
128 */
129void sha4_hmac_finish( sha4_context *ctx, unsigned char output[64] );
130
131/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000132 * \brief SHA-512 HMAC context reset
133 *
134 * \param ctx HMAC context to be reset
135 */
136void sha4_hmac_reset( sha4_context *ctx );
137
138/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
140 *
141 * \param key HMAC secret key
142 * \param keylen length of the HMAC key
143 * \param input buffer holding the data
144 * \param ilen length of the input data
145 * \param output HMAC-SHA-384/512 result
146 * \param is384 0 = use SHA512, 1 = use SHA384
147 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000148void sha4_hmac( const unsigned char *key, int keylen,
149 const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 unsigned char output[64], int is384 );
151
152/**
153 * \brief Checkup routine
154 *
155 * \return 0 if successful, or 1 if the test failed
156 */
157int sha4_self_test( int verbose );
158
159#ifdef __cplusplus
160}
161#endif
162
163#endif /* sha4.h */