blob: 18bde93d354b50ae66dd9b6b8b449c91473fc3e8 [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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020031#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000032
Bence Szépkútic662b362021-05-27 11:25:03 +020033#include "mbedtls/build_info.h"
Paul Bakker90995b52013-06-24 19:20:35 +020034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020036#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000037
Gilles Peskined2971572021-07-26 18:48:10 +020038/** SHA-1 input data was malformed. */
39#define MBEDTLS_ERR_SHA1_BAD_INPUT_DATA -0x0073
Gilles Peskinea381fe82018-01-23 18:16:11 +010040
Paul Bakker407a0da2013-06-27 14:29:21 +020041#ifdef __cplusplus
42extern "C" {
43#endif
44
Ron Eldorb2aacec2017-05-18 16:53:08 +030045#if !defined(MBEDTLS_SHA1_ALT)
46// Regular implementation
47//
48
Paul Bakker5121ce52009-01-03 21:22:43 +000049/**
Rose Zadik44833d92018-01-26 08:41:09 +000050 * \brief The SHA-1 context structure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010051 *
52 * \warning SHA-1 is considered a weak message digest and its use
53 * constitutes a security risk. We recommend considering
54 * stronger message digests instead.
55 *
Paul Bakker5121ce52009-01-03 21:22:43 +000056 */
Gilles Peskine449bd832023-01-11 14:50:10 +010057typedef struct mbedtls_sha1_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020058 uint32_t MBEDTLS_PRIVATE(total)[2]; /*!< The number of Bytes processed. */
59 uint32_t MBEDTLS_PRIVATE(state)[5]; /*!< The intermediate digest state. */
60 unsigned char MBEDTLS_PRIVATE(buffer)[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Ron Eldorb2aacec2017-05-18 16:53:08 +030064#else /* MBEDTLS_SHA1_ALT */
65#include "sha1_alt.h"
66#endif /* MBEDTLS_SHA1_ALT */
67
Paul Bakker5121ce52009-01-03 21:22:43 +000068/**
Rose Zadik44833d92018-01-26 08:41:09 +000069 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020070 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010071 * \warning SHA-1 is considered a weak message digest and its use
72 * constitutes a security risk. We recommend considering
73 * stronger message digests instead.
74 *
Rose Zadik82741422018-03-27 12:49:48 +010075 * \param ctx The SHA-1 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050076 * This must not be \c NULL.
Rose Zadik82741422018-03-27 12:49:48 +010077 *
Paul Bakker5b4af392014-06-26 12:09:34 +020078 */
Gilles Peskine449bd832023-01-11 14:50:10 +010079void mbedtls_sha1_init(mbedtls_sha1_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020080
81/**
Rose Zadik44833d92018-01-26 08:41:09 +000082 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020083 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010084 * \warning SHA-1 is considered a weak message digest and its use
85 * constitutes a security risk. We recommend considering
86 * stronger message digests instead.
87 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050088 * \param ctx The SHA-1 context to clear. This may be \c NULL,
89 * in which case this function does nothing. If it is
90 * not \c NULL, it must point to an initialized
91 * SHA-1 context.
Rose Zadik82741422018-03-27 12:49:48 +010092 *
Paul Bakker5b4af392014-06-26 12:09:34 +020093 */
Gilles Peskine449bd832023-01-11 14:50:10 +010094void mbedtls_sha1_free(mbedtls_sha1_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020095
96/**
Rose Zadik44833d92018-01-26 08:41:09 +000097 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020098 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010099 * \warning SHA-1 is considered a weak message digest and its use
100 * constitutes a security risk. We recommend considering
101 * stronger message digests instead.
102 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500103 * \param dst The SHA-1 context to clone to. This must be initialized.
104 * \param src The SHA-1 context to clone from. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +0100105 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
108 const mbedtls_sha1_context *src);
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200109
110/**
Rose Zadik44833d92018-01-26 08:41:09 +0000111 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100113 * \warning SHA-1 is considered a weak message digest and its use
114 * constitutes a security risk. We recommend considering
115 * stronger message digests instead.
116 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500117 * \param ctx The SHA-1 context to initialize. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +0100118 *
119 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500120 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100121 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123int mbedtls_sha1_starts(mbedtls_sha1_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125/**
Rose Zadik44833d92018-01-26 08:41:09 +0000126 * \brief This function feeds an input buffer into an ongoing SHA-1
127 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100129 * \warning SHA-1 is considered a weak message digest and its use
130 * constitutes a security risk. We recommend considering
131 * stronger message digests instead.
132 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 * \param ctx The SHA-1 context. This must be initialized
134 * and have a hash operation started.
Rose Zadik82741422018-03-27 12:49:48 +0100135 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500136 * This must be a readable buffer of length \p ilen Bytes.
137 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100138 *
139 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500140 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142int mbedtls_sha1_update(mbedtls_sha1_context *ctx,
143 const unsigned char *input,
144 size_t ilen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
146/**
Rose Zadik44833d92018-01-26 08:41:09 +0000147 * \brief This function finishes the SHA-1 operation, and writes
148 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100150 * \warning SHA-1 is considered a weak message digest and its use
151 * constitutes a security risk. We recommend considering
152 * stronger message digests instead.
153 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500154 * \param ctx The SHA-1 context to use. This must be initialized and
155 * have a hash operation started.
156 * \param output The SHA-1 checksum result. This must be a writable
157 * buffer of length \c 20 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100158 *
159 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500160 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162int mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
163 unsigned char output[20]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100165/**
Rose Zadik82741422018-03-27 12:49:48 +0100166 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100167 *
168 * \warning SHA-1 is considered a weak message digest and its use
169 * constitutes a security risk. We recommend considering
170 * stronger message digests instead.
171 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500172 * \param ctx The SHA-1 context to use. This must be initialized.
173 * \param data The data block being processed. This must be a
174 * readable buffer of length \c 64 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100175 *
176 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500177 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100178 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100179 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
181 const unsigned char data[64]);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100182
Paul Bakker5121ce52009-01-03 21:22:43 +0000183/**
Rose Zadik44833d92018-01-26 08:41:09 +0000184 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 *
Rose Zadik44833d92018-01-26 08:41:09 +0000186 * The function allocates the context, performs the
187 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100188 *
Rose Zadik44833d92018-01-26 08:41:09 +0000189 * The SHA-1 result is calculated as
190 * output = SHA-1(input buffer).
191 *
Rose Zadik82741422018-03-27 12:49:48 +0100192 * \warning SHA-1 is considered a weak message digest and its use
193 * constitutes a security risk. We recommend considering
194 * stronger message digests instead.
195 *
Rose Zadik44833d92018-01-26 08:41:09 +0000196 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 * This must be a readable buffer of length \p ilen Bytes.
198 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000199 * \param output The SHA-1 checksum result.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500200 * This must be a writable buffer of length \c 20 Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000201 *
Rose Zadik82741422018-03-27 12:49:48 +0100202 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500203 * \return A negative error code on failure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100204 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100206int mbedtls_sha1(const unsigned char *input,
207 size_t ilen,
208 unsigned char output[20]);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100209
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500210#if defined(MBEDTLS_SELF_TEST)
211
Paul Bakker5121ce52009-01-03 21:22:43 +0000212/**
Rose Zadik44833d92018-01-26 08:41:09 +0000213 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100215 * \warning SHA-1 is considered a weak message digest and its use
216 * constitutes a security risk. We recommend considering
217 * stronger message digests instead.
218 *
Rose Zadik82741422018-03-27 12:49:48 +0100219 * \return \c 0 on success.
220 * \return \c 1 on failure.
221 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223int mbedtls_sha1_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000224
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500225#endif /* MBEDTLS_SELF_TEST */
226
Paul Bakker5121ce52009-01-03 21:22:43 +0000227#ifdef __cplusplus
228}
229#endif
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* mbedtls_sha1.h */