blob: 9dde5b89e90f9e11971f501f88bd391a4387ab97 [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
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010035#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
36 !defined(inline) && !defined(__cplusplus)
37#define inline __inline
38#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if !defined(MBEDTLS_SHA1_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020041// Regular implementation
42//
43
Paul Bakker407a0da2013-06-27 14:29:21 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Paul Bakker5121ce52009-01-03 21:22:43 +000048/**
49 * \brief SHA-1 context structure
50 */
51typedef struct
52{
Paul Bakker5c2364c2012-10-01 14:41:15 +000053 uint32_t total[2]; /*!< number of bytes processed */
54 uint32_t state[5]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000055 unsigned char buffer[64]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000056}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057mbedtls_sha1_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000058
Paul Bakker5121ce52009-01-03 21:22:43 +000059/**
Paul Bakker5b4af392014-06-26 12:09:34 +020060 * \brief Initialize SHA-1 context
61 *
62 * \param ctx SHA-1 context to be initialized
63 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064void mbedtls_sha1_init( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020065
66/**
67 * \brief Clear SHA-1 context
68 *
69 * \param ctx SHA-1 context to be cleared
70 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071void mbedtls_sha1_free( mbedtls_sha1_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020072
73/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020074 * \brief Clone (the state of) a SHA-1 context
75 *
76 * \param dst The destination context
77 * \param src The context to be cloned
78 */
79void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
80 const mbedtls_sha1_context *src );
81
82/**
Paul Bakker5121ce52009-01-03 21:22:43 +000083 * \brief SHA-1 context setup
84 *
85 * \param ctx context to be initialized
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010086 *
87 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000088 */
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010089int mbedtls_sha1_starts_ext( mbedtls_sha1_context *ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +000090
91/**
92 * \brief SHA-1 process buffer
93 *
94 * \param ctx SHA-1 context
95 * \param input buffer holding the data
96 * \param ilen length of the input data
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010097 *
98 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000099 */
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100100int mbedtls_sha1_update_ext( mbedtls_sha1_context *ctx,
101 const unsigned char *input,
102 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104/**
105 * \brief SHA-1 final digest
106 *
107 * \param ctx SHA-1 context
108 * \param output SHA-1 checksum result
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100109 *
110 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 */
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100112int mbedtls_sha1_finish_ext( mbedtls_sha1_context *ctx,
113 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100115/**
116 * \brief SHA-1 process data block (internal use only)
117 *
118 * \param ctx SHA-1 context
119 * \param data buffer holding one block of data
120 *
121 * \return 0 if successful
122 */
123int mbedtls_sha1_process_ext( mbedtls_sha1_context *ctx,
124 const unsigned char data[64] );
125
126#if !defined(MBEDTLS_DEPRECATED_REMOVED)
127#if defined(MBEDTLS_DEPRECATED_WARNING)
128#define MBEDTLS_DEPRECATED __attribute__((deprecated))
129#else
130#define MBEDTLS_DEPRECATED
131#endif
132/**
133 * \brief SHA-1 context setup
134 *
135 * \deprecated Superseded by mbedtls_sha1_starts_ext() in 2.5.0
136 *
137 * \param ctx context to be initialized
138 */
139MBEDTLS_DEPRECATED static inline void mbedtls_sha1_starts(
140 mbedtls_sha1_context *ctx )
141{
142 mbedtls_sha1_starts_ext( ctx );
143}
144
145/**
146 * \brief SHA-1 process buffer
147 *
148 * \deprecated Superseded by mbedtls_sha1_update_ext() in 2.5.0
149 *
150 * \param ctx SHA-1 context
151 * \param input buffer holding the data
152 * \param ilen length of the input data
153 */
154MBEDTLS_DEPRECATED static inline void mbedtls_sha1_update(
155 mbedtls_sha1_context *ctx,
156 const unsigned char *input,
157 size_t ilen )
158{
159 mbedtls_sha1_update_ext( ctx, input, ilen );
160}
161
162/**
163 * \brief SHA-1 final digest
164 *
165 * \deprecated Superseded by mbedtls_sha1_finish_ext() in 2.5.0
166 *
167 * \param ctx SHA-1 context
168 * \param output SHA-1 checksum result
169 */
170MBEDTLS_DEPRECATED static inline void mbedtls_sha1_finish(
171 mbedtls_sha1_context *ctx,
172 unsigned char output[20] )
173{
174 mbedtls_sha1_finish_ext( ctx, output );
175}
176
177/**
178 * \brief SHA-1 process data block (internal use only)
179 *
180 * \deprecated Superseded by mbedtls_sha1_process_ext() in 2.5.0
181 *
182 * \param ctx SHA-1 context
183 * \param data buffer holding one block of data
184 */
185MBEDTLS_DEPRECATED static inline void mbedtls_sha1_process(
186 mbedtls_sha1_context *ctx,
187 const unsigned char data[64] )
188{
189 mbedtls_sha1_process_ext( ctx, data );
190}
191
192#undef MBEDTLS_DEPRECATED
193#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200194
195#ifdef __cplusplus
196}
197#endif
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#else /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200200#include "sha1_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#endif /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200202
203#ifdef __cplusplus
204extern "C" {
205#endif
206
Paul Bakker5121ce52009-01-03 21:22:43 +0000207/**
208 * \brief Output = SHA-1( input buffer )
209 *
210 * \param input buffer holding the data
211 * \param ilen length of the input data
212 * \param output SHA-1 checksum result
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100213 *
214 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000215 */
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100216int mbedtls_sha1_ext( const unsigned char *input,
217 size_t ilen,
218 unsigned char output[20] );
219
220#if !defined(MBEDTLS_DEPRECATED_REMOVED)
221#if defined(MBEDTLS_DEPRECATED_WARNING)
222#define MBEDTLS_DEPRECATED __attribute__((deprecated))
223#else
224#define MBEDTLS_DEPRECATED
225#endif
226/**
227 * \brief Output = SHA-1( input buffer )
228 *
229 * \deprecated Superseded by mbedtls_sha1_ext() in 2.5.0
230 *
231 * \param input buffer holding the data
232 * \param ilen length of the input data
233 * \param output SHA-1 checksum result
234 */
235MBEDTLS_DEPRECATED static inline void mbedtls_sha1( const unsigned char *input,
236 size_t ilen,
237 unsigned char output[20] )
238{
239 mbedtls_sha1_ext( input, ilen, output );
240}
241
242#undef MBEDTLS_DEPRECATED
243#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000246 * \brief Checkup routine
247 *
248 * \return 0 if successful, or 1 if the test failed
249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
Paul Bakker5121ce52009-01-03 21:22:43 +0000252#ifdef __cplusplus
253}
254#endif
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#endif /* mbedtls_sha1.h */