blob: 0082a64e95657983d56f604979596baf6955d58b [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 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00008 * This file is part of mbed TLS (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 Bakkerd2681d82013-06-30 14:49:12 +020027#ifndef POLARSSL_SHA512_H
28#define POLARSSL_SHA512_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker90995b52013-06-24 19:20:35 +020035
Paul Bakker23986e52011-04-24 08:57:21 +000036#include <string.h>
37
Paul Bakker5121ce52009-01-03 21:22:43 +000038#if defined(_MSC_VER) || defined(__WATCOMC__)
39 #define UL64(x) x##ui64
Paul Bakker5c2364c2012-10-01 14:41:15 +000040 typedef unsigned __int64 uint64_t;
Paul Bakker5121ce52009-01-03 21:22:43 +000041#else
Paul Bakker5c2364c2012-10-01 14:41:15 +000042 #include <inttypes.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000043 #define UL64(x) x##ULL
Paul Bakker5121ce52009-01-03 21:22:43 +000044#endif
45
Paul Bakker9e36f042013-06-30 14:34:05 +020046#define POLARSSL_ERR_SHA512_FILE_IO_ERROR -0x007A /**< Read/write error in file. */
Paul Bakker5c2364c2012-10-01 14:41:15 +000047
Paul Bakker9e36f042013-06-30 14:34:05 +020048#if !defined(POLARSSL_SHA512_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020049// Regular implementation
50//
51
Paul Bakker407a0da2013-06-27 14:29:21 +020052#ifdef __cplusplus
53extern "C" {
54#endif
55
Paul Bakker5121ce52009-01-03 21:22:43 +000056/**
57 * \brief SHA-512 context structure
58 */
59typedef struct
60{
Paul Bakker5c2364c2012-10-01 14:41:15 +000061 uint64_t total[2]; /*!< number of bytes processed */
62 uint64_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000063 unsigned char buffer[128]; /*!< data block being processed */
64
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 */
68}
Paul Bakker9e36f042013-06-30 14:34:05 +020069sha512_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakker5121ce52009-01-03 21:22:43 +000071/**
Paul Bakker5b4af392014-06-26 12:09:34 +020072 * \brief Initialize SHA-512 context
73 *
74 * \param ctx SHA-512 context to be initialized
75 */
76void sha512_init( sha512_context *ctx );
77
78/**
79 * \brief Clear SHA-512 context
80 *
81 * \param ctx SHA-512 context to be cleared
82 */
83void sha512_free( sha512_context *ctx );
84
85/**
Paul Bakker5121ce52009-01-03 21:22:43 +000086 * \brief SHA-512 context setup
87 *
88 * \param ctx context to be initialized
89 * \param is384 0 = use SHA512, 1 = use SHA384
90 */
Paul Bakker9e36f042013-06-30 14:34:05 +020091void sha512_starts( sha512_context *ctx, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93/**
94 * \brief SHA-512 process buffer
95 *
96 * \param ctx SHA-512 context
97 * \param input buffer holding the data
98 * \param ilen length of the input data
99 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200100void sha512_update( sha512_context *ctx, const unsigned char *input,
101 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
103/**
104 * \brief SHA-512 final digest
105 *
106 * \param ctx SHA-512 context
107 * \param output SHA-384/512 checksum result
108 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200109void sha512_finish( sha512_context *ctx, unsigned char output[64] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Paul Bakker90995b52013-06-24 19:20:35 +0200111#ifdef __cplusplus
112}
113#endif
114
Paul Bakker9e36f042013-06-30 14:34:05 +0200115#else /* POLARSSL_SHA512_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200116#include "sha512_alt.h"
Paul Bakker9e36f042013-06-30 14:34:05 +0200117#endif /* POLARSSL_SHA512_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200118
119#ifdef __cplusplus
120extern "C" {
121#endif
122
Paul Bakker5121ce52009-01-03 21:22:43 +0000123/**
124 * \brief Output = SHA-512( input buffer )
125 *
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
130 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200131void sha512( const unsigned char *input, size_t ilen,
132 unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134/**
135 * \brief Output = SHA-512( file contents )
136 *
137 * \param path input file name
138 * \param output SHA-384/512 checksum result
139 * \param is384 0 = use SHA512, 1 = use SHA384
140 *
Paul Bakker9e36f042013-06-30 14:34:05 +0200141 * \return 0 if successful, or POLARSSL_ERR_SHA512_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200143int sha512_file( const char *path, unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145/**
146 * \brief SHA-512 HMAC context setup
147 *
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
152 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200153void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key,
154 size_t keylen, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
156/**
157 * \brief SHA-512 HMAC process buffer
158 *
159 * \param ctx HMAC context
160 * \param input buffer holding the data
161 * \param ilen length of the input data
162 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200163void sha512_hmac_update( sha512_context *ctx, const unsigned char *input,
164 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166/**
167 * \brief SHA-512 HMAC final digest
168 *
169 * \param ctx HMAC context
170 * \param output SHA-384/512 HMAC checksum result
171 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200172void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
174/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000175 * \brief SHA-512 HMAC context reset
176 *
177 * \param ctx HMAC context to be reset
178 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200179void sha512_hmac_reset( sha512_context *ctx );
Paul Bakker7d3b6612010-03-21 16:23:13 +0000180
181/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
183 *
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
190 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200191void sha512_hmac( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000192 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 unsigned char output[64], int is384 );
194
195/**
196 * \brief Checkup routine
197 *
198 * \return 0 if successful, or 1 if the test failed
199 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200200int sha512_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100202/* Internal use */
Paul Bakker9e36f042013-06-30 14:34:05 +0200203void sha512_process( sha512_context *ctx, const unsigned char data[128] );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100204
Paul Bakker5121ce52009-01-03 21:22:43 +0000205#ifdef __cplusplus
206}
207#endif
208
Paul Bakkerd2681d82013-06-30 14:49:12 +0200209#endif /* sha512.h */