blob: 96da3feeeced67bbcd132ba025b64bb3f4a3a2f2 [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/*
Rose Zadik44833d92018-01-26 08:41:09 +000014 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 * SPDX-License-Identifier: Apache-2.0
16 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000028 *
Rose Zadik44833d92018-01-26 08:41:09 +000029 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#ifndef MBEDTLS_SHA1_H
32#define MBEDTLS_SHA1_H
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020035#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#endif
Paul Bakker90995b52013-06-24 19:20:35 +020039
Rich Evans00ab4702015-02-06 13:43:58 +000040#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020041#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000042
Ron Eldor9924bdc2018-10-04 10:59:13 +030043/* MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea381fe82018-01-23 18:16:11 +010044#define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035 /**< SHA-1 hardware accelerator failed */
Andres Amaya Garciaf7c43b32018-12-09 19:12:19 +000045#define MBEDTLS_ERR_SHA1_BAD_INPUT_DATA -0x0073 /**< Invalid input data. */
Gilles Peskinea381fe82018-01-23 18:16:11 +010046
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Ron Eldorb2aacec2017-05-18 16:53:08 +030051#if !defined(MBEDTLS_SHA1_ALT)
52// Regular implementation
53//
54
Paul Bakker5121ce52009-01-03 21:22:43 +000055/**
Rose Zadik44833d92018-01-26 08:41:09 +000056 * \brief The SHA-1 context structure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010057 *
58 * \warning SHA-1 is considered a weak message digest and its use
59 * constitutes a security risk. We recommend considering
60 * stronger message digests instead.
61 *
Paul Bakker5121ce52009-01-03 21:22:43 +000062 */
Dawid Drozd428cc522018-07-24 10:02:47 +020063typedef struct mbedtls_sha1_context
Paul Bakker5121ce52009-01-03 21:22:43 +000064{
Rose Zadik44833d92018-01-26 08:41:09 +000065 uint32_t total[2]; /*!< The number of Bytes processed. */
66 uint32_t state[5]; /*!< The intermediate digest state. */
67 unsigned char buffer[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000068}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Ron Eldorb2aacec2017-05-18 16:53:08 +030071#else /* MBEDTLS_SHA1_ALT */
72#include "sha1_alt.h"
73#endif /* MBEDTLS_SHA1_ALT */
74
Paul Bakker5121ce52009-01-03 21:22:43 +000075/**
Rose Zadik44833d92018-01-26 08:41:09 +000076 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020077 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010078 * \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 *
Rose Zadik82741422018-03-27 12:49:48 +010082 * \param ctx The SHA-1 context to initialize.
83 *
Paul Bakker5b4af392014-06-26 12:09:34 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020086
87/**
Rose Zadik44833d92018-01-26 08:41:09 +000088 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020089 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010090 * \warning SHA-1 is considered a weak message digest and its use
91 * constitutes a security risk. We recommend considering
92 * stronger message digests instead.
93 *
Rose Zadik82741422018-03-27 12:49:48 +010094 * \param ctx The SHA-1 context to clear.
95 *
Paul Bakker5b4af392014-06-26 12:09:34 +020096 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020098
99/**
Rose Zadik44833d92018-01-26 08:41:09 +0000100 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200101 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100102 * \warning SHA-1 is considered a weak message digest and its use
103 * constitutes a security risk. We recommend considering
104 * stronger message digests instead.
105 *
Rose Zadik92d66b82018-04-17 10:36:56 +0100106 * \param dst The SHA-1 context to clone to.
107 * \param src The SHA-1 context to clone from.
Rose Zadik82741422018-03-27 12:49:48 +0100108 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200109 */
110void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
111 const mbedtls_sha1_context *src );
112
113/**
Rose Zadik44833d92018-01-26 08:41:09 +0000114 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100116 * \warning SHA-1 is considered a weak message digest and its use
117 * constitutes a security risk. We recommend considering
118 * stronger message digests instead.
119 *
Rose Zadik92d66b82018-04-17 10:36:56 +0100120 * \param ctx The SHA-1 context to initialize.
Rose Zadik82741422018-03-27 12:49:48 +0100121 *
122 * \return \c 0 on success.
123 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100125int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127/**
Rose Zadik44833d92018-01-26 08:41:09 +0000128 * \brief This function feeds an input buffer into an ongoing SHA-1
129 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100131 * \warning SHA-1 is considered a weak message digest and its use
132 * constitutes a security risk. We recommend considering
133 * stronger message digests instead.
134 *
Rose Zadik82741422018-03-27 12:49:48 +0100135 * \param ctx The SHA-1 context.
136 * \param input The buffer holding the input data.
137 * \param ilen The length of the input data.
138 *
139 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100141int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100142 const unsigned char *input,
143 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145/**
Rose Zadik44833d92018-01-26 08:41:09 +0000146 * \brief This function finishes the SHA-1 operation, and writes
147 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100149 * \warning SHA-1 is considered a weak message digest and its use
150 * constitutes a security risk. We recommend considering
151 * stronger message digests instead.
152 *
Rose Zadik82741422018-03-27 12:49:48 +0100153 * \param ctx The SHA-1 context.
154 * \param output The SHA-1 checksum result.
155 *
156 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100158int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100159 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100161/**
Rose Zadik82741422018-03-27 12:49:48 +0100162 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100163 *
164 * \warning SHA-1 is considered a weak message digest and its use
165 * constitutes a security risk. We recommend considering
166 * stronger message digests instead.
167 *
Rose Zadik82741422018-03-27 12:49:48 +0100168 * \param ctx The SHA-1 context.
169 * \param data The data block being processed.
170 *
171 * \return \c 0 on success.
172 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100173 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100174int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
175 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100176
177#if !defined(MBEDTLS_DEPRECATED_REMOVED)
178#if defined(MBEDTLS_DEPRECATED_WARNING)
179#define MBEDTLS_DEPRECATED __attribute__((deprecated))
180#else
181#define MBEDTLS_DEPRECATED
182#endif
183/**
Rose Zadik82741422018-03-27 12:49:48 +0100184 * \brief This function starts a SHA-1 checksum calculation.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100185 *
186 * \warning SHA-1 is considered a weak message digest and its use
187 * constitutes a security risk. We recommend considering
188 * stronger message digests instead.
189 *
Rose Zadik82741422018-03-27 12:49:48 +0100190 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0.
191 *
Rose Zadik92d66b82018-04-17 10:36:56 +0100192 * \param ctx The SHA-1 context to initialize.
Rose Zadik82741422018-03-27 12:49:48 +0100193 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100194 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000195MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100196
197/**
Rose Zadik82741422018-03-27 12:49:48 +0100198 * \brief This function feeds an input buffer into an ongoing SHA-1
199 * checksum calculation.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100200 *
201 * \warning SHA-1 is considered a weak message digest and its use
202 * constitutes a security risk. We recommend considering
203 * stronger message digests instead.
204 *
Rose Zadik82741422018-03-27 12:49:48 +0100205 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0.
206 *
207 * \param ctx The SHA-1 context.
208 * \param input The buffer holding the input data.
209 * \param ilen The length of the input data.
210 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100211 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000212MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
213 const unsigned char *input,
214 size_t ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100215
216/**
Rose Zadik82741422018-03-27 12:49:48 +0100217 * \brief This function finishes the SHA-1 operation, and writes
218 * the result to the output buffer.
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 *
Rose Zadik82741422018-03-27 12:49:48 +0100224 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0.
225 *
226 * \param ctx The SHA-1 context.
227 * \param output The SHA-1 checksum result.
228 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100229 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000230MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
231 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100232
233/**
Rose Zadik82741422018-03-27 12:49:48 +0100234 * \brief SHA-1 process data block (internal use only).
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 *
Rose Zadik82741422018-03-27 12:49:48 +0100240 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0.
241 *
242 * \param ctx The SHA-1 context.
243 * \param data The data block being processed.
244 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100245 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000246MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
247 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100248
249#undef MBEDTLS_DEPRECATED
250#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200251
Paul Bakker5121ce52009-01-03 21:22:43 +0000252/**
Rose Zadik44833d92018-01-26 08:41:09 +0000253 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 *
Rose Zadik44833d92018-01-26 08:41:09 +0000255 * The function allocates the context, performs the
256 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100257 *
Rose Zadik44833d92018-01-26 08:41:09 +0000258 * The SHA-1 result is calculated as
259 * output = SHA-1(input buffer).
260 *
Rose Zadik82741422018-03-27 12:49:48 +0100261 * \warning SHA-1 is considered a weak message digest and its use
262 * constitutes a security risk. We recommend considering
263 * stronger message digests instead.
264 *
Rose Zadik44833d92018-01-26 08:41:09 +0000265 * \param input The buffer holding the input data.
266 * \param ilen The length of the input data.
267 * \param output The SHA-1 checksum result.
268 *
Rose Zadik82741422018-03-27 12:49:48 +0100269 * \return \c 0 on success.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100270 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100272int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100273 size_t ilen,
274 unsigned char output[20] );
275
276#if !defined(MBEDTLS_DEPRECATED_REMOVED)
277#if defined(MBEDTLS_DEPRECATED_WARNING)
278#define MBEDTLS_DEPRECATED __attribute__((deprecated))
279#else
280#define MBEDTLS_DEPRECATED
281#endif
282/**
Gilles Peskine2e1934a2018-04-18 16:05:29 +0200283 * \brief This function calculates the SHA-1 checksum of a buffer.
Rose Zadik82741422018-03-27 12:49:48 +0100284 *
285 * The function allocates the context, performs the
286 * calculation, and frees the context.
287 *
288 * The SHA-1 result is calculated as
289 * output = SHA-1(input buffer).
290 *
291 * \warning SHA-1 is considered a weak message digest and its use
292 * constitutes a security risk. We recommend considering
293 * stronger message digests instead.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100294 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100295 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100296 *
Rose Zadik44833d92018-01-26 08:41:09 +0000297 * \param input The buffer holding the input data.
298 * \param ilen The length of the input data.
299 * \param output The SHA-1 checksum result.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100300 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000302MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input,
303 size_t ilen,
304 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100305
306#undef MBEDTLS_DEPRECATED
307#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000308
309/**
Rose Zadik44833d92018-01-26 08:41:09 +0000310 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100312 * \warning SHA-1 is considered a weak message digest and its use
313 * constitutes a security risk. We recommend considering
314 * stronger message digests instead.
315 *
Rose Zadik82741422018-03-27 12:49:48 +0100316 * \return \c 0 on success.
317 * \return \c 1 on failure.
318 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
Paul Bakker5121ce52009-01-03 21:22:43 +0000322#ifdef __cplusplus
323}
324#endif
325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#endif /* mbedtls_sha1.h */