blob: 813a96ca958a9dc8d47173dc90ecadf23d562ef5 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file sha2.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief SHA-256 cryptographic hash function
5 *
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
30/**
31 * \brief SHA-256 context structure
32 */
33typedef struct
34{
35 unsigned long total[2]; /*!< number of bytes processed */
36 unsigned long state[8]; /*!< intermediate digest state */
37 unsigned char buffer[64]; /*!< data block being processed */
38
39 unsigned char ipad[64]; /*!< HMAC: inner padding */
40 unsigned char opad[64]; /*!< HMAC: outer padding */
41 int is224; /*!< 0 => SHA-256, else SHA-224 */
42}
43sha2_context;
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49/**
50 * \brief SHA-256 context setup
51 *
52 * \param ctx context to be initialized
53 * \param is224 0 = use SHA256, 1 = use SHA224
54 */
55void sha2_starts( sha2_context *ctx, int is224 );
56
57/**
58 * \brief SHA-256 process buffer
59 *
60 * \param ctx SHA-256 context
61 * \param input buffer holding the data
62 * \param ilen length of the input data
63 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000064void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000065
66/**
67 * \brief SHA-256 final digest
68 *
69 * \param ctx SHA-256 context
70 * \param output SHA-224/256 checksum result
71 */
72void sha2_finish( sha2_context *ctx, unsigned char output[32] );
73
74/**
75 * \brief Output = SHA-256( input buffer )
76 *
77 * \param input buffer holding the data
78 * \param ilen length of the input data
79 * \param output SHA-224/256 checksum result
80 * \param is224 0 = use SHA256, 1 = use SHA224
81 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000082void sha2( const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +000083 unsigned char output[32], int is224 );
84
85/**
86 * \brief Output = SHA-256( file contents )
87 *
88 * \param path input file name
89 * \param output SHA-224/256 checksum result
90 * \param is224 0 = use SHA256, 1 = use SHA224
91 *
92 * \return 0 if successful, 1 if fopen failed,
93 * or 2 if fread failed
94 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000095int sha2_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000096
97/**
98 * \brief SHA-256 HMAC context setup
99 *
100 * \param ctx HMAC context to be initialized
101 * \param key HMAC secret key
102 * \param keylen length of the HMAC key
103 * \param is224 0 = use SHA256, 1 = use SHA224
104 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000105void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, int keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 int is224 );
107
108/**
109 * \brief SHA-256 HMAC process buffer
110 *
111 * \param ctx HMAC context
112 * \param input buffer holding the data
113 * \param ilen length of the input data
114 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000115void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
117/**
118 * \brief SHA-256 HMAC final digest
119 *
120 * \param ctx HMAC context
121 * \param output SHA-224/256 HMAC checksum result
122 */
123void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
124
125/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000126 * \brief SHA-256 HMAC context reset
127 *
128 * \param ctx HMAC context to be reset
129 */
130void sha2_hmac_reset( sha2_context *ctx );
131
132/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
134 *
135 * \param key HMAC secret key
136 * \param keylen length of the HMAC key
137 * \param input buffer holding the data
138 * \param ilen length of the input data
139 * \param output HMAC-SHA-224/256 result
140 * \param is224 0 = use SHA256, 1 = use SHA224
141 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000142void sha2_hmac( const unsigned char *key, int keylen,
143 const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 unsigned char output[32], int is224 );
145
146/**
147 * \brief Checkup routine
148 *
149 * \return 0 if successful, or 1 if the test failed
150 */
151int sha2_self_test( int verbose );
152
153#ifdef __cplusplus
154}
155#endif
156
157#endif /* sha2.h */