blob: 90b5e3e5404cfe0105e16c494b6a3f701a9ac054 [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é-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.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__)
36 #define UL64(x) x##ui64
Paul Bakker5c2364c2012-10-01 14:41:15 +000037 typedef unsigned __int64 uint64_t;
Paul Bakker5121ce52009-01-03 21:22:43 +000038#else
Paul Bakker5c2364c2012-10-01 14:41:15 +000039 #include <inttypes.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000040 #define UL64(x) x##ULL
Paul Bakker5121ce52009-01-03 21:22:43 +000041#endif
42
Paul Bakker9e36f042013-06-30 14:34:05 +020043#define POLARSSL_ERR_SHA512_FILE_IO_ERROR -0x007A /**< Read/write error in file. */
Paul Bakker5c2364c2012-10-01 14:41:15 +000044
Paul Bakker9e36f042013-06-30 14:34:05 +020045#if !defined(POLARSSL_SHA512_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020046// Regular implementation
47//
48
Paul Bakker407a0da2013-06-27 14:29:21 +020049#ifdef __cplusplus
50extern "C" {
51#endif
52
Paul Bakker5121ce52009-01-03 21:22:43 +000053/**
54 * \brief SHA-512 context structure
55 */
56typedef struct
57{
Paul Bakker5c2364c2012-10-01 14:41:15 +000058 uint64_t total[2]; /*!< number of bytes processed */
59 uint64_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000060 unsigned char buffer[128]; /*!< data block being processed */
61
62 unsigned char ipad[128]; /*!< HMAC: inner padding */
63 unsigned char opad[128]; /*!< HMAC: outer padding */
64 int is384; /*!< 0 => SHA-512, else SHA-384 */
65}
Paul Bakker9e36f042013-06-30 14:34:05 +020066sha512_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker5121ce52009-01-03 21:22:43 +000068/**
Paul Bakker5b4af392014-06-26 12:09:34 +020069 * \brief Initialize SHA-512 context
70 *
71 * \param ctx SHA-512 context to be initialized
72 */
73void sha512_init( sha512_context *ctx );
74
75/**
76 * \brief Clear SHA-512 context
77 *
78 * \param ctx SHA-512 context to be cleared
79 */
80void sha512_free( sha512_context *ctx );
81
82/**
Paul Bakker5121ce52009-01-03 21:22:43 +000083 * \brief SHA-512 context setup
84 *
85 * \param ctx context to be initialized
86 * \param is384 0 = use SHA512, 1 = use SHA384
87 */
Paul Bakker9e36f042013-06-30 14:34:05 +020088void sha512_starts( sha512_context *ctx, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90/**
91 * \brief SHA-512 process buffer
92 *
93 * \param ctx SHA-512 context
94 * \param input buffer holding the data
95 * \param ilen length of the input data
96 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020097void sha512_update( sha512_context *ctx, const unsigned char *input,
98 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000099
100/**
101 * \brief SHA-512 final digest
102 *
103 * \param ctx SHA-512 context
104 * \param output SHA-384/512 checksum result
105 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200106void sha512_finish( sha512_context *ctx, unsigned char output[64] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Paul Bakker90995b52013-06-24 19:20:35 +0200108#ifdef __cplusplus
109}
110#endif
111
Paul Bakker9e36f042013-06-30 14:34:05 +0200112#else /* POLARSSL_SHA512_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200113#include "sha512_alt.h"
Paul Bakker9e36f042013-06-30 14:34:05 +0200114#endif /* POLARSSL_SHA512_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200115
116#ifdef __cplusplus
117extern "C" {
118#endif
119
Paul Bakker5121ce52009-01-03 21:22:43 +0000120/**
121 * \brief Output = SHA-512( input buffer )
122 *
123 * \param input buffer holding the data
124 * \param ilen length of the input data
125 * \param output SHA-384/512 checksum result
126 * \param is384 0 = use SHA512, 1 = use SHA384
127 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200128void sha512( const unsigned char *input, size_t ilen,
129 unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
131/**
132 * \brief Output = SHA-512( file contents )
133 *
134 * \param path input file name
135 * \param output SHA-384/512 checksum result
136 * \param is384 0 = use SHA512, 1 = use SHA384
137 *
Paul Bakker9e36f042013-06-30 14:34:05 +0200138 * \return 0 if successful, or POLARSSL_ERR_SHA512_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200140int sha512_file( const char *path, unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142/**
143 * \brief SHA-512 HMAC context setup
144 *
145 * \param ctx HMAC context to be initialized
146 * \param is384 0 = use SHA512, 1 = use SHA384
147 * \param key HMAC secret key
148 * \param keylen length of the HMAC key
149 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200150void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key,
151 size_t keylen, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
153/**
154 * \brief SHA-512 HMAC process buffer
155 *
156 * \param ctx HMAC context
157 * \param input buffer holding the data
158 * \param ilen length of the input data
159 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200160void sha512_hmac_update( sha512_context *ctx, const unsigned char *input,
161 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000162
163/**
164 * \brief SHA-512 HMAC final digest
165 *
166 * \param ctx HMAC context
167 * \param output SHA-384/512 HMAC checksum result
168 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200169void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000172 * \brief SHA-512 HMAC context reset
173 *
174 * \param ctx HMAC context to be reset
175 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200176void sha512_hmac_reset( sha512_context *ctx );
Paul Bakker7d3b6612010-03-21 16:23:13 +0000177
178/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
180 *
181 * \param key HMAC secret key
182 * \param keylen length of the HMAC key
183 * \param input buffer holding the data
184 * \param ilen length of the input data
185 * \param output HMAC-SHA-384/512 result
186 * \param is384 0 = use SHA512, 1 = use SHA384
187 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200188void sha512_hmac( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000189 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 unsigned char output[64], int is384 );
191
192/**
193 * \brief Checkup routine
194 *
195 * \return 0 if successful, or 1 if the test failed
196 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200197int sha512_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100199/* Internal use */
Paul Bakker9e36f042013-06-30 14:34:05 +0200200void sha512_process( sha512_context *ctx, const unsigned char data[128] );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100201
Paul Bakker5121ce52009-01-03 21:22:43 +0000202#ifdef __cplusplus
203}
204#endif
205
Paul Bakkerd2681d82013-06-30 14:49:12 +0200206#endif /* sha512.h */