blob: 5fd02d3c0c6aa57d508247ad1d461bd01018f2bc [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 * SPDX-License-Identifier: Apache-2.0
13 *
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000025 *
Rose Zadik44833d92018-01-26 08:41:09 +000026 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#ifndef MBEDTLS_SHA1_H
29#define MBEDTLS_SHA1_H
Paul Bakker5121ce52009-01-03 21:22:43 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020032#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#endif
Paul Bakker90995b52013-06-24 19:20:35 +020036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020038#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000039
Gilles Peskinea381fe82018-01-23 18:16:11 +010040#define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035 /**< SHA-1 hardware accelerator failed */
41
Paul Bakker407a0da2013-06-27 14:29:21 +020042#ifdef __cplusplus
43extern "C" {
44#endif
45
Ron Eldorb2aacec2017-05-18 16:53:08 +030046#if !defined(MBEDTLS_SHA1_ALT)
47// Regular implementation
48//
49
Paul Bakker5121ce52009-01-03 21:22:43 +000050/**
Rose Zadik44833d92018-01-26 08:41:09 +000051 * \brief The SHA-1 context structure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010052 *
53 * \warning SHA-1 is considered a weak message digest and its use
54 * constitutes a security risk. We recommend considering
55 * stronger message digests instead.
56 *
Paul Bakker5121ce52009-01-03 21:22:43 +000057 */
58typedef struct
59{
Rose Zadik44833d92018-01-26 08:41:09 +000060 uint32_t total[2]; /*!< The number of Bytes processed. */
61 uint32_t state[5]; /*!< The intermediate digest state. */
62 unsigned char buffer[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Ron Eldorb2aacec2017-05-18 16:53:08 +030066#else /* MBEDTLS_SHA1_ALT */
67#include "sha1_alt.h"
68#endif /* MBEDTLS_SHA1_ALT */
69
Paul Bakker5121ce52009-01-03 21:22:43 +000070/**
Rose Zadik44833d92018-01-26 08:41:09 +000071 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020072 *
Rose Zadik44833d92018-01-26 08:41:09 +000073 * \param ctx The SHA-1 context to initialize.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010074 *
75 * \warning SHA-1 is considered a weak message digest and its use
76 * constitutes a security risk. We recommend considering
77 * stronger message digests instead.
78 *
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 *
Rose Zadik44833d92018-01-26 08:41:09 +000085 * \param ctx The SHA-1 context to clear.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010086 *
87 * \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 *
Paul Bakker5b4af392014-06-26 12:09:34 +020091 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020093
94/**
Rose Zadik44833d92018-01-26 08:41:09 +000095 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020096 *
Rose Zadik44833d92018-01-26 08:41:09 +000097 * \param dst The destination context.
98 * \param src The context to clone.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010099 *
100 * \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 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200104 */
105void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
106 const mbedtls_sha1_context *src );
107
108/**
Rose Zadik44833d92018-01-26 08:41:09 +0000109 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 *
Rose Zadik44833d92018-01-26 08:41:09 +0000111 * \param ctx The context to initialize.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100112 *
Rose Zadik44833d92018-01-26 08:41:09 +0000113 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100114 *
115 * \warning SHA-1 is considered a weak message digest and its use
116 * constitutes a security risk. We recommend considering
117 * stronger message digests instead.
118 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100120int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121
122/**
Rose Zadik44833d92018-01-26 08:41:09 +0000123 * \brief This function feeds an input buffer into an ongoing SHA-1
124 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 *
Rose Zadik44833d92018-01-26 08:41:09 +0000126 * \param ctx The SHA-1 context.
127 * \param input The buffer holding the input data.
128 * \param ilen The length of the input data.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100129 *
Rose Zadik44833d92018-01-26 08:41:09 +0000130 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100131 *
132 * \warning SHA-1 is considered a weak message digest and its use
133 * constitutes a security risk. We recommend considering
134 * stronger message digests instead.
135 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100137int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100138 const unsigned char *input,
139 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141/**
Rose Zadik44833d92018-01-26 08:41:09 +0000142 * \brief This function finishes the SHA-1 operation, and writes
143 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 *
Rose Zadik44833d92018-01-26 08:41:09 +0000145 * \param ctx The SHA-1 context.
146 * \param output The SHA-1 checksum result.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100147 *
Rose Zadik44833d92018-01-26 08:41:09 +0000148 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100149 *
150 * \warning SHA-1 is considered a weak message digest and its use
151 * constitutes a security risk. We recommend considering
152 * stronger message digests instead.
153 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100155int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100156 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100158/**
159 * \brief SHA-1 process data block (internal use only)
160 *
161 * \param ctx SHA-1 context
Rose Zadik44833d92018-01-26 08:41:09 +0000162 * \param data The data block being processed.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100163 *
Rose Zadik44833d92018-01-26 08:41:09 +0000164 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100165 *
166 * \warning SHA-1 is considered a weak message digest and its use
167 * constitutes a security risk. We recommend considering
168 * stronger message digests instead.
169 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100170 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100171int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
172 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100173
174#if !defined(MBEDTLS_DEPRECATED_REMOVED)
175#if defined(MBEDTLS_DEPRECATED_WARNING)
176#define MBEDTLS_DEPRECATED __attribute__((deprecated))
177#else
178#define MBEDTLS_DEPRECATED
179#endif
180/**
181 * \brief SHA-1 context setup
182 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100183 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100184 *
Rose Zadik44833d92018-01-26 08:41:09 +0000185 * \param ctx The SHA-1 context to be initialized.
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 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000192MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100193
194/**
195 * \brief SHA-1 process buffer
196 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100197 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100198 *
Rose Zadik44833d92018-01-26 08:41:09 +0000199 * \param ctx The SHA-1 context.
200 * \param input The buffer holding the input data.
201 * \param ilen The length of the input data.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100202 *
203 * \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 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100207 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000208MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
209 const unsigned char *input,
210 size_t ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100211
212/**
213 * \brief SHA-1 final digest
214 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100215 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100216 *
Rose Zadik44833d92018-01-26 08:41:09 +0000217 * \param ctx The SHA-1 context.
218 * \param output The SHA-1 checksum result.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100219 *
220 * \warning SHA-1 is considered a weak message digest and its use
221 * constitutes a security risk. We recommend considering
222 * stronger message digests instead.
223 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100224 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000225MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
226 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100227
228/**
229 * \brief SHA-1 process data block (internal use only)
230 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100231 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100232 *
Rose Zadik44833d92018-01-26 08:41:09 +0000233 * \param ctx The SHA-1 context.
234 * \param data The data block being processed.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100235 *
236 * \warning SHA-1 is considered a weak message digest and its use
237 * constitutes a security risk. We recommend considering
238 * stronger message digests instead.
239 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100240 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000241MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
242 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100243
244#undef MBEDTLS_DEPRECATED
245#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200246
Paul Bakker5121ce52009-01-03 21:22:43 +0000247/**
Rose Zadik44833d92018-01-26 08:41:09 +0000248 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 *
Rose Zadik44833d92018-01-26 08:41:09 +0000250 * The function allocates the context, performs the
251 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100252 *
Rose Zadik44833d92018-01-26 08:41:09 +0000253 * The SHA-1 result is calculated as
254 * output = SHA-1(input buffer).
255 *
256 * \param input The buffer holding the input data.
257 * \param ilen The length of the input data.
258 * \param output The SHA-1 checksum result.
259 *
260 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100261 *
262 * \warning SHA-1 is considered a weak message digest and its use
263 * constitutes a security risk. We recommend considering
264 * stronger message digests instead.
265 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100267int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100268 size_t ilen,
269 unsigned char output[20] );
270
271#if !defined(MBEDTLS_DEPRECATED_REMOVED)
272#if defined(MBEDTLS_DEPRECATED_WARNING)
273#define MBEDTLS_DEPRECATED __attribute__((deprecated))
274#else
275#define MBEDTLS_DEPRECATED
276#endif
277/**
278 * \brief Output = SHA-1( input buffer )
279 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100280 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100281 *
Rose Zadik44833d92018-01-26 08:41:09 +0000282 * \param input The buffer holding the input data.
283 * \param ilen The length of the input data.
284 * \param output The SHA-1 checksum result.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100285 *
286 * \warning SHA-1 is considered a weak message digest and its use
287 * constitutes a security risk. We recommend considering
288 * stronger message digests instead.
289 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000291MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input,
292 size_t ilen,
293 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100294
295#undef MBEDTLS_DEPRECATED
296#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
298/**
Rose Zadik44833d92018-01-26 08:41:09 +0000299 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 *
Rose Zadik44833d92018-01-26 08:41:09 +0000301 * \return \c 0 on success, or \c 1 on failure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100302 *
303 * \warning SHA-1 is considered a weak message digest and its use
304 * constitutes a security risk. We recommend considering
305 * stronger message digests instead.
306 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
Paul Bakker5121ce52009-01-03 21:22:43 +0000310#ifdef __cplusplus
311}
312#endif
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314#endif /* mbedtls_sha1.h */