4 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
7 * Joined copyright on original XySSL code with: Christophe Devine
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.
24 /* $Cambridge: exim/src/src/pdkim/sha2.h,v 1.3 2009/12/07 13:05:07 tom Exp $ */
26 #ifndef POLARSSL_SHA2_H
27 #define POLARSSL_SHA2_H
30 * \brief SHA-256 context structure
32 #ifndef HAVE_SHA2_CONTEXT
33 #define HAVE_SHA2_CONTEXT
34 typedef struct sha2_context sha2_context;
39 unsigned long total[2]; /*!< number of bytes processed */
40 unsigned long state[8]; /*!< intermediate digest state */
41 unsigned char buffer[64]; /*!< data block being processed */
43 unsigned char ipad[64]; /*!< HMAC: inner padding */
44 unsigned char opad[64]; /*!< HMAC: outer padding */
45 int is224; /*!< 0 => SHA-256, else SHA-224 */
53 * \brief SHA-256 context setup
55 * \param ctx context to be initialized
56 * \param is224 0 = use SHA256, 1 = use SHA224
58 void sha2_starts( sha2_context *ctx, int is224 );
61 * \brief SHA-256 process buffer
63 * \param ctx SHA-256 context
64 * \param input buffer holding the data
65 * \param ilen length of the input data
67 void sha2_update( sha2_context *ctx, unsigned char *input, int ilen );
70 * \brief SHA-256 final digest
72 * \param ctx SHA-256 context
73 * \param output SHA-224/256 checksum result
75 void sha2_finish( sha2_context *ctx, unsigned char output[32] );
78 * \brief Output = SHA-256( input buffer )
80 * \param input buffer holding the data
81 * \param ilen length of the input data
82 * \param output SHA-224/256 checksum result
83 * \param is224 0 = use SHA256, 1 = use SHA224
85 void sha2( unsigned char *input, int ilen,
86 unsigned char output[32], int is224 );
89 * \brief Output = SHA-256( file contents )
91 * \param path input file name
92 * \param output SHA-224/256 checksum result
93 * \param is224 0 = use SHA256, 1 = use SHA224
95 * \return 0 if successful, 1 if fopen failed,
96 * or 2 if fread failed
98 int sha2_file( char *path, unsigned char output[32], int is224 );
101 * \brief SHA-256 HMAC context setup
103 * \param ctx HMAC context to be initialized
104 * \param key HMAC secret key
105 * \param keylen length of the HMAC key
106 * \param is224 0 = use SHA256, 1 = use SHA224
108 void sha2_hmac_starts( sha2_context *ctx, unsigned char *key, int keylen,
112 * \brief SHA-256 HMAC process buffer
114 * \param ctx HMAC context
115 * \param input buffer holding the data
116 * \param ilen length of the input data
118 void sha2_hmac_update( sha2_context *ctx, unsigned char *input, int ilen );
121 * \brief SHA-256 HMAC final digest
123 * \param ctx HMAC context
124 * \param output SHA-224/256 HMAC checksum result
126 void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
129 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
131 * \param key HMAC secret key
132 * \param keylen length of the HMAC key
133 * \param input buffer holding the data
134 * \param ilen length of the input data
135 * \param output HMAC-SHA-224/256 result
136 * \param is224 0 = use SHA256, 1 = use SHA224
138 void sha2_hmac( unsigned char *key, int keylen,
139 unsigned char *input, int ilen,
140 unsigned char output[32], int is224 );