blob: 700a3483159ae785f79021289528f656f0aa1e30 [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.
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Rose Zadik44833d92018-01-26 08:41:09 +00007 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000021 *
Rose Zadik44833d92018-01-26 08:41:09 +000022 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_SHA1_H
25#define MBEDTLS_SHA1_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000035
Gilles Peskinea381fe82018-01-23 18:16:11 +010036#define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035 /**< SHA-1 hardware accelerator failed */
37
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010038#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
39 !defined(inline) && !defined(__cplusplus)
40#define inline __inline
41#endif
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_SHA1_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020044// Regular implementation
45//
46
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/**
Rose Zadik44833d92018-01-26 08:41:09 +000052 * \brief The SHA-1 context structure.
Paul Bakker5121ce52009-01-03 21:22:43 +000053 */
54typedef struct
55{
Rose Zadik44833d92018-01-26 08:41:09 +000056 uint32_t total[2]; /*!< The number of Bytes processed. */
57 uint32_t state[5]; /*!< The intermediate digest state. */
58 unsigned char buffer[64]; /*!< The data block being processed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000059}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Paul Bakker5121ce52009-01-03 21:22:43 +000062/**
Rose Zadik44833d92018-01-26 08:41:09 +000063 * \brief This function initializes a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020064 *
Rose Zadik44833d92018-01-26 08:41:09 +000065 * \param ctx The SHA-1 context to initialize.
Paul Bakker5b4af392014-06-26 12:09:34 +020066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020068
69/**
Rose Zadik44833d92018-01-26 08:41:09 +000070 * \brief This function clears a SHA-1 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020071 *
Rose Zadik44833d92018-01-26 08:41:09 +000072 * \param ctx The SHA-1 context to clear.
Paul Bakker5b4af392014-06-26 12:09:34 +020073 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020075
76/**
Rose Zadik44833d92018-01-26 08:41:09 +000077 * \brief This function clones the state of a SHA-1 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020078 *
Rose Zadik44833d92018-01-26 08:41:09 +000079 * \param dst The destination context.
80 * \param src The context to clone.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020081 */
82void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
83 const mbedtls_sha1_context *src );
84
85/**
Rose Zadik44833d92018-01-26 08:41:09 +000086 * \brief This function starts a SHA-1 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000087 *
Rose Zadik44833d92018-01-26 08:41:09 +000088 * \param ctx The context to initialize.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010089 *
Rose Zadik44833d92018-01-26 08:41:09 +000090 * \return \c 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000091 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010092int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +000093
94/**
Rose Zadik44833d92018-01-26 08:41:09 +000095 * \brief This function feeds an input buffer into an ongoing SHA-1
96 * checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000097 *
Rose Zadik44833d92018-01-26 08:41:09 +000098 * \param ctx The SHA-1 context.
99 * \param input The buffer holding the input data.
100 * \param ilen The length of the input data.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100101 *
Rose Zadik44833d92018-01-26 08:41:09 +0000102 * \return \c 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100104int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100105 const unsigned char *input,
106 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108/**
Rose Zadik44833d92018-01-26 08:41:09 +0000109 * \brief This function finishes the SHA-1 operation, and writes
110 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 *
Rose Zadik44833d92018-01-26 08:41:09 +0000112 * \param ctx The SHA-1 context.
113 * \param output The SHA-1 checksum result.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100114 *
Rose Zadik44833d92018-01-26 08:41:09 +0000115 * \return \c 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100117int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100118 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100120/**
121 * \brief SHA-1 process data block (internal use only)
122 *
123 * \param ctx SHA-1 context
Rose Zadik44833d92018-01-26 08:41:09 +0000124 * \param data The data block being processed.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100125 *
Rose Zadik44833d92018-01-26 08:41:09 +0000126 * \return \c 0 if successful
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100127 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100128int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
129 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100130
131#if !defined(MBEDTLS_DEPRECATED_REMOVED)
132#if defined(MBEDTLS_DEPRECATED_WARNING)
133#define MBEDTLS_DEPRECATED __attribute__((deprecated))
134#else
135#define MBEDTLS_DEPRECATED
136#endif
137/**
138 * \brief SHA-1 context setup
139 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100140 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100141 *
Rose Zadik44833d92018-01-26 08:41:09 +0000142 * \param ctx The SHA-1 context to be initialized.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100143 */
144MBEDTLS_DEPRECATED static inline void mbedtls_sha1_starts(
145 mbedtls_sha1_context *ctx )
146{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100147 mbedtls_sha1_starts_ret( ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100148}
149
150/**
151 * \brief SHA-1 process buffer
152 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100153 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100154 *
Rose Zadik44833d92018-01-26 08:41:09 +0000155 * \param ctx The SHA-1 context.
156 * \param input The buffer holding the input data.
157 * \param ilen The length of the input data.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100158 */
159MBEDTLS_DEPRECATED static inline void mbedtls_sha1_update(
160 mbedtls_sha1_context *ctx,
161 const unsigned char *input,
162 size_t ilen )
163{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100164 mbedtls_sha1_update_ret( ctx, input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100165}
166
167/**
168 * \brief SHA-1 final digest
169 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100170 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100171 *
Rose Zadik44833d92018-01-26 08:41:09 +0000172 * \param ctx The SHA-1 context.
173 * \param output The SHA-1 checksum result.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100174 */
175MBEDTLS_DEPRECATED static inline void mbedtls_sha1_finish(
176 mbedtls_sha1_context *ctx,
177 unsigned char output[20] )
178{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100179 mbedtls_sha1_finish_ret( ctx, output );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100180}
181
182/**
183 * \brief SHA-1 process data block (internal use only)
184 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100185 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100186 *
Rose Zadik44833d92018-01-26 08:41:09 +0000187 * \param ctx The SHA-1 context.
188 * \param data The data block being processed.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100189 */
190MBEDTLS_DEPRECATED static inline void mbedtls_sha1_process(
191 mbedtls_sha1_context *ctx,
192 const unsigned char data[64] )
193{
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100194 mbedtls_internal_sha1_process( ctx, data );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100195}
196
197#undef MBEDTLS_DEPRECATED
198#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200199
200#ifdef __cplusplus
201}
202#endif
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#else /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200205#include "sha1_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#endif /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200207
208#ifdef __cplusplus
209extern "C" {
210#endif
211
Paul Bakker5121ce52009-01-03 21:22:43 +0000212/**
Rose Zadik44833d92018-01-26 08:41:09 +0000213 * \brief This function calculates the SHA-1 checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 *
Rose Zadik44833d92018-01-26 08:41:09 +0000215 * The function allocates the context, performs the
216 * calculation, and frees the context.
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100217 *
Rose Zadik44833d92018-01-26 08:41:09 +0000218 * The SHA-1 result is calculated as
219 * output = SHA-1(input buffer).
220 *
221 * \param input The buffer holding the input data.
222 * \param ilen The length of the input data.
223 * \param output The SHA-1 checksum result.
224 *
225 * \return \c 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100227int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100228 size_t ilen,
229 unsigned char output[20] );
230
231#if !defined(MBEDTLS_DEPRECATED_REMOVED)
232#if defined(MBEDTLS_DEPRECATED_WARNING)
233#define MBEDTLS_DEPRECATED __attribute__((deprecated))
234#else
235#define MBEDTLS_DEPRECATED
236#endif
237/**
238 * \brief Output = SHA-1( input buffer )
239 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100240 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100241 *
Rose Zadik44833d92018-01-26 08:41:09 +0000242 * \param input The buffer holding the input data.
243 * \param ilen The length of the input data.
244 * \param output The SHA-1 checksum result.
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 */
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100246MBEDTLS_DEPRECATED static inline void mbedtls_sha1( const unsigned char *input,
247 size_t ilen,
248 unsigned char output[20] )
249{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100250 mbedtls_sha1_ret( input, ilen, output );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100251}
252
253#undef MBEDTLS_DEPRECATED
254#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
256/**
Rose Zadik44833d92018-01-26 08:41:09 +0000257 * \brief The SHA-1 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 *
Rose Zadik44833d92018-01-26 08:41:09 +0000259 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000262
Paul Bakker5121ce52009-01-03 21:22:43 +0000263#ifdef __cplusplus
264}
265#endif
266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* mbedtls_sha1.h */