blob: 4ae8ad60770046f9982068dd9f624d426b87f4cd [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 Zadik44833d92018-01-26 08:41:09 +00004 * \brief The SHA-1 cryptographic hash function.
Hanno Beckerbbca8c52017-09-25 14:53:51 +01005 *
6 * \warning SHA-1 is considered a weak message digest and its use constitutes
7 * a security risk. We recommend considering stronger message
8 * digests instead.
Darryl Greena40a1012018-01-05 15:33:17 +00009 */
10/*
Bence Szépkúti44bfbe32020-08-19 16:54:51 +020011 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020012 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
13 *
14 * This file is provided under the Apache License 2.0, or the
15 * GNU General Public License v2.0 or later.
16 *
17 * **********
18 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020019 *
20 * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 * not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000031 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020032 * **********
33 *
34 * **********
35 * GNU General Public License v2.0 or later:
36 *
37 * This program is free software; you can redistribute it and/or modify
38 * it under the terms of the GNU General Public License as published by
39 * the Free Software Foundation; either version 2 of the License, or
40 * (at your option) any later version.
41 *
42 * This program is distributed in the hope that it will be useful,
43 * but WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 * GNU General Public License for more details.
46 *
47 * You should have received a copy of the GNU General Public License along
48 * with this program; if not, write to the Free Software Foundation, Inc.,
49 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
50 *
51 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000052 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#ifndef MBEDTLS_SHA1_H
54#define MBEDTLS_SHA1_H
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020057#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020060#endif
Paul Bakker90995b52013-06-24 19:20:35 +020061
Rich Evans00ab4702015-02-06 13:43:58 +000062#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020063#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000064
Gilles Peskinea381fe82018-01-23 18:16:11 +010065#define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035 /**< SHA-1 hardware accelerator failed */
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if !defined(MBEDTLS_SHA1_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020068// Regular implementation
69//
70
Paul Bakker407a0da2013-06-27 14:29:21 +020071#ifdef __cplusplus
72extern "C" {
73#endif
74
Paul Bakker5121ce52009-01-03 21:22:43 +000075/**
Rose Zadik44833d92018-01-26 08:41:09 +000076 * \brief The SHA-1 context structure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010077 *
78 * \warning SHA-1 is considered a weak message digest and its use
79 * constitutes a security risk. We recommend considering
80 * stronger message digests instead.
81 *
Paul Bakker5121ce52009-01-03 21:22:43 +000082 */
83typedef struct
84{
Rose Zadik44833d92018-01-26 08:41:09 +000085 uint32_t total[2]; /*!< The number of Bytes processed. */
86 uint32_t state[5]; /*!< The intermediate digest state. */
87 unsigned char buffer[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000088}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Paul Bakker5121ce52009-01-03 21:22:43 +000091/**
Rose Zadik44833d92018-01-26 08:41:09 +000092 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020093 *
Rose Zadik44833d92018-01-26 08:41:09 +000094 * \param ctx The SHA-1 context to initialize.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010095 *
96 * \warning SHA-1 is considered a weak message digest and its use
97 * constitutes a security risk. We recommend considering
98 * stronger message digests instead.
99 *
Paul Bakker5b4af392014-06-26 12:09:34 +0200100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200102
103/**
Rose Zadik44833d92018-01-26 08:41:09 +0000104 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +0200105 *
Rose Zadik44833d92018-01-26 08:41:09 +0000106 * \param ctx The SHA-1 context to clear.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100107 *
108 * \warning SHA-1 is considered a weak message digest and its use
109 * constitutes a security risk. We recommend considering
110 * stronger message digests instead.
111 *
Paul Bakker5b4af392014-06-26 12:09:34 +0200112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200114
115/**
Rose Zadik44833d92018-01-26 08:41:09 +0000116 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200117 *
Rose Zadik44833d92018-01-26 08:41:09 +0000118 * \param dst The destination context.
119 * \param src The context to clone.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100120 *
121 * \warning SHA-1 is considered a weak message digest and its use
122 * constitutes a security risk. We recommend considering
123 * stronger message digests instead.
124 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200125 */
126void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
127 const mbedtls_sha1_context *src );
128
129/**
Rose Zadik44833d92018-01-26 08:41:09 +0000130 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 *
Rose Zadik44833d92018-01-26 08:41:09 +0000132 * \param ctx The context to initialize.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100133 *
Rose Zadik44833d92018-01-26 08:41:09 +0000134 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100135 *
136 * \warning SHA-1 is considered a weak message digest and its use
137 * constitutes a security risk. We recommend considering
138 * stronger message digests instead.
139 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100141int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143/**
Rose Zadik44833d92018-01-26 08:41:09 +0000144 * \brief This function feeds an input buffer into an ongoing SHA-1
145 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 *
Rose Zadik44833d92018-01-26 08:41:09 +0000147 * \param ctx The SHA-1 context.
148 * \param input The buffer holding the input data.
149 * \param ilen The length of the input data.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100150 *
Rose Zadik44833d92018-01-26 08:41:09 +0000151 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100152 *
153 * \warning SHA-1 is considered a weak message digest and its use
154 * constitutes a security risk. We recommend considering
155 * stronger message digests instead.
156 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100158int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100159 const unsigned char *input,
160 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
162/**
Rose Zadik44833d92018-01-26 08:41:09 +0000163 * \brief This function finishes the SHA-1 operation, and writes
164 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 *
Rose Zadik44833d92018-01-26 08:41:09 +0000166 * \param ctx The SHA-1 context.
167 * \param output The SHA-1 checksum result.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100168 *
Rose Zadik44833d92018-01-26 08:41:09 +0000169 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100170 *
171 * \warning SHA-1 is considered a weak message digest and its use
172 * constitutes a security risk. We recommend considering
173 * stronger message digests instead.
174 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100176int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100177 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100179/**
180 * \brief SHA-1 process data block (internal use only)
181 *
182 * \param ctx SHA-1 context
Rose Zadik44833d92018-01-26 08:41:09 +0000183 * \param data The data block being processed.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100184 *
Rose Zadik44833d92018-01-26 08:41:09 +0000185 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100186 *
187 * \warning SHA-1 is considered a weak message digest and its use
188 * constitutes a security risk. We recommend considering
189 * stronger message digests instead.
190 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100191 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100192int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
193 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100194
195#if !defined(MBEDTLS_DEPRECATED_REMOVED)
196#if defined(MBEDTLS_DEPRECATED_WARNING)
197#define MBEDTLS_DEPRECATED __attribute__((deprecated))
198#else
199#define MBEDTLS_DEPRECATED
200#endif
201/**
202 * \brief SHA-1 context setup
203 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100204 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100205 *
Rose Zadik44833d92018-01-26 08:41:09 +0000206 * \param ctx The SHA-1 context to be initialized.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100207 *
208 * \warning SHA-1 is considered a weak message digest and its use
209 * constitutes a security risk. We recommend considering
210 * stronger message digests instead.
211 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100212 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000213MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100214
215/**
216 * \brief SHA-1 process buffer
217 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100218 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100219 *
Rose Zadik44833d92018-01-26 08:41:09 +0000220 * \param ctx The SHA-1 context.
221 * \param input The buffer holding the input data.
222 * \param ilen The length of the input data.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100223 *
224 * \warning SHA-1 is considered a weak message digest and its use
225 * constitutes a security risk. We recommend considering
226 * stronger message digests instead.
227 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100228 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000229MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
230 const unsigned char *input,
231 size_t ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100232
233/**
234 * \brief SHA-1 final digest
235 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100236 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100237 *
Rose Zadik44833d92018-01-26 08:41:09 +0000238 * \param ctx The SHA-1 context.
239 * \param output The SHA-1 checksum result.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100240 *
241 * \warning SHA-1 is considered a weak message digest and its use
242 * constitutes a security risk. We recommend considering
243 * stronger message digests instead.
244 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100245 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000246MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
247 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100248
249/**
250 * \brief SHA-1 process data block (internal use only)
251 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100252 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100253 *
Rose Zadik44833d92018-01-26 08:41:09 +0000254 * \param ctx The SHA-1 context.
255 * \param data The data block being processed.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100256 *
257 * \warning SHA-1 is considered a weak message digest and its use
258 * constitutes a security risk. We recommend considering
259 * stronger message digests instead.
260 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100261 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000262MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
263 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100264
265#undef MBEDTLS_DEPRECATED
266#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200267
268#ifdef __cplusplus
269}
270#endif
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#else /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200273#include "sha1_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#endif /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200275
276#ifdef __cplusplus
277extern "C" {
278#endif
279
Paul Bakker5121ce52009-01-03 21:22:43 +0000280/**
Rose Zadik44833d92018-01-26 08:41:09 +0000281 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 *
Rose Zadik44833d92018-01-26 08:41:09 +0000283 * The function allocates the context, performs the
284 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100285 *
Rose Zadik44833d92018-01-26 08:41:09 +0000286 * The SHA-1 result is calculated as
287 * output = SHA-1(input buffer).
288 *
289 * \param input The buffer holding the input data.
290 * \param ilen The length of the input data.
291 * \param output The SHA-1 checksum result.
292 *
293 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100294 *
295 * \warning SHA-1 is considered a weak message digest and its use
296 * constitutes a security risk. We recommend considering
297 * stronger message digests instead.
298 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100300int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100301 size_t ilen,
302 unsigned char output[20] );
303
304#if !defined(MBEDTLS_DEPRECATED_REMOVED)
305#if defined(MBEDTLS_DEPRECATED_WARNING)
306#define MBEDTLS_DEPRECATED __attribute__((deprecated))
307#else
308#define MBEDTLS_DEPRECATED
309#endif
310/**
311 * \brief Output = SHA-1( input buffer )
312 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100313 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100314 *
Rose Zadik44833d92018-01-26 08:41:09 +0000315 * \param input The buffer holding the input data.
316 * \param ilen The length of the input data.
317 * \param output The SHA-1 checksum result.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100318 *
319 * \warning SHA-1 is considered a weak message digest and its use
320 * constitutes a security risk. We recommend considering
321 * stronger message digests instead.
322 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000323 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000324MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input,
325 size_t ilen,
326 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100327
328#undef MBEDTLS_DEPRECATED
329#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000330
331/**
Rose Zadik44833d92018-01-26 08:41:09 +0000332 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 *
Rose Zadik44833d92018-01-26 08:41:09 +0000334 * \return \c 0 on success, or \c 1 on failure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100335 *
336 * \warning SHA-1 is considered a weak message digest and its use
337 * constitutes a security risk. We recommend considering
338 * stronger message digests instead.
339 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000342
Paul Bakker5121ce52009-01-03 21:22:43 +0000343#ifdef __cplusplus
344}
345#endif
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#endif /* mbedtls_sha1.h */