blob: c0cc74fef828785807f9e7b5aa5e160323800406 [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 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00006 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00008 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Paul Bakker40e46942009-01-03 21:51:57 +000024#ifndef POLARSSL_SHA4_H
25#define POLARSSL_SHA4_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Paul Bakker4087c472013-06-12 16:49:10 +020027#include "config.h"
28
Paul Bakker23986e52011-04-24 08:57:21 +000029#include <string.h>
30
Paul Bakker5121ce52009-01-03 21:22:43 +000031#if defined(_MSC_VER) || defined(__WATCOMC__)
32 #define UL64(x) x##ui64
Paul Bakker5c2364c2012-10-01 14:41:15 +000033 typedef unsigned __int64 uint64_t;
Paul Bakker5121ce52009-01-03 21:22:43 +000034#else
Paul Bakker5c2364c2012-10-01 14:41:15 +000035 #include <inttypes.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000036 #define UL64(x) x##ULL
Paul Bakker5121ce52009-01-03 21:22:43 +000037#endif
38
Paul Bakker5c2364c2012-10-01 14:41:15 +000039#define POLARSSL_ERR_SHA4_FILE_IO_ERROR -0x007A /**< Read/write error in file. */
40
Paul Bakker4087c472013-06-12 16:49:10 +020041#if !defined(POLARSSL_SHA1_ALT)
42// Regular implementation
43//
44
Paul Bakker5121ce52009-01-03 21:22:43 +000045/**
46 * \brief SHA-512 context structure
47 */
48typedef struct
49{
Paul Bakker5c2364c2012-10-01 14:41:15 +000050 uint64_t total[2]; /*!< number of bytes processed */
51 uint64_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000052 unsigned char buffer[128]; /*!< data block being processed */
53
54 unsigned char ipad[128]; /*!< HMAC: inner padding */
55 unsigned char opad[128]; /*!< HMAC: outer padding */
56 int is384; /*!< 0 => SHA-512, else SHA-384 */
57}
58sha4_context;
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63
64/**
65 * \brief SHA-512 context setup
66 *
67 * \param ctx context to be initialized
68 * \param is384 0 = use SHA512, 1 = use SHA384
69 */
70void sha4_starts( sha4_context *ctx, int is384 );
71
72/**
73 * \brief SHA-512 process buffer
74 *
75 * \param ctx SHA-512 context
76 * \param input buffer holding the data
77 * \param ilen length of the input data
78 */
Paul Bakker23986e52011-04-24 08:57:21 +000079void sha4_update( sha4_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81/**
82 * \brief SHA-512 final digest
83 *
84 * \param ctx SHA-512 context
85 * \param output SHA-384/512 checksum result
86 */
87void sha4_finish( sha4_context *ctx, unsigned char output[64] );
88
Paul Bakker4087c472013-06-12 16:49:10 +020089#ifdef __cplusplus
90}
91#endif
92
93#else /* POLARSSL_SHA4_ALT */
94#include "sha4_alt.h"
95#endif /* POLARSSL_SHA4_ALT */
96
97#ifdef __cplusplus
98extern "C" {
99#endif
100
Paul Bakker5121ce52009-01-03 21:22:43 +0000101/**
102 * \brief Output = SHA-512( input buffer )
103 *
104 * \param input buffer holding the data
105 * \param ilen length of the input data
106 * \param output SHA-384/512 checksum result
107 * \param is384 0 = use SHA512, 1 = use SHA384
108 */
Paul Bakker23986e52011-04-24 08:57:21 +0000109void sha4( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 unsigned char output[64], int is384 );
111
112/**
113 * \brief Output = SHA-512( file contents )
114 *
115 * \param path input file name
116 * \param output SHA-384/512 checksum result
117 * \param is384 0 = use SHA512, 1 = use SHA384
118 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000119 * \return 0 if successful, or POLARSSL_ERR_SHA4_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000121int sha4_file( const char *path, unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
123/**
124 * \brief SHA-512 HMAC context setup
125 *
126 * \param ctx HMAC context to be initialized
127 * \param is384 0 = use SHA512, 1 = use SHA384
128 * \param key HMAC secret key
129 * \param keylen length of the HMAC key
130 */
Paul Bakker23986e52011-04-24 08:57:21 +0000131void sha4_hmac_starts( sha4_context *ctx, const unsigned char *key, size_t keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 int is384 );
133
134/**
135 * \brief SHA-512 HMAC process buffer
136 *
137 * \param ctx HMAC context
138 * \param input buffer holding the data
139 * \param ilen length of the input data
140 */
Paul Bakker23986e52011-04-24 08:57:21 +0000141void sha4_hmac_update( sha4_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143/**
144 * \brief SHA-512 HMAC final digest
145 *
146 * \param ctx HMAC context
147 * \param output SHA-384/512 HMAC checksum result
148 */
149void sha4_hmac_finish( sha4_context *ctx, unsigned char output[64] );
150
151/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000152 * \brief SHA-512 HMAC context reset
153 *
154 * \param ctx HMAC context to be reset
155 */
156void sha4_hmac_reset( sha4_context *ctx );
157
158/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
160 *
161 * \param key HMAC secret key
162 * \param keylen length of the HMAC key
163 * \param input buffer holding the data
164 * \param ilen length of the input data
165 * \param output HMAC-SHA-384/512 result
166 * \param is384 0 = use SHA512, 1 = use SHA384
167 */
Paul Bakker23986e52011-04-24 08:57:21 +0000168void sha4_hmac( const unsigned char *key, size_t keylen,
169 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000170 unsigned char output[64], int is384 );
171
172/**
173 * \brief Checkup routine
174 *
175 * \return 0 if successful, or 1 if the test failed
176 */
177int sha4_self_test( int verbose );
178
179#ifdef __cplusplus
180}
181#endif
182
183#endif /* sha4.h */