blob: 52ae204d442e0a607101ee5d8027efa0e6b0e040 [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 *
Rose Zadik27ff1202018-01-26 11:01:31 +00004 * \brief The SHA-384 and SHA-512 cryptographic hash function.
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Rose Zadik27ff1202018-01-26 11:01:31 +00007 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), 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 *
Rose Zadik27ff1202018-01-26 11:01:31 +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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_SHA512_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/**
Rose Zadik27ff1202018-01-26 11:01:31 +000051 * \brief The SHA-512 context structure.
52 *
53 * The structure is used both for SHA-384 and for SHA-512
54 * checksum calculations. The choice between these two is
55 * made in the call to mbedtls_sha512_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000056 */
57typedef struct
58{
Rose Zadik27ff1202018-01-26 11:01:31 +000059 uint64_t total[2]; /*!< The number of Bytes processed. */
60 uint64_t state[8]; /*!< The intermediate digest state. */
61 unsigned char buffer[128]; /*!< The data block being processed. */
62 int is384; /*!< Determines which function to use.
63 * <ul><li>0: Use SHA-512.</li>
64 * <li>1: Use SHA-384.</li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +000065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066mbedtls_sha512_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Paul Bakker5121ce52009-01-03 21:22:43 +000068/**
Rose Zadik27ff1202018-01-26 11:01:31 +000069 * \brief This function initializes a SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020070 *
Rose Zadik27ff1202018-01-26 11:01:31 +000071 * \param ctx The SHA-512 context to initialize.
Paul Bakker5b4af392014-06-26 12:09:34 +020072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073void mbedtls_sha512_init( mbedtls_sha512_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020074
75/**
Rose Zadik27ff1202018-01-26 11:01:31 +000076 * \brief This function clears a SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020077 *
Rose Zadik27ff1202018-01-26 11:01:31 +000078 * \param ctx The SHA-512 context to clear.
Paul Bakker5b4af392014-06-26 12:09:34 +020079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_sha512_free( mbedtls_sha512_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020081
82/**
Rose Zadik27ff1202018-01-26 11:01:31 +000083 * \brief This function clones the state of a SHA-512 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020084 *
Rose Zadik27ff1202018-01-26 11:01:31 +000085 * \param dst The destination context.
86 * \param src The context to clone.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020087 */
88void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
89 const mbedtls_sha512_context *src );
90
91/**
Rose Zadik27ff1202018-01-26 11:01:31 +000092 * \brief This function starts a SHA-384 or SHA-512 checksum
93 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000094 *
Rose Zadik27ff1202018-01-26 11:01:31 +000095 * \param ctx The SHA-512 context to initialize.
96 * \param is384 Determines which function to use.
97 * <ul><li>0: Use SHA-512.</li>
98 * <li>1: Use SHA-384.</li></ul>
Andres Amaya Garcia614c6892017-05-02 12:07:26 +010099 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000100 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100102int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000105 * \brief This function feeds an input buffer into an ongoing
106 * SHA-512 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000108 * \param ctx The SHA-512 context.
109 * \param input The buffer holding the input data.
110 * \param ilen The length of the input data.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100111 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000112 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100114int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
Rose Zadik27ff1202018-01-26 11:01:31 +0000115 const unsigned char *input,
116 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
118/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000119 * \brief This function finishes the SHA-512 operation, and writes
120 * the result to the output buffer. This function is for
121 * internal use only.
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000123 * \param ctx The SHA-512 context.
124 * \param output The SHA-384 or SHA-512 checksum result.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100125 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000126 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100128int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100129 unsigned char output[64] );
130
131/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000132 * \brief This function processes a single data block within
133 * the ongoing SHA-512 computation.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100134 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000135 * \param ctx The SHA-512 context.
136 * \param data The buffer holding one block of data.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100137 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000138 * \return \c 0 on success.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100139 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100140int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
141 const unsigned char data[128] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100142#if !defined(MBEDTLS_DEPRECATED_REMOVED)
143#if defined(MBEDTLS_DEPRECATED_WARNING)
144#define MBEDTLS_DEPRECATED __attribute__((deprecated))
145#else
146#define MBEDTLS_DEPRECATED
147#endif
148/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000149 * \brief This function starts a SHA-384 or SHA-512 checksum
150 * calculation.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100151 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100152 * \deprecated Superseded by mbedtls_sha512_starts_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100153 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000154 * \param ctx The SHA-512 context to initialize.
155 * \param is384 Determines which function to use.
156 * <ul><li>0: Use SHA-512.</li>
157 * <li>1: Use SHA-384.</li></ul>
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100158 */
159MBEDTLS_DEPRECATED static inline void mbedtls_sha512_starts(
160 mbedtls_sha512_context *ctx,
161 int is384 )
162{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100163 mbedtls_sha512_starts_ret( ctx, is384 );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100164}
165
166/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000167 * \brief This function feeds an input buffer into an ongoing
168 * SHA-512 checksum calculation.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100169 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100170 * \deprecated Superseded by mbedtls_sha512_update_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100171 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000172 * \param ctx The SHA-512 context.
173 * \param input The buffer holding the data.
174 * \param ilen The length of the input data.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100175 */
176MBEDTLS_DEPRECATED static inline void mbedtls_sha512_update(
177 mbedtls_sha512_context *ctx,
178 const unsigned char *input,
179 size_t ilen )
180{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100181 mbedtls_sha512_update_ret( ctx, input, ilen );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100182}
183
184/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000185 * \brief This function finishes the SHA-512 operation, and writes
186 * the result to the output buffer.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100187 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100188 * \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100189 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000190 * \param ctx The SHA-512 context.
191 * \param output The SHA-384 or SHA-512 checksum result.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100192 */
193MBEDTLS_DEPRECATED static inline void mbedtls_sha512_finish(
194 mbedtls_sha512_context *ctx,
195 unsigned char output[64] )
196{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100197 mbedtls_sha512_finish_ret( ctx, output );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100198}
199
200/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000201 * \brief This function processes a single data block within
202 * the ongoing SHA-512 computation. This function is for
203 * internal use only.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100204 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100205 * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100206 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000207 * \param ctx The SHA-512 context.
208 * \param data The buffer holding one block of data.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100209 */
210MBEDTLS_DEPRECATED static inline void mbedtls_sha512_process(
211 mbedtls_sha512_context *ctx,
212 const unsigned char data[128] )
213{
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100214 mbedtls_internal_sha512_process( ctx, data );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100215}
216
217#undef MBEDTLS_DEPRECATED
218#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Paul Bakker90995b52013-06-24 19:20:35 +0200220#ifdef __cplusplus
221}
222#endif
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#else /* MBEDTLS_SHA512_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200225#include "sha512_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#endif /* MBEDTLS_SHA512_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200227
228#ifdef __cplusplus
229extern "C" {
230#endif
231
Paul Bakker5121ce52009-01-03 21:22:43 +0000232/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000233 * \brief This function calculates the SHA-512 or SHA-384
234 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000236 * The function allocates the context, performs the
237 * calculation, and frees the context.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100238 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000239 * The SHA-512 result is calculated as
240 * output = SHA-512(input buffer).
241 *
242 * \param input The buffer holding the input data.
243 * \param ilen The length of the input data.
244 * \param output The SHA-384 or SHA-512 checksum result.
245 * \param is384 Determines which function to use.
246 * <ul><li>0: Use SHA-512.</li>
247 * <li>1: Use SHA-384.</li></ul>
248 *
249 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100251int mbedtls_sha512_ret( const unsigned char *input,
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100252 size_t ilen,
253 unsigned char output[64],
254 int is384 );
255
256#if !defined(MBEDTLS_DEPRECATED_REMOVED)
257#if defined(MBEDTLS_DEPRECATED_WARNING)
258#define MBEDTLS_DEPRECATED __attribute__((deprecated))
259#else
260#define MBEDTLS_DEPRECATED
261#endif
262/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000263 * \brief This function calculates the SHA-512 or SHA-384
264 * checksum of a buffer.
265 *
266 * The function allocates the context, performs the
267 * calculation, and frees the context.
268 *
269 * The SHA-512 result is calculated as
270 * output = SHA-512(input buffer).
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100271 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100272 * \deprecated Superseded by mbedtls_sha512_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100273 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000274 * \param input The buffer holding the data.
275 * \param ilen The length of the input data.
276 * \param output The SHA-384 or SHA-512 checksum result.
277 * \param is384 Determines which function to use.
278 * <ul><li>0: Use SHA-512.</li>
279 * <li>1: Use SHA-384.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 */
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100281MBEDTLS_DEPRECATED static inline void mbedtls_sha512(
282 const unsigned char *input,
283 size_t ilen,
284 unsigned char output[64],
285 int is384 )
286{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100287 mbedtls_sha512_ret( input, ilen, output, is384 );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100288}
289
290#undef MBEDTLS_DEPRECATED
291#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Rose Zadik27ff1202018-01-26 11:01:31 +0000292 /**
293 * \brief The SHA-384 or SHA-512 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000295 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297int mbedtls_sha512_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000298
Paul Bakker5121ce52009-01-03 21:22:43 +0000299#ifdef __cplusplus
300}
301#endif
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#endif /* mbedtls_sha512.h */