blob: 6b55174a5ebf59abe4c2704aa3074e37b8b8fdb6 [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 */
Dawid Drozd428cc522018-07-24 10:02:47 +020057typedef struct mbedtls_sha1_context
Paul Bakker5121ce52009-01-03 21:22:43 +000058{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020059 uint32_t MBEDTLS_PRIVATE(total)[2]; /*!< The number of Bytes processed. */
60 uint32_t MBEDTLS_PRIVATE(state)[5]; /*!< The intermediate digest state. */
61 unsigned char MBEDTLS_PRIVATE(buffer)[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000062}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Ron Eldorb2aacec2017-05-18 16:53:08 +030065#else /* MBEDTLS_SHA1_ALT */
66#include "sha1_alt.h"
67#endif /* MBEDTLS_SHA1_ALT */
68
Paul Bakker5121ce52009-01-03 21:22:43 +000069/**
Rose Zadik44833d92018-01-26 08:41:09 +000070 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020071 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010072 * \warning SHA-1 is considered a weak message digest and its use
73 * constitutes a security risk. We recommend considering
74 * stronger message digests instead.
75 *
Rose Zadik82741422018-03-27 12:49:48 +010076 * \param ctx The SHA-1 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050077 * This must not be \c NULL.
Rose Zadik82741422018-03-27 12:49:48 +010078 *
Paul Bakker5b4af392014-06-26 12:09:34 +020079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020081
82/**
Rose Zadik44833d92018-01-26 08:41:09 +000083 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020084 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010085 * \warning SHA-1 is considered a weak message digest and its use
86 * constitutes a security risk. We recommend considering
87 * stronger message digests instead.
88 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050089 * \param ctx The SHA-1 context to clear. This may be \c NULL,
90 * in which case this function does nothing. If it is
91 * not \c NULL, it must point to an initialized
92 * SHA-1 context.
Rose Zadik82741422018-03-27 12:49:48 +010093 *
Paul Bakker5b4af392014-06-26 12:09:34 +020094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020096
97/**
Rose Zadik44833d92018-01-26 08:41:09 +000098 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020099 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100100 * \warning SHA-1 is considered a weak message digest and its use
101 * constitutes a security risk. We recommend considering
102 * stronger message digests instead.
103 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500104 * \param dst The SHA-1 context to clone to. This must be initialized.
105 * \param src The SHA-1 context to clone from. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +0100106 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200107 */
108void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
109 const mbedtls_sha1_context *src );
110
111/**
Rose Zadik44833d92018-01-26 08:41:09 +0000112 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100114 * \warning SHA-1 is considered a weak message digest and its use
115 * constitutes a security risk. We recommend considering
116 * stronger message digests instead.
117 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500118 * \param ctx The SHA-1 context to initialize. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +0100119 *
120 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100122 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 */
TRodziewicz26371e42021-06-08 16:45:41 +0200124int mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
126/**
Rose Zadik44833d92018-01-26 08:41:09 +0000127 * \brief This function feeds an input buffer into an ongoing SHA-1
128 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100130 * \warning SHA-1 is considered a weak message digest and its use
131 * constitutes a security risk. We recommend considering
132 * stronger message digests instead.
133 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500134 * \param ctx The SHA-1 context. This must be initialized
135 * and have a hash operation started.
Rose Zadik82741422018-03-27 12:49:48 +0100136 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 * This must be a readable buffer of length \p ilen Bytes.
138 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100139 *
140 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500141 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 */
TRodziewicz26371e42021-06-08 16:45:41 +0200143int mbedtls_sha1_update( mbedtls_sha1_context *ctx,
144 const unsigned char *input,
145 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
147/**
Rose Zadik44833d92018-01-26 08:41:09 +0000148 * \brief This function finishes the SHA-1 operation, and writes
149 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100151 * \warning SHA-1 is considered a weak message digest and its use
152 * constitutes a security risk. We recommend considering
153 * stronger message digests instead.
154 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500155 * \param ctx The SHA-1 context to use. This must be initialized and
156 * have a hash operation started.
157 * \param output The SHA-1 checksum result. This must be a writable
158 * buffer of length \c 20 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100159 *
160 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500161 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 */
TRodziewicz26371e42021-06-08 16:45:41 +0200163int mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
164 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100166/**
Rose Zadik82741422018-03-27 12:49:48 +0100167 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100168 *
169 * \warning SHA-1 is considered a weak message digest and its use
170 * constitutes a security risk. We recommend considering
171 * stronger message digests instead.
172 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500173 * \param ctx The SHA-1 context to use. This must be initialized.
174 * \param data The data block being processed. This must be a
175 * readable buffer of length \c 64 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100176 *
177 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500178 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100179 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100180 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100181int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
182 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100183
Paul Bakker5121ce52009-01-03 21:22:43 +0000184/**
Rose Zadik44833d92018-01-26 08:41:09 +0000185 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 *
Rose Zadik44833d92018-01-26 08:41:09 +0000187 * The function allocates the context, performs the
188 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100189 *
Rose Zadik44833d92018-01-26 08:41:09 +0000190 * The SHA-1 result is calculated as
191 * output = SHA-1(input buffer).
192 *
Rose Zadik82741422018-03-27 12:49:48 +0100193 * \warning SHA-1 is considered a weak message digest and its use
194 * constitutes a security risk. We recommend considering
195 * stronger message digests instead.
196 *
Rose Zadik44833d92018-01-26 08:41:09 +0000197 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500198 * This must be a readable buffer of length \p ilen Bytes.
199 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000200 * \param output The SHA-1 checksum result.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500201 * This must be a writable buffer of length \c 20 Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000202 *
Rose Zadik82741422018-03-27 12:49:48 +0100203 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500204 * \return A negative error code on failure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100205 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 */
TRodziewicz26371e42021-06-08 16:45:41 +0200207int mbedtls_sha1( const unsigned char *input,
208 size_t ilen,
209 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100210
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500211#if defined(MBEDTLS_SELF_TEST)
212
Paul Bakker5121ce52009-01-03 21:22:43 +0000213/**
Rose Zadik44833d92018-01-26 08:41:09 +0000214 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100216 * \warning SHA-1 is considered a weak message digest and its use
217 * constitutes a security risk. We recommend considering
218 * stronger message digests instead.
219 *
Rose Zadik82741422018-03-27 12:49:48 +0100220 * \return \c 0 on success.
221 * \return \c 1 on failure.
222 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500226#endif /* MBEDTLS_SELF_TEST */
227
Paul Bakker5121ce52009-01-03 21:22:43 +0000228#ifdef __cplusplus
229}
230#endif
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#endif /* mbedtls_sha1.h */