| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** | 
| Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 2 | * \file sha512.h | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * | 
| Paul Bakker | f3b86c1 | 2011-01-27 15:24:17 +0000 | [diff] [blame] | 4 | * \brief SHA-384 and SHA-512 cryptographic hash function | 
| Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 5 | * | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 6 | *  Copyright (C) 2006-2014, Brainspark B.V. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * | 
|  | 8 | *  This file is part of PolarSSL (http://www.polarssl.org) | 
| Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 9 | *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 10 | * | 
| Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 11 | *  All rights reserved. | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 12 | * | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 13 | *  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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | */ | 
| Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 27 | #ifndef POLARSSL_SHA512_H | 
|  | 28 | #define POLARSSL_SHA512_H | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 |  | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #if !defined(POLARSSL_CONFIG_FILE) | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 31 | #include "config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 32 | #else | 
|  | 33 | #include POLARSSL_CONFIG_FILE | 
|  | 34 | #endif | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 35 |  | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 36 | #include <string.h> | 
|  | 37 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 38 | #if defined(_MSC_VER) || defined(__WATCOMC__) | 
|  | 39 | #define UL64(x) x##ui64 | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 40 | typedef unsigned __int64 uint64_t; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 41 | #else | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 42 | #include <inttypes.h> | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | #define UL64(x) x##ULL | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 44 | #endif | 
|  | 45 |  | 
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 46 | #define POLARSSL_ERR_SHA512_FILE_IO_ERROR              -0x007A  /**< Read/write error in file. */ | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 47 |  | 
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 48 | #if !defined(POLARSSL_SHA512_ALT) | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 49 | // Regular implementation | 
|  | 50 | // | 
|  | 51 |  | 
| Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 52 | #ifdef __cplusplus | 
|  | 53 | extern "C" { | 
|  | 54 | #endif | 
|  | 55 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 56 | /** | 
|  | 57 | * \brief          SHA-512 context structure | 
|  | 58 | */ | 
|  | 59 | typedef struct | 
|  | 60 | { | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 61 | uint64_t total[2];          /*!< number of bytes processed  */ | 
|  | 62 | uint64_t state[8];          /*!< intermediate digest state  */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 63 | 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 69 | sha512_context; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 70 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | /** | 
|  | 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 77 | void sha512_starts( sha512_context *ctx, int is384 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 |  | 
|  | 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 Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 86 | void sha512_update( sha512_context *ctx, const unsigned char *input, | 
|  | 87 | size_t ilen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 88 |  | 
|  | 89 | /** | 
|  | 90 | * \brief          SHA-512 final digest | 
|  | 91 | * | 
|  | 92 | * \param ctx      SHA-512 context | 
|  | 93 | * \param output   SHA-384/512 checksum result | 
|  | 94 | */ | 
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 95 | void sha512_finish( sha512_context *ctx, unsigned char output[64] ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 |  | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 97 | #ifdef __cplusplus | 
|  | 98 | } | 
|  | 99 | #endif | 
|  | 100 |  | 
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 101 | #else  /* POLARSSL_SHA512_ALT */ | 
| Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 102 | #include "sha512_alt.h" | 
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 103 | #endif /* POLARSSL_SHA512_ALT */ | 
| Paul Bakker | 90995b5 | 2013-06-24 19:20:35 +0200 | [diff] [blame] | 104 |  | 
|  | 105 | #ifdef __cplusplus | 
|  | 106 | extern "C" { | 
|  | 107 | #endif | 
|  | 108 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 109 | /** | 
|  | 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 117 | void sha512( const unsigned char *input, size_t ilen, | 
|  | 118 | unsigned char output[64], int is384 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 119 |  | 
|  | 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 127 | * \return         0 if successful, or POLARSSL_ERR_SHA512_FILE_IO_ERROR | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | */ | 
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 129 | int sha512_file( const char *path, unsigned char output[64], int is384 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 |  | 
|  | 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 139 | void sha512_hmac_starts( sha512_context *ctx, const unsigned char *key, | 
|  | 140 | size_t keylen, int is384 ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 |  | 
|  | 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 Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 149 | void sha512_hmac_update( sha512_context *ctx, const unsigned char *input, | 
|  | 150 | size_t ilen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 151 |  | 
|  | 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 158 | void sha512_hmac_finish( sha512_context *ctx, unsigned char output[64] ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 159 |  | 
|  | 160 | /** | 
| Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 161 | * \brief          SHA-512 HMAC context reset | 
|  | 162 | * | 
|  | 163 | * \param ctx      HMAC context to be reset | 
|  | 164 | */ | 
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 165 | void sha512_hmac_reset( sha512_context *ctx ); | 
| Paul Bakker | 7d3b661 | 2010-03-21 16:23:13 +0000 | [diff] [blame] | 166 |  | 
|  | 167 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | * \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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 177 | void sha512_hmac( const unsigned char *key, size_t keylen, | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 178 | const unsigned char *input, size_t ilen, | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 179 | 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 Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 186 | int sha512_self_test( int verbose ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 187 |  | 
| Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 188 | /* Internal use */ | 
| Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 189 | void sha512_process( sha512_context *ctx, const unsigned char data[128] ); | 
| Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 190 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 191 | #ifdef __cplusplus | 
|  | 192 | } | 
|  | 193 | #endif | 
|  | 194 |  | 
| Paul Bakker | d2681d8 | 2013-06-30 14:49:12 +0200 | [diff] [blame] | 195 | #endif /* sha512.h */ |