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