blob: d9f3855ea20f5265f6b4e9833b5328c5f50b98ce [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file sha2.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker77b385e2009-07-28 17:23:11 +00004 * Copyright (C) 2006-2009, Paul Bakker <polarssl_maintainer at polarssl.org>
5 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00006 *
Paul Bakker77b385e2009-07-28 17:23:11 +00007 * Joined copyright on original XySSL code with: Christophe Devine
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00008 *
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.
13 *
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.
18 *
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.
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Paul Bakker40e46942009-01-03 21:51:57 +000023#ifndef POLARSSL_SHA2_H
24#define POLARSSL_SHA2_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
26/**
27 * \brief SHA-256 context structure
28 */
29typedef struct
30{
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 */
34
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 */
38}
39sha2_context;
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/**
46 * \brief SHA-256 context setup
47 *
48 * \param ctx context to be initialized
49 * \param is224 0 = use SHA256, 1 = use SHA224
50 */
51void sha2_starts( sha2_context *ctx, int is224 );
52
53/**
54 * \brief SHA-256 process buffer
55 *
56 * \param ctx SHA-256 context
57 * \param input buffer holding the data
58 * \param ilen length of the input data
59 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000060void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +000061
62/**
63 * \brief SHA-256 final digest
64 *
65 * \param ctx SHA-256 context
66 * \param output SHA-224/256 checksum result
67 */
68void sha2_finish( sha2_context *ctx, unsigned char output[32] );
69
70/**
71 * \brief Output = SHA-256( input buffer )
72 *
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
77 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000078void sha2( const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +000079 unsigned char output[32], int is224 );
80
81/**
82 * \brief Output = SHA-256( file contents )
83 *
84 * \param path input file name
85 * \param output SHA-224/256 checksum result
86 * \param is224 0 = use SHA256, 1 = use SHA224
87 *
88 * \return 0 if successful, 1 if fopen failed,
89 * or 2 if fread failed
90 */
Paul Bakkerff60ee62010-03-16 21:09:09 +000091int sha2_file( const char *path, unsigned char output[32], int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93/**
94 * \brief SHA-256 HMAC context setup
95 *
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
100 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000101void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, int keylen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 int is224 );
103
104/**
105 * \brief SHA-256 HMAC process buffer
106 *
107 * \param ctx HMAC context
108 * \param input buffer holding the data
109 * \param ilen length of the input data
110 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000111void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, int ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113/**
114 * \brief SHA-256 HMAC final digest
115 *
116 * \param ctx HMAC context
117 * \param output SHA-224/256 HMAC checksum result
118 */
119void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] );
120
121/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000122 * \brief SHA-256 HMAC context reset
123 *
124 * \param ctx HMAC context to be reset
125 */
126void sha2_hmac_reset( sha2_context *ctx );
127
128/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 * \brief Output = HMAC-SHA-256( hmac key, input buffer )
130 *
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
137 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000138void sha2_hmac( const unsigned char *key, int keylen,
139 const unsigned char *input, int ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 unsigned char output[32], int is224 );
141
142/**
143 * \brief Checkup routine
144 *
145 * \return 0 if successful, or 1 if the test failed
146 */
147int sha2_self_test( int verbose );
148
149#ifdef __cplusplus
150}
151#endif
152
153#endif /* sha2.h */