blob: 7d964b810c4c2b5048441a1961d637ae835323fa [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
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 Bakker5c2364c2012-10-01 14:41:15 +000034#ifdef _MSC_VER
35#include <basetsd.h>
36typedef UINT32 uint32_t;
37#else
38#include <inttypes.h>
39#endif
40
Paul Bakker9e36f042013-06-30 14:34:05 +020041#define POLARSSL_ERR_SHA256_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */
Paul Bakker69e095c2011-12-10 21:55:01 +000042
Paul Bakker9e36f042013-06-30 14:34:05 +020043#if !defined(POLARSSL_SHA256_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020044// Regular implementation
45//
46
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/**
52 * \brief SHA-256 context structure
53 */
54typedef struct
55{
Paul Bakker5c2364c2012-10-01 14:41:15 +000056 uint32_t total[2]; /*!< number of bytes processed */
57 uint32_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000058 unsigned char buffer[64]; /*!< data block being processed */
59
60 unsigned char ipad[64]; /*!< HMAC: inner padding */
61 unsigned char opad[64]; /*!< HMAC: outer padding */
62 int is224; /*!< 0 => SHA-256, else SHA-224 */
63}
Paul Bakker9e36f042013-06-30 14:34:05 +020064sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Paul Bakker5121ce52009-01-03 21:22:43 +000066/**
67 * \brief SHA-256 context setup
68 *
69 * \param ctx context to be initialized
70 * \param is224 0 = use SHA256, 1 = use SHA224
71 */
Paul Bakker9e36f042013-06-30 14:34:05 +020072void sha256_starts( sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000073
74/**
75 * \brief SHA-256 process buffer
76 *
77 * \param ctx SHA-256 context
78 * \param input buffer holding the data
79 * \param ilen length of the input data
80 */
Paul Bakker9e36f042013-06-30 14:34:05 +020081void sha256_update( sha256_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83/**
84 * \brief SHA-256 final digest
85 *
86 * \param ctx SHA-256 context
87 * \param output SHA-224/256 checksum result
88 */
Paul Bakker9e36f042013-06-30 14:34:05 +020089void sha256_finish( sha256_context *ctx, unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Paul Bakker90995b52013-06-24 19:20:35 +020091/* Internal use */
Paul Bakker9e36f042013-06-30 14:34:05 +020092void sha256_process( sha256_context *ctx, const unsigned char data[64] );
Paul Bakker90995b52013-06-24 19:20:35 +020093
94#ifdef __cplusplus
95}
96#endif
97
Paul Bakker9e36f042013-06-30 14:34:05 +020098#else /* POLARSSL_SHA256_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +020099#include "sha256_alt.h"
Paul Bakker9e36f042013-06-30 14:34:05 +0200100#endif /* POLARSSL_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200101
102#ifdef __cplusplus
103extern "C" {
104#endif
105
Paul Bakker5121ce52009-01-03 21:22:43 +0000106/**
107 * \brief Output = SHA-256( input buffer )
108 *
109 * \param input buffer holding the data
110 * \param ilen length of the input data
111 * \param output SHA-224/256 checksum result
112 * \param is224 0 = use SHA256, 1 = use SHA224
113 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200114void sha256( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 unsigned char output[32], int is224 );
116
117/**
118 * \brief Output = SHA-256( file contents )
119 *
120 * \param path input file name
121 * \param output SHA-224/256 checksum result
122 * \param is224 0 = use SHA256, 1 = use SHA224
123 *
Paul Bakker9e36f042013-06-30 14:34:05 +0200124 * \return 0 if successful, or POLARSSL_ERR_SHA256_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200126int sha256_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
128/**
129 * \brief SHA-256 HMAC context setup
130 *
131 * \param ctx HMAC context to be initialized
132 * \param key HMAC secret key
133 * \param keylen length of the HMAC key
134 * \param is224 0 = use SHA256, 1 = use SHA224
135 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200136void sha256_hmac_starts( sha256_context *ctx, const unsigned char *key,
137 size_t keylen, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
139/**
140 * \brief SHA-256 HMAC process buffer
141 *
142 * \param ctx HMAC context
143 * \param input buffer holding the data
144 * \param ilen length of the input data
145 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200146void sha256_hmac_update( sha256_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147
148/**
149 * \brief SHA-256 HMAC final digest
150 *
151 * \param ctx HMAC context
152 * \param output SHA-224/256 HMAC checksum result
153 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200154void sha256_hmac_finish( sha256_context *ctx, unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
156/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000157 * \brief SHA-256 HMAC context reset
158 *
159 * \param ctx HMAC context to be reset
160 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200161void sha256_hmac_reset( sha256_context *ctx );
Paul Bakker7d3b6612010-03-21 16:23:13 +0000162
163/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
165 *
166 * \param key HMAC secret key
167 * \param keylen length of the HMAC key
168 * \param input buffer holding the data
169 * \param ilen length of the input data
170 * \param output HMAC-SHA-224/256 result
171 * \param is224 0 = use SHA256, 1 = use SHA224
172 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200173void sha256_hmac( const unsigned char *key, size_t keylen,
174 const unsigned char *input, size_t ilen,
175 unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
177/**
178 * \brief Checkup routine
179 *
180 * \return 0 if successful, or 1 if the test failed
181 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200182int sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Paul Bakker5121ce52009-01-03 21:22:43 +0000184#ifdef __cplusplus
185}
186#endif
187
Paul Bakkerd2681d82013-06-30 14:49:12 +0200188#endif /* sha256.h */