blob: 7a7319f26ae29f9e31bec40554ca630e93b8489f [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 * SPDX-License-Identifier: Apache-2.0
16 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#ifndef MBEDTLS_SHA1_H
30#define MBEDTLS_SHA1_H
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010033#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#endif
Paul Bakker90995b52013-06-24 19:20:35 +020037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020039#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000040
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020041/* MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020042/** SHA-1 hardware accelerator failed */
43#define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035
44/** SHA-1 input data was malformed. */
45#define MBEDTLS_ERR_SHA1_BAD_INPUT_DATA -0x0073
Gilles Peskinea381fe82018-01-23 18:16:11 +010046
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Ron Eldorb2aacec2017-05-18 16:53:08 +030051#if !defined(MBEDTLS_SHA1_ALT)
52// Regular implementation
53//
54
Paul Bakker5121ce52009-01-03 21:22:43 +000055/**
Rose Zadik44833d92018-01-26 08:41:09 +000056 * \brief The SHA-1 context structure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010057 *
58 * \warning SHA-1 is considered a weak message digest and its use
59 * constitutes a security risk. We recommend considering
60 * stronger message digests instead.
61 *
Paul Bakker5121ce52009-01-03 21:22:43 +000062 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063typedef struct mbedtls_sha1_context {
Rose Zadik44833d92018-01-26 08:41:09 +000064 uint32_t total[2]; /*!< The number of Bytes processed. */
65 uint32_t state[5]; /*!< The intermediate digest state. */
66 unsigned char buffer[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Ron Eldorb2aacec2017-05-18 16:53:08 +030070#else /* MBEDTLS_SHA1_ALT */
71#include "sha1_alt.h"
72#endif /* MBEDTLS_SHA1_ALT */
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/**
Rose Zadik44833d92018-01-26 08:41:09 +000075 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020076 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010077 * \warning SHA-1 is considered a weak message digest and its use
78 * constitutes a security risk. We recommend considering
79 * stronger message digests instead.
80 *
Rose Zadik82741422018-03-27 12:49:48 +010081 * \param ctx The SHA-1 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050082 * This must not be \c NULL.
Rose Zadik82741422018-03-27 12:49:48 +010083 *
Paul Bakker5b4af392014-06-26 12:09:34 +020084 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085void mbedtls_sha1_init(mbedtls_sha1_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020086
87/**
Rose Zadik44833d92018-01-26 08:41:09 +000088 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020089 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010090 * \warning SHA-1 is considered a weak message digest and its use
91 * constitutes a security risk. We recommend considering
92 * stronger message digests instead.
93 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050094 * \param ctx The SHA-1 context to clear. This may be \c NULL,
95 * in which case this function does nothing. If it is
96 * not \c NULL, it must point to an initialized
97 * SHA-1 context.
Rose Zadik82741422018-03-27 12:49:48 +010098 *
Paul Bakker5b4af392014-06-26 12:09:34 +020099 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100void mbedtls_sha1_free(mbedtls_sha1_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +0200101
102/**
Rose Zadik44833d92018-01-26 08:41:09 +0000103 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200104 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100105 * \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 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500109 * \param dst The SHA-1 context to clone to. This must be initialized.
110 * \param src The SHA-1 context to clone from. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +0100111 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200112 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
114 const mbedtls_sha1_context *src);
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200115
116/**
Rose Zadik44833d92018-01-26 08:41:09 +0000117 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100119 * \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 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500123 * \param ctx The SHA-1 context to initialize. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +0100124 *
125 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500126 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100127 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100129int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
131/**
Rose Zadik44833d92018-01-26 08:41:09 +0000132 * \brief This function feeds an input buffer into an ongoing SHA-1
133 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100135 * \warning SHA-1 is considered a weak message digest and its use
136 * constitutes a security risk. We recommend considering
137 * stronger message digests instead.
138 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500139 * \param ctx The SHA-1 context. This must be initialized
140 * and have a hash operation started.
Rose Zadik82741422018-03-27 12:49:48 +0100141 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 * This must be a readable buffer of length \p ilen Bytes.
143 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100144 *
145 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500146 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx,
149 const unsigned char *input,
150 size_t ilen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152/**
Rose Zadik44833d92018-01-26 08:41:09 +0000153 * \brief This function finishes the SHA-1 operation, and writes
154 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100156 * \warning SHA-1 is considered a weak message digest and its use
157 * constitutes a security risk. We recommend considering
158 * stronger message digests instead.
159 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500160 * \param ctx The SHA-1 context to use. This must be initialized and
161 * have a hash operation started.
162 * \param output The SHA-1 checksum result. This must be a writable
163 * buffer of length \c 20 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100164 *
165 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500166 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100168int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx,
169 unsigned char output[20]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100171/**
Rose Zadik82741422018-03-27 12:49:48 +0100172 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100173 *
174 * \warning SHA-1 is considered a weak message digest and its use
175 * constitutes a security risk. We recommend considering
176 * stronger message digests instead.
177 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500178 * \param ctx The SHA-1 context to use. This must be initialized.
179 * \param data The data block being processed. This must be a
180 * readable buffer of length \c 64 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100181 *
182 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100184 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100185 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
187 const unsigned char data[64]);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100188
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200189#if !defined(MBEDTLS_DEPRECATED_REMOVED)
190#if defined(MBEDTLS_DEPRECATED_WARNING)
191#define MBEDTLS_DEPRECATED __attribute__((deprecated))
192#else
193#define MBEDTLS_DEPRECATED
194#endif
195/**
196 * \brief This function starts a SHA-1 checksum calculation.
197 *
198 * \warning SHA-1 is considered a weak message digest and its use
199 * constitutes a security risk. We recommend considering
200 * stronger message digests instead.
201 *
202 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0.
203 *
204 * \param ctx The SHA-1 context to initialize. This must be initialized.
205 *
206 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207MBEDTLS_DEPRECATED void mbedtls_sha1_starts(mbedtls_sha1_context *ctx);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200208
209/**
210 * \brief This function feeds an input buffer into an ongoing SHA-1
211 * checksum calculation.
212 *
213 * \warning SHA-1 is considered a weak message digest and its use
214 * constitutes a security risk. We recommend considering
215 * stronger message digests instead.
216 *
217 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0.
218 *
219 * \param ctx The SHA-1 context. This must be initialized and
220 * have a hash operation started.
221 * \param input The buffer holding the input data.
222 * This must be a readable buffer of length \p ilen Bytes.
223 * \param ilen The length of the input data \p input in Bytes.
224 *
225 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226MBEDTLS_DEPRECATED void mbedtls_sha1_update(mbedtls_sha1_context *ctx,
227 const unsigned char *input,
228 size_t ilen);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200229
230/**
231 * \brief This function finishes the SHA-1 operation, and writes
232 * the result to the output buffer.
233 *
234 * \warning SHA-1 is considered a weak message digest and its use
235 * constitutes a security risk. We recommend considering
236 * stronger message digests instead.
237 *
238 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0.
239 *
240 * \param ctx The SHA-1 context. This must be initialized and
241 * have a hash operation started.
242 * \param output The SHA-1 checksum result.
243 * This must be a writable buffer of length \c 20 Bytes.
244 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245MBEDTLS_DEPRECATED void mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
246 unsigned char output[20]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200247
248/**
249 * \brief SHA-1 process data block (internal use only).
250 *
251 * \warning SHA-1 is considered a weak message digest and its use
252 * constitutes a security risk. We recommend considering
253 * stronger message digests instead.
254 *
255 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0.
256 *
257 * \param ctx The SHA-1 context. This must be initialized.
258 * \param data The data block being processed.
259 * This must be a readable buffer of length \c 64 bytes.
260 *
261 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262MBEDTLS_DEPRECATED void mbedtls_sha1_process(mbedtls_sha1_context *ctx,
263 const unsigned char data[64]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200264
265#undef MBEDTLS_DEPRECATED
266#endif /* !MBEDTLS_DEPRECATED_REMOVED */
267
Paul Bakker5121ce52009-01-03 21:22:43 +0000268/**
Rose Zadik44833d92018-01-26 08:41:09 +0000269 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 *
Rose Zadik44833d92018-01-26 08:41:09 +0000271 * The function allocates the context, performs the
272 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100273 *
Rose Zadik44833d92018-01-26 08:41:09 +0000274 * The SHA-1 result is calculated as
275 * output = SHA-1(input buffer).
276 *
Rose Zadik82741422018-03-27 12:49:48 +0100277 * \warning SHA-1 is considered a weak message digest and its use
278 * constitutes a security risk. We recommend considering
279 * stronger message digests instead.
280 *
Rose Zadik44833d92018-01-26 08:41:09 +0000281 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 * This must be a readable buffer of length \p ilen Bytes.
283 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000284 * \param output The SHA-1 checksum result.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500285 * This must be a writable buffer of length \c 20 Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000286 *
Rose Zadik82741422018-03-27 12:49:48 +0100287 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288 * \return A negative error code on failure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100289 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291int mbedtls_sha1_ret(const unsigned char *input,
292 size_t ilen,
293 unsigned char output[20]);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100294
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200295#if !defined(MBEDTLS_DEPRECATED_REMOVED)
296#if defined(MBEDTLS_DEPRECATED_WARNING)
297#define MBEDTLS_DEPRECATED __attribute__((deprecated))
298#else
299#define MBEDTLS_DEPRECATED
300#endif
301/**
302 * \brief This function calculates the SHA-1 checksum of a buffer.
303 *
304 * The function allocates the context, performs the
305 * calculation, and frees the context.
306 *
307 * The SHA-1 result is calculated as
308 * output = SHA-1(input buffer).
309 *
310 * \warning SHA-1 is considered a weak message digest and its use
311 * constitutes a security risk. We recommend considering
312 * stronger message digests instead.
313 *
314 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
315 *
316 * \param input The buffer holding the input data.
317 * This must be a readable buffer of length \p ilen Bytes.
318 * \param ilen The length of the input data \p input in Bytes.
319 * \param output The SHA-1 checksum result. This must be a writable
320 * buffer of size \c 20 Bytes.
321 *
322 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323MBEDTLS_DEPRECATED void mbedtls_sha1(const unsigned char *input,
324 size_t ilen,
325 unsigned char output[20]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200326
327#undef MBEDTLS_DEPRECATED
328#endif /* !MBEDTLS_DEPRECATED_REMOVED */
329
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500330#if defined(MBEDTLS_SELF_TEST)
331
Paul Bakker5121ce52009-01-03 21:22:43 +0000332/**
Rose Zadik44833d92018-01-26 08:41:09 +0000333 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100335 * \warning SHA-1 is considered a weak message digest and its use
336 * constitutes a security risk. We recommend considering
337 * stronger message digests instead.
338 *
Rose Zadik82741422018-03-27 12:49:48 +0100339 * \return \c 0 on success.
340 * \return \c 1 on failure.
341 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343int mbedtls_sha1_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500345#endif /* MBEDTLS_SELF_TEST */
346
Paul Bakker5121ce52009-01-03 21:22:43 +0000347#ifdef __cplusplus
348}
349#endif
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#endif /* mbedtls_sha1.h */