blob: f3c00fa20dda523467c121dd82f123104a45139a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
Paul Bakkerd2681d82013-06-30 14:49:12 +02002 * \file sha256.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +00004 * \brief SHA-224 and SHA-256 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_SHA256_H
28#define POLARSSL_SHA256_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 Bakkerfa6a6202013-10-28 18:48:30 +010038#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000039#include <basetsd.h>
40typedef UINT32 uint32_t;
41#else
42#include <inttypes.h>
43#endif
44
Paul Bakker9e36f042013-06-30 14:34:05 +020045#define POLARSSL_ERR_SHA256_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */
Paul Bakker69e095c2011-12-10 21:55:01 +000046
Paul Bakker9e36f042013-06-30 14:34:05 +020047#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020048// Regular implementation
49//
50
Paul Bakker407a0da2013-06-27 14:29:21 +020051#ifdef __cplusplus
52extern "C" {
53#endif
54
Paul Bakker5121ce52009-01-03 21:22:43 +000055/**
56 * \brief SHA-256 context structure
57 */
58typedef struct
59{
Paul Bakker5c2364c2012-10-01 14:41:15 +000060 uint32_t total[2]; /*!< number of bytes processed */
61 uint32_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000062 unsigned char buffer[64]; /*!< data block being processed */
63
64 unsigned char ipad[64]; /*!< HMAC: inner padding */
65 unsigned char opad[64]; /*!< HMAC: outer padding */
66 int is224; /*!< 0 => SHA-256, else SHA-224 */
67}
Paul Bakker9e36f042013-06-30 14:34:05 +020068sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Paul Bakker5121ce52009-01-03 21:22:43 +000070/**
71 * \brief SHA-256 context setup
72 *
73 * \param ctx context to be initialized
74 * \param is224 0 = use SHA256, 1 = use SHA224
75 */
Paul Bakker9e36f042013-06-30 14:34:05 +020076void sha256_starts( sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000077
78/**
79 * \brief SHA-256 process buffer
80 *
81 * \param ctx SHA-256 context
82 * \param input buffer holding the data
83 * \param ilen length of the input data
84 */
Paul Bakker9e36f042013-06-30 14:34:05 +020085void sha256_update( sha256_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000086
87/**
88 * \brief SHA-256 final digest
89 *
90 * \param ctx SHA-256 context
91 * \param output SHA-224/256 checksum result
92 */
Paul Bakker9e36f042013-06-30 14:34:05 +020093void sha256_finish( sha256_context *ctx, unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Paul Bakker90995b52013-06-24 19:20:35 +020095/* Internal use */
Paul Bakker9e36f042013-06-30 14:34:05 +020096void sha256_process( sha256_context *ctx, const unsigned char data[64] );
Paul Bakker90995b52013-06-24 19:20:35 +020097
98#ifdef __cplusplus
99}
100#endif
101
Paul Bakker9e36f042013-06-30 14:34:05 +0200102#else /* POLARSSL_SHA256_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200103#include "sha256_alt.h"
Paul Bakker9e36f042013-06-30 14:34:05 +0200104#endif /* POLARSSL_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200105
106#ifdef __cplusplus
107extern "C" {
108#endif
109
Paul Bakker5121ce52009-01-03 21:22:43 +0000110/**
111 * \brief Output = SHA-256( input buffer )
112 *
113 * \param input buffer holding the data
114 * \param ilen length of the input data
115 * \param output SHA-224/256 checksum result
116 * \param is224 0 = use SHA256, 1 = use SHA224
117 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200118void sha256( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 unsigned char output[32], int is224 );
120
121/**
122 * \brief Output = SHA-256( file contents )
123 *
124 * \param path input file name
125 * \param output SHA-224/256 checksum result
126 * \param is224 0 = use SHA256, 1 = use SHA224
127 *
Paul Bakker9e36f042013-06-30 14:34:05 +0200128 * \return 0 if successful, or POLARSSL_ERR_SHA256_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200130int sha256_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
132/**
133 * \brief SHA-256 HMAC context setup
134 *
135 * \param ctx HMAC context to be initialized
136 * \param key HMAC secret key
137 * \param keylen length of the HMAC key
138 * \param is224 0 = use SHA256, 1 = use SHA224
139 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200140void sha256_hmac_starts( sha256_context *ctx, const unsigned char *key,
141 size_t keylen, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143/**
144 * \brief SHA-256 HMAC process buffer
145 *
146 * \param ctx HMAC context
147 * \param input buffer holding the data
148 * \param ilen length of the input data
149 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200150void sha256_hmac_update( sha256_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152/**
153 * \brief SHA-256 HMAC final digest
154 *
155 * \param ctx HMAC context
156 * \param output SHA-224/256 HMAC checksum result
157 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200158void sha256_hmac_finish( sha256_context *ctx, unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
160/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000161 * \brief SHA-256 HMAC context reset
162 *
163 * \param ctx HMAC context to be reset
164 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200165void sha256_hmac_reset( sha256_context *ctx );
Paul Bakker7d3b6612010-03-21 16:23:13 +0000166
167/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 * \brief Output = HMAC-SHA-256( 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-224/256 result
175 * \param is224 0 = use SHA256, 1 = use SHA224
176 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200177void sha256_hmac( const unsigned char *key, size_t keylen,
178 const unsigned char *input, size_t ilen,
179 unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
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 sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Paul Bakker5121ce52009-01-03 21:22:43 +0000188#ifdef __cplusplus
189}
190#endif
191
Paul Bakkerd2681d82013-06-30 14:49:12 +0200192#endif /* sha256.h */