1 /* $Cambridge: exim/src/src/pdkim/sha2.h,v 1.1.2.1 2009/02/24 13:13:47 tom Exp $ */
5 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
7 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef POLARSSL_SHA2_H
24 #define POLARSSL_SHA2_H
27 * \brief SHA-256 context structure
31 unsigned long total[2]; /*!< number of bytes processed */
32 unsigned long state[8]; /*!< intermediate digest state */
33 unsigned char buffer[64]; /*!< data block being processed */
35 unsigned char ipad[64]; /*!< HMAC: inner padding */
36 unsigned char opad[64]; /*!< HMAC: outer padding */
37 int is224; /*!< 0 => SHA-256, else SHA-224 */
46 * \brief SHA-256 context setup
48 * \param ctx context to be initialized
49 * \param is224 0 = use SHA256, 1 = use SHA224
51 void sha2_starts( sha2_context *ctx, int is224 );
54 * \brief SHA-256 process buffer
56 * \param ctx SHA-256 context
57 * \param input buffer holding the data
58 * \param ilen length of the input data
60 void sha2_update( sha2_context *ctx, unsigned char *input, int ilen );
63 * \brief SHA-256 final digest
65 * \param ctx SHA-256 context
66 * \param output SHA-224/256 checksum result
68 void sha2_finish( sha2_context *ctx, unsigned char output[32] );
71 * \brief Output = SHA-256( input buffer )
73 * \param input buffer holding the data
74 * \param ilen length of the input data
75 * \param output SHA-224/256 checksum result
76 * \param is224 0 = use SHA256, 1 = use SHA224
78 void sha2( unsigned char *input, int ilen,
79 unsigned char output[32], int is224 );
82 * \brief Output = SHA-256( file contents )
84 * \param path input file name
85 * \param output SHA-224/256 checksum result
86 * \param is224 0 = use SHA256, 1 = use SHA224
88 * \return 0 if successful, 1 if fopen failed,
89 * or 2 if fread failed
91 int sha2_file( char *path, unsigned char output[32], int is224 );
94 * \brief SHA-256 HMAC context setup
96 * \param ctx HMAC context to be initialized
97 * \param key HMAC secret key
98 * \param keylen length of the HMAC key
99 * \param is224 0 = use SHA256, 1 = use SHA224
101 void sha2_hmac_starts( sha2_context *ctx, unsigned char *key, int keylen,
105 * \brief SHA-256 HMAC process buffer
107 * \param ctx HMAC context
108 * \param input buffer holding the data
109 * \param ilen length of the input data
111 void sha2_hmac_update( sha2_context *ctx, unsigned char *input, int ilen );
114 * \brief SHA-256 HMAC final digest
116 * \param ctx HMAC context
117 * \param output SHA-224/256 HMAC checksum result
119 void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
122 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
124 * \param key HMAC secret key
125 * \param keylen length of the HMAC key
126 * \param input buffer holding the data
127 * \param ilen length of the input data
128 * \param output HMAC-SHA-224/256 result
129 * \param is224 0 = use SHA256, 1 = use SHA224
131 void sha2_hmac( unsigned char *key, int keylen,
132 unsigned char *input, int ilen,
133 unsigned char output[32], int is224 );