blob: bc4713b5e2a4ee58590e3df91acf282531a03453 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file sha2.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker83ded912010-03-21 17:46:26 +00004 * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakker77b385e2009-07-28 17:23:11 +00005 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
Paul Bakker40e46942009-01-03 21:51:57 +000021#ifndef POLARSSL_SHA2_H
22#define POLARSSL_SHA2_H
Paul Bakker5121ce52009-01-03 21:22:43 +000023
24/**
25 * \brief SHA-256 context structure
26 */
27typedef struct
28{
29 unsigned long total[2]; /*!< number of bytes processed */
30 unsigned long state[8]; /*!< intermediate digest state */
31 unsigned char buffer[64]; /*!< data block being processed */
32
33 unsigned char ipad[64]; /*!< HMAC: inner padding */
34 unsigned char opad[64]; /*!< HMAC: outer padding */
35 int is224; /*!< 0 => SHA-256, else SHA-224 */
36}
37sha2_context;
38
39#ifdef __cplusplus
40extern "C" {
41#endif
42
43/**
44 * \brief SHA-256 context setup
45 *
46 * \param ctx context to be initialized
47 * \param is224 0 = use SHA256, 1 = use SHA224
48 */
49void sha2_starts( sha2_context *ctx, int is224 );
50
51/**
52 * \brief SHA-256 process buffer
53 *
54 * \param ctx SHA-256 context
55 * \param input buffer holding the data
56 * \param ilen length of the input data
57 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000058void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000059
60/**
61 * \brief SHA-256 final digest
62 *
63 * \param ctx SHA-256 context
64 * \param output SHA-224/256 checksum result
65 */
66void sha2_finish( sha2_context *ctx, unsigned char output[32] );
67
68/**
69 * \brief Output = SHA-256( input buffer )
70 *
71 * \param input buffer holding the data
72 * \param ilen length of the input data
73 * \param output SHA-224/256 checksum result
74 * \param is224 0 = use SHA256, 1 = use SHA224
75 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000076void sha2( const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +000077 unsigned char output[32], int is224 );
78
79/**
80 * \brief Output = SHA-256( file contents )
81 *
82 * \param path input file name
83 * \param output SHA-224/256 checksum result
84 * \param is224 0 = use SHA256, 1 = use SHA224
85 *
86 * \return 0 if successful, 1 if fopen failed,
87 * or 2 if fread failed
88 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000089int sha2_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91/**
92 * \brief SHA-256 HMAC context setup
93 *
94 * \param ctx HMAC context to be initialized
95 * \param key HMAC secret key
96 * \param keylen length of the HMAC key
97 * \param is224 0 = use SHA256, 1 = use SHA224
98 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000099void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, int keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 int is224 );
101
102/**
103 * \brief SHA-256 HMAC process buffer
104 *
105 * \param ctx HMAC context
106 * \param input buffer holding the data
107 * \param ilen length of the input data
108 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000109void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
111/**
112 * \brief SHA-256 HMAC final digest
113 *
114 * \param ctx HMAC context
115 * \param output SHA-224/256 HMAC checksum result
116 */
117void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
118
119/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000120 * \brief SHA-256 HMAC context reset
121 *
122 * \param ctx HMAC context to be reset
123 */
124void sha2_hmac_reset( sha2_context *ctx );
125
126/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
128 *
129 * \param key HMAC secret key
130 * \param keylen length of the HMAC key
131 * \param input buffer holding the data
132 * \param ilen length of the input data
133 * \param output HMAC-SHA-224/256 result
134 * \param is224 0 = use SHA256, 1 = use SHA224
135 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000136void sha2_hmac( const unsigned char *key, int keylen,
137 const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 unsigned char output[32], int is224 );
139
140/**
141 * \brief Checkup routine
142 *
143 * \return 0 if successful, or 1 if the test failed
144 */
145int sha2_self_test( int verbose );
146
147#ifdef __cplusplus
148}
149#endif
150
151#endif /* sha2.h */