blob: e4f8650216958e12fc7270e9c5e1575a8621b20a [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
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010042#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
43 !defined(inline) && !defined(__cplusplus)
44#define inline __inline
45#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_SHA1_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020048// Regular implementation
49//
50
Paul Bakker407a0da2013-06-27 14:29:21 +020051#ifdef __cplusplus
52extern "C" {
53#endif
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 */
63typedef struct
64{
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
Paul Bakker5121ce52009-01-03 21:22:43 +000071/**
Rose Zadik44833d92018-01-26 08:41:09 +000072 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020073 *
Rose Zadik44833d92018-01-26 08:41:09 +000074 * \param ctx The SHA-1 context to initialize.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010075 *
76 * \warning SHA-1 is considered a weak message digest and its use
77 * constitutes a security risk. We recommend considering
78 * stronger message digests instead.
79 *
Paul Bakker5b4af392014-06-26 12:09:34 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020082
83/**
Rose Zadik44833d92018-01-26 08:41:09 +000084 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020085 *
Rose Zadik44833d92018-01-26 08:41:09 +000086 * \param ctx The SHA-1 context to clear.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010087 *
88 * \warning SHA-1 is considered a weak message digest and its use
89 * constitutes a security risk. We recommend considering
90 * stronger message digests instead.
91 *
Paul Bakker5b4af392014-06-26 12:09:34 +020092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020094
95/**
Rose Zadik44833d92018-01-26 08:41:09 +000096 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020097 *
Rose Zadik44833d92018-01-26 08:41:09 +000098 * \param dst The destination context.
99 * \param src The context to clone.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100100 *
101 * \warning SHA-1 is considered a weak message digest and its use
102 * constitutes a security risk. We recommend considering
103 * stronger message digests instead.
104 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200105 */
106void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
107 const mbedtls_sha1_context *src );
108
109/**
Rose Zadik44833d92018-01-26 08:41:09 +0000110 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 *
Rose Zadik44833d92018-01-26 08:41:09 +0000112 * \param ctx The context to initialize.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100113 *
Rose Zadik44833d92018-01-26 08:41:09 +0000114 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100115 *
116 * \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 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100121int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
123/**
Rose Zadik44833d92018-01-26 08:41:09 +0000124 * \brief This function feeds an input buffer into an ongoing SHA-1
125 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 *
Rose Zadik44833d92018-01-26 08:41:09 +0000127 * \param ctx The SHA-1 context.
128 * \param input The buffer holding the input data.
129 * \param ilen The length of the input data.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100130 *
Rose Zadik44833d92018-01-26 08:41:09 +0000131 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100132 *
133 * \warning SHA-1 is considered a weak message digest and its use
134 * constitutes a security risk. We recommend considering
135 * stronger message digests instead.
136 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100138int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100139 const unsigned char *input,
140 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
142/**
Rose Zadik44833d92018-01-26 08:41:09 +0000143 * \brief This function finishes the SHA-1 operation, and writes
144 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 *
Rose Zadik44833d92018-01-26 08:41:09 +0000146 * \param ctx The SHA-1 context.
147 * \param output The SHA-1 checksum result.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100148 *
Rose Zadik44833d92018-01-26 08:41:09 +0000149 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100150 *
151 * \warning SHA-1 is considered a weak message digest and its use
152 * constitutes a security risk. We recommend considering
153 * stronger message digests instead.
154 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100156int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100157 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100159/**
160 * \brief SHA-1 process data block (internal use only)
161 *
162 * \param ctx SHA-1 context
Rose Zadik44833d92018-01-26 08:41:09 +0000163 * \param data The data block being processed.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100164 *
Rose Zadik44833d92018-01-26 08:41:09 +0000165 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100166 *
167 * \warning SHA-1 is considered a weak message digest and its use
168 * constitutes a security risk. We recommend considering
169 * stronger message digests instead.
170 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100171 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100172int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
173 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100174
175#if !defined(MBEDTLS_DEPRECATED_REMOVED)
176#if defined(MBEDTLS_DEPRECATED_WARNING)
177#define MBEDTLS_DEPRECATED __attribute__((deprecated))
178#else
179#define MBEDTLS_DEPRECATED
180#endif
181/**
182 * \brief SHA-1 context setup
183 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100184 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100185 *
Rose Zadik44833d92018-01-26 08:41:09 +0000186 * \param ctx The SHA-1 context to be initialized.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100187 *
188 * \warning SHA-1 is considered a weak message digest and its use
189 * constitutes a security risk. We recommend considering
190 * stronger message digests instead.
191 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100192 */
193MBEDTLS_DEPRECATED static inline void mbedtls_sha1_starts(
194 mbedtls_sha1_context *ctx )
195{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100196 mbedtls_sha1_starts_ret( ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100197}
198
199/**
200 * \brief SHA-1 process buffer
201 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100202 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100203 *
Rose Zadik44833d92018-01-26 08:41:09 +0000204 * \param ctx The SHA-1 context.
205 * \param input The buffer holding the input data.
206 * \param ilen The length of the input data.
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 */
213MBEDTLS_DEPRECATED static inline void mbedtls_sha1_update(
214 mbedtls_sha1_context *ctx,
215 const unsigned char *input,
216 size_t ilen )
217{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100218 mbedtls_sha1_update_ret( ctx, input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100219}
220
221/**
222 * \brief SHA-1 final digest
223 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100224 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100225 *
Rose Zadik44833d92018-01-26 08:41:09 +0000226 * \param ctx The SHA-1 context.
227 * \param output The SHA-1 checksum result.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100228 *
229 * \warning SHA-1 is considered a weak message digest and its use
230 * constitutes a security risk. We recommend considering
231 * stronger message digests instead.
232 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100233 */
234MBEDTLS_DEPRECATED static inline void mbedtls_sha1_finish(
235 mbedtls_sha1_context *ctx,
236 unsigned char output[20] )
237{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100238 mbedtls_sha1_finish_ret( ctx, output );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100239}
240
241/**
242 * \brief SHA-1 process data block (internal use only)
243 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100244 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100245 *
Rose Zadik44833d92018-01-26 08:41:09 +0000246 * \param ctx The SHA-1 context.
247 * \param data The data block being processed.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100248 *
249 * \warning SHA-1 is considered a weak message digest and its use
250 * constitutes a security risk. We recommend considering
251 * stronger message digests instead.
252 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100253 */
254MBEDTLS_DEPRECATED static inline void mbedtls_sha1_process(
255 mbedtls_sha1_context *ctx,
256 const unsigned char data[64] )
257{
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100258 mbedtls_internal_sha1_process( ctx, data );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100259}
260
261#undef MBEDTLS_DEPRECATED
262#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200263
264#ifdef __cplusplus
265}
266#endif
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#else /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200269#include "sha1_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#endif /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200271
272#ifdef __cplusplus
273extern "C" {
274#endif
275
Paul Bakker5121ce52009-01-03 21:22:43 +0000276/**
Rose Zadik44833d92018-01-26 08:41:09 +0000277 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 *
Rose Zadik44833d92018-01-26 08:41:09 +0000279 * The function allocates the context, performs the
280 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100281 *
Rose Zadik44833d92018-01-26 08:41:09 +0000282 * The SHA-1 result is calculated as
283 * output = SHA-1(input buffer).
284 *
285 * \param input The buffer holding the input data.
286 * \param ilen The length of the input data.
287 * \param output The SHA-1 checksum result.
288 *
289 * \return \c 0 if successful
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100290 *
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.
294 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100296int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100297 size_t ilen,
298 unsigned char output[20] );
299
300#if !defined(MBEDTLS_DEPRECATED_REMOVED)
301#if defined(MBEDTLS_DEPRECATED_WARNING)
302#define MBEDTLS_DEPRECATED __attribute__((deprecated))
303#else
304#define MBEDTLS_DEPRECATED
305#endif
306/**
307 * \brief Output = SHA-1( input buffer )
308 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100309 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100310 *
Rose Zadik44833d92018-01-26 08:41:09 +0000311 * \param input The buffer holding the input data.
312 * \param ilen The length of the input data.
313 * \param output The SHA-1 checksum result.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100314 *
315 * \warning SHA-1 is considered a weak message digest and its use
316 * constitutes a security risk. We recommend considering
317 * stronger message digests instead.
318 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 */
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100320MBEDTLS_DEPRECATED static inline void mbedtls_sha1( const unsigned char *input,
321 size_t ilen,
322 unsigned char output[20] )
323{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100324 mbedtls_sha1_ret( input, ilen, output );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100325}
326
327#undef MBEDTLS_DEPRECATED
328#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330/**
Rose Zadik44833d92018-01-26 08:41:09 +0000331 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000332 *
Rose Zadik44833d92018-01-26 08:41:09 +0000333 * \return \c 0 on success, or \c 1 on failure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100334 *
335 * \warning SHA-1 is considered a weak message digest and its use
336 * constitutes a security risk. We recommend considering
337 * stronger message digests instead.
338 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000341
Paul Bakker5121ce52009-01-03 21:22:43 +0000342#ifdef __cplusplus
343}
344#endif
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346#endif /* mbedtls_sha1.h */