ea75b30c |
1 | /** |
2 | * \file sha1.h |
3 | * |
4 | * \brief SHA-1 cryptographic hash function |
5 | * |
6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
7 | * |
8 | * This file is part of mbed TLS (https://tls.mbed.org) |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License as published by |
12 | * the Free Software Foundation; either version 2 of the License, or |
13 | * (at your option) any later version. |
14 | * |
15 | * This program is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU General Public License along |
21 | * with this program; if not, write to the Free Software Foundation, Inc., |
22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
23 | */ |
24 | #ifndef POLARSSL_SHA1_H |
25 | #define POLARSSL_SHA1_H |
26 | |
27 | #if !defined(POLARSSL_CONFIG_FILE) |
28 | //#include "config.h" |
29 | /** |
30 | * \def POLARSSL_SHA1_C |
31 | * |
32 | * Enable the SHA1 cryptographic hash algorithm. |
33 | * |
34 | * Module: library/sha1.c |
35 | * Caller: library/md.c |
36 | * library/ssl_cli.c |
37 | * library/ssl_srv.c |
38 | * library/ssl_tls.c |
39 | * library/x509write_crt.c |
40 | * |
41 | * This module is required for SSL/TLS and SHA1-signed certificates. |
42 | */ |
43 | #define POLARSSL_SHA1_C |
44 | |
45 | #else |
46 | #include POLARSSL_CONFIG_FILE |
47 | #endif |
48 | |
49 | #include <stddef.h> |
50 | |
51 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) |
52 | #include <basetsd.h> |
53 | typedef UINT32 uint32_t; |
54 | #else |
55 | #include <inttypes.h> |
56 | #endif |
57 | |
58 | #define POLARSSL_ERR_SHA1_FILE_IO_ERROR -0x0076 /**< Read/write error in file. */ |
59 | |
60 | #if !defined(POLARSSL_SHA1_ALT) |
61 | // Regular implementation |
62 | // |
63 | |
64 | #ifdef __cplusplus |
65 | extern "C" { |
66 | #endif |
67 | |
68 | /** |
69 | * \brief SHA-1 context structure |
70 | */ |
71 | typedef struct |
72 | { |
73 | uint32_t total[2]; /*!< number of bytes processed */ |
74 | uint32_t state[5]; /*!< intermediate digest state */ |
75 | unsigned char buffer[64]; /*!< data block being processed */ |
76 | |
77 | unsigned char ipad[64]; /*!< HMAC: inner padding */ |
78 | unsigned char opad[64]; /*!< HMAC: outer padding */ |
79 | } |
80 | sha1_context; |
81 | |
82 | /** |
83 | * \brief Initialize SHA-1 context |
84 | * |
85 | * \param ctx SHA-1 context to be initialized |
86 | */ |
87 | void sha1_init( sha1_context *ctx ); |
88 | |
89 | /** |
90 | * \brief Clear SHA-1 context |
91 | * |
92 | * \param ctx SHA-1 context to be cleared |
93 | */ |
94 | void sha1_free( sha1_context *ctx ); |
95 | |
96 | /** |
97 | * \brief SHA-1 context setup |
98 | * |
99 | * \param ctx context to be initialized |
100 | */ |
101 | void sha1_starts( sha1_context *ctx ); |
102 | |
103 | /** |
104 | * \brief SHA-1 process buffer |
105 | * |
106 | * \param ctx SHA-1 context |
107 | * \param input buffer holding the data |
108 | * \param ilen length of the input data |
109 | */ |
110 | void sha1_update( sha1_context *ctx, const unsigned char *input, size_t ilen ); |
111 | |
112 | /** |
113 | * \brief SHA-1 final digest |
114 | * |
115 | * \param ctx SHA-1 context |
116 | * \param output SHA-1 checksum result |
117 | */ |
118 | void sha1_finish( sha1_context *ctx, unsigned char output[20] ); |
119 | |
120 | /* Internal use */ |
121 | void sha1_process( sha1_context *ctx, const unsigned char data[64] ); |
122 | |
123 | #ifdef __cplusplus |
124 | } |
125 | #endif |
126 | |
127 | #else /* POLARSSL_SHA1_ALT */ |
128 | #include "sha1_alt.h" |
129 | #endif /* POLARSSL_SHA1_ALT */ |
130 | |
131 | #ifdef __cplusplus |
132 | extern "C" { |
133 | #endif |
134 | |
135 | /** |
136 | * \brief Output = SHA-1( input buffer ) |
137 | * |
138 | * \param input buffer holding the data |
139 | * \param ilen length of the input data |
140 | * \param output SHA-1 checksum result |
141 | */ |
142 | void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] ); |
143 | |
144 | /** |
145 | * \brief Output = SHA-1( file contents ) |
146 | * |
147 | * \param path input file name |
148 | * \param output SHA-1 checksum result |
149 | * |
150 | * \return 0 if successful, or POLARSSL_ERR_SHA1_FILE_IO_ERROR |
151 | */ |
152 | int sha1_file( const char *path, unsigned char output[20] ); |
153 | |
154 | /** |
155 | * \brief SHA-1 HMAC context setup |
156 | * |
157 | * \param ctx HMAC context to be initialized |
158 | * \param key HMAC secret key |
159 | * \param keylen length of the HMAC key |
160 | */ |
161 | void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, |
162 | size_t keylen ); |
163 | |
164 | /** |
165 | * \brief SHA-1 HMAC process buffer |
166 | * |
167 | * \param ctx HMAC context |
168 | * \param input buffer holding the data |
169 | * \param ilen length of the input data |
170 | */ |
171 | void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, |
172 | size_t ilen ); |
173 | |
174 | /** |
175 | * \brief SHA-1 HMAC final digest |
176 | * |
177 | * \param ctx HMAC context |
178 | * \param output SHA-1 HMAC checksum result |
179 | */ |
180 | void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] ); |
181 | |
182 | /** |
183 | * \brief SHA-1 HMAC context reset |
184 | * |
185 | * \param ctx HMAC context to be reset |
186 | */ |
187 | void sha1_hmac_reset( sha1_context *ctx ); |
188 | |
189 | /** |
190 | * \brief Output = HMAC-SHA-1( hmac key, input buffer ) |
191 | * |
192 | * \param key HMAC secret key |
193 | * \param keylen length of the HMAC key |
194 | * \param input buffer holding the data |
195 | * \param ilen length of the input data |
196 | * \param output HMAC-SHA-1 result |
197 | */ |
198 | void sha1_hmac( const unsigned char *key, size_t keylen, |
199 | const unsigned char *input, size_t ilen, |
200 | unsigned char output[20] ); |
201 | |
202 | /** |
203 | * \brief Checkup routine |
204 | * |
205 | * \return 0 if successful, or 1 if the test failed |
206 | */ |
207 | int sha1_self_test( int verbose ); |
208 | |
209 | #ifdef __cplusplus |
210 | } |
211 | #endif |
212 | |
213 | #endif /* sha1.h */ |