blob: 5563676e38f3eb870c883cfd84552d3c7e5c0476 [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 Bakker5c2364c2012-10-01 14:41:15 +000034 typedef unsigned __int64 uint64_t;
Paul Bakker5121ce52009-01-03 21:22:43 +000035#else
Paul Bakker5c2364c2012-10-01 14:41:15 +000036 #include <inttypes.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000037 #define UL64(x) x##ULL
Paul Bakker5121ce52009-01-03 21:22:43 +000038#endif
39
Paul Bakker5c2364c2012-10-01 14:41:15 +000040#define POLARSSL_ERR_SHA4_FILE_IO_ERROR -0x007A /**< Read/write error in file. */
41
Paul Bakker5121ce52009-01-03 21:22:43 +000042/**
43 * \brief SHA-512 context structure
44 */
45typedef struct
46{
Paul Bakker5c2364c2012-10-01 14:41:15 +000047 uint64_t total[2]; /*!< number of bytes processed */
48 uint64_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000049 unsigned char buffer[128]; /*!< data block being processed */
50
51 unsigned char ipad[128]; /*!< HMAC: inner padding */
52 unsigned char opad[128]; /*!< HMAC: outer padding */
53 int is384; /*!< 0 => SHA-512, else SHA-384 */
54}
55sha4_context;
56
57#ifdef __cplusplus
58extern "C" {
59#endif
60
61/**
62 * \brief SHA-512 context setup
63 *
64 * \param ctx context to be initialized
65 * \param is384 0 = use SHA512, 1 = use SHA384
66 */
67void sha4_starts( sha4_context *ctx, int is384 );
68
69/**
70 * \brief SHA-512 process buffer
71 *
72 * \param ctx SHA-512 context
73 * \param input buffer holding the data
74 * \param ilen length of the input data
75 */
Paul Bakker23986e52011-04-24 08:57:21 +000076void sha4_update( sha4_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000077
78/**
79 * \brief SHA-512 final digest
80 *
81 * \param ctx SHA-512 context
82 * \param output SHA-384/512 checksum result
83 */
84void sha4_finish( sha4_context *ctx, unsigned char output[64] );
85
86/**
87 * \brief Output = SHA-512( input buffer )
88 *
89 * \param input buffer holding the data
90 * \param ilen length of the input data
91 * \param output SHA-384/512 checksum result
92 * \param is384 0 = use SHA512, 1 = use SHA384
93 */
Paul Bakker23986e52011-04-24 08:57:21 +000094void sha4( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +000095 unsigned char output[64], int is384 );
96
97/**
98 * \brief Output = SHA-512( file contents )
99 *
100 * \param path input file name
101 * \param output SHA-384/512 checksum result
102 * \param is384 0 = use SHA512, 1 = use SHA384
103 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000104 * \return 0 if successful, or POLARSSL_ERR_SHA4_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000106int sha4_file( const char *path, unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108/**
109 * \brief SHA-512 HMAC context setup
110 *
111 * \param ctx HMAC context to be initialized
112 * \param is384 0 = use SHA512, 1 = use SHA384
113 * \param key HMAC secret key
114 * \param keylen length of the HMAC key
115 */
Paul Bakker23986e52011-04-24 08:57:21 +0000116void sha4_hmac_starts( sha4_context *ctx, const unsigned char *key, size_t keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 int is384 );
118
119/**
120 * \brief SHA-512 HMAC process buffer
121 *
122 * \param ctx HMAC context
123 * \param input buffer holding the data
124 * \param ilen length of the input data
125 */
Paul Bakker23986e52011-04-24 08:57:21 +0000126void sha4_hmac_update( sha4_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
128/**
129 * \brief SHA-512 HMAC final digest
130 *
131 * \param ctx HMAC context
132 * \param output SHA-384/512 HMAC checksum result
133 */
134void sha4_hmac_finish( sha4_context *ctx, unsigned char output[64] );
135
136/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000137 * \brief SHA-512 HMAC context reset
138 *
139 * \param ctx HMAC context to be reset
140 */
141void sha4_hmac_reset( sha4_context *ctx );
142
143/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
145 *
146 * \param key HMAC secret key
147 * \param keylen length of the HMAC key
148 * \param input buffer holding the data
149 * \param ilen length of the input data
150 * \param output HMAC-SHA-384/512 result
151 * \param is384 0 = use SHA512, 1 = use SHA384
152 */
Paul Bakker23986e52011-04-24 08:57:21 +0000153void sha4_hmac( const unsigned char *key, size_t keylen,
154 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 unsigned char output[64], int is384 );
156
157/**
158 * \brief Checkup routine
159 *
160 * \return 0 if successful, or 1 if the test failed
161 */
162int sha4_self_test( int verbose );
163
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100164/* Internal use */
165void sha4_process( sha4_context *ctx, const unsigned char data[128] );
166
Paul Bakker5121ce52009-01-03 21:22:43 +0000167#ifdef __cplusplus
168}
169#endif
170
171#endif /* sha4.h */