blob: 195996dbb83975dee453caffae1a2508ca1211e6 [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é-Gonnard860b5162015-01-28 17:12:07 +00008 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Paul Bakkerd2681d82013-06-30 14:49:12 +020024#ifndef POLARSSL_SHA256_H
25#define POLARSSL_SHA256_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
30#include POLARSSL_CONFIG_FILE
31#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Paul Bakker23986e52011-04-24 08:57:21 +000033#include <string.h>
34
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000036#include <basetsd.h>
37typedef UINT32 uint32_t;
38#else
39#include <inttypes.h>
40#endif
41
Paul Bakker9e36f042013-06-30 14:34:05 +020042#define POLARSSL_ERR_SHA256_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */
Paul Bakker69e095c2011-12-10 21:55:01 +000043
Paul Bakker9e36f042013-06-30 14:34:05 +020044#if !defined(POLARSSL_SHA256_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-256 context structure
54 */
55typedef struct
56{
Paul Bakker5c2364c2012-10-01 14:41:15 +000057 uint32_t total[2]; /*!< number of bytes processed */
58 uint32_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000059 unsigned char buffer[64]; /*!< data block being processed */
60
61 unsigned char ipad[64]; /*!< HMAC: inner padding */
62 unsigned char opad[64]; /*!< HMAC: outer padding */
63 int is224; /*!< 0 => SHA-256, else SHA-224 */
64}
Paul Bakker9e36f042013-06-30 14:34:05 +020065sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Paul Bakker5121ce52009-01-03 21:22:43 +000067/**
Paul Bakker5b4af392014-06-26 12:09:34 +020068 * \brief Initialize SHA-256 context
69 *
70 * \param ctx SHA-256 context to be initialized
71 */
72void sha256_init( sha256_context *ctx );
73
74/**
75 * \brief Clear SHA-256 context
76 *
77 * \param ctx SHA-256 context to be cleared
78 */
79void sha256_free( sha256_context *ctx );
80
81/**
Paul Bakker5121ce52009-01-03 21:22:43 +000082 * \brief SHA-256 context setup
83 *
84 * \param ctx context to be initialized
85 * \param is224 0 = use SHA256, 1 = use SHA224
86 */
Paul Bakker9e36f042013-06-30 14:34:05 +020087void sha256_starts( sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000088
89/**
90 * \brief SHA-256 process buffer
91 *
92 * \param ctx SHA-256 context
93 * \param input buffer holding the data
94 * \param ilen length of the input data
95 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020096void sha256_update( sha256_context *ctx, const unsigned char *input,
97 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99/**
100 * \brief SHA-256 final digest
101 *
102 * \param ctx SHA-256 context
103 * \param output SHA-224/256 checksum result
104 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200105void sha256_finish( sha256_context *ctx, unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
Paul Bakker90995b52013-06-24 19:20:35 +0200107/* Internal use */
Paul Bakker9e36f042013-06-30 14:34:05 +0200108void sha256_process( sha256_context *ctx, const unsigned char data[64] );
Paul Bakker90995b52013-06-24 19:20:35 +0200109
110#ifdef __cplusplus
111}
112#endif
113
Paul Bakker9e36f042013-06-30 14:34:05 +0200114#else /* POLARSSL_SHA256_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200115#include "sha256_alt.h"
Paul Bakker9e36f042013-06-30 14:34:05 +0200116#endif /* POLARSSL_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200117
118#ifdef __cplusplus
119extern "C" {
120#endif
121
Paul Bakker5121ce52009-01-03 21:22:43 +0000122/**
123 * \brief Output = SHA-256( input buffer )
124 *
125 * \param input buffer holding the data
126 * \param ilen length of the input data
127 * \param output SHA-224/256 checksum result
128 * \param is224 0 = use SHA256, 1 = use SHA224
129 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200130void sha256( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 unsigned char output[32], int is224 );
132
133/**
134 * \brief Output = SHA-256( file contents )
135 *
136 * \param path input file name
137 * \param output SHA-224/256 checksum result
138 * \param is224 0 = use SHA256, 1 = use SHA224
139 *
Paul Bakker9e36f042013-06-30 14:34:05 +0200140 * \return 0 if successful, or POLARSSL_ERR_SHA256_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200142int sha256_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144/**
145 * \brief SHA-256 HMAC context setup
146 *
147 * \param ctx HMAC context to be initialized
148 * \param key HMAC secret key
149 * \param keylen length of the HMAC key
150 * \param is224 0 = use SHA256, 1 = use SHA224
151 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200152void sha256_hmac_starts( sha256_context *ctx, const unsigned char *key,
153 size_t keylen, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
155/**
156 * \brief SHA-256 HMAC process buffer
157 *
158 * \param ctx HMAC context
159 * \param input buffer holding the data
160 * \param ilen length of the input data
161 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200162void sha256_hmac_update( sha256_context *ctx, const unsigned char *input,
163 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
165/**
166 * \brief SHA-256 HMAC final digest
167 *
168 * \param ctx HMAC context
169 * \param output SHA-224/256 HMAC checksum result
170 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200171void sha256_hmac_finish( sha256_context *ctx, unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
173/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000174 * \brief SHA-256 HMAC context reset
175 *
176 * \param ctx HMAC context to be reset
177 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200178void sha256_hmac_reset( sha256_context *ctx );
Paul Bakker7d3b6612010-03-21 16:23:13 +0000179
180/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
182 *
183 * \param key HMAC secret key
184 * \param keylen length of the HMAC key
185 * \param input buffer holding the data
186 * \param ilen length of the input data
187 * \param output HMAC-SHA-224/256 result
188 * \param is224 0 = use SHA256, 1 = use SHA224
189 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200190void sha256_hmac( const unsigned char *key, size_t keylen,
191 const unsigned char *input, size_t ilen,
192 unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
194/**
195 * \brief Checkup routine
196 *
197 * \return 0 if successful, or 1 if the test failed
198 */
Paul Bakker9e36f042013-06-30 14:34:05 +0200199int sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
Paul Bakker5121ce52009-01-03 21:22:43 +0000201#ifdef __cplusplus
202}
203#endif
204
Paul Bakkerd2681d82013-06-30 14:49:12 +0200205#endif /* sha256.h */