blob: bcaeab5eb94d4108c60e523bcae9dfe18da93d09 [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 */
45
Paul Bakker407a0da2013-06-27 14:29:21 +020046#ifdef __cplusplus
47extern "C" {
48#endif
49
Ron Eldorb2aacec2017-05-18 16:53:08 +030050#if !defined(MBEDTLS_SHA1_ALT)
51// Regular implementation
52//
53
Paul Bakker5121ce52009-01-03 21:22:43 +000054/**
Rose Zadik44833d92018-01-26 08:41:09 +000055 * \brief The SHA-1 context structure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010056 *
57 * \warning SHA-1 is considered a weak message digest and its use
58 * constitutes a security risk. We recommend considering
59 * stronger message digests instead.
60 *
Paul Bakker5121ce52009-01-03 21:22:43 +000061 */
Dawid Drozd428cc522018-07-24 10:02:47 +020062typedef struct mbedtls_sha1_context
Paul Bakker5121ce52009-01-03 21:22:43 +000063{
Rose Zadik44833d92018-01-26 08:41:09 +000064 uint32_t total[2]; /*!< The number of Bytes processed. */
65 uint32_t state[5]; /*!< The intermediate digest state. */
66 unsigned char buffer[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Ron Eldorb2aacec2017-05-18 16:53:08 +030070#else /* MBEDTLS_SHA1_ALT */
71#include "sha1_alt.h"
72#endif /* MBEDTLS_SHA1_ALT */
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/**
Rose Zadik44833d92018-01-26 08:41:09 +000075 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020076 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010077 * \warning SHA-1 is considered a weak message digest and its use
78 * constitutes a security risk. We recommend considering
79 * stronger message digests instead.
80 *
Rose Zadik82741422018-03-27 12:49:48 +010081 * \param ctx The SHA-1 context to initialize.
82 *
Paul Bakker5b4af392014-06-26 12:09:34 +020083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020085
86/**
Rose Zadik44833d92018-01-26 08:41:09 +000087 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020088 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010089 * \warning SHA-1 is considered a weak message digest and its use
90 * constitutes a security risk. We recommend considering
91 * stronger message digests instead.
92 *
Rose Zadik82741422018-03-27 12:49:48 +010093 * \param ctx The SHA-1 context to clear.
94 *
Paul Bakker5b4af392014-06-26 12:09:34 +020095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020097
98/**
Rose Zadik44833d92018-01-26 08:41:09 +000099 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200100 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100101 * \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 *
Rose Zadik92d66b82018-04-17 10:36:56 +0100105 * \param dst The SHA-1 context to clone to.
106 * \param src The SHA-1 context to clone from.
Rose Zadik82741422018-03-27 12:49:48 +0100107 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200108 */
109void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
110 const mbedtls_sha1_context *src );
111
112/**
Rose Zadik44833d92018-01-26 08:41:09 +0000113 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100115 * \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 *
Rose Zadik92d66b82018-04-17 10:36:56 +0100119 * \param ctx The SHA-1 context to initialize.
Rose Zadik82741422018-03-27 12:49:48 +0100120 *
121 * \return \c 0 on success.
122 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100124int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
126/**
Rose Zadik44833d92018-01-26 08:41:09 +0000127 * \brief This function feeds an input buffer into an ongoing SHA-1
128 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100130 * \warning SHA-1 is considered a weak message digest and its use
131 * constitutes a security risk. We recommend considering
132 * stronger message digests instead.
133 *
Rose Zadik82741422018-03-27 12:49:48 +0100134 * \param ctx The SHA-1 context.
135 * \param input The buffer holding the input data.
136 * \param ilen The length of the input data.
137 *
138 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100140int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100141 const unsigned char *input,
142 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000143
144/**
Rose Zadik44833d92018-01-26 08:41:09 +0000145 * \brief This function finishes the SHA-1 operation, and writes
146 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100148 * \warning SHA-1 is considered a weak message digest and its use
149 * constitutes a security risk. We recommend considering
150 * stronger message digests instead.
151 *
Rose Zadik82741422018-03-27 12:49:48 +0100152 * \param ctx The SHA-1 context.
153 * \param output The SHA-1 checksum result.
154 *
155 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100157int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100158 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100160/**
Rose Zadik82741422018-03-27 12:49:48 +0100161 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100162 *
163 * \warning SHA-1 is considered a weak message digest and its use
164 * constitutes a security risk. We recommend considering
165 * stronger message digests instead.
166 *
Rose Zadik82741422018-03-27 12:49:48 +0100167 * \param ctx The SHA-1 context.
168 * \param data The data block being processed.
169 *
170 * \return \c 0 on success.
171 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100172 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100173int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
174 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100175
176#if !defined(MBEDTLS_DEPRECATED_REMOVED)
177#if defined(MBEDTLS_DEPRECATED_WARNING)
178#define MBEDTLS_DEPRECATED __attribute__((deprecated))
179#else
180#define MBEDTLS_DEPRECATED
181#endif
182/**
Rose Zadik82741422018-03-27 12:49:48 +0100183 * \brief This function starts a SHA-1 checksum calculation.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100184 *
185 * \warning SHA-1 is considered a weak message digest and its use
186 * constitutes a security risk. We recommend considering
187 * stronger message digests instead.
188 *
Rose Zadik82741422018-03-27 12:49:48 +0100189 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0.
190 *
Rose Zadik92d66b82018-04-17 10:36:56 +0100191 * \param ctx The SHA-1 context to initialize.
Rose Zadik82741422018-03-27 12:49:48 +0100192 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100193 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000194MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100195
196/**
Rose Zadik82741422018-03-27 12:49:48 +0100197 * \brief This function feeds an input buffer into an ongoing SHA-1
198 * checksum calculation.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100199 *
200 * \warning SHA-1 is considered a weak message digest and its use
201 * constitutes a security risk. We recommend considering
202 * stronger message digests instead.
203 *
Rose Zadik82741422018-03-27 12:49:48 +0100204 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0.
205 *
206 * \param ctx The SHA-1 context.
207 * \param input The buffer holding the input data.
208 * \param ilen The length of the input data.
209 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100210 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000211MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
212 const unsigned char *input,
213 size_t ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100214
215/**
Rose Zadik82741422018-03-27 12:49:48 +0100216 * \brief This function finishes the SHA-1 operation, and writes
217 * the result to the output buffer.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100218 *
219 * \warning SHA-1 is considered a weak message digest and its use
220 * constitutes a security risk. We recommend considering
221 * stronger message digests instead.
222 *
Rose Zadik82741422018-03-27 12:49:48 +0100223 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0.
224 *
225 * \param ctx The SHA-1 context.
226 * \param output The SHA-1 checksum result.
227 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100228 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000229MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
230 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100231
232/**
Rose Zadik82741422018-03-27 12:49:48 +0100233 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100234 *
235 * \warning SHA-1 is considered a weak message digest and its use
236 * constitutes a security risk. We recommend considering
237 * stronger message digests instead.
238 *
Rose Zadik82741422018-03-27 12:49:48 +0100239 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0.
240 *
241 * \param ctx The SHA-1 context.
242 * \param data The data block being processed.
243 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100244 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000245MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
246 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100247
248#undef MBEDTLS_DEPRECATED
249#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200250
Paul Bakker5121ce52009-01-03 21:22:43 +0000251/**
Rose Zadik44833d92018-01-26 08:41:09 +0000252 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 *
Rose Zadik44833d92018-01-26 08:41:09 +0000254 * The function allocates the context, performs the
255 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100256 *
Rose Zadik44833d92018-01-26 08:41:09 +0000257 * The SHA-1 result is calculated as
258 * output = SHA-1(input buffer).
259 *
Rose Zadik82741422018-03-27 12:49:48 +0100260 * \warning SHA-1 is considered a weak message digest and its use
261 * constitutes a security risk. We recommend considering
262 * stronger message digests instead.
263 *
Rose Zadik44833d92018-01-26 08:41:09 +0000264 * \param input The buffer holding the input data.
265 * \param ilen The length of the input data.
266 * \param output The SHA-1 checksum result.
267 *
Rose Zadik82741422018-03-27 12:49:48 +0100268 * \return \c 0 on success.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100269 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100271int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100272 size_t ilen,
273 unsigned char output[20] );
274
275#if !defined(MBEDTLS_DEPRECATED_REMOVED)
276#if defined(MBEDTLS_DEPRECATED_WARNING)
277#define MBEDTLS_DEPRECATED __attribute__((deprecated))
278#else
279#define MBEDTLS_DEPRECATED
280#endif
281/**
Gilles Peskine2e1934a2018-04-18 16:05:29 +0200282 * \brief This function calculates the SHA-1 checksum of a buffer.
Rose Zadik82741422018-03-27 12:49:48 +0100283 *
284 * The function allocates the context, performs the
285 * calculation, and frees the context.
286 *
287 * The SHA-1 result is calculated as
288 * output = SHA-1(input buffer).
289 *
290 * \warning SHA-1 is considered a weak message digest and its use
291 * constitutes a security risk. We recommend considering
292 * stronger message digests instead.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100293 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100294 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100295 *
Rose Zadik44833d92018-01-26 08:41:09 +0000296 * \param input The buffer holding the input data.
297 * \param ilen The length of the input data.
298 * \param output The SHA-1 checksum result.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100299 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000301MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input,
302 size_t ilen,
303 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100304
305#undef MBEDTLS_DEPRECATED
306#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308/**
Rose Zadik44833d92018-01-26 08:41:09 +0000309 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100311 * \warning SHA-1 is considered a weak message digest and its use
312 * constitutes a security risk. We recommend considering
313 * stronger message digests instead.
314 *
Rose Zadik82741422018-03-27 12:49:48 +0100315 * \return \c 0 on success.
316 * \return \c 1 on failure.
317 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Paul Bakker5121ce52009-01-03 21:22:43 +0000321#ifdef __cplusplus
322}
323#endif
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#endif /* mbedtls_sha1.h */