blob: 06a85f10d9a775277d860931f16982c51557adf5 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file sha4.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +00004 * \brief SHA-384 and SHA-512 cryptographic hash function
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
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
Paul Bakker23986e52011-04-24 08:57:21 +000030#include <string.h>
31
Paul Bakker5121ce52009-01-03 21:22:43 +000032#if defined(_MSC_VER) || defined(__WATCOMC__)
33 #define UL64(x) x##ui64
Paul Bakker4f5ae802011-12-04 22:10:28 +000034 #define long64 __int64
Paul Bakker5121ce52009-01-03 21:22:43 +000035#else
36 #define UL64(x) x##ULL
Paul Bakker4f5ae802011-12-04 22:10:28 +000037 #define long64 long long
Paul Bakker5121ce52009-01-03 21:22:43 +000038#endif
39
40/**
41 * \brief SHA-512 context structure
42 */
43typedef struct
44{
Paul Bakker4f5ae802011-12-04 22:10:28 +000045 unsigned long64 total[2]; /*!< number of bytes processed */
46 unsigned long64 state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000047 unsigned char buffer[128]; /*!< data block being processed */
48
49 unsigned char ipad[128]; /*!< HMAC: inner padding */
50 unsigned char opad[128]; /*!< HMAC: outer padding */
51 int is384; /*!< 0 => SHA-512, else SHA-384 */
52}
53sha4_context;
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
59/**
60 * \brief SHA-512 context setup
61 *
62 * \param ctx context to be initialized
63 * \param is384 0 = use SHA512, 1 = use SHA384
64 */
65void sha4_starts( sha4_context *ctx, int is384 );
66
67/**
68 * \brief SHA-512 process buffer
69 *
70 * \param ctx SHA-512 context
71 * \param input buffer holding the data
72 * \param ilen length of the input data
73 */
Paul Bakker23986e52011-04-24 08:57:21 +000074void sha4_update( sha4_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000075
76/**
77 * \brief SHA-512 final digest
78 *
79 * \param ctx SHA-512 context
80 * \param output SHA-384/512 checksum result
81 */
82void sha4_finish( sha4_context *ctx, unsigned char output[64] );
83
84/**
85 * \brief Output = SHA-512( input buffer )
86 *
87 * \param input buffer holding the data
88 * \param ilen length of the input data
89 * \param output SHA-384/512 checksum result
90 * \param is384 0 = use SHA512, 1 = use SHA384
91 */
Paul Bakker23986e52011-04-24 08:57:21 +000092void sha4( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +000093 unsigned char output[64], int is384 );
94
95/**
96 * \brief Output = SHA-512( file contents )
97 *
98 * \param path input file name
99 * \param output SHA-384/512 checksum result
100 * \param is384 0 = use SHA512, 1 = use SHA384
101 *
102 * \return 0 if successful, 1 if fopen failed,
103 * or 2 if fread failed
104 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000105int sha4_file( const char *path, unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107/**
108 * \brief SHA-512 HMAC context setup
109 *
110 * \param ctx HMAC context to be initialized
111 * \param is384 0 = use SHA512, 1 = use SHA384
112 * \param key HMAC secret key
113 * \param keylen length of the HMAC key
114 */
Paul Bakker23986e52011-04-24 08:57:21 +0000115void sha4_hmac_starts( sha4_context *ctx, const unsigned char *key, size_t keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 int is384 );
117
118/**
119 * \brief SHA-512 HMAC process buffer
120 *
121 * \param ctx HMAC context
122 * \param input buffer holding the data
123 * \param ilen length of the input data
124 */
Paul Bakker23986e52011-04-24 08:57:21 +0000125void sha4_hmac_update( sha4_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127/**
128 * \brief SHA-512 HMAC final digest
129 *
130 * \param ctx HMAC context
131 * \param output SHA-384/512 HMAC checksum result
132 */
133void sha4_hmac_finish( sha4_context *ctx, unsigned char output[64] );
134
135/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000136 * \brief SHA-512 HMAC context reset
137 *
138 * \param ctx HMAC context to be reset
139 */
140void sha4_hmac_reset( sha4_context *ctx );
141
142/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
144 *
145 * \param key HMAC secret key
146 * \param keylen length of the HMAC key
147 * \param input buffer holding the data
148 * \param ilen length of the input data
149 * \param output HMAC-SHA-384/512 result
150 * \param is384 0 = use SHA512, 1 = use SHA384
151 */
Paul Bakker23986e52011-04-24 08:57:21 +0000152void sha4_hmac( const unsigned char *key, size_t keylen,
153 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 unsigned char output[64], int is384 );
155
156/**
157 * \brief Checkup routine
158 *
159 * \return 0 if successful, or 1 if the test failed
160 */
161int sha4_self_test( int verbose );
162
163#ifdef __cplusplus
164}
165#endif
166
167#endif /* sha4.h */