blob: 4d3a1640188ee05d39f60a6540335208a9b197eb [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
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, 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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +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/**
52 * \brief SHA-1 context structure
53 */
54typedef struct
55{
Paul Bakker5c2364c2012-10-01 14:41:15 +000056 uint32_t total[2]; /*!< number of bytes processed */
57 uint32_t state[5]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000058 unsigned char buffer[64]; /*!< 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/**
Paul Bakker5b4af392014-06-26 12:09:34 +020063 * \brief Initialize SHA-1 context
64 *
65 * \param ctx SHA-1 context to be initialized
66 */
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/**
70 * \brief Clear SHA-1 context
71 *
72 * \param ctx SHA-1 context to be cleared
73 */
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/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020077 * \brief Clone (the state of) a SHA-1 context
78 *
79 * \param dst The destination context
80 * \param src The context to be cloned
81 */
82void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
83 const mbedtls_sha1_context *src );
84
85/**
Paul Bakker5121ce52009-01-03 21:22:43 +000086 * \brief SHA-1 context setup
87 *
88 * \param ctx context to be initialized
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +010089 *
90 * \return 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/**
95 * \brief SHA-1 process buffer
96 *
97 * \param ctx SHA-1 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +010098 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +000099 * \param ilen length of the input data
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100100 *
101 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100103int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100104 const unsigned char *input,
105 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107/**
108 * \brief SHA-1 final digest
109 *
110 * \param ctx SHA-1 context
111 * \param output SHA-1 checksum result
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100112 *
113 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100115int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100116 unsigned char output[20] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100118/**
119 * \brief SHA-1 process data block (internal use only)
120 *
121 * \param ctx SHA-1 context
122 * \param data buffer holding one block of data
123 *
124 * \return 0 if successful
125 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100126int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
127 const unsigned char data[64] );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100128
129#if !defined(MBEDTLS_DEPRECATED_REMOVED)
130#if defined(MBEDTLS_DEPRECATED_WARNING)
131#define MBEDTLS_DEPRECATED __attribute__((deprecated))
132#else
133#define MBEDTLS_DEPRECATED
134#endif
135/**
136 * \brief SHA-1 context setup
137 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100138 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100139 *
140 * \param ctx context to be initialized
141 */
142MBEDTLS_DEPRECATED static inline void mbedtls_sha1_starts(
143 mbedtls_sha1_context *ctx )
144{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100145 mbedtls_sha1_starts_ret( ctx );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100146}
147
148/**
149 * \brief SHA-1 process buffer
150 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100151 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100152 *
153 * \param ctx SHA-1 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100154 * \param input buffer holding the data
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100155 * \param ilen length of the input data
156 */
157MBEDTLS_DEPRECATED static inline void mbedtls_sha1_update(
158 mbedtls_sha1_context *ctx,
159 const unsigned char *input,
160 size_t ilen )
161{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100162 mbedtls_sha1_update_ret( ctx, input, ilen );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100163}
164
165/**
166 * \brief SHA-1 final digest
167 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100168 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100169 *
170 * \param ctx SHA-1 context
171 * \param output SHA-1 checksum result
172 */
173MBEDTLS_DEPRECATED static inline void mbedtls_sha1_finish(
174 mbedtls_sha1_context *ctx,
175 unsigned char output[20] )
176{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100177 mbedtls_sha1_finish_ret( ctx, output );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100178}
179
180/**
181 * \brief SHA-1 process data block (internal use only)
182 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100183 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100184 *
185 * \param ctx SHA-1 context
186 * \param data buffer holding one block of data
187 */
188MBEDTLS_DEPRECATED static inline void mbedtls_sha1_process(
189 mbedtls_sha1_context *ctx,
190 const unsigned char data[64] )
191{
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100192 mbedtls_internal_sha1_process( ctx, data );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100193}
194
195#undef MBEDTLS_DEPRECATED
196#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200197
198#ifdef __cplusplus
199}
200#endif
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#else /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200203#include "sha1_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#endif /* MBEDTLS_SHA1_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200205
206#ifdef __cplusplus
207extern "C" {
208#endif
209
Paul Bakker5121ce52009-01-03 21:22:43 +0000210/**
211 * \brief Output = SHA-1( input buffer )
212 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100213 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 * \param ilen length of the input data
215 * \param output SHA-1 checksum result
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100216 *
217 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100219int mbedtls_sha1_ret( const unsigned char *input,
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100220 size_t ilen,
221 unsigned char output[20] );
222
223#if !defined(MBEDTLS_DEPRECATED_REMOVED)
224#if defined(MBEDTLS_DEPRECATED_WARNING)
225#define MBEDTLS_DEPRECATED __attribute__((deprecated))
226#else
227#define MBEDTLS_DEPRECATED
228#endif
229/**
230 * \brief Output = SHA-1( input buffer )
231 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100232 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100233 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100234 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 * \param ilen length of the input data
236 * \param output SHA-1 checksum result
237 */
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100238MBEDTLS_DEPRECATED static inline void mbedtls_sha1( const unsigned char *input,
239 size_t ilen,
240 unsigned char output[20] )
241{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100242 mbedtls_sha1_ret( input, ilen, output );
Andres Amaya Garcia034ea7e2017-04-28 15:14:50 +0100243}
244
245#undef MBEDTLS_DEPRECATED
246#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 * \brief Checkup routine
250 *
251 * \return 0 if successful, or 1 if the test failed
252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253int mbedtls_sha1_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000254
Paul Bakker5121ce52009-01-03 21:22:43 +0000255#ifdef __cplusplus
256}
257#endif
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#endif /* mbedtls_sha1.h */