blob: 01c02a31035dacebee0ce50a4f7fcd8387dec5c0 [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/*
Rose Zadik44833d92018-01-26 08:41:09 +000011 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
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 * **********
52 *
Rose Zadik44833d92018-01-26 08:41:09 +000053 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#ifndef MBEDTLS_SHA1_H
56#define MBEDTLS_SHA1_H
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020059#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020060#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020062#endif
Paul Bakker90995b52013-06-24 19:20:35 +020063
Rich Evans00ab4702015-02-06 13:43:58 +000064#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020065#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000066
Gilles Peskinea381fe82018-01-23 18:16:11 +010067#define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035 /**< SHA-1 hardware accelerator failed */
68
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if !defined(MBEDTLS_SHA1_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020070// Regular implementation
71//
72
Paul Bakker407a0da2013-06-27 14:29:21 +020073#ifdef __cplusplus
74extern "C" {
75#endif
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/**
Rose Zadik44833d92018-01-26 08:41:09 +000078 * \brief The SHA-1 context structure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010079 *
80 * \warning SHA-1 is considered a weak message digest and its use
81 * constitutes a security risk. We recommend considering
82 * stronger message digests instead.
83 *
Paul Bakker5121ce52009-01-03 21:22:43 +000084 */
85typedef struct
86{
Rose Zadik44833d92018-01-26 08:41:09 +000087 uint32_t total[2]; /*!< The number of Bytes processed. */
88 uint32_t state[5]; /*!< The intermediate digest state. */
89 unsigned char buffer[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000090}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Paul Bakker5121ce52009-01-03 21:22:43 +000093/**
Rose Zadik44833d92018-01-26 08:41:09 +000094 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020095 *
Rose Zadik44833d92018-01-26 08:41:09 +000096 * \param ctx The SHA-1 context to initialize.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010097 *
98 * \warning SHA-1 is considered a weak message digest and its use
99 * constitutes a security risk. We recommend considering
100 * stronger message digests instead.
101 *
Paul Bakker5b4af392014-06-26 12:09:34 +0200102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200104
105/**
Rose Zadik44833d92018-01-26 08:41:09 +0000106 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +0200107 *
Rose Zadik44833d92018-01-26 08:41:09 +0000108 * \param ctx The SHA-1 context to clear.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100109 *
110 * \warning SHA-1 is considered a weak message digest and its use
111 * constitutes a security risk. We recommend considering
112 * stronger message digests instead.
113 *
Paul Bakker5b4af392014-06-26 12:09:34 +0200114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200116
117/**
Rose Zadik44833d92018-01-26 08:41:09 +0000118 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200119 *
Rose Zadik44833d92018-01-26 08:41:09 +0000120 * \param dst The destination context.
121 * \param src The context to clone.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100122 *
123 * \warning SHA-1 is considered a weak message digest and its use
124 * constitutes a security risk. We recommend considering
125 * stronger message digests instead.
126 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200127 */
128void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
129 const mbedtls_sha1_context *src );
130
131/**
Rose Zadik44833d92018-01-26 08:41:09 +0000132 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 *
Rose Zadik44833d92018-01-26 08:41:09 +0000134 * \param ctx The context to initialize.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100135 *
Rose Zadik44833d92018-01-26 08:41:09 +0000136 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100137 *
138 * \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 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100143int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145/**
Rose Zadik44833d92018-01-26 08:41:09 +0000146 * \brief This function feeds an input buffer into an ongoing SHA-1
147 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 *
Rose Zadik44833d92018-01-26 08:41:09 +0000149 * \param ctx The SHA-1 context.
150 * \param input The buffer holding the input data.
151 * \param ilen The length of the input data.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100152 *
Rose Zadik44833d92018-01-26 08:41:09 +0000153 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100154 *
155 * \warning SHA-1 is considered a weak message digest and its use
156 * constitutes a security risk. We recommend considering
157 * stronger message digests instead.
158 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100160int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100161 const unsigned char *input,
162 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
164/**
Rose Zadik44833d92018-01-26 08:41:09 +0000165 * \brief This function finishes the SHA-1 operation, and writes
166 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 *
Rose Zadik44833d92018-01-26 08:41:09 +0000168 * \param ctx The SHA-1 context.
169 * \param output The SHA-1 checksum result.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100170 *
Rose Zadik44833d92018-01-26 08:41:09 +0000171 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100172 *
173 * \warning SHA-1 is considered a weak message digest and its use
174 * constitutes a security risk. We recommend considering
175 * stronger message digests instead.
176 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100178int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100179 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000180
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100181/**
182 * \brief SHA-1 process data block (internal use only)
183 *
184 * \param ctx SHA-1 context
Rose Zadik44833d92018-01-26 08:41:09 +0000185 * \param data The data block being processed.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100186 *
Rose Zadik44833d92018-01-26 08:41:09 +0000187 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100188 *
189 * \warning SHA-1 is considered a weak message digest and its use
190 * constitutes a security risk. We recommend considering
191 * stronger message digests instead.
192 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100193 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100194int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
195 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100196
197#if !defined(MBEDTLS_DEPRECATED_REMOVED)
198#if defined(MBEDTLS_DEPRECATED_WARNING)
199#define MBEDTLS_DEPRECATED __attribute__((deprecated))
200#else
201#define MBEDTLS_DEPRECATED
202#endif
203/**
204 * \brief SHA-1 context setup
205 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100206 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100207 *
Rose Zadik44833d92018-01-26 08:41:09 +0000208 * \param ctx The SHA-1 context to be initialized.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100209 *
210 * \warning SHA-1 is considered a weak message digest and its use
211 * constitutes a security risk. We recommend considering
212 * stronger message digests instead.
213 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100214 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000215MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100216
217/**
218 * \brief SHA-1 process buffer
219 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100220 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100221 *
Rose Zadik44833d92018-01-26 08:41:09 +0000222 * \param ctx The SHA-1 context.
223 * \param input The buffer holding the input data.
224 * \param ilen The length of the input data.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100225 *
226 * \warning SHA-1 is considered a weak message digest and its use
227 * constitutes a security risk. We recommend considering
228 * stronger message digests instead.
229 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100230 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000231MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
232 const unsigned char *input,
233 size_t ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100234
235/**
236 * \brief SHA-1 final digest
237 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100238 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100239 *
Rose Zadik44833d92018-01-26 08:41:09 +0000240 * \param ctx The SHA-1 context.
241 * \param output The SHA-1 checksum result.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100242 *
243 * \warning SHA-1 is considered a weak message digest and its use
244 * constitutes a security risk. We recommend considering
245 * stronger message digests instead.
246 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100247 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000248MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
249 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100250
251/**
252 * \brief SHA-1 process data block (internal use only)
253 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100254 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100255 *
Rose Zadik44833d92018-01-26 08:41:09 +0000256 * \param ctx The SHA-1 context.
257 * \param data The data block being processed.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100258 *
259 * \warning SHA-1 is considered a weak message digest and its use
260 * constitutes a security risk. We recommend considering
261 * stronger message digests instead.
262 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100263 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000264MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
265 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100266
267#undef MBEDTLS_DEPRECATED
268#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200269
270#ifdef __cplusplus
271}
272#endif
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#else /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200275#include "sha1_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#endif /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200277
278#ifdef __cplusplus
279extern "C" {
280#endif
281
Paul Bakker5121ce52009-01-03 21:22:43 +0000282/**
Rose Zadik44833d92018-01-26 08:41:09 +0000283 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 *
Rose Zadik44833d92018-01-26 08:41:09 +0000285 * The function allocates the context, performs the
286 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100287 *
Rose Zadik44833d92018-01-26 08:41:09 +0000288 * The SHA-1 result is calculated as
289 * output = SHA-1(input buffer).
290 *
291 * \param input The buffer holding the input data.
292 * \param ilen The length of the input data.
293 * \param output The SHA-1 checksum result.
294 *
295 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100296 *
297 * \warning SHA-1 is considered a weak message digest and its use
298 * constitutes a security risk. We recommend considering
299 * stronger message digests instead.
300 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100302int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100303 size_t ilen,
304 unsigned char output[20] );
305
306#if !defined(MBEDTLS_DEPRECATED_REMOVED)
307#if defined(MBEDTLS_DEPRECATED_WARNING)
308#define MBEDTLS_DEPRECATED __attribute__((deprecated))
309#else
310#define MBEDTLS_DEPRECATED
311#endif
312/**
313 * \brief Output = SHA-1( input buffer )
314 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100315 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100316 *
Rose Zadik44833d92018-01-26 08:41:09 +0000317 * \param input The buffer holding the input data.
318 * \param ilen The length of the input data.
319 * \param output The SHA-1 checksum result.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100320 *
321 * \warning SHA-1 is considered a weak message digest and its use
322 * constitutes a security risk. We recommend considering
323 * stronger message digests instead.
324 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000326MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input,
327 size_t ilen,
328 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100329
330#undef MBEDTLS_DEPRECATED
331#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000332
333/**
Rose Zadik44833d92018-01-26 08:41:09 +0000334 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000335 *
Rose Zadik44833d92018-01-26 08:41:09 +0000336 * \return \c 0 on success, or \c 1 on failure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100337 *
338 * \warning SHA-1 is considered a weak message digest and its use
339 * constitutes a security risk. We recommend considering
340 * stronger message digests instead.
341 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000344
Paul Bakker5121ce52009-01-03 21:22:43 +0000345#ifdef __cplusplus
346}
347#endif
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#endif /* mbedtls_sha1.h */