blob: 7453c44d4dfbef25ca1fbc9ddf166bcfe5b2afb8 [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
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_SHA512_H
25#define MBEDTLS_SHA512_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 Bakker5121ce52009-01-03 21:22:43 +000035
Gilles Peskinea381fe82018-01-23 18:16:11 +010036#define MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED -0x0039 /**< SHA-512 hardware accelerator failed */
37
Andres Amaya Garcia614c6892017-05-02 12:07:26 +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_SHA512_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-512 context structure
53 */
54typedef struct
55{
Paul Bakker5c2364c2012-10-01 14:41:15 +000056 uint64_t total[2]; /*!< number of bytes processed */
57 uint64_t state[8]; /*!< intermediate digest state */
Paul Bakker5121ce52009-01-03 21:22:43 +000058 unsigned char buffer[128]; /*!< data block being processed */
Paul Bakker5121ce52009-01-03 21:22:43 +000059 int is384; /*!< 0 => SHA-512, else SHA-384 */
60}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061mbedtls_sha512_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Paul Bakker5121ce52009-01-03 21:22:43 +000063/**
Paul Bakker5b4af392014-06-26 12:09:34 +020064 * \brief Initialize SHA-512 context
65 *
66 * \param ctx SHA-512 context to be initialized
67 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068void mbedtls_sha512_init( mbedtls_sha512_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020069
70/**
71 * \brief Clear SHA-512 context
72 *
73 * \param ctx SHA-512 context to be cleared
74 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075void mbedtls_sha512_free( mbedtls_sha512_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020076
77/**
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020078 * \brief Clone (the state of) a SHA-512 context
79 *
80 * \param dst The destination context
81 * \param src The context to be cloned
82 */
83void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
84 const mbedtls_sha512_context *src );
85
86/**
Paul Bakker5121ce52009-01-03 21:22:43 +000087 * \brief SHA-512 context setup
88 *
89 * \param ctx context to be initialized
90 * \param is384 0 = use SHA512, 1 = use SHA384
Andres Amaya Garcia614c6892017-05-02 12:07:26 +010091 *
92 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +000093 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010094int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96/**
97 * \brief SHA-512 process buffer
98 *
99 * \param ctx SHA-512 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100100 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 * \param ilen length of the input data
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100102 *
103 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100105int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100106 const unsigned char *input,
107 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109/**
110 * \brief SHA-512 final digest
111 *
112 * \param ctx SHA-512 context
113 * \param output SHA-384/512 checksum result
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100114 *
115 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100117int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100118 unsigned char output[64] );
119
120/**
121 * \brief SHA-512 process data block (internal use only)
122 *
123 * \param ctx SHA-512 context
124 * \param data buffer holding one block of data
125 *
126 * \return 0 if successful
127 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100128int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
129 const unsigned char data[128] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +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-512 context setup
139 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100140 * \deprecated Superseded by mbedtls_sha512_starts_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100141 *
142 * \param ctx context to be initialized
143 * \param is384 0 = use SHA512, 1 = use SHA384
144 */
145MBEDTLS_DEPRECATED static inline void mbedtls_sha512_starts(
146 mbedtls_sha512_context *ctx,
147 int is384 )
148{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100149 mbedtls_sha512_starts_ret( ctx, is384 );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100150}
151
152/**
153 * \brief SHA-512 process buffer
154 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100155 * \deprecated Superseded by mbedtls_sha512_update_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100156 *
157 * \param ctx SHA-512 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100158 * \param input buffer holding the data
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100159 * \param ilen length of the input data
160 */
161MBEDTLS_DEPRECATED static inline void mbedtls_sha512_update(
162 mbedtls_sha512_context *ctx,
163 const unsigned char *input,
164 size_t ilen )
165{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100166 mbedtls_sha512_update_ret( ctx, input, ilen );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100167}
168
169/**
170 * \brief SHA-512 final digest
171 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100172 * \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100173 *
174 * \param ctx SHA-512 context
175 * \param output SHA-384/512 checksum result
176 */
177MBEDTLS_DEPRECATED static inline void mbedtls_sha512_finish(
178 mbedtls_sha512_context *ctx,
179 unsigned char output[64] )
180{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100181 mbedtls_sha512_finish_ret( ctx, output );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100182}
183
184/**
185 * \brief SHA-512 process data block (internal use only)
186 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100187 * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100188 *
189 * \param ctx SHA-512 context
190 * \param data buffer holding one block of data
191 */
192MBEDTLS_DEPRECATED static inline void mbedtls_sha512_process(
193 mbedtls_sha512_context *ctx,
194 const unsigned char data[128] )
195{
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100196 mbedtls_internal_sha512_process( ctx, data );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100197}
198
199#undef MBEDTLS_DEPRECATED
200#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
Paul Bakker90995b52013-06-24 19:20:35 +0200202#ifdef __cplusplus
203}
204#endif
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#else /* MBEDTLS_SHA512_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200207#include "sha512_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#endif /* MBEDTLS_SHA512_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200209
210#ifdef __cplusplus
211extern "C" {
212#endif
213
Paul Bakker5121ce52009-01-03 21:22:43 +0000214/**
215 * \brief Output = SHA-512( input buffer )
216 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100217 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 * \param ilen length of the input data
219 * \param output SHA-384/512 checksum result
220 * \param is384 0 = use SHA512, 1 = use SHA384
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100221 *
222 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100224int mbedtls_sha512_ret( const unsigned char *input,
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100225 size_t ilen,
226 unsigned char output[64],
227 int is384 );
228
229#if !defined(MBEDTLS_DEPRECATED_REMOVED)
230#if defined(MBEDTLS_DEPRECATED_WARNING)
231#define MBEDTLS_DEPRECATED __attribute__((deprecated))
232#else
233#define MBEDTLS_DEPRECATED
234#endif
235/**
236 * \brief Output = SHA-512( input buffer )
237 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100238 * \deprecated Superseded by mbedtls_sha512_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100239 *
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100240 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 * \param ilen length of the input data
242 * \param output SHA-384/512 checksum result
243 * \param is384 0 = use SHA512, 1 = use SHA384
244 */
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100245MBEDTLS_DEPRECATED static inline void mbedtls_sha512(
246 const unsigned char *input,
247 size_t ilen,
248 unsigned char output[64],
249 int is384 )
250{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100251 mbedtls_sha512_ret( input, ilen, output, is384 );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100252}
253
254#undef MBEDTLS_DEPRECATED
255#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
257/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 * \brief Checkup routine
259 *
260 * \return 0 if successful, or 1 if the test failed
261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262int mbedtls_sha512_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
Paul Bakker5121ce52009-01-03 21:22:43 +0000264#ifdef __cplusplus
265}
266#endif
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#endif /* mbedtls_sha512.h */