blob: 61d81f168d72749b6ae3c4539cddc87ddf8f4d10 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file sha1.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik82741422018-03-27 12:49:48 +01004 * \brief This file contains SHA-1 definitions and functions.
5 *
Darryl Green11999bb2018-03-13 15:22:58 +00006 * The Secure Hash Algorithm 1 (SHA-1) cryptographic hash function is defined in
Rose Zadik82741422018-03-27 12:49:48 +01007 * <em>FIPS 180-4: Secure Hash Standard (SHS)</em>.
Hanno Beckerbbca8c52017-09-25 14:53:51 +01008 *
9 * \warning SHA-1 is considered a weak message digest and its use constitutes
10 * a security risk. We recommend considering stronger message
11 * digests instead.
Darryl Greena40a1012018-01-05 15:33:17 +000012 */
13/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020014 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +000015 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +000016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#ifndef MBEDTLS_SHA1_H
18#define MBEDTLS_SHA1_H
Paul Bakker5121ce52009-01-03 21:22:43 +000019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker90995b52013-06-24 19:20:35 +020025
Rich Evans00ab4702015-02-06 13:43:58 +000026#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020027#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000028
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020029/* MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020030/** SHA-1 hardware accelerator failed */
31#define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035
32/** SHA-1 input data was malformed. */
33#define MBEDTLS_ERR_SHA1_BAD_INPUT_DATA -0x0073
Gilles Peskinea381fe82018-01-23 18:16:11 +010034
Paul Bakker407a0da2013-06-27 14:29:21 +020035#ifdef __cplusplus
36extern "C" {
37#endif
38
Ron Eldorb2aacec2017-05-18 16:53:08 +030039#if !defined(MBEDTLS_SHA1_ALT)
40// Regular implementation
41//
42
Paul Bakker5121ce52009-01-03 21:22:43 +000043/**
Rose Zadik44833d92018-01-26 08:41:09 +000044 * \brief The SHA-1 context structure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010045 *
46 * \warning SHA-1 is considered a weak message digest and its use
47 * constitutes a security risk. We recommend considering
48 * stronger message digests instead.
49 *
Paul Bakker5121ce52009-01-03 21:22:43 +000050 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051typedef struct mbedtls_sha1_context {
Rose Zadik44833d92018-01-26 08:41:09 +000052 uint32_t total[2]; /*!< The number of Bytes processed. */
53 uint32_t state[5]; /*!< The intermediate digest state. */
54 unsigned char buffer[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000055}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Ron Eldorb2aacec2017-05-18 16:53:08 +030058#else /* MBEDTLS_SHA1_ALT */
59#include "sha1_alt.h"
60#endif /* MBEDTLS_SHA1_ALT */
61
Paul Bakker5121ce52009-01-03 21:22:43 +000062/**
Rose Zadik44833d92018-01-26 08:41:09 +000063 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020064 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010065 * \warning SHA-1 is considered a weak message digest and its use
66 * constitutes a security risk. We recommend considering
67 * stronger message digests instead.
68 *
Rose Zadik82741422018-03-27 12:49:48 +010069 * \param ctx The SHA-1 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050070 * This must not be \c NULL.
Rose Zadik82741422018-03-27 12:49:48 +010071 *
Paul Bakker5b4af392014-06-26 12:09:34 +020072 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073void mbedtls_sha1_init(mbedtls_sha1_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020074
75/**
Rose Zadik44833d92018-01-26 08:41:09 +000076 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020077 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010078 * \warning SHA-1 is considered a weak message digest and its use
79 * constitutes a security risk. We recommend considering
80 * stronger message digests instead.
81 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050082 * \param ctx The SHA-1 context to clear. This may be \c NULL,
83 * in which case this function does nothing. If it is
84 * not \c NULL, it must point to an initialized
85 * SHA-1 context.
Rose Zadik82741422018-03-27 12:49:48 +010086 *
Paul Bakker5b4af392014-06-26 12:09:34 +020087 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088void mbedtls_sha1_free(mbedtls_sha1_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020089
90/**
Rose Zadik44833d92018-01-26 08:41:09 +000091 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020092 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010093 * \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 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 * \param dst The SHA-1 context to clone to. This must be initialized.
98 * \param src The SHA-1 context to clone from. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +010099 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200100 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
102 const mbedtls_sha1_context *src);
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200103
104/**
Rose Zadik44833d92018-01-26 08:41:09 +0000105 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100107 * \warning SHA-1 is considered a weak message digest and its use
108 * constitutes a security risk. We recommend considering
109 * stronger message digests instead.
110 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500111 * \param ctx The SHA-1 context to initialize. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +0100112 *
113 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500114 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100115 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119/**
Rose Zadik44833d92018-01-26 08:41:09 +0000120 * \brief This function feeds an input buffer into an ongoing SHA-1
121 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100123 * \warning SHA-1 is considered a weak message digest and its use
124 * constitutes a security risk. We recommend considering
125 * stronger message digests instead.
126 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500127 * \param ctx The SHA-1 context. This must be initialized
128 * and have a hash operation started.
Rose Zadik82741422018-03-27 12:49:48 +0100129 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500130 * This must be a readable buffer of length \p ilen Bytes.
131 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100132 *
133 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500134 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx,
137 const unsigned char *input,
138 size_t ilen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
140/**
Rose Zadik44833d92018-01-26 08:41:09 +0000141 * \brief This function finishes the SHA-1 operation, and writes
142 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100144 * \warning SHA-1 is considered a weak message digest and its use
145 * constitutes a security risk. We recommend considering
146 * stronger message digests instead.
147 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500148 * \param ctx The SHA-1 context to use. This must be initialized and
149 * have a hash operation started.
150 * \param output The SHA-1 checksum result. This must be a writable
151 * buffer of length \c 20 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100152 *
153 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500154 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx,
157 unsigned char output[20]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100159/**
Rose Zadik82741422018-03-27 12:49:48 +0100160 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100161 *
162 * \warning SHA-1 is considered a weak message digest and its use
163 * constitutes a security risk. We recommend considering
164 * stronger message digests instead.
165 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500166 * \param ctx The SHA-1 context to use. This must be initialized.
167 * \param data The data block being processed. This must be a
168 * readable buffer of length \c 64 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100169 *
170 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500171 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100172 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100173 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
175 const unsigned char data[64]);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100176
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200177#if !defined(MBEDTLS_DEPRECATED_REMOVED)
178#if defined(MBEDTLS_DEPRECATED_WARNING)
179#define MBEDTLS_DEPRECATED __attribute__((deprecated))
180#else
181#define MBEDTLS_DEPRECATED
182#endif
183/**
184 * \brief This function starts a SHA-1 checksum calculation.
185 *
186 * \warning SHA-1 is considered a weak message digest and its use
187 * constitutes a security risk. We recommend considering
188 * stronger message digests instead.
189 *
190 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0.
191 *
192 * \param ctx The SHA-1 context to initialize. This must be initialized.
193 *
194 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195MBEDTLS_DEPRECATED void mbedtls_sha1_starts(mbedtls_sha1_context *ctx);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200196
197/**
198 * \brief This function feeds an input buffer into an ongoing SHA-1
199 * checksum calculation.
200 *
201 * \warning SHA-1 is considered a weak message digest and its use
202 * constitutes a security risk. We recommend considering
203 * stronger message digests instead.
204 *
205 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0.
206 *
207 * \param ctx The SHA-1 context. This must be initialized and
208 * have a hash operation started.
209 * \param input The buffer holding the input data.
210 * This must be a readable buffer of length \p ilen Bytes.
211 * \param ilen The length of the input data \p input in Bytes.
212 *
213 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100214MBEDTLS_DEPRECATED void mbedtls_sha1_update(mbedtls_sha1_context *ctx,
215 const unsigned char *input,
216 size_t ilen);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200217
218/**
219 * \brief This function finishes the SHA-1 operation, and writes
220 * the result to the output buffer.
221 *
222 * \warning SHA-1 is considered a weak message digest and its use
223 * constitutes a security risk. We recommend considering
224 * stronger message digests instead.
225 *
226 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0.
227 *
228 * \param ctx The SHA-1 context. This must be initialized and
229 * have a hash operation started.
230 * \param output The SHA-1 checksum result.
231 * This must be a writable buffer of length \c 20 Bytes.
232 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233MBEDTLS_DEPRECATED void mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
234 unsigned char output[20]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200235
236/**
237 * \brief SHA-1 process data block (internal use only).
238 *
239 * \warning SHA-1 is considered a weak message digest and its use
240 * constitutes a security risk. We recommend considering
241 * stronger message digests instead.
242 *
243 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0.
244 *
245 * \param ctx The SHA-1 context. This must be initialized.
246 * \param data The data block being processed.
247 * This must be a readable buffer of length \c 64 bytes.
248 *
249 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100250MBEDTLS_DEPRECATED void mbedtls_sha1_process(mbedtls_sha1_context *ctx,
251 const unsigned char data[64]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200252
253#undef MBEDTLS_DEPRECATED
254#endif /* !MBEDTLS_DEPRECATED_REMOVED */
255
Paul Bakker5121ce52009-01-03 21:22:43 +0000256/**
Rose Zadik44833d92018-01-26 08:41:09 +0000257 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 *
Rose Zadik44833d92018-01-26 08:41:09 +0000259 * The function allocates the context, performs the
260 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100261 *
Rose Zadik44833d92018-01-26 08:41:09 +0000262 * The SHA-1 result is calculated as
263 * output = SHA-1(input buffer).
264 *
Rose Zadik82741422018-03-27 12:49:48 +0100265 * \warning SHA-1 is considered a weak message digest and its use
266 * constitutes a security risk. We recommend considering
267 * stronger message digests instead.
268 *
Rose Zadik44833d92018-01-26 08:41:09 +0000269 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500270 * This must be a readable buffer of length \p ilen Bytes.
271 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000272 * \param output The SHA-1 checksum result.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500273 * This must be a writable buffer of length \c 20 Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000274 *
Rose Zadik82741422018-03-27 12:49:48 +0100275 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500276 * \return A negative error code on failure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100277 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279int mbedtls_sha1_ret(const unsigned char *input,
280 size_t ilen,
281 unsigned char output[20]);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100282
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200283#if !defined(MBEDTLS_DEPRECATED_REMOVED)
284#if defined(MBEDTLS_DEPRECATED_WARNING)
285#define MBEDTLS_DEPRECATED __attribute__((deprecated))
286#else
287#define MBEDTLS_DEPRECATED
288#endif
289/**
290 * \brief This function calculates the SHA-1 checksum of a buffer.
291 *
292 * The function allocates the context, performs the
293 * calculation, and frees the context.
294 *
295 * The SHA-1 result is calculated as
296 * output = SHA-1(input buffer).
297 *
298 * \warning SHA-1 is considered a weak message digest and its use
299 * constitutes a security risk. We recommend considering
300 * stronger message digests instead.
301 *
302 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
303 *
304 * \param input The buffer holding the input data.
305 * This must be a readable buffer of length \p ilen Bytes.
306 * \param ilen The length of the input data \p input in Bytes.
307 * \param output The SHA-1 checksum result. This must be a writable
308 * buffer of size \c 20 Bytes.
309 *
310 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311MBEDTLS_DEPRECATED void mbedtls_sha1(const unsigned char *input,
312 size_t ilen,
313 unsigned char output[20]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200314
315#undef MBEDTLS_DEPRECATED
316#endif /* !MBEDTLS_DEPRECATED_REMOVED */
317
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500318#if defined(MBEDTLS_SELF_TEST)
319
Paul Bakker5121ce52009-01-03 21:22:43 +0000320/**
Rose Zadik44833d92018-01-26 08:41:09 +0000321 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100323 * \warning SHA-1 is considered a weak message digest and its use
324 * constitutes a security risk. We recommend considering
325 * stronger message digests instead.
326 *
Rose Zadik82741422018-03-27 12:49:48 +0100327 * \return \c 0 on success.
328 * \return \c 1 on failure.
329 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100331int mbedtls_sha1_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500333#endif /* MBEDTLS_SELF_TEST */
334
Paul Bakker5121ce52009-01-03 21:22:43 +0000335#ifdef __cplusplus
336}
337#endif
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#endif /* mbedtls_sha1.h */