blob: 88f0e8c1c6597bd5b7cbcbbf0462299e16446270 [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
Gilles Peskinea381fe82018-01-23 18:16:11 +010043#define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035 /**< SHA-1 hardware accelerator failed */
44
Paul Bakker407a0da2013-06-27 14:29:21 +020045#ifdef __cplusplus
46extern "C" {
47#endif
48
Ron Eldorb2aacec2017-05-18 16:53:08 +030049#if !defined(MBEDTLS_SHA1_ALT)
50// Regular implementation
51//
52
Paul Bakker5121ce52009-01-03 21:22:43 +000053/**
Rose Zadik44833d92018-01-26 08:41:09 +000054 * \brief The SHA-1 context structure.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010055 *
56 * \warning SHA-1 is considered a weak message digest and its use
57 * constitutes a security risk. We recommend considering
58 * stronger message digests instead.
59 *
Paul Bakker5121ce52009-01-03 21:22:43 +000060 */
61typedef struct
62{
Rose Zadik44833d92018-01-26 08:41:09 +000063 uint32_t total[2]; /*!< The number of Bytes processed. */
64 uint32_t state[5]; /*!< The intermediate digest state. */
65 unsigned char buffer[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Ron Eldorb2aacec2017-05-18 16:53:08 +030069#else /* MBEDTLS_SHA1_ALT */
70#include "sha1_alt.h"
71#endif /* MBEDTLS_SHA1_ALT */
72
Paul Bakker5121ce52009-01-03 21:22:43 +000073/**
Rose Zadik44833d92018-01-26 08:41:09 +000074 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020075 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010076 * \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 *
Rose Zadik82741422018-03-27 12:49:48 +010080 * \param ctx The SHA-1 context to initialize.
81 *
Paul Bakker5b4af392014-06-26 12:09:34 +020082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020084
85/**
Rose Zadik44833d92018-01-26 08:41:09 +000086 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020087 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010088 * \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 *
Rose Zadik82741422018-03-27 12:49:48 +010092 * \param ctx The SHA-1 context to clear.
93 *
Paul Bakker5b4af392014-06-26 12:09:34 +020094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020096
97/**
Rose Zadik44833d92018-01-26 08:41:09 +000098 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020099 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100100 * \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 *
Rose Zadik92d66b82018-04-17 10:36:56 +0100104 * \param dst The SHA-1 context to clone to.
105 * \param src The SHA-1 context to clone from.
Rose Zadik82741422018-03-27 12:49:48 +0100106 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200107 */
108void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
109 const mbedtls_sha1_context *src );
110
111/**
Rose Zadik44833d92018-01-26 08:41:09 +0000112 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100114 * \warning SHA-1 is considered a weak message digest and its use
115 * constitutes a security risk. We recommend considering
116 * stronger message digests instead.
117 *
Rose Zadik92d66b82018-04-17 10:36:56 +0100118 * \param ctx The SHA-1 context to initialize.
Rose Zadik82741422018-03-27 12:49:48 +0100119 *
120 * \return \c 0 on success.
121 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100123int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
125/**
Rose Zadik44833d92018-01-26 08:41:09 +0000126 * \brief This function feeds an input buffer into an ongoing SHA-1
127 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100129 * \warning SHA-1 is considered a weak message digest and its use
130 * constitutes a security risk. We recommend considering
131 * stronger message digests instead.
132 *
Rose Zadik82741422018-03-27 12:49:48 +0100133 * \param ctx The SHA-1 context.
134 * \param input The buffer holding the input data.
135 * \param ilen The length of the input data.
136 *
137 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100139int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100140 const unsigned char *input,
141 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
143/**
Rose Zadik44833d92018-01-26 08:41:09 +0000144 * \brief This function finishes the SHA-1 operation, and writes
145 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100147 * \warning SHA-1 is considered a weak message digest and its use
148 * constitutes a security risk. We recommend considering
149 * stronger message digests instead.
150 *
Rose Zadik82741422018-03-27 12:49:48 +0100151 * \param ctx The SHA-1 context.
152 * \param output The SHA-1 checksum result.
153 *
154 * \return \c 0 on success.
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/**
Rose Zadik82741422018-03-27 12:49:48 +0100160 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100161 *
162 * \warning SHA-1 is considered a weak message digest and its use
163 * constitutes a security risk. We recommend considering
164 * stronger message digests instead.
165 *
Rose Zadik82741422018-03-27 12:49:48 +0100166 * \param ctx The SHA-1 context.
167 * \param data The data block being processed.
168 *
169 * \return \c 0 on success.
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/**
Rose Zadik82741422018-03-27 12:49:48 +0100182 * \brief This function starts a SHA-1 checksum calculation.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100183 *
184 * \warning SHA-1 is considered a weak message digest and its use
185 * constitutes a security risk. We recommend considering
186 * stronger message digests instead.
187 *
Rose Zadik82741422018-03-27 12:49:48 +0100188 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0.
189 *
Rose Zadik92d66b82018-04-17 10:36:56 +0100190 * \param ctx The SHA-1 context to initialize.
Rose Zadik82741422018-03-27 12:49:48 +0100191 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100192 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000193MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100194
195/**
Rose Zadik82741422018-03-27 12:49:48 +0100196 * \brief This function feeds an input buffer into an ongoing SHA-1
197 * checksum calculation.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100198 *
199 * \warning SHA-1 is considered a weak message digest and its use
200 * constitutes a security risk. We recommend considering
201 * stronger message digests instead.
202 *
Rose Zadik82741422018-03-27 12:49:48 +0100203 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0.
204 *
205 * \param ctx The SHA-1 context.
206 * \param input The buffer holding the input data.
207 * \param ilen The length of the input data.
208 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100209 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000210MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
211 const unsigned char *input,
212 size_t ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100213
214/**
Rose Zadik82741422018-03-27 12:49:48 +0100215 * \brief This function finishes the SHA-1 operation, and writes
216 * the result to the output buffer.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100217 *
218 * \warning SHA-1 is considered a weak message digest and its use
219 * constitutes a security risk. We recommend considering
220 * stronger message digests instead.
221 *
Rose Zadik82741422018-03-27 12:49:48 +0100222 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0.
223 *
224 * \param ctx The SHA-1 context.
225 * \param output The SHA-1 checksum result.
226 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100227 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000228MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
229 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100230
231/**
Rose Zadik82741422018-03-27 12:49:48 +0100232 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100233 *
234 * \warning SHA-1 is considered a weak message digest and its use
235 * constitutes a security risk. We recommend considering
236 * stronger message digests instead.
237 *
Rose Zadik82741422018-03-27 12:49:48 +0100238 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0.
239 *
240 * \param ctx The SHA-1 context.
241 * \param data The data block being processed.
242 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100243 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000244MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
245 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100246
247#undef MBEDTLS_DEPRECATED
248#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200249
Paul Bakker5121ce52009-01-03 21:22:43 +0000250/**
Rose Zadik44833d92018-01-26 08:41:09 +0000251 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 *
Rose Zadik44833d92018-01-26 08:41:09 +0000253 * The function allocates the context, performs the
254 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100255 *
Rose Zadik44833d92018-01-26 08:41:09 +0000256 * The SHA-1 result is calculated as
257 * output = SHA-1(input buffer).
258 *
Rose Zadik82741422018-03-27 12:49:48 +0100259 * \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 *
Rose Zadik44833d92018-01-26 08:41:09 +0000263 * \param input The buffer holding the input data.
264 * \param ilen The length of the input data.
265 * \param output The SHA-1 checksum result.
266 *
Rose Zadik82741422018-03-27 12:49:48 +0100267 * \return \c 0 on success.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100268 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100270int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100271 size_t ilen,
272 unsigned char output[20] );
273
274#if !defined(MBEDTLS_DEPRECATED_REMOVED)
275#if defined(MBEDTLS_DEPRECATED_WARNING)
276#define MBEDTLS_DEPRECATED __attribute__((deprecated))
277#else
278#define MBEDTLS_DEPRECATED
279#endif
280/**
Gilles Peskine2e1934a2018-04-18 16:05:29 +0200281 * \brief This function calculates the SHA-1 checksum of a buffer.
Rose Zadik82741422018-03-27 12:49:48 +0100282 *
283 * The function allocates the context, performs the
284 * calculation, and frees the context.
285 *
286 * The SHA-1 result is calculated as
287 * output = SHA-1(input buffer).
288 *
289 * \warning SHA-1 is considered a weak message digest and its use
290 * constitutes a security risk. We recommend considering
291 * stronger message digests instead.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100292 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100293 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100294 *
Rose Zadik44833d92018-01-26 08:41:09 +0000295 * \param input The buffer holding the input data.
296 * \param ilen The length of the input data.
297 * \param output The SHA-1 checksum result.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100298 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000300MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input,
301 size_t ilen,
302 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100303
304#undef MBEDTLS_DEPRECATED
305#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000306
Ron Eldorfa8f6352017-06-20 15:48:46 +0300307#if defined(MBEDTLS_SELF_TEST)
308
Paul Bakker5121ce52009-01-03 21:22:43 +0000309/**
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
Ron Eldorfa8f6352017-06-20 15:48:46 +0300322#endif /* MBEDTLS_SELF_TEST */
323
Paul Bakker5121ce52009-01-03 21:22:43 +0000324#ifdef __cplusplus
325}
326#endif
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328#endif /* mbedtls_sha1.h */