blob: 0975e2d70db96eefb41c9a6556f32bfc293146a8 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010034#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#endif
Paul Bakker90995b52013-06-24 19:20:35 +020038
Rich Evans00ab4702015-02-06 13:43:58 +000039#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020040#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000041
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050042#define MBEDTLS_ERR_SHA1_BAD_INPUT_DATA -0x0073 /**< SHA-1 input data was malformed. */
Gilles Peskinea381fe82018-01-23 18:16:11 +010043
Paul Bakker407a0da2013-06-27 14:29:21 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Ron Eldorb2aacec2017-05-18 16:53:08 +030048#if !defined(MBEDTLS_SHA1_ALT)
49// Regular implementation
50//
51
Paul Bakker5121ce52009-01-03 21:22:43 +000052/**
Rose Zadik44833d92018-01-26 08:41:09 +000053 * \brief The SHA-1 context structure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010054 *
55 * \warning SHA-1 is considered a weak message digest and its use
56 * constitutes a security risk. We recommend considering
57 * stronger message digests instead.
58 *
Paul Bakker5121ce52009-01-03 21:22:43 +000059 */
Dawid Drozd428cc522018-07-24 10:02:47 +020060typedef struct mbedtls_sha1_context
Paul Bakker5121ce52009-01-03 21:22:43 +000061{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020062 uint32_t MBEDTLS_PRIVATE(total)[2]; /*!< The number of Bytes processed. */
63 uint32_t MBEDTLS_PRIVATE(state)[5]; /*!< The intermediate digest state. */
64 unsigned char MBEDTLS_PRIVATE(buffer)[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Ron Eldorb2aacec2017-05-18 16:53:08 +030068#else /* MBEDTLS_SHA1_ALT */
69#include "sha1_alt.h"
70#endif /* MBEDTLS_SHA1_ALT */
71
Paul Bakker5121ce52009-01-03 21:22:43 +000072/**
Rose Zadik44833d92018-01-26 08:41:09 +000073 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020074 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010075 * \warning SHA-1 is considered a weak message digest and its use
76 * constitutes a security risk. We recommend considering
77 * stronger message digests instead.
78 *
Rose Zadik82741422018-03-27 12:49:48 +010079 * \param ctx The SHA-1 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050080 * This must not be \c NULL.
Rose Zadik82741422018-03-27 12:49:48 +010081 *
Paul Bakker5b4af392014-06-26 12:09:34 +020082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020084
85/**
Rose Zadik44833d92018-01-26 08:41:09 +000086 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020087 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010088 * \warning SHA-1 is considered a weak message digest and its use
89 * constitutes a security risk. We recommend considering
90 * stronger message digests instead.
91 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050092 * \param ctx The SHA-1 context to clear. This may be \c NULL,
93 * in which case this function does nothing. If it is
94 * not \c NULL, it must point to an initialized
95 * SHA-1 context.
Rose Zadik82741422018-03-27 12:49:48 +010096 *
Paul Bakker5b4af392014-06-26 12:09:34 +020097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020099
100/**
Rose Zadik44833d92018-01-26 08:41:09 +0000101 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200102 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100103 * \warning SHA-1 is considered a weak message digest and its use
104 * constitutes a security risk. We recommend considering
105 * stronger message digests instead.
106 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500107 * \param dst The SHA-1 context to clone to. This must be initialized.
108 * \param src The SHA-1 context to clone from. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +0100109 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200110 */
111void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
112 const mbedtls_sha1_context *src );
113
114/**
Rose Zadik44833d92018-01-26 08:41:09 +0000115 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100117 * \warning SHA-1 is considered a weak message digest and its use
118 * constitutes a security risk. We recommend considering
119 * stronger message digests instead.
120 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121 * \param ctx The SHA-1 context to initialize. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +0100122 *
123 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100125 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 */
TRodziewicz26371e42021-06-08 16:45:41 +0200127int mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/**
Rose Zadik44833d92018-01-26 08:41:09 +0000130 * \brief This function feeds an input buffer into an ongoing SHA-1
131 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100133 * \warning SHA-1 is considered a weak message digest and its use
134 * constitutes a security risk. We recommend considering
135 * stronger message digests instead.
136 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 * \param ctx The SHA-1 context. This must be initialized
138 * and have a hash operation started.
Rose Zadik82741422018-03-27 12:49:48 +0100139 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500140 * This must be a readable buffer of length \p ilen Bytes.
141 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100142 *
143 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500144 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 */
TRodziewicz26371e42021-06-08 16:45:41 +0200146int mbedtls_sha1_update( mbedtls_sha1_context *ctx,
147 const unsigned char *input,
148 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
150/**
Rose Zadik44833d92018-01-26 08:41:09 +0000151 * \brief This function finishes the SHA-1 operation, and writes
152 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100154 * \warning SHA-1 is considered a weak message digest and its use
155 * constitutes a security risk. We recommend considering
156 * stronger message digests instead.
157 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500158 * \param ctx The SHA-1 context to use. This must be initialized and
159 * have a hash operation started.
160 * \param output The SHA-1 checksum result. This must be a writable
161 * buffer of length \c 20 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100162 *
163 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500164 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
TRodziewicz26371e42021-06-08 16:45:41 +0200166int mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
167 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100169/**
Rose Zadik82741422018-03-27 12:49:48 +0100170 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100171 *
172 * \warning SHA-1 is considered a weak message digest and its use
173 * constitutes a security risk. We recommend considering
174 * stronger message digests instead.
175 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500176 * \param ctx The SHA-1 context to use. This must be initialized.
177 * \param data The data block being processed. This must be a
178 * readable buffer of length \c 64 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100179 *
180 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500181 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100182 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100183 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100184int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
185 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100186
Paul Bakker5121ce52009-01-03 21:22:43 +0000187/**
Rose Zadik44833d92018-01-26 08:41:09 +0000188 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 *
Rose Zadik44833d92018-01-26 08:41:09 +0000190 * The function allocates the context, performs the
191 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100192 *
Rose Zadik44833d92018-01-26 08:41:09 +0000193 * The SHA-1 result is calculated as
194 * output = SHA-1(input buffer).
195 *
Rose Zadik82741422018-03-27 12:49:48 +0100196 * \warning SHA-1 is considered a weak message digest and its use
197 * constitutes a security risk. We recommend considering
198 * stronger message digests instead.
199 *
Rose Zadik44833d92018-01-26 08:41:09 +0000200 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500201 * This must be a readable buffer of length \p ilen Bytes.
202 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000203 * \param output The SHA-1 checksum result.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500204 * This must be a writable buffer of length \c 20 Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000205 *
Rose Zadik82741422018-03-27 12:49:48 +0100206 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500207 * \return A negative error code on failure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100208 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 */
TRodziewicz26371e42021-06-08 16:45:41 +0200210int mbedtls_sha1( const unsigned char *input,
211 size_t ilen,
212 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100213
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214#if defined(MBEDTLS_SELF_TEST)
215
Paul Bakker5121ce52009-01-03 21:22:43 +0000216/**
Rose Zadik44833d92018-01-26 08:41:09 +0000217 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100219 * \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 *
Rose Zadik82741422018-03-27 12:49:48 +0100223 * \return \c 0 on success.
224 * \return \c 1 on failure.
225 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500229#endif /* MBEDTLS_SELF_TEST */
230
Paul Bakker5121ce52009-01-03 21:22:43 +0000231#ifdef __cplusplus
232}
233#endif
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#endif /* mbedtls_sha1.h */