blob: 4a43c01015d0cdaa1ccb173175e02dcfd16b7b16 [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 *
6 * The Secure Hash Algorithm 1 (SHA-1) cryptographic hash function is defined in
7 * <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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_SHA1_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020046// Regular implementation
47//
48
Paul Bakker407a0da2013-06-27 14:29:21 +020049#ifdef __cplusplus
50extern "C" {
51#endif
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
Paul Bakker5121ce52009-01-03 21:22:43 +000069/**
Rose Zadik44833d92018-01-26 08:41:09 +000070 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020071 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010072 * \warning SHA-1 is considered a weak message digest and its use
73 * constitutes a security risk. We recommend considering
74 * stronger message digests instead.
75 *
Rose Zadik82741422018-03-27 12:49:48 +010076 * \param ctx The SHA-1 context to initialize.
77 *
Paul Bakker5b4af392014-06-26 12:09:34 +020078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020080
81/**
Rose Zadik44833d92018-01-26 08:41:09 +000082 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020083 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010084 * \warning SHA-1 is considered a weak message digest and its use
85 * constitutes a security risk. We recommend considering
86 * stronger message digests instead.
87 *
Rose Zadik82741422018-03-27 12:49:48 +010088 * \param ctx The SHA-1 context to clear.
89 *
Paul Bakker5b4af392014-06-26 12:09:34 +020090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020092
93/**
Rose Zadik44833d92018-01-26 08:41:09 +000094 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020095 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +010096 * \warning SHA-1 is considered a weak message digest and its use
97 * constitutes a security risk. We recommend considering
98 * stronger message digests instead.
99 *
Rose Zadik82741422018-03-27 12:49:48 +0100100 * \param dst The destination context.
101 * \param src The context to clone.
102 *
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200103 */
104void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
105 const mbedtls_sha1_context *src );
106
107/**
Rose Zadik44833d92018-01-26 08:41:09 +0000108 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100110 * \warning SHA-1 is considered a weak message digest and its use
111 * constitutes a security risk. We recommend considering
112 * stronger message digests instead.
113 *
Rose Zadik82741422018-03-27 12:49:48 +0100114 * \param ctx The context to initialize.
115 *
116 * \return \c 0 on success.
117 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100119int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121/**
Rose Zadik44833d92018-01-26 08:41:09 +0000122 * \brief This function feeds an input buffer into an ongoing SHA-1
123 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100125 * \warning SHA-1 is considered a weak message digest and its use
126 * constitutes a security risk. We recommend considering
127 * stronger message digests instead.
128 *
Rose Zadik82741422018-03-27 12:49:48 +0100129 * \param ctx The SHA-1 context.
130 * \param input The buffer holding the input data.
131 * \param ilen The length of the input data.
132 *
133 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100135int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100136 const unsigned char *input,
137 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
139/**
Rose Zadik44833d92018-01-26 08:41:09 +0000140 * \brief This function finishes the SHA-1 operation, and writes
141 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100143 * \warning SHA-1 is considered a weak message digest and its use
144 * constitutes a security risk. We recommend considering
145 * stronger message digests instead.
146 *
Rose Zadik82741422018-03-27 12:49:48 +0100147 * \param ctx The SHA-1 context.
148 * \param output The SHA-1 checksum result.
149 *
150 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100152int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100153 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100155/**
Rose Zadik82741422018-03-27 12:49:48 +0100156 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100157 *
158 * \warning SHA-1 is considered a weak message digest and its use
159 * constitutes a security risk. We recommend considering
160 * stronger message digests instead.
161 *
Rose Zadik82741422018-03-27 12:49:48 +0100162 * \param ctx The SHA-1 context.
163 * \param data The data block being processed.
164 *
165 * \return \c 0 on success.
166 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100167 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100168int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
169 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100170
171#if !defined(MBEDTLS_DEPRECATED_REMOVED)
172#if defined(MBEDTLS_DEPRECATED_WARNING)
173#define MBEDTLS_DEPRECATED __attribute__((deprecated))
174#else
175#define MBEDTLS_DEPRECATED
176#endif
177/**
Rose Zadik82741422018-03-27 12:49:48 +0100178 * \brief This function starts a SHA-1 checksum calculation.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100179 *
180 * \warning SHA-1 is considered a weak message digest and its use
181 * constitutes a security risk. We recommend considering
182 * stronger message digests instead.
183 *
Rose Zadik82741422018-03-27 12:49:48 +0100184 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0.
185 *
186 * \param ctx The context to initialize.
187 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100188 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000189MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100190
191/**
Rose Zadik82741422018-03-27 12:49:48 +0100192 * \brief This function feeds an input buffer into an ongoing SHA-1
193 * checksum calculation.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100194 *
195 * \warning SHA-1 is considered a weak message digest and its use
196 * constitutes a security risk. We recommend considering
197 * stronger message digests instead.
198 *
Rose Zadik82741422018-03-27 12:49:48 +0100199 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0.
200 *
201 * \param ctx The SHA-1 context.
202 * \param input The buffer holding the input data.
203 * \param ilen The length of the input data.
204 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100205 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000206MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
207 const unsigned char *input,
208 size_t ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100209
210/**
Rose Zadik82741422018-03-27 12:49:48 +0100211 * \brief This function finishes the SHA-1 operation, and writes
212 * the result to the output buffer.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100213 *
214 * \warning SHA-1 is considered a weak message digest and its use
215 * constitutes a security risk. We recommend considering
216 * stronger message digests instead.
217 *
Rose Zadik82741422018-03-27 12:49:48 +0100218 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0.
219 *
220 * \param ctx The SHA-1 context.
221 * \param output The SHA-1 checksum result.
222 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100223 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000224MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
225 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100226
227/**
Rose Zadik82741422018-03-27 12:49:48 +0100228 * \brief SHA-1 process data block (internal use only).
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100229 *
230 * \warning SHA-1 is considered a weak message digest and its use
231 * constitutes a security risk. We recommend considering
232 * stronger message digests instead.
233 *
Rose Zadik82741422018-03-27 12:49:48 +0100234 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0.
235 *
236 * \param ctx The SHA-1 context.
237 * \param data The data block being processed.
238 *
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100239 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000240MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
241 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100242
243#undef MBEDTLS_DEPRECATED
244#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200245
246#ifdef __cplusplus
247}
248#endif
249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#else /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200251#include "sha1_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#endif /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200253
254#ifdef __cplusplus
255extern "C" {
256#endif
257
Paul Bakker5121ce52009-01-03 21:22:43 +0000258/**
Rose Zadik44833d92018-01-26 08:41:09 +0000259 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 *
Rose Zadik44833d92018-01-26 08:41:09 +0000261 * The function allocates the context, performs the
262 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100263 *
Rose Zadik44833d92018-01-26 08:41:09 +0000264 * The SHA-1 result is calculated as
265 * output = SHA-1(input buffer).
266 *
Rose Zadik82741422018-03-27 12:49:48 +0100267 * \warning SHA-1 is considered a weak message digest and its use
268 * constitutes a security risk. We recommend considering
269 * stronger message digests instead.
270 *
Rose Zadik44833d92018-01-26 08:41:09 +0000271 * \param input The buffer holding the input data.
272 * \param ilen The length of the input data.
273 * \param output The SHA-1 checksum result.
274 *
Rose Zadik82741422018-03-27 12:49:48 +0100275 * \return \c 0 on success.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100276 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100278int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100279 size_t ilen,
280 unsigned char output[20] );
281
282#if !defined(MBEDTLS_DEPRECATED_REMOVED)
283#if defined(MBEDTLS_DEPRECATED_WARNING)
284#define MBEDTLS_DEPRECATED __attribute__((deprecated))
285#else
286#define MBEDTLS_DEPRECATED
287#endif
288/**
Rose Zadik82741422018-03-27 12:49:48 +0100289* \brief This function calculates the SHA-1 checksum of a buffer.
290 *
291 * The function allocates the context, performs the
292 * calculation, and frees the context.
293 *
294 * The SHA-1 result is calculated as
295 * output = SHA-1(input buffer).
296 *
297 * \warning SHA-1 is considered a weak message digest and its use
298 * constitutes a security risk. We recommend considering
299 * stronger message digests instead.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100300 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100301 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100302 *
Rose Zadik44833d92018-01-26 08:41:09 +0000303 * \param input The buffer holding the input data.
304 * \param ilen The length of the input data.
305 * \param output The SHA-1 checksum result.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100306 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000308MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input,
309 size_t ilen,
310 unsigned char output[20] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100311
312#undef MBEDTLS_DEPRECATED
313#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000314
315/**
Rose Zadik44833d92018-01-26 08:41:09 +0000316 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 *
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100318 * \warning SHA-1 is considered a weak message digest and its use
319 * constitutes a security risk. We recommend considering
320 * stronger message digests instead.
321 *
Rose Zadik82741422018-03-27 12:49:48 +0100322 * \return \c 0 on success.
323 * \return \c 1 on failure.
324 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000327
Paul Bakker5121ce52009-01-03 21:22:43 +0000328#ifdef __cplusplus
329}
330#endif
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#endif /* mbedtls_sha1.h */