blob: 49ccb9720d36e616cfe67544b22d78dddbc677bc [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file sha1.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief SHA-1 cryptographic hash function
5 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * 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.
Hanno Beckerce0c9db2017-09-28 15:39:45 +010023 *
24 * \warning SHA-1 is considered a weak message digest and its use constitutes
25 * a security risk. We recommend considering stronger message
26 * digests instead.
27 *
Paul Bakker5121ce52009-01-03 21:22:43 +000028 */
Paul Bakker40e46942009-01-03 21:51:57 +000029#ifndef POLARSSL_SHA1_H
30#define POLARSSL_SHA1_H
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020033#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
35#include POLARSSL_CONFIG_FILE
36#endif
Paul Bakker90995b52013-06-24 19:20:35 +020037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000039
Paul Bakkerfa6a6202013-10-28 18:48:30 +010040#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000041#include <basetsd.h>
42typedef UINT32 uint32_t;
43#else
44#include <inttypes.h>
45#endif
46
Paul Bakker69e095c2011-12-10 21:55:01 +000047#define POLARSSL_ERR_SHA1_FILE_IO_ERROR -0x0076 /**< Read/write error in file. */
48
Paul Bakker90995b52013-06-24 19:20:35 +020049#if !defined(POLARSSL_SHA1_ALT)
50// Regular implementation
51//
52
Paul Bakker407a0da2013-06-27 14:29:21 +020053#ifdef __cplusplus
54extern "C" {
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057/**
58 * \brief SHA-1 context structure
Hanno Beckerce0c9db2017-09-28 15:39:45 +010059 *
60 * \warning SHA-1 is considered a weak message digest and its use
61 * constitutes a security risk. We recommend considering
62 * stronger message digests instead.
63 *
Paul Bakker5121ce52009-01-03 21:22:43 +000064 */
65typedef struct
66{
Paul Bakker5c2364c2012-10-01 14:41:15 +000067 uint32_t total[2]; /*!< number of bytes processed */
68 uint32_t state[5]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000069 unsigned char buffer[64]; /*!< data block being processed */
70
71 unsigned char ipad[64]; /*!< HMAC: inner padding */
72 unsigned char opad[64]; /*!< HMAC: outer padding */
73}
74sha1_context;
75
Paul Bakker5121ce52009-01-03 21:22:43 +000076/**
Paul Bakker5b4af392014-06-26 12:09:34 +020077 * \brief Initialize SHA-1 context
78 *
79 * \param ctx SHA-1 context to be initialized
Hanno Beckerce0c9db2017-09-28 15:39:45 +010080 *
81 * \warning SHA-1 is considered a weak message digest and its use
82 * constitutes a security risk. We recommend considering
83 * stronger message digests instead.
84 *
Paul Bakker5b4af392014-06-26 12:09:34 +020085 */
86void sha1_init( sha1_context *ctx );
87
88/**
89 * \brief Clear SHA-1 context
90 *
91 * \param ctx SHA-1 context to be cleared
Hanno Beckerce0c9db2017-09-28 15:39:45 +010092 *
93 * \warning SHA-1 is considered a weak message digest and its use
94 * constitutes a security risk. We recommend considering
95 * stronger message digests instead.
96 *
Paul Bakker5b4af392014-06-26 12:09:34 +020097 */
98void sha1_free( sha1_context *ctx );
99
100/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 * \brief SHA-1 context setup
102 *
103 * \param ctx context to be initialized
Hanno Beckerce0c9db2017-09-28 15:39:45 +0100104 *
105 * \warning SHA-1 is considered a weak message digest and its use
106 * constitutes a security risk. We recommend considering
107 * stronger message digests instead.
108 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 */
110void sha1_starts( sha1_context *ctx );
111
112/**
113 * \brief SHA-1 process buffer
114 *
115 * \param ctx SHA-1 context
116 * \param input buffer holding the data
117 * \param ilen length of the input data
Hanno Beckerce0c9db2017-09-28 15:39:45 +0100118 *
119 * \warning SHA-1 is considered a weak message digest and its use
120 * constitutes a security risk. We recommend considering
121 * stronger message digests instead.
122 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 */
Paul Bakker23986e52011-04-24 08:57:21 +0000124void sha1_update( sha1_context *ctx, const unsigned char *input, size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
126/**
127 * \brief SHA-1 final digest
128 *
129 * \param ctx SHA-1 context
130 * \param output SHA-1 checksum result
Hanno Beckerce0c9db2017-09-28 15:39:45 +0100131 *
132 * \warning SHA-1 is considered a weak message digest and its use
133 * constitutes a security risk. We recommend considering
134 * stronger message digests instead.
135 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 */
137void sha1_finish( sha1_context *ctx, unsigned char output[20] );
138
Paul Bakker90995b52013-06-24 19:20:35 +0200139/* Internal use */
140void sha1_process( sha1_context *ctx, const unsigned char data[64] );
141
142#ifdef __cplusplus
143}
144#endif
145
146#else /* POLARSSL_SHA1_ALT */
147#include "sha1_alt.h"
148#endif /* POLARSSL_SHA1_ALT */
149
150#ifdef __cplusplus
151extern "C" {
152#endif
153
Paul Bakker5121ce52009-01-03 21:22:43 +0000154/**
155 * \brief Output = SHA-1( input buffer )
156 *
157 * \param input buffer holding the data
158 * \param ilen length of the input data
159 * \param output SHA-1 checksum result
Hanno Beckerce0c9db2017-09-28 15:39:45 +0100160 *
161 * \warning SHA-1 is considered a weak message digest and its use
162 * constitutes a security risk. We recommend considering
163 * stronger message digests instead.
164 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Paul Bakker23986e52011-04-24 08:57:21 +0000166void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168/**
169 * \brief Output = SHA-1( file contents )
170 *
171 * \param path input file name
172 * \param output SHA-1 checksum result
173 *
Paul Bakker69e095c2011-12-10 21:55:01 +0000174 * \return 0 if successful, or POLARSSL_ERR_SHA1_FILE_IO_ERROR
Hanno Beckerce0c9db2017-09-28 15:39:45 +0100175 *
176 * \warning SHA-1 is considered a weak message digest and its use
177 * constitutes a security risk. We recommend considering
178 * stronger message digests instead.
179 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 */
Paul Bakkerff60ee62010-03-16 21:09:09 +0000181int sha1_file( const char *path, unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000182
183/**
184 * \brief SHA-1 HMAC context setup
185 *
186 * \param ctx HMAC context to be initialized
187 * \param key HMAC secret key
188 * \param keylen length of the HMAC key
Hanno Beckerce0c9db2017-09-28 15:39:45 +0100189 *
190 * \warning SHA-1 is considered a weak message digest and its use
191 * constitutes a security risk. We recommend considering
192 * stronger message digests instead.
193 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200195void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key,
196 size_t keylen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
198/**
199 * \brief SHA-1 HMAC process buffer
200 *
201 * \param ctx HMAC context
202 * \param input buffer holding the data
203 * \param ilen length of the input data
Hanno Beckerce0c9db2017-09-28 15:39:45 +0100204 *
205 * \warning SHA-1 is considered a weak message digest and its use
206 * constitutes a security risk. We recommend considering
207 * stronger message digests instead.
208 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200210void sha1_hmac_update( sha1_context *ctx, const unsigned char *input,
211 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
213/**
214 * \brief SHA-1 HMAC final digest
215 *
216 * \param ctx HMAC context
217 * \param output SHA-1 HMAC checksum result
Hanno Beckerce0c9db2017-09-28 15:39:45 +0100218 *
219 * \warning SHA-1 is considered a weak message digest and its use
220 * constitutes a security risk. We recommend considering
221 * stronger message digests instead.
222 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 */
224void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] );
225
226/**
Paul Bakker7d3b6612010-03-21 16:23:13 +0000227 * \brief SHA-1 HMAC context reset
228 *
229 * \param ctx HMAC context to be reset
Hanno Beckerce0c9db2017-09-28 15:39:45 +0100230 *
231 * \warning SHA-1 is considered a weak message digest and its use
232 * constitutes a security risk. We recommend considering
233 * stronger message digests instead.
234 *
Paul Bakker7d3b6612010-03-21 16:23:13 +0000235 */
236void sha1_hmac_reset( sha1_context *ctx );
237
238/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 * \brief Output = HMAC-SHA-1( hmac key, input buffer )
240 *
241 * \param key HMAC secret key
242 * \param keylen length of the HMAC key
243 * \param input buffer holding the data
244 * \param ilen length of the input data
245 * \param output HMAC-SHA-1 result
Hanno Beckerce0c9db2017-09-28 15:39:45 +0100246 *
247 * \warning SHA-1 is considered a weak message digest and its use
248 * constitutes a security risk. We recommend considering
249 * stronger message digests instead.
250 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 */
Paul Bakker23986e52011-04-24 08:57:21 +0000252void sha1_hmac( const unsigned char *key, size_t keylen,
253 const unsigned char *input, size_t ilen,
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 unsigned char output[20] );
255
256/**
257 * \brief Checkup routine
258 *
259 * \return 0 if successful, or 1 if the test failed
Hanno Beckerce0c9db2017-09-28 15:39:45 +0100260 *
261 * \warning SHA-1 is considered a weak message digest and its use
262 * constitutes a security risk. We recommend considering
263 * stronger message digests instead.
264 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 */
266int sha1_self_test( int verbose );
267
Paul Bakker5121ce52009-01-03 21:22:43 +0000268#ifdef __cplusplus
269}
270#endif
271
272#endif /* sha1.h */