blob: b8908af22619f0ee8e0315d83ac2b8270298cbc6 [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 Bakker90995b52013-06-24 19:20:35 +02006 * Copyright (C) 2006-2013, 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 Bakker9e36f042013-06-30 14:34:05 +020086void sha512_update( sha512_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000087
88/**
89 * \brief SHA-512 final digest
90 *
91 * \param ctx SHA-512 context
92 * \param output SHA-384/512 checksum result
93 */
Paul Bakker9e36f042013-06-30 14:34:05 +020094void sha512_finish( sha512_context *ctx, unsigned char output[64] );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Paul Bakker90995b52013-06-24 19:20:35 +020096#ifdef __cplusplus
97}
98#endif
99
Paul Bakker9e36f042013-06-30 14:34:05 +0200100#else /* POLARSSL_SHA512_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200101#include "sha512_alt.h"
Paul Bakker9e36f042013-06-30 14:34:05 +0200102#endif /* POLARSSL_SHA512_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200103
104#ifdef __cplusplus
105extern "C" {
106#endif
107
Paul Bakker5121ce52009-01-03 21:22:43 +0000108/**
109 * \brief Output = SHA-512( input buffer )
110 *
111 * \param input buffer holding the data
112 * \param ilen length of the input data
113 * \param output SHA-384/512 checksum result
114 * \param is384 0 = use SHA512, 1 = use SHA384
115 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200116void sha512( const unsigned char *input, size_t ilen,
117 unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119/**
120 * \brief Output = SHA-512( file contents )
121 *
122 * \param path input file name
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 +0200126 * \return 0 if successful, or POLARSSL_ERR_SHA512_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200128int sha512_file( const char *path, unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
130/**
131 * \brief SHA-512 HMAC context setup
132 *
133 * \param ctx HMAC context to be initialized
134 * \param is384 0 = use SHA512, 1 = use SHA384
135 * \param key HMAC secret key
136 * \param keylen length of the HMAC key
137 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200138void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key,
139 size_t keylen, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141/**
142 * \brief SHA-512 HMAC process buffer
143 *
144 * \param ctx HMAC context
145 * \param input buffer holding the data
146 * \param ilen length of the input data
147 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200148void sha512_hmac_update( sha512_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
150/**
151 * \brief SHA-512 HMAC final digest
152 *
153 * \param ctx HMAC context
154 * \param output SHA-384/512 HMAC checksum result
155 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200156void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000159 * \brief SHA-512 HMAC context reset
160 *
161 * \param ctx HMAC context to be reset
162 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200163void sha512_hmac_reset( sha512_context *ctx );
Paul Bakker7d3b6612010-03-21 16:23:13 +0000164
165/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
167 *
168 * \param key HMAC secret key
169 * \param keylen length of the HMAC key
170 * \param input buffer holding the data
171 * \param ilen length of the input data
172 * \param output HMAC-SHA-384/512 result
173 * \param is384 0 = use SHA512, 1 = use SHA384
174 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200175void sha512_hmac( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000176 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 unsigned char output[64], int is384 );
178
179/**
180 * \brief Checkup routine
181 *
182 * \return 0 if successful, or 1 if the test failed
183 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200184int sha512_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100186/* Internal use */
Paul Bakker9e36f042013-06-30 14:34:05 +0200187void sha512_process( sha512_context *ctx, const unsigned char data[128] );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100188
Paul Bakker5121ce52009-01-03 21:22:43 +0000189#ifdef __cplusplus
190}
191#endif
192
Paul Bakkerd2681d82013-06-30 14:49:12 +0200193#endif /* sha512.h */