Commit | Line | Data |
---|---|---|
80a47a2c TK |
1 | /** |
2 | * \file sha2.h | |
3 | * | |
62d3e98d TK |
4 | * Copyright (C) 2006-2010, Brainspark B.V. |
5 | * | |
6 | * This file is part of PolarSSL (http://www.polarssl.org) | |
7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> | |
80a47a2c | 8 | * |
62d3e98d | 9 | * All rights reserved. |
80a47a2c TK |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2 of the License, or | |
14 | * (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License along | |
22 | * with this program; if not, write to the Free Software Foundation, Inc., | |
23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
24 | */ | |
25 | ||
80a47a2c TK |
26 | #ifndef POLARSSL_SHA2_H |
27 | #define POLARSSL_SHA2_H | |
28 | ||
29 | /** | |
30 | * \brief SHA-256 context structure | |
31 | */ | |
32 | #ifndef HAVE_SHA2_CONTEXT | |
33 | #define HAVE_SHA2_CONTEXT | |
34 | typedef struct sha2_context sha2_context; | |
35 | #endif | |
36 | ||
37 | struct sha2_context | |
38 | { | |
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 */ | |
42 | ||
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 */ | |
46 | }; | |
47 | ||
48 | #ifdef __cplusplus | |
49 | extern "C" { | |
50 | #endif | |
51 | ||
52 | /** | |
53 | * \brief SHA-256 context setup | |
54 | * | |
55 | * \param ctx context to be initialized | |
56 | * \param is224 0 = use SHA256, 1 = use SHA224 | |
57 | */ | |
58 | void sha2_starts( sha2_context *ctx, int is224 ); | |
59 | ||
60 | /** | |
61 | * \brief SHA-256 process buffer | |
62 | * | |
63 | * \param ctx SHA-256 context | |
64 | * \param input buffer holding the data | |
65 | * \param ilen length of the input data | |
66 | */ | |
62d3e98d | 67 | void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen ); |
80a47a2c TK |
68 | |
69 | /** | |
70 | * \brief SHA-256 final digest | |
71 | * | |
72 | * \param ctx SHA-256 context | |
73 | * \param output SHA-224/256 checksum result | |
74 | */ | |
75 | void sha2_finish( sha2_context *ctx, unsigned char output[32] ); | |
76 | ||
77 | /** | |
78 | * \brief Output = SHA-256( input buffer ) | |
79 | * | |
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 | |
84 | */ | |
62d3e98d | 85 | void sha2( const unsigned char *input, int ilen, |
80a47a2c TK |
86 | unsigned char output[32], int is224 ); |
87 | ||
88 | /** | |
89 | * \brief Output = SHA-256( file contents ) | |
90 | * | |
91 | * \param path input file name | |
92 | * \param output SHA-224/256 checksum result | |
93 | * \param is224 0 = use SHA256, 1 = use SHA224 | |
94 | * | |
95 | * \return 0 if successful, 1 if fopen failed, | |
96 | * or 2 if fread failed | |
97 | */ | |
62d3e98d | 98 | int sha2_file( const char *path, unsigned char output[32], int is224 ); |
80a47a2c TK |
99 | |
100 | /** | |
101 | * \brief SHA-256 HMAC context setup | |
102 | * | |
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 | |
107 | */ | |
62d3e98d | 108 | void sha2_hmac_starts( sha2_context *ctx, const unsigned char *key, int keylen, |
80a47a2c TK |
109 | int is224 ); |
110 | ||
111 | /** | |
112 | * \brief SHA-256 HMAC process buffer | |
113 | * | |
114 | * \param ctx HMAC context | |
115 | * \param input buffer holding the data | |
116 | * \param ilen length of the input data | |
117 | */ | |
62d3e98d | 118 | void sha2_hmac_update( sha2_context *ctx, const unsigned char *input, int ilen ); |
80a47a2c TK |
119 | |
120 | /** | |
121 | * \brief SHA-256 HMAC final digest | |
122 | * | |
123 | * \param ctx HMAC context | |
124 | * \param output SHA-224/256 HMAC checksum result | |
125 | */ | |
126 | void sha2_hmac_finish( sha2_context *ctx, unsigned char output[32] ); | |
127 | ||
62d3e98d TK |
128 | /** |
129 | * \brief SHA-256 HMAC context reset | |
130 | * | |
131 | * \param ctx HMAC context to be reset | |
132 | */ | |
133 | void sha2_hmac_reset( sha2_context *ctx ); | |
134 | ||
80a47a2c TK |
135 | /** |
136 | * \brief Output = HMAC-SHA-256( hmac key, input buffer ) | |
137 | * | |
138 | * \param key HMAC secret key | |
139 | * \param keylen length of the HMAC key | |
140 | * \param input buffer holding the data | |
141 | * \param ilen length of the input data | |
142 | * \param output HMAC-SHA-224/256 result | |
143 | * \param is224 0 = use SHA256, 1 = use SHA224 | |
144 | */ | |
62d3e98d TK |
145 | void sha2_hmac( const unsigned char *key, int keylen, |
146 | const unsigned char *input, int ilen, | |
80a47a2c TK |
147 | unsigned char output[32], int is224 ); |
148 | ||
149 | #ifdef __cplusplus | |
150 | } | |
151 | #endif | |
152 | ||
153 | #endif /* sha2.h */ |