blob: 054988d6b9c736e197fd1991a47782f861f90b9e [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file sha2.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 Bakker84f12b72010-07-18 10:13:04 +00006 * Copyright (C) 2006-2010, 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 Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_SHA2_H
28#define POLARSSL_SHA2_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Paul Bakker23986e52011-04-24 08:57:21 +000030#include <string.h>
31
Paul Bakker5c2364c2012-10-01 14:41:15 +000032#ifdef _MSC_VER
33#include <basetsd.h>
34typedef UINT32 uint32_t;
35#else
36#include <inttypes.h>
37#endif
38
Paul Bakker69e095c2011-12-10 21:55:01 +000039#define POLARSSL_ERR_SHA2_FILE_IO_ERROR -0x0078 /**< Read/write error in file. */
40
Paul Bakker5121ce52009-01-03 21:22:43 +000041/**
42 * \brief SHA-256 context structure
43 */
44typedef struct
45{
Paul Bakker5c2364c2012-10-01 14:41:15 +000046 uint32_t total[2]; /*!< number of bytes processed */
47 uint32_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000048 unsigned char buffer[64]; /*!< data block being processed */
49
50 unsigned char ipad[64]; /*!< HMAC: inner padding */
51 unsigned char opad[64]; /*!< HMAC: outer padding */
52 int is224; /*!< 0 => SHA-256, else SHA-224 */
53}
54sha2_context;
55
56#ifdef __cplusplus
57extern "C" {
58#endif
59
60/**
61 * \brief SHA-256 context setup
62 *
63 * \param ctx context to be initialized
64 * \param is224 0 = use SHA256, 1 = use SHA224
65 */
66void sha2_starts( sha2_context *ctx, int is224 );
67
68/**
69 * \brief SHA-256 process buffer
70 *
71 * \param ctx SHA-256 context
72 * \param input buffer holding the data
73 * \param ilen length of the input data
74 */
Paul Bakker23986e52011-04-24 08:57:21 +000075void sha2_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000076
77/**
78 * \brief SHA-256 final digest
79 *
80 * \param ctx SHA-256 context
81 * \param output SHA-224/256 checksum result
82 */
83void sha2_finish( sha2_context *ctx, unsigned char output[32] );
84
85/**
86 * \brief Output = SHA-256( input buffer )
87 *
88 * \param input buffer holding the data
89 * \param ilen length of the input data
90 * \param output SHA-224/256 checksum result
91 * \param is224 0 = use SHA256, 1 = use SHA224
92 */
Paul Bakker23986e52011-04-24 08:57:21 +000093void sha2( const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +000094 unsigned char output[32], int is224 );
95
96/**
97 * \brief Output = SHA-256( file contents )
98 *
99 * \param path input file name
100 * \param output SHA-224/256 checksum result
101 * \param is224 0 = use SHA256, 1 = use SHA224
102 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000103 * \return 0 if successful, or POLARSSL_ERR_SHA2_FILE_IO_ERROR
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000105int sha2_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107/**
108 * \brief SHA-256 HMAC context setup
109 *
110 * \param ctx HMAC context to be initialized
111 * \param key HMAC secret key
112 * \param keylen length of the HMAC key
113 * \param is224 0 = use SHA256, 1 = use SHA224
114 */
Paul Bakker23986e52011-04-24 08:57:21 +0000115void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, size_t keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 int is224 );
117
118/**
119 * \brief SHA-256 HMAC process buffer
120 *
121 * \param ctx HMAC context
122 * \param input buffer holding the data
123 * \param ilen length of the input data
124 */
Paul Bakker23986e52011-04-24 08:57:21 +0000125void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127/**
128 * \brief SHA-256 HMAC final digest
129 *
130 * \param ctx HMAC context
131 * \param output SHA-224/256 HMAC checksum result
132 */
133void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
134
135/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000136 * \brief SHA-256 HMAC context reset
137 *
138 * \param ctx HMAC context to be reset
139 */
140void sha2_hmac_reset( sha2_context *ctx );
141
142/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
144 *
145 * \param key HMAC secret key
146 * \param keylen length of the HMAC key
147 * \param input buffer holding the data
148 * \param ilen length of the input data
149 * \param output HMAC-SHA-224/256 result
150 * \param is224 0 = use SHA256, 1 = use SHA224
151 */
Paul Bakker23986e52011-04-24 08:57:21 +0000152void sha2_hmac( const unsigned char *key, size_t keylen,
153 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 unsigned char output[32], int is224 );
155
156/**
157 * \brief Checkup routine
158 *
159 * \return 0 if successful, or 1 if the test failed
160 */
161int sha2_self_test( int verbose );
162
163#ifdef __cplusplus
164}
165#endif
166
167#endif /* sha2.h */