blob: 4b31c02504ae1656ada37969d8efd7a1d138362b [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
Paul Bakker5121ce52009-01-03 21:22:43 +000033/**
Rose Zadik44833d92018-01-26 08:41:09 +000034 * \brief The SHA-1 context structure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010035 *
36 * \warning SHA-1 is considered a weak message digest and its use
37 * constitutes a security risk. We recommend considering
38 * stronger message digests instead.
39 *
Paul Bakker5121ce52009-01-03 21:22:43 +000040 */
Gilles Peskine449bd832023-01-11 14:50:10 +010041typedef struct mbedtls_sha1_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020042 uint32_t MBEDTLS_PRIVATE(total)[2]; /*!< The number of Bytes processed. */
43 uint32_t MBEDTLS_PRIVATE(state)[5]; /*!< The intermediate digest state. */
44 unsigned char MBEDTLS_PRIVATE(buffer)[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Paul Bakker5121ce52009-01-03 21:22:43 +000048/**
Rose Zadik44833d92018-01-26 08:41:09 +000049 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020050 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010051 * \warning SHA-1 is considered a weak message digest and its use
52 * constitutes a security risk. We recommend considering
53 * stronger message digests instead.
54 *
Rose Zadik82741422018-03-27 12:49:48 +010055 * \param ctx The SHA-1 context to initialize.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050056 * This must not be \c NULL.
Rose Zadik82741422018-03-27 12:49:48 +010057 *
Paul Bakker5b4af392014-06-26 12:09:34 +020058 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059void mbedtls_sha1_init(mbedtls_sha1_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020060
61/**
Rose Zadik44833d92018-01-26 08:41:09 +000062 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020063 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010064 * \warning SHA-1 is considered a weak message digest and its use
65 * constitutes a security risk. We recommend considering
66 * stronger message digests instead.
67 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050068 * \param ctx The SHA-1 context to clear. This may be \c NULL,
69 * in which case this function does nothing. If it is
70 * not \c NULL, it must point to an initialized
71 * SHA-1 context.
Rose Zadik82741422018-03-27 12:49:48 +010072 *
Paul Bakker5b4af392014-06-26 12:09:34 +020073 */
Gilles Peskine449bd832023-01-11 14:50:10 +010074void mbedtls_sha1_free(mbedtls_sha1_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020075
76/**
Rose Zadik44833d92018-01-26 08:41:09 +000077 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020078 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010079 * \warning SHA-1 is considered a weak message digest and its use
80 * constitutes a security risk. We recommend considering
81 * stronger message digests instead.
82 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050083 * \param dst The SHA-1 context to clone to. This must be initialized.
84 * \param src The SHA-1 context to clone from. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +010085 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020086 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087void mbedtls_sha1_clone(mbedtls_sha1_context *dst,
88 const mbedtls_sha1_context *src);
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020089
90/**
Rose Zadik44833d92018-01-26 08:41:09 +000091 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000092 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010093 * \warning SHA-1 is considered a weak message digest and its use
94 * constitutes a security risk. We recommend considering
95 * stronger message digests instead.
96 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 * \param ctx The SHA-1 context to initialize. This must be initialized.
Rose Zadik82741422018-03-27 12:49:48 +010098 *
99 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500100 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100101 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103int mbedtls_sha1_starts(mbedtls_sha1_context *ctx);
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105/**
Rose Zadik44833d92018-01-26 08:41:09 +0000106 * \brief This function feeds an input buffer into an ongoing SHA-1
107 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100109 * \warning SHA-1 is considered a weak message digest and its use
110 * constitutes a security risk. We recommend considering
111 * stronger message digests instead.
112 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500113 * \param ctx The SHA-1 context. This must be initialized
114 * and have a hash operation started.
Rose Zadik82741422018-03-27 12:49:48 +0100115 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500116 * This must be a readable buffer of length \p ilen Bytes.
117 * \param ilen The length of the input data \p input in Bytes.
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.
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122int mbedtls_sha1_update(mbedtls_sha1_context *ctx,
123 const unsigned char *input,
124 size_t ilen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
126/**
Rose Zadik44833d92018-01-26 08:41:09 +0000127 * \brief This function finishes the SHA-1 operation, and writes
128 * the result to the output buffer.
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 to use. This must be initialized and
135 * have a hash operation started.
136 * \param output The SHA-1 checksum result. This must be a writable
137 * buffer of length \c 20 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_finish(mbedtls_sha1_context *ctx,
143 unsigned char output[20]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100145/**
Rose Zadik82741422018-03-27 12:49:48 +0100146 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100147 *
148 * \warning SHA-1 is considered a weak message digest and its use
149 * constitutes a security risk. We recommend considering
150 * stronger message digests instead.
151 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500152 * \param ctx The SHA-1 context to use. This must be initialized.
153 * \param data The data block being processed. This must be a
154 * readable buffer of length \c 64 Bytes.
Rose Zadik82741422018-03-27 12:49:48 +0100155 *
156 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500157 * \return A negative error code on failure.
Rose Zadik82741422018-03-27 12:49:48 +0100158 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx,
161 const unsigned char data[64]);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100162
Paul Bakker5121ce52009-01-03 21:22:43 +0000163/**
Rose Zadik44833d92018-01-26 08:41:09 +0000164 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 *
Rose Zadik44833d92018-01-26 08:41:09 +0000166 * The function allocates the context, performs the
167 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100168 *
Rose Zadik44833d92018-01-26 08:41:09 +0000169 * The SHA-1 result is calculated as
170 * output = SHA-1(input buffer).
171 *
Rose Zadik82741422018-03-27 12:49:48 +0100172 * \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 *
Rose Zadik44833d92018-01-26 08:41:09 +0000176 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500177 * This must be a readable buffer of length \p ilen Bytes.
178 * \param ilen The length of the input data \p input in Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000179 * \param output The SHA-1 checksum result.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500180 * This must be a writable buffer of length \c 20 Bytes.
Rose Zadik44833d92018-01-26 08:41:09 +0000181 *
Rose Zadik82741422018-03-27 12:49:48 +0100182 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 * \return A negative error code on failure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100184 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186int mbedtls_sha1(const unsigned char *input,
187 size_t ilen,
188 unsigned char output[20]);
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100189
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500190#if defined(MBEDTLS_SELF_TEST)
191
Paul Bakker5121ce52009-01-03 21:22:43 +0000192/**
Rose Zadik44833d92018-01-26 08:41:09 +0000193 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100195 * \warning SHA-1 is considered a weak message digest and its use
196 * constitutes a security risk. We recommend considering
197 * stronger message digests instead.
198 *
Rose Zadik82741422018-03-27 12:49:48 +0100199 * \return \c 0 on success.
200 * \return \c 1 on failure.
201 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000202 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203int mbedtls_sha1_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500205#endif /* MBEDTLS_SELF_TEST */
206
Paul Bakker5121ce52009-01-03 21:22:43 +0000207#ifdef __cplusplus
208}
209#endif
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#endif /* mbedtls_sha1.h */