blob: 0fbdb3b71748a36eac49fb07a27113d987ee6b3c [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file sha512.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +00004 * \brief SHA-384 and SHA-512 cryptographic hash function
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
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_SHA512_H
24#define MBEDTLS_SHA512_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 Bakker5121ce52009-01-03 21:22:43 +000034
Andres Amaya Garcia614c6892017-05-02 12:07:26 +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_SHA512_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-512 context structure
50 */
51typedef struct
52{
Paul Bakker5c2364c2012-10-01 14:41:15 +000053 uint64_t total[2]; /*!< number of bytes processed */
54 uint64_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000055 unsigned char buffer[128]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000056 int is384; /*!< 0 => SHA-512, else SHA-384 */
57}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058mbedtls_sha512_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Paul Bakker5121ce52009-01-03 21:22:43 +000060/**
Paul Bakker5b4af392014-06-26 12:09:34 +020061 * \brief Initialize SHA-512 context
62 *
63 * \param ctx SHA-512 context to be initialized
64 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065void mbedtls_sha512_init( mbedtls_sha512_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020066
67/**
68 * \brief Clear SHA-512 context
69 *
70 * \param ctx SHA-512 context to be cleared
71 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072void mbedtls_sha512_free( mbedtls_sha512_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020073
74/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020075 * \brief Clone (the state of) a SHA-512 context
76 *
77 * \param dst The destination context
78 * \param src The context to be cloned
79 */
80void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
81 const mbedtls_sha512_context *src );
82
83/**
Paul Bakker5121ce52009-01-03 21:22:43 +000084 * \brief SHA-512 context setup
85 *
86 * \param ctx context to be initialized
87 * \param is384 0 = use SHA512, 1 = use SHA384
Andres Amaya Garcia614c6892017-05-02 12:07:26 +010088 *
89 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000090 */
Andres Amaya Garcia614c6892017-05-02 12:07:26 +010091int mbedtls_sha512_starts_ext( mbedtls_sha512_context *ctx, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93/**
94 * \brief SHA-512 process buffer
95 *
96 * \param ctx SHA-512 context
97 * \param input buffer holding the data
98 * \param ilen length of the input data
Andres Amaya Garcia614c6892017-05-02 12:07:26 +010099 *
100 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 */
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100102int mbedtls_sha512_update_ext( mbedtls_sha512_context *ctx,
103 const unsigned char *input,
104 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
106/**
107 * \brief SHA-512 final digest
108 *
109 * \param ctx SHA-512 context
110 * \param output SHA-384/512 checksum result
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100111 *
112 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 */
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100114int mbedtls_sha512_finish_ext( mbedtls_sha512_context *ctx,
115 unsigned char output[64] );
116
117/**
118 * \brief SHA-512 process data block (internal use only)
119 *
120 * \param ctx SHA-512 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_sha512_process( mbedtls_sha512_context *ctx,
126 const unsigned char data[128] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +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-512 context setup
136 *
137 * \deprecated Superseded by mbedtls_sha512_starts_ext() in 2.5.0
138 *
139 * \param ctx context to be initialized
140 * \param is384 0 = use SHA512, 1 = use SHA384
141 */
142MBEDTLS_DEPRECATED static inline void mbedtls_sha512_starts(
143 mbedtls_sha512_context *ctx,
144 int is384 )
145{
146 mbedtls_sha512_starts_ext( ctx, is384 );
147}
148
149/**
150 * \brief SHA-512 process buffer
151 *
152 * \deprecated Superseded by mbedtls_sha512_update_ext() in 2.5.0
153 *
154 * \param ctx SHA-512 context
155 * \param input buffer holding the data
156 * \param ilen length of the input data
157 */
158MBEDTLS_DEPRECATED static inline void mbedtls_sha512_update(
159 mbedtls_sha512_context *ctx,
160 const unsigned char *input,
161 size_t ilen )
162{
163 mbedtls_sha512_update_ext( ctx, input, ilen );
164}
165
166/**
167 * \brief SHA-512 final digest
168 *
169 * \deprecated Superseded by mbedtls_sha512_finish_ext() in 2.5.0
170 *
171 * \param ctx SHA-512 context
172 * \param output SHA-384/512 checksum result
173 */
174MBEDTLS_DEPRECATED static inline void mbedtls_sha512_finish(
175 mbedtls_sha512_context *ctx,
176 unsigned char output[64] )
177{
178 mbedtls_sha512_finish_ext( ctx, output );
179}
180
181/**
182 * \brief SHA-512 process data block (internal use only)
183 *
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100184 * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.5.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100185 *
186 * \param ctx SHA-512 context
187 * \param data buffer holding one block of data
188 */
189MBEDTLS_DEPRECATED static inline void mbedtls_sha512_process(
190 mbedtls_sha512_context *ctx,
191 const unsigned char data[128] )
192{
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100193 mbedtls_internal_sha512_process( ctx, data );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100194}
195
196#undef MBEDTLS_DEPRECATED
197#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Paul Bakker90995b52013-06-24 19:20:35 +0200199#ifdef __cplusplus
200}
201#endif
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#else /* MBEDTLS_SHA512_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200204#include "sha512_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#endif /* MBEDTLS_SHA512_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200206
207#ifdef __cplusplus
208extern "C" {
209#endif
210
Paul Bakker5121ce52009-01-03 21:22:43 +0000211/**
212 * \brief Output = SHA-512( input buffer )
213 *
214 * \param input buffer holding the data
215 * \param ilen length of the input data
216 * \param output SHA-384/512 checksum result
217 * \param is384 0 = use SHA512, 1 = use SHA384
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100218 *
219 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 */
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100221int mbedtls_sha512_ext( const unsigned char *input,
222 size_t ilen,
223 unsigned char output[64],
224 int is384 );
225
226#if !defined(MBEDTLS_DEPRECATED_REMOVED)
227#if defined(MBEDTLS_DEPRECATED_WARNING)
228#define MBEDTLS_DEPRECATED __attribute__((deprecated))
229#else
230#define MBEDTLS_DEPRECATED
231#endif
232/**
233 * \brief Output = SHA-512( input buffer )
234 *
235 * \deprecated Superseded by mbedtls_sha512_ext() in 2.5.0
236 *
237 * \param input buffer holding the data
238 * \param ilen length of the input data
239 * \param output SHA-384/512 checksum result
240 * \param is384 0 = use SHA512, 1 = use SHA384
241 */
242MBEDTLS_DEPRECATED static inline void mbedtls_sha512(
243 const unsigned char *input,
244 size_t ilen,
245 unsigned char output[64],
246 int is384 )
247{
248 mbedtls_sha512_ext( input, ilen, output, is384 );
249}
250
251#undef MBEDTLS_DEPRECATED
252#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000253
254/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 * \brief Checkup routine
256 *
257 * \return 0 if successful, or 1 if the test failed
258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259int mbedtls_sha512_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
Paul Bakker5121ce52009-01-03 21:22:43 +0000261#ifdef __cplusplus
262}
263#endif
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#endif /* mbedtls_sha512.h */