blob: 2c61637ee7d1997a9b60efdf3e80a447117ee8cf [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
Paul Bakker90995b52013-06-24 19:20:35 +020030#include "config.h"
31
Paul Bakker23986e52011-04-24 08:57:21 +000032#include <string.h>
33
Paul Bakker5121ce52009-01-03 21:22:43 +000034#if defined(_MSC_VER) || defined(__WATCOMC__)
35 #define UL64(x) x##ui64
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 #define UL64(x) x##ULL
Paul Bakker5121ce52009-01-03 21:22:43 +000040#endif
41
Paul Bakker9e36f042013-06-30 14:34:05 +020042#define POLARSSL_ERR_SHA512_FILE_IO_ERROR -0x007A /**< Read/write error in file. */
Paul Bakker5c2364c2012-10-01 14:41:15 +000043
Paul Bakker9e36f042013-06-30 14:34:05 +020044#if !defined(POLARSSL_SHA512_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020045// Regular implementation
46//
47
Paul Bakker407a0da2013-06-27 14:29:21 +020048#ifdef __cplusplus
49extern "C" {
50#endif
51
Paul Bakker5121ce52009-01-03 21:22:43 +000052/**
53 * \brief SHA-512 context structure
54 */
55typedef struct
56{
Paul Bakker5c2364c2012-10-01 14:41:15 +000057 uint64_t total[2]; /*!< number of bytes processed */
58 uint64_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000059 unsigned char buffer[128]; /*!< data block being processed */
60
61 unsigned char ipad[128]; /*!< HMAC: inner padding */
62 unsigned char opad[128]; /*!< HMAC: outer padding */
63 int is384; /*!< 0 => SHA-512, else SHA-384 */
64}
Paul Bakker9e36f042013-06-30 14:34:05 +020065sha512_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Paul Bakker5121ce52009-01-03 21:22:43 +000067/**
68 * \brief SHA-512 context setup
69 *
70 * \param ctx context to be initialized
71 * \param is384 0 = use SHA512, 1 = use SHA384
72 */
Paul Bakker9e36f042013-06-30 14:34:05 +020073void sha512_starts( sha512_context *ctx, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +000074
75/**
76 * \brief SHA-512 process buffer
77 *
78 * \param ctx SHA-512 context
79 * \param input buffer holding the data
80 * \param ilen length of the input data
81 */
Paul Bakker9e36f042013-06-30 14:34:05 +020082void sha512_update( sha512_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000083
84/**
85 * \brief SHA-512 final digest
86 *
87 * \param ctx SHA-512 context
88 * \param output SHA-384/512 checksum result
89 */
Paul Bakker9e36f042013-06-30 14:34:05 +020090void sha512_finish( sha512_context *ctx, unsigned char output[64] );
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Paul Bakker90995b52013-06-24 19:20:35 +020092#ifdef __cplusplus
93}
94#endif
95
Paul Bakker9e36f042013-06-30 14:34:05 +020096#else /* POLARSSL_SHA512_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +020097#include "sha512_alt.h"
Paul Bakker9e36f042013-06-30 14:34:05 +020098#endif /* POLARSSL_SHA512_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +020099
100#ifdef __cplusplus
101extern "C" {
102#endif
103
Paul Bakker5121ce52009-01-03 21:22:43 +0000104/**
105 * \brief Output = SHA-512( input buffer )
106 *
107 * \param input buffer holding the data
108 * \param ilen length of the input data
109 * \param output SHA-384/512 checksum result
110 * \param is384 0 = use SHA512, 1 = use SHA384
111 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200112void sha512( const unsigned char *input, size_t ilen,
113 unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115/**
116 * \brief Output = SHA-512( file contents )
117 *
118 * \param path input file name
119 * \param output SHA-384/512 checksum result
120 * \param is384 0 = use SHA512, 1 = use SHA384
121 *
Paul Bakker9e36f042013-06-30 14:34:05 +0200122 * \return 0 if successful, or POLARSSL_ERR_SHA512_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200124int sha512_file( const char *path, unsigned char output[64], int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
126/**
127 * \brief SHA-512 HMAC context setup
128 *
129 * \param ctx HMAC context to be initialized
130 * \param is384 0 = use SHA512, 1 = use SHA384
131 * \param key HMAC secret key
132 * \param keylen length of the HMAC key
133 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200134void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key,
135 size_t keylen, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
137/**
138 * \brief SHA-512 HMAC process buffer
139 *
140 * \param ctx HMAC context
141 * \param input buffer holding the data
142 * \param ilen length of the input data
143 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200144void sha512_hmac_update( sha512_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146/**
147 * \brief SHA-512 HMAC final digest
148 *
149 * \param ctx HMAC context
150 * \param output SHA-384/512 HMAC checksum result
151 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200152void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
154/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000155 * \brief SHA-512 HMAC context reset
156 *
157 * \param ctx HMAC context to be reset
158 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200159void sha512_hmac_reset( sha512_context *ctx );
Paul Bakker7d3b6612010-03-21 16:23:13 +0000160
161/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 * \brief Output = HMAC-SHA-512( hmac key, input buffer )
163 *
164 * \param key HMAC secret key
165 * \param keylen length of the HMAC key
166 * \param input buffer holding the data
167 * \param ilen length of the input data
168 * \param output HMAC-SHA-384/512 result
169 * \param is384 0 = use SHA512, 1 = use SHA384
170 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200171void sha512_hmac( const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000172 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 unsigned char output[64], int is384 );
174
175/**
176 * \brief Checkup routine
177 *
178 * \return 0 if successful, or 1 if the test failed
179 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200180int sha512_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100182/* Internal use */
Paul Bakker9e36f042013-06-30 14:34:05 +0200183void sha512_process( sha512_context *ctx, const unsigned char data[128] );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100184
Paul Bakker5121ce52009-01-03 21:22:43 +0000185#ifdef __cplusplus
186}
187#endif
188
Paul Bakkerd2681d82013-06-30 14:49:12 +0200189#endif /* sha512.h */