blob: 15d266cd479ecc4dc16ecf0748a6bb8841938ef5 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
Paul Bakkerd2681d82013-06-30 14:49:12 +02002 * \file sha512.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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +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 Bakkerd2681d82013-06-30 14:49:12 +020024#ifndef POLARSSL_SHA512_H
25#define POLARSSL_SHA512_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000034
Paul Bakker5121ce52009-01-03 21:22:43 +000035#if defined(_MSC_VER) || defined(__WATCOMC__)
Paul Bakker5c2364c2012-10-01 14:41:15 +000036 typedef unsigned __int64 uint64_t;
Paul Bakker5121ce52009-01-03 21:22:43 +000037#else
Paul Bakker5c2364c2012-10-01 14:41:15 +000038 #include <inttypes.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000039#endif
40
Paul Bakker9e36f042013-06-30 14:34:05 +020041#define POLARSSL_ERR_SHA512_FILE_IO_ERROR -0x007A /**< Read/write error in file. */
Paul Bakker5c2364c2012-10-01 14:41:15 +000042
Paul Bakker9e36f042013-06-30 14:34:05 +020043#if !defined(POLARSSL_SHA512_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020044// Regular implementation
45//
46
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/**
52 * \brief SHA-512 context structure
53 */
54typedef struct
55{
Paul Bakker5c2364c2012-10-01 14:41:15 +000056 uint64_t total[2]; /*!< number of bytes processed */
57 uint64_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000058 unsigned char buffer[128]; /*!< data block being processed */
59
60 unsigned char ipad[128]; /*!< HMAC: inner padding */
61 unsigned char opad[128]; /*!< HMAC: outer padding */
62 int is384; /*!< 0 => SHA-512, else SHA-384 */
63}
Paul Bakker9e36f042013-06-30 14:34:05 +020064sha512_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Paul Bakker5121ce52009-01-03 21:22:43 +000066/**
Paul Bakker5b4af392014-06-26 12:09:34 +020067 * \brief Initialize SHA-512 context
68 *
69 * \param ctx SHA-512 context to be initialized
70 */
71void sha512_init( sha512_context *ctx );
72
73/**
74 * \brief Clear SHA-512 context
75 *
76 * \param ctx SHA-512 context to be cleared
77 */
78void sha512_free( sha512_context *ctx );
79
80/**
Paul Bakker5121ce52009-01-03 21:22:43 +000081 * \brief SHA-512 context setup
82 *
83 * \param ctx context to be initialized
84 * \param is384 0 = use SHA512, 1 = use SHA384
85 */
Paul Bakker9e36f042013-06-30 14:34:05 +020086void sha512_starts( sha512_context *ctx, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88/**
89 * \brief SHA-512 process buffer
90 *
91 * \param ctx SHA-512 context
92 * \param input buffer holding the data
93 * \param ilen length of the input data
94 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020095void sha512_update( sha512_context *ctx, const unsigned char *input,
96 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98/**
99 * \brief SHA-512 final digest
100 *
101 * \param ctx SHA-512 context
102 * \param output SHA-384/512 checksum result
103 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200104void sha512_finish( sha512_context *ctx, unsigned char output[64] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Paul Bakker90995b52013-06-24 19:20:35 +0200106#ifdef __cplusplus
107}
108#endif
109
Paul Bakker9e36f042013-06-30 14:34:05 +0200110#else /* POLARSSL_SHA512_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200111#include "sha512_alt.h"
Paul Bakker9e36f042013-06-30 14:34:05 +0200112#endif /* POLARSSL_SHA512_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200113
114#ifdef __cplusplus
115extern "C" {
116#endif
117
Paul Bakker5121ce52009-01-03 21:22:43 +0000118/**
119 * \brief Output = SHA-512( input buffer )
120 *
121 * \param input buffer holding the data
122 * \param ilen length of the input data
123 * \param output SHA-384/512 checksum result
124 * \param is384 0 = use SHA512, 1 = use SHA384
125 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200126void sha512( const unsigned char *input, size_t ilen,
127 unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/**
130 * \brief Output = SHA-512( file contents )
131 *
132 * \param path input file name
133 * \param output SHA-384/512 checksum result
134 * \param is384 0 = use SHA512, 1 = use SHA384
135 *
Paul Bakker9e36f042013-06-30 14:34:05 +0200136 * \return 0 if successful, or POLARSSL_ERR_SHA512_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200138int sha512_file( const char *path, unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140/**
141 * \brief SHA-512 HMAC context setup
142 *
143 * \param ctx HMAC context to be initialized
144 * \param is384 0 = use SHA512, 1 = use SHA384
145 * \param key HMAC secret key
146 * \param keylen length of the HMAC key
147 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200148void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key,
149 size_t keylen, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
151/**
152 * \brief SHA-512 HMAC process buffer
153 *
154 * \param ctx HMAC context
155 * \param input buffer holding the data
156 * \param ilen length of the input data
157 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200158void sha512_hmac_update( sha512_context *ctx, const unsigned char *input,
159 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161/**
162 * \brief SHA-512 HMAC final digest
163 *
164 * \param ctx HMAC context
165 * \param output SHA-384/512 HMAC checksum result
166 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200167void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000170 * \brief SHA-512 HMAC context reset
171 *
172 * \param ctx HMAC context to be reset
173 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200174void sha512_hmac_reset( sha512_context *ctx );
Paul Bakker7d3b6612010-03-21 16:23:13 +0000175
176/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
178 *
179 * \param key HMAC secret key
180 * \param keylen length of the HMAC key
181 * \param input buffer holding the data
182 * \param ilen length of the input data
183 * \param output HMAC-SHA-384/512 result
184 * \param is384 0 = use SHA512, 1 = use SHA384
185 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200186void sha512_hmac( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000187 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 unsigned char output[64], int is384 );
189
190/**
191 * \brief Checkup routine
192 *
193 * \return 0 if successful, or 1 if the test failed
194 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200195int sha512_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100197/* Internal use */
Paul Bakker9e36f042013-06-30 14:34:05 +0200198void sha512_process( sha512_context *ctx, const unsigned char data[128] );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100199
Paul Bakker5121ce52009-01-03 21:22:43 +0000200#ifdef __cplusplus
201}
202#endif
203
Paul Bakkerd2681d82013-06-30 14:49:12 +0200204#endif /* sha512.h */