blob: b879ee6aa676ea4914c7579fad6e88f8e1122259 [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 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief SHA-1 cryptographic hash function
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_SHA1_H
24#define MBEDTLS_SHA1_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker90995b52013-06-24 19:20:35 +020031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020033#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000034
Gilles Peskinea381fe82018-01-23 18:16:11 +010035#define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035 /**< SHA-1 hardware accelerator failed */
36
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010037#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
38 !defined(inline) && !defined(__cplusplus)
39#define inline __inline
40#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_SHA1_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020043// Regular implementation
44//
45
Paul Bakker407a0da2013-06-27 14:29:21 +020046#ifdef __cplusplus
47extern "C" {
48#endif
49
Paul Bakker5121ce52009-01-03 21:22:43 +000050/**
51 * \brief SHA-1 context structure
52 */
53typedef struct
54{
Paul Bakker5c2364c2012-10-01 14:41:15 +000055 uint32_t total[2]; /*!< number of bytes processed */
56 uint32_t state[5]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000057 unsigned char buffer[64]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000058}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Paul Bakker5121ce52009-01-03 21:22:43 +000061/**
Paul Bakker5b4af392014-06-26 12:09:34 +020062 * \brief Initialize SHA-1 context
63 *
64 * \param ctx SHA-1 context to be initialized
65 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020067
68/**
69 * \brief Clear SHA-1 context
70 *
71 * \param ctx SHA-1 context to be cleared
72 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020074
75/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020076 * \brief Clone (the state of) a SHA-1 context
77 *
78 * \param dst The destination context
79 * \param src The context to be cloned
80 */
81void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
82 const mbedtls_sha1_context *src );
83
84/**
Paul Bakker5121ce52009-01-03 21:22:43 +000085 * \brief SHA-1 context setup
86 *
87 * \param ctx context to be initialized
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010088 *
89 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000090 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010091int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93/**
94 * \brief SHA-1 process buffer
95 *
96 * \param ctx SHA-1 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +010097 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +000098 * \param ilen length of the input data
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010099 *
100 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100102int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100103 const unsigned char *input,
104 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
106/**
107 * \brief SHA-1 final digest
108 *
109 * \param ctx SHA-1 context
110 * \param output SHA-1 checksum result
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100111 *
112 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100114int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100115 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100117/**
118 * \brief SHA-1 process data block (internal use only)
119 *
120 * \param ctx SHA-1 context
121 * \param data buffer holding one block of data
122 *
123 * \return 0 if successful
124 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100125int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
126 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100127
128#if !defined(MBEDTLS_DEPRECATED_REMOVED)
129#if defined(MBEDTLS_DEPRECATED_WARNING)
130#define MBEDTLS_DEPRECATED __attribute__((deprecated))
131#else
132#define MBEDTLS_DEPRECATED
133#endif
134/**
135 * \brief SHA-1 context setup
136 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100137 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100138 *
139 * \param ctx context to be initialized
140 */
141MBEDTLS_DEPRECATED static inline void mbedtls_sha1_starts(
142 mbedtls_sha1_context *ctx )
143{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100144 mbedtls_sha1_starts_ret( ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100145}
146
147/**
148 * \brief SHA-1 process buffer
149 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100150 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100151 *
152 * \param ctx SHA-1 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100153 * \param input buffer holding the data
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100154 * \param ilen length of the input data
155 */
156MBEDTLS_DEPRECATED static inline void mbedtls_sha1_update(
157 mbedtls_sha1_context *ctx,
158 const unsigned char *input,
159 size_t ilen )
160{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100161 mbedtls_sha1_update_ret( ctx, input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100162}
163
164/**
165 * \brief SHA-1 final digest
166 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100167 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100168 *
169 * \param ctx SHA-1 context
170 * \param output SHA-1 checksum result
171 */
172MBEDTLS_DEPRECATED static inline void mbedtls_sha1_finish(
173 mbedtls_sha1_context *ctx,
174 unsigned char output[20] )
175{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100176 mbedtls_sha1_finish_ret( ctx, output );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100177}
178
179/**
180 * \brief SHA-1 process data block (internal use only)
181 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100182 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100183 *
184 * \param ctx SHA-1 context
185 * \param data buffer holding one block of data
186 */
187MBEDTLS_DEPRECATED static inline void mbedtls_sha1_process(
188 mbedtls_sha1_context *ctx,
189 const unsigned char data[64] )
190{
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100191 mbedtls_internal_sha1_process( ctx, data );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100192}
193
194#undef MBEDTLS_DEPRECATED
195#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200196
197#ifdef __cplusplus
198}
199#endif
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#else /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200202#include "sha1_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#endif /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200204
205#ifdef __cplusplus
206extern "C" {
207#endif
208
Paul Bakker5121ce52009-01-03 21:22:43 +0000209/**
210 * \brief Output = SHA-1( input buffer )
211 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100212 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 * \param ilen length of the input data
214 * \param output SHA-1 checksum result
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100215 *
216 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000217 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100218int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100219 size_t ilen,
220 unsigned char output[20] );
221
222#if !defined(MBEDTLS_DEPRECATED_REMOVED)
223#if defined(MBEDTLS_DEPRECATED_WARNING)
224#define MBEDTLS_DEPRECATED __attribute__((deprecated))
225#else
226#define MBEDTLS_DEPRECATED
227#endif
228/**
229 * \brief Output = SHA-1( input buffer )
230 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100231 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100232 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100233 * \param input buffer holding the data
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100234 * \param ilen length of the input data
235 * \param output SHA-1 checksum result
236 */
237MBEDTLS_DEPRECATED static inline void mbedtls_sha1( const unsigned char *input,
238 size_t ilen,
239 unsigned char output[20] )
240{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100241 mbedtls_sha1_ret( input, ilen, output );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100242}
243
244#undef MBEDTLS_DEPRECATED
245#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
247/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 * \brief Checkup routine
249 *
250 * \return 0 if successful, or 1 if the test failed
251 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
Paul Bakker5121ce52009-01-03 21:22:43 +0000254#ifdef __cplusplus
255}
256#endif
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#endif /* mbedtls_sha1.h */