blob: c60564f487a447e2ac95b333853fe1fbc2b2724f [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 *
8 * This file is part of PolarSSL (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/**
72 * \brief SHA-512 context setup
73 *
74 * \param ctx context to be initialized
75 * \param is384 0 = use SHA512, 1 = use SHA384
76 */
Paul Bakker9e36f042013-06-30 14:34:05 +020077void sha512_starts( sha512_context *ctx, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79/**
80 * \brief SHA-512 process buffer
81 *
82 * \param ctx SHA-512 context
83 * \param input buffer holding the data
84 * \param ilen length of the input data
85 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020086void sha512_update( sha512_context *ctx, const unsigned char *input,
87 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000088
89/**
90 * \brief SHA-512 final digest
91 *
92 * \param ctx SHA-512 context
93 * \param output SHA-384/512 checksum result
94 */
Paul Bakker9e36f042013-06-30 14:34:05 +020095void sha512_finish( sha512_context *ctx, unsigned char output[64] );
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Paul Bakker90995b52013-06-24 19:20:35 +020097#ifdef __cplusplus
98}
99#endif
100
Paul Bakker9e36f042013-06-30 14:34:05 +0200101#else /* POLARSSL_SHA512_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200102#include "sha512_alt.h"
Paul Bakker9e36f042013-06-30 14:34:05 +0200103#endif /* POLARSSL_SHA512_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200104
105#ifdef __cplusplus
106extern "C" {
107#endif
108
Paul Bakker5121ce52009-01-03 21:22:43 +0000109/**
110 * \brief Output = SHA-512( input buffer )
111 *
112 * \param input buffer holding the data
113 * \param ilen length of the input data
114 * \param output SHA-384/512 checksum result
115 * \param is384 0 = use SHA512, 1 = use SHA384
116 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200117void sha512( const unsigned char *input, size_t ilen,
118 unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
120/**
121 * \brief Output = SHA-512( file contents )
122 *
123 * \param path input file name
124 * \param output SHA-384/512 checksum result
125 * \param is384 0 = use SHA512, 1 = use SHA384
126 *
Paul Bakker9e36f042013-06-30 14:34:05 +0200127 * \return 0 if successful, or POLARSSL_ERR_SHA512_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200129int sha512_file( const char *path, unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
131/**
132 * \brief SHA-512 HMAC context setup
133 *
134 * \param ctx HMAC context to be initialized
135 * \param is384 0 = use SHA512, 1 = use SHA384
136 * \param key HMAC secret key
137 * \param keylen length of the HMAC key
138 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200139void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key,
140 size_t keylen, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142/**
143 * \brief SHA-512 HMAC process buffer
144 *
145 * \param ctx HMAC context
146 * \param input buffer holding the data
147 * \param ilen length of the input data
148 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200149void sha512_hmac_update( sha512_context *ctx, const unsigned char *input,
150 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152/**
153 * \brief SHA-512 HMAC final digest
154 *
155 * \param ctx HMAC context
156 * \param output SHA-384/512 HMAC checksum result
157 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200158void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
160/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000161 * \brief SHA-512 HMAC context reset
162 *
163 * \param ctx HMAC context to be reset
164 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200165void sha512_hmac_reset( sha512_context *ctx );
Paul Bakker7d3b6612010-03-21 16:23:13 +0000166
167/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
169 *
170 * \param key HMAC secret key
171 * \param keylen length of the HMAC key
172 * \param input buffer holding the data
173 * \param ilen length of the input data
174 * \param output HMAC-SHA-384/512 result
175 * \param is384 0 = use SHA512, 1 = use SHA384
176 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200177void sha512_hmac( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000178 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 unsigned char output[64], int is384 );
180
181/**
182 * \brief Checkup routine
183 *
184 * \return 0 if successful, or 1 if the test failed
185 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200186int sha512_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100188/* Internal use */
Paul Bakker9e36f042013-06-30 14:34:05 +0200189void sha512_process( sha512_context *ctx, const unsigned char data[128] );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100190
Paul Bakker5121ce52009-01-03 21:22:43 +0000191#ifdef __cplusplus
192}
193#endif
194
Paul Bakkerd2681d82013-06-30 14:49:12 +0200195#endif /* sha512.h */