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.
26 /* $Cambridge: exim/src/src/pdkim/sha2.h,v 1.3 2009/12/07 13:05:07 tom Exp $ */
28 #ifndef POLARSSL_SHA2_H
29 #define POLARSSL_SHA2_H
32 * \brief SHA-256 context structure
34 #ifndef HAVE_SHA2_CONTEXT
35 #define HAVE_SHA2_CONTEXT
36 typedef struct sha2_context sha2_context;
41 unsigned long total[2]; /*!< number of bytes processed */
42 unsigned long state[8]; /*!< intermediate digest state */
43 unsigned char buffer[64]; /*!< data block being processed */
45 unsigned char ipad[64]; /*!< HMAC: inner padding */
46 unsigned char opad[64]; /*!< HMAC: outer padding */
47 int is224; /*!< 0 => SHA-256, else SHA-224 */
55 * \brief SHA-256 context setup
57 * \param ctx context to be initialized
58 * \param is224 0 = use SHA256, 1 = use SHA224
60 void sha2_starts( sha2_context *ctx, int is224 );
63 * \brief SHA-256 process buffer
65 * \param ctx SHA-256 context
66 * \param input buffer holding the data
67 * \param ilen length of the input data
69 void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen );
72 * \brief SHA-256 final digest
74 * \param ctx SHA-256 context
75 * \param output SHA-224/256 checksum result
77 void sha2_finish( sha2_context *ctx, unsigned char output[32] );
80 * \brief Output = SHA-256( input buffer )
82 * \param input buffer holding the data
83 * \param ilen length of the input data
84 * \param output SHA-224/256 checksum result
85 * \param is224 0 = use SHA256, 1 = use SHA224
87 void sha2( const unsigned char *input, int ilen,
88 unsigned char output[32], int is224 );
91 * \brief Output = SHA-256( file contents )
93 * \param path input file name
94 * \param output SHA-224/256 checksum result
95 * \param is224 0 = use SHA256, 1 = use SHA224
97 * \return 0 if successful, 1 if fopen failed,
98 * or 2 if fread failed
100 int sha2_file( const char *path, unsigned char output[32], int is224 );
103 * \brief SHA-256 HMAC context setup
105 * \param ctx HMAC context to be initialized
106 * \param key HMAC secret key
107 * \param keylen length of the HMAC key
108 * \param is224 0 = use SHA256, 1 = use SHA224
110 void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, int keylen,
114 * \brief SHA-256 HMAC process buffer
116 * \param ctx HMAC context
117 * \param input buffer holding the data
118 * \param ilen length of the input data
120 void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, int ilen );
123 * \brief SHA-256 HMAC final digest
125 * \param ctx HMAC context
126 * \param output SHA-224/256 HMAC checksum result
128 void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
131 * \brief SHA-256 HMAC context reset
133 * \param ctx HMAC context to be reset
135 void sha2_hmac_reset( sha2_context *ctx );
138 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
140 * \param key HMAC secret key
141 * \param keylen length of the HMAC key
142 * \param input buffer holding the data
143 * \param ilen length of the input data
144 * \param output HMAC-SHA-224/256 result
145 * \param is224 0 = use SHA256, 1 = use SHA224
147 void sha2_hmac( const unsigned char *key, int keylen,
148 const unsigned char *input, int ilen,
149 unsigned char output[32], int is224 );