4 * Copyright (C) 2006-2010, Brainspark B.V.
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #ifndef POLARSSL_SHA2_H
26 #define POLARSSL_SHA2_H
29 * \brief SHA-256 context structure
31 #ifndef HAVE_SHA2_CONTEXT
32 #define HAVE_SHA2_CONTEXT
33 typedef struct sha2_context sha2_context;
38 unsigned long total[2]; /*!< number of bytes processed */
39 unsigned long state[8]; /*!< intermediate digest state */
40 unsigned char buffer[64]; /*!< data block being processed */
42 unsigned char ipad[64]; /*!< HMAC: inner padding */
43 unsigned char opad[64]; /*!< HMAC: outer padding */
44 int is224; /*!< 0 => SHA-256, else SHA-224 */
52 * \brief SHA-256 context setup
54 * \param ctx context to be initialized
55 * \param is224 0 = use SHA256, 1 = use SHA224
57 void sha2_starts( sha2_context *ctx, int is224 );
60 * \brief SHA-256 process buffer
62 * \param ctx SHA-256 context
63 * \param input buffer holding the data
64 * \param ilen length of the input data
66 void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen );
69 * \brief SHA-256 final digest
71 * \param ctx SHA-256 context
72 * \param output SHA-224/256 checksum result
74 void sha2_finish( sha2_context *ctx, unsigned char output[32] );
77 * \brief Output = SHA-256( input buffer )
79 * \param input buffer holding the data
80 * \param ilen length of the input data
81 * \param output SHA-224/256 checksum result
82 * \param is224 0 = use SHA256, 1 = use SHA224
84 void sha2( const unsigned char *input, int ilen,
85 unsigned char output[32], int is224 );
88 * \brief Output = SHA-256( file contents )
90 * \param path input file name
91 * \param output SHA-224/256 checksum result
92 * \param is224 0 = use SHA256, 1 = use SHA224
94 * \return 0 if successful, 1 if fopen failed,
95 * or 2 if fread failed
97 int sha2_file( const char *path, unsigned char output[32], int is224 );
100 * \brief SHA-256 HMAC context setup
102 * \param ctx HMAC context to be initialized
103 * \param key HMAC secret key
104 * \param keylen length of the HMAC key
105 * \param is224 0 = use SHA256, 1 = use SHA224
107 void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, int keylen,
111 * \brief SHA-256 HMAC process buffer
113 * \param ctx HMAC context
114 * \param input buffer holding the data
115 * \param ilen length of the input data
117 void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, int ilen );
120 * \brief SHA-256 HMAC final digest
122 * \param ctx HMAC context
123 * \param output SHA-224/256 HMAC checksum result
125 void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
128 * \brief SHA-256 HMAC context reset
130 * \param ctx HMAC context to be reset
132 void sha2_hmac_reset( sha2_context *ctx );
135 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
137 * \param key HMAC secret key
138 * \param keylen length of the HMAC key
139 * \param input buffer holding the data
140 * \param ilen length of the input data
141 * \param output HMAC-SHA-224/256 result
142 * \param is224 0 = use SHA256, 1 = use SHA224
144 void sha2_hmac( const unsigned char *key, int keylen,
145 const unsigned char *input, int ilen,
146 unsigned char output[32], int is224 );
149 * \brief Checkup routine
151 * \return 0 if successful, or 1 if the test failed
153 int sha2_self_test( int verbose );