4 * \brief SHA-384 and SHA-512 cryptographic hash function
6 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
8 * This file is part of mbed TLS (https://polarssl.org)
10 * 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.
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.
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.
24 #ifndef POLARSSL_SHA512_H
25 #define POLARSSL_SHA512_H
27 // Don't include polarssl/config.h since we are compiling outside of PolarSSL
29 #if !defined(POLARSSL_CONFIG_FILE)
32 #include POLARSSL_CONFIG_FILE
38 #if defined(_MSC_VER) || defined(__WATCOMC__)
39 #define UL64(x) x##ui64
40 typedef unsigned __int64 uint64_t;
43 #define UL64(x) x##ULL
46 #define POLARSSL_ERR_SHA512_FILE_IO_ERROR -0x007A /**< Read/write error in file. */
48 #if !defined(POLARSSL_SHA512_ALT)
49 // Regular implementation
57 * \brief SHA-512 context structure
61 uint64_t total[2]; /*!< number of bytes processed */
62 uint64_t state[8]; /*!< intermediate digest state */
63 unsigned char buffer[128]; /*!< data block being processed */
65 unsigned char ipad[128]; /*!< HMAC: inner padding */
66 unsigned char opad[128]; /*!< HMAC: outer padding */
67 int is384; /*!< 0 => SHA-512, else SHA-384 */
72 * \brief Initialize SHA-512 context
74 * \param ctx SHA-512 context to be initialized
76 void sha512_init( sha512_context *ctx );
79 * \brief Clear SHA-512 context
81 * \param ctx SHA-512 context to be cleared
83 void sha512_free( sha512_context *ctx );
86 * \brief SHA-512 context setup
88 * \param ctx context to be initialized
89 * \param is384 0 = use SHA512, 1 = use SHA384
91 void sha512_starts( sha512_context *ctx, int is384 );
94 * \brief SHA-512 process buffer
96 * \param ctx SHA-512 context
97 * \param input buffer holding the data
98 * \param ilen length of the input data
100 void sha512_update( sha512_context *ctx, const unsigned char *input,
104 * \brief SHA-512 final digest
106 * \param ctx SHA-512 context
107 * \param output SHA-384/512 checksum result
109 void sha512_finish( sha512_context *ctx, unsigned char output[64] );
115 #else /* POLARSSL_SHA512_ALT */
116 #include "sha512_alt.h"
117 #endif /* POLARSSL_SHA512_ALT */
124 * \brief Output = SHA-512( input buffer )
126 * \param input buffer holding the data
127 * \param ilen length of the input data
128 * \param output SHA-384/512 checksum result
129 * \param is384 0 = use SHA512, 1 = use SHA384
131 void sha512( const unsigned char *input, size_t ilen,
132 unsigned char output[64], int is384 );
135 * \brief Output = SHA-512( file contents )
137 * \param path input file name
138 * \param output SHA-384/512 checksum result
139 * \param is384 0 = use SHA512, 1 = use SHA384
141 * \return 0 if successful, or POLARSSL_ERR_SHA512_FILE_IO_ERROR
143 int sha512_file( const char *path, unsigned char output[64], int is384 );
146 * \brief SHA-512 HMAC context setup
148 * \param ctx HMAC context to be initialized
149 * \param is384 0 = use SHA512, 1 = use SHA384
150 * \param key HMAC secret key
151 * \param keylen length of the HMAC key
153 void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key,
154 size_t keylen, int is384 );
157 * \brief SHA-512 HMAC process buffer
159 * \param ctx HMAC context
160 * \param input buffer holding the data
161 * \param ilen length of the input data
163 void sha512_hmac_update( sha512_context *ctx, const unsigned char *input,
167 * \brief SHA-512 HMAC final digest
169 * \param ctx HMAC context
170 * \param output SHA-384/512 HMAC checksum result
172 void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] );
175 * \brief SHA-512 HMAC context reset
177 * \param ctx HMAC context to be reset
179 void sha512_hmac_reset( sha512_context *ctx );
182 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
184 * \param key HMAC secret key
185 * \param keylen length of the HMAC key
186 * \param input buffer holding the data
187 * \param ilen length of the input data
188 * \param output HMAC-SHA-384/512 result
189 * \param is384 0 = use SHA512, 1 = use SHA384
191 void sha512_hmac( const unsigned char *key, size_t keylen,
192 const unsigned char *input, size_t ilen,
193 unsigned char output[64], int is384 );
196 * \brief Checkup routine
198 * \return 0 if successful, or 1 if the test failed
200 int sha512_self_test( int verbose );
203 void sha512_process( sha512_context *ctx, const unsigned char data[128] );
209 #endif /* sha512.h */