blob: 8f1aa1949387dd7aa6440e2ec87e916eb544d575 [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 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00008 * This file is part of mbed TLS (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/**
Paul Bakker5b4af392014-06-26 12:09:34 +020071 * \brief Initialize SHA-256 context
72 *
73 * \param ctx SHA-256 context to be initialized
74 */
75void sha256_init( sha256_context *ctx );
76
77/**
78 * \brief Clear SHA-256 context
79 *
80 * \param ctx SHA-256 context to be cleared
81 */
82void sha256_free( sha256_context *ctx );
83
84/**
Paul Bakker5121ce52009-01-03 21:22:43 +000085 * \brief SHA-256 context setup
86 *
87 * \param ctx context to be initialized
88 * \param is224 0 = use SHA256, 1 = use SHA224
89 */
Paul Bakker9e36f042013-06-30 14:34:05 +020090void sha256_starts( sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92/**
93 * \brief SHA-256 process buffer
94 *
95 * \param ctx SHA-256 context
96 * \param input buffer holding the data
97 * \param ilen length of the input data
98 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020099void sha256_update( sha256_context *ctx, const unsigned char *input,
100 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102/**
103 * \brief SHA-256 final digest
104 *
105 * \param ctx SHA-256 context
106 * \param output SHA-224/256 checksum result
107 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200108void sha256_finish( sha256_context *ctx, unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
Paul Bakker90995b52013-06-24 19:20:35 +0200110/* Internal use */
Paul Bakker9e36f042013-06-30 14:34:05 +0200111void sha256_process( sha256_context *ctx, const unsigned char data[64] );
Paul Bakker90995b52013-06-24 19:20:35 +0200112
113#ifdef __cplusplus
114}
115#endif
116
Paul Bakker9e36f042013-06-30 14:34:05 +0200117#else /* POLARSSL_SHA256_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200118#include "sha256_alt.h"
Paul Bakker9e36f042013-06-30 14:34:05 +0200119#endif /* POLARSSL_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200120
121#ifdef __cplusplus
122extern "C" {
123#endif
124
Paul Bakker5121ce52009-01-03 21:22:43 +0000125/**
126 * \brief Output = SHA-256( input buffer )
127 *
128 * \param input buffer holding the data
129 * \param ilen length of the input data
130 * \param output SHA-224/256 checksum result
131 * \param is224 0 = use SHA256, 1 = use SHA224
132 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200133void sha256( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 unsigned char output[32], int is224 );
135
136/**
137 * \brief Output = SHA-256( file contents )
138 *
139 * \param path input file name
140 * \param output SHA-224/256 checksum result
141 * \param is224 0 = use SHA256, 1 = use SHA224
142 *
Paul Bakker9e36f042013-06-30 14:34:05 +0200143 * \return 0 if successful, or POLARSSL_ERR_SHA256_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200145int sha256_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147/**
148 * \brief SHA-256 HMAC context setup
149 *
150 * \param ctx HMAC context to be initialized
151 * \param key HMAC secret key
152 * \param keylen length of the HMAC key
153 * \param is224 0 = use SHA256, 1 = use SHA224
154 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200155void sha256_hmac_starts( sha256_context *ctx, const unsigned char *key,
156 size_t keylen, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
158/**
159 * \brief SHA-256 HMAC process buffer
160 *
161 * \param ctx HMAC context
162 * \param input buffer holding the data
163 * \param ilen length of the input data
164 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200165void sha256_hmac_update( sha256_context *ctx, const unsigned char *input,
166 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168/**
169 * \brief SHA-256 HMAC final digest
170 *
171 * \param ctx HMAC context
172 * \param output SHA-224/256 HMAC checksum result
173 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200174void sha256_hmac_finish( sha256_context *ctx, unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
176/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000177 * \brief SHA-256 HMAC context reset
178 *
179 * \param ctx HMAC context to be reset
180 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200181void sha256_hmac_reset( sha256_context *ctx );
Paul Bakker7d3b6612010-03-21 16:23:13 +0000182
183/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
185 *
186 * \param key HMAC secret key
187 * \param keylen length of the HMAC key
188 * \param input buffer holding the data
189 * \param ilen length of the input data
190 * \param output HMAC-SHA-224/256 result
191 * \param is224 0 = use SHA256, 1 = use SHA224
192 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200193void sha256_hmac( const unsigned char *key, size_t keylen,
194 const unsigned char *input, size_t ilen,
195 unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197/**
198 * \brief Checkup routine
199 *
200 * \return 0 if successful, or 1 if the test failed
201 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200202int sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
Paul Bakker5121ce52009-01-03 21:22:43 +0000204#ifdef __cplusplus
205}
206#endif
207
Paul Bakkerd2681d82013-06-30 14:49:12 +0200208#endif /* sha256.h */