4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #ifndef TROPICSSL_SHA2_H
36 #define TROPICSSL_SHA2_H
39 * \brief SHA-256 context structure
42 unsigned long total
[2]; /*!< number of bytes processed */
43 unsigned long state
[8]; /*!< intermediate digest state */
44 unsigned char buffer
[64]; /*!< data block being processed */
46 unsigned char ipad
[64]; /*!< HMAC: inner padding */
47 unsigned char opad
[64]; /*!< HMAC: outer padding */
48 int is224
; /*!< 0 => SHA-256, else SHA-224 */
56 * \brief SHA-256 context setup
58 * \param ctx context to be initialized
59 * \param is224 0 = use SHA256, 1 = use SHA224
61 void sha2_starts(sha2_context
* ctx
, int is224
);
64 * \brief SHA-256 process buffer
66 * \param ctx SHA-256 context
67 * \param input buffer holding the data
68 * \param ilen length of the input data
70 void sha2_update(sha2_context
* ctx
, const unsigned char *input
, int ilen
);
73 * \brief SHA-256 final digest
75 * \param ctx SHA-256 context
76 * \param output SHA-224/256 checksum result
78 void sha2_finish(sha2_context
* ctx
, unsigned char output
[32]);
81 * \brief Output = SHA-256( input buffer )
83 * \param input buffer holding the data
84 * \param ilen length of the input data
85 * \param output SHA-224/256 checksum result
86 * \param is224 0 = use SHA256, 1 = use SHA224
88 void sha2(const unsigned char *input
, int ilen
,
89 unsigned char output
[32], int is224
);
92 * \brief Output = SHA-256( file contents )
94 * \param path input file name
95 * \param output SHA-224/256 checksum result
96 * \param is224 0 = use SHA256, 1 = use SHA224
98 * \return 0 if successful, 1 if fopen failed,
99 * or 2 if fread failed
101 int sha2_file(const char *path
, unsigned char output
[32], int is224
);
104 * \brief SHA-256 HMAC context setup
106 * \param ctx HMAC context to be initialized
107 * \param key HMAC secret key
108 * \param keylen length of the HMAC key
109 * \param is224 0 = use SHA256, 1 = use SHA224
111 void sha2_hmac_starts(sha2_context
* ctx
, const unsigned char *key
,
112 int keylen
, int is224
);
115 * \brief SHA-256 HMAC process buffer
117 * \param ctx HMAC context
118 * \param input buffer holding the data
119 * \param ilen length of the input data
121 void sha2_hmac_update(sha2_context
* ctx
, const unsigned char *input
,
125 * \brief SHA-256 HMAC final digest
127 * \param ctx HMAC context
128 * \param output SHA-224/256 HMAC checksum result
130 void sha2_hmac_finish(sha2_context
* ctx
, unsigned char output
[32]);
133 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
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
142 void sha2_hmac(const unsigned char *key
, int keylen
,
143 const unsigned char *input
, int ilen
,
144 unsigned char output
[32], int is224
);
147 * \brief Checkup routine
149 * \return 0 if successful, or 1 if the test failed
151 int sha2_self_test(int verbose
);