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