blob: 592ffd13f244947f85fa19a406db946f7b0aacdc [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 Rodgman16799db2023-11-02 19:47:20 +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
Mateusz Starzyk846f0212021-05-19 19:44:07 +020019#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000020
Bence Szépkútic662b362021-05-27 11:25:03 +020021#include "mbedtls/build_info.h"
Paul Bakker90995b52013-06-24 19:20:35 +020022
Rich Evans00ab4702015-02-06 13:43:58 +000023#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020024#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000025
Gilles Peskined2971572021-07-26 18:48:10 +020026/** SHA-1 input data was malformed. */
27#define MBEDTLS_ERR_SHA1_BAD_INPUT_DATA -0x0073
Gilles Peskinea381fe82018-01-23 18:16:11 +010028
Paul Bakker407a0da2013-06-27 14:29:21 +020029#ifdef __cplusplus
30extern "C" {
31#endif
32
Ron Eldorb2aacec2017-05-18 16:53:08 +030033#if !defined(MBEDTLS_SHA1_ALT)
34// Regular implementation
35//
36
Paul Bakker5121ce52009-01-03 21:22:43 +000037/**
Rose Zadik44833d92018-01-26 08:41:09 +000038 * \brief The SHA-1 context structure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010039 *
40 * \warning SHA-1 is considered a weak message digest and its use
41 * constitutes a security risk. We recommend considering
42 * stronger message digests instead.
43 *
Paul Bakker5121ce52009-01-03 21:22:43 +000044 */
Gilles Peskine449bd832023-01-11 14:50:10 +010045typedef struct mbedtls_sha1_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020046 uint32_t MBEDTLS_PRIVATE(total)[2]; /*!< The number of Bytes processed. */
47 uint32_t MBEDTLS_PRIVATE(state)[5]; /*!< The intermediate digest state. */
48 unsigned char MBEDTLS_PRIVATE(buffer)[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000049}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Ron Eldorb2aacec2017-05-18 16:53:08 +030052#else /* MBEDTLS_SHA1_ALT */
53#include "sha1_alt.h"
54#endif /* MBEDTLS_SHA1_ALT */
55
Paul Bakker5121ce52009-01-03 21:22:43 +000056/**
Rose Zadik44833d92018-01-26 08:41:09 +000057 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020058 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010059 * \warning SHA-1 is considered a weak message digest and its use
60 * constitutes a security risk. We recommend considering
61 * stronger message digests instead.
62 *
Rose Zadik82741422018-03-27 12:49:48 +010063 * \param ctx The SHA-1 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050064 * This must not be \c NULL.
Rose Zadik82741422018-03-27 12:49:48 +010065 *
Paul Bakker5b4af392014-06-26 12:09:34 +020066 */
Gilles Peskine449bd832023-01-11 14:50:10 +010067void mbedtls_sha1_init(mbedtls_sha1_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020068
69/**
Rose Zadik44833d92018-01-26 08:41:09 +000070 * \brief This function clears 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 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050076 * \param ctx The SHA-1 context to clear. This may be \c NULL,
77 * in which case this function does nothing. If it is
78 * not \c NULL, it must point to an initialized
79 * SHA-1 context.
Rose Zadik82741422018-03-27 12:49:48 +010080 *
Paul Bakker5b4af392014-06-26 12:09:34 +020081 */
Gilles Peskine449bd832023-01-11 14:50:10 +010082void mbedtls_sha1_free(mbedtls_sha1_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020083
84/**
Rose Zadik44833d92018-01-26 08:41:09 +000085 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020086 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010087 * \warning SHA-1 is considered a weak message digest and its use
88 * constitutes a security risk. We recommend considering
89 * stronger message digests instead.
90 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050091 * \param dst The SHA-1 context to clone to. This must be initialized.
92 * \param src The SHA-1 context to clone from. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +010093 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020094 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
96 const mbedtls_sha1_context *src);
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020097
98/**
Rose Zadik44833d92018-01-26 08:41:09 +000099 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100101 * \warning SHA-1 is considered a weak message digest and its use
102 * constitutes a security risk. We recommend considering
103 * stronger message digests instead.
104 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500105 * \param ctx The SHA-1 context to initialize. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +0100106 *
107 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500108 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100109 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111int mbedtls_sha1_starts(mbedtls_sha1_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113/**
Rose Zadik44833d92018-01-26 08:41:09 +0000114 * \brief This function feeds an input buffer into an ongoing SHA-1
115 * 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. This must be initialized
122 * and have a hash operation started.
Rose Zadik82741422018-03-27 12:49:48 +0100123 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * This must be a readable buffer of length \p ilen Bytes.
125 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100126 *
127 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500128 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130int mbedtls_sha1_update(mbedtls_sha1_context *ctx,
131 const unsigned char *input,
132 size_t ilen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134/**
Rose Zadik44833d92018-01-26 08:41:09 +0000135 * \brief This function finishes the SHA-1 operation, and writes
136 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100138 * \warning SHA-1 is considered a weak message digest and its use
139 * constitutes a security risk. We recommend considering
140 * stronger message digests instead.
141 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 * \param ctx The SHA-1 context to use. This must be initialized and
143 * have a hash operation started.
144 * \param output The SHA-1 checksum result. This must be a writable
145 * buffer of length \c 20 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100146 *
147 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500148 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100150int mbedtls_sha1_finish(mbedtls_sha1_context *ctx,
151 unsigned char output[20]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100153/**
Rose Zadik82741422018-03-27 12:49:48 +0100154 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100155 *
156 * \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.
161 * \param data The data block being processed. This must be a
162 * readable buffer of length \c 64 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100163 *
164 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500165 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100166 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100167 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
169 const unsigned char data[64]);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100170
Paul Bakker5121ce52009-01-03 21:22:43 +0000171/**
Rose Zadik44833d92018-01-26 08:41:09 +0000172 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 *
Rose Zadik44833d92018-01-26 08:41:09 +0000174 * The function allocates the context, performs the
175 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100176 *
Rose Zadik44833d92018-01-26 08:41:09 +0000177 * The SHA-1 result is calculated as
178 * output = SHA-1(input buffer).
179 *
Rose Zadik82741422018-03-27 12:49:48 +0100180 * \warning SHA-1 is considered a weak message digest and its use
181 * constitutes a security risk. We recommend considering
182 * stronger message digests instead.
183 *
Rose Zadik44833d92018-01-26 08:41:09 +0000184 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500185 * This must be a readable buffer of length \p ilen Bytes.
186 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000187 * \param output The SHA-1 checksum result.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500188 * This must be a writable buffer of length \c 20 Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000189 *
Rose Zadik82741422018-03-27 12:49:48 +0100190 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 * \return A negative error code on failure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100192 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194int mbedtls_sha1(const unsigned char *input,
195 size_t ilen,
196 unsigned char output[20]);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100197
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500198#if defined(MBEDTLS_SELF_TEST)
199
Paul Bakker5121ce52009-01-03 21:22:43 +0000200/**
Rose Zadik44833d92018-01-26 08:41:09 +0000201 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100203 * \warning SHA-1 is considered a weak message digest and its use
204 * constitutes a security risk. We recommend considering
205 * stronger message digests instead.
206 *
Rose Zadik82741422018-03-27 12:49:48 +0100207 * \return \c 0 on success.
208 * \return \c 1 on failure.
209 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211int mbedtls_sha1_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500213#endif /* MBEDTLS_SELF_TEST */
214
Paul Bakker5121ce52009-01-03 21:22:43 +0000215#ifdef __cplusplus
216}
217#endif
218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#endif /* mbedtls_sha1.h */