blob: c4eb5a93c78d6340d77e60d996bc7d030eb249fa [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
Paul Bakker407a0da2013-06-27 14:29:21 +020038#ifdef __cplusplus
39extern "C" {
40#endif
41
Ron Eldorb2aacec2017-05-18 16:53:08 +030042#if !defined(MBEDTLS_SHA512_ALT)
43// Regular implementation
44//
45
Paul Bakker5121ce52009-01-03 21:22:43 +000046/**
Rose Zadik27ff1202018-01-26 11:01:31 +000047 * \brief The SHA-512 context structure.
48 *
49 * The structure is used both for SHA-384 and for SHA-512
50 * checksum calculations. The choice between these two is
51 * made in the call to mbedtls_sha512_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000052 */
53typedef struct
54{
Rose Zadik27ff1202018-01-26 11:01:31 +000055 uint64_t total[2]; /*!< The number of Bytes processed. */
56 uint64_t state[8]; /*!< The intermediate digest state. */
57 unsigned char buffer[128]; /*!< The data block being processed. */
58 int is384; /*!< Determines which function to use.
59 * <ul><li>0: Use SHA-512.</li>
60 * <li>1: Use SHA-384.</li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +000061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062mbedtls_sha512_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Ron Eldorb2aacec2017-05-18 16:53:08 +030064#else /* MBEDTLS_SHA512_ALT */
65#include "sha512_alt.h"
66#endif /* MBEDTLS_SHA512_ALT */
67
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 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000159MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
160 int is384 );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100161
162/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000163 * \brief This function feeds an input buffer into an ongoing
164 * SHA-512 checksum calculation.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100165 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100166 * \deprecated Superseded by mbedtls_sha512_update_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100167 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000168 * \param ctx The SHA-512 context.
169 * \param input The buffer holding the data.
170 * \param ilen The length of the input data.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100171 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000172MBEDTLS_DEPRECATED void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
173 const unsigned char *input,
174 size_t ilen );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100175
176/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000177 * \brief This function finishes the SHA-512 operation, and writes
178 * the result to the output buffer.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100179 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100180 * \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100181 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000182 * \param ctx The SHA-512 context.
183 * \param output The SHA-384 or SHA-512 checksum result.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100184 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000185MBEDTLS_DEPRECATED void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
186 unsigned char output[64] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100187
188/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000189 * \brief This function processes a single data block within
190 * the ongoing SHA-512 computation. This function is for
191 * internal use only.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100192 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100193 * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100194 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000195 * \param ctx The SHA-512 context.
196 * \param data The buffer holding one block of data.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100197 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000198MBEDTLS_DEPRECATED void mbedtls_sha512_process(
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100199 mbedtls_sha512_context *ctx,
Jaeden Amero041039f2018-02-19 15:28:08 +0000200 const unsigned char data[128] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100201
202#undef MBEDTLS_DEPRECATED
203#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
205/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000206 * \brief This function calculates the SHA-512 or SHA-384
207 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000209 * The function allocates the context, performs the
210 * calculation, and frees the context.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100211 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000212 * The SHA-512 result is calculated as
213 * output = SHA-512(input buffer).
214 *
215 * \param input The buffer holding the input data.
216 * \param ilen The length of the input data.
217 * \param output The SHA-384 or SHA-512 checksum result.
218 * \param is384 Determines which function to use.
219 * <ul><li>0: Use SHA-512.</li>
220 * <li>1: Use SHA-384.</li></ul>
221 *
222 * \return \c 0 on success.
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/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000236 * \brief This function calculates the SHA-512 or SHA-384
237 * checksum of a buffer.
238 *
239 * The function allocates the context, performs the
240 * calculation, and frees the context.
241 *
242 * The SHA-512 result is calculated as
243 * output = SHA-512(input buffer).
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100244 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100245 * \deprecated Superseded by mbedtls_sha512_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100246 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000247 * \param input The buffer holding the data.
248 * \param ilen The length of the input data.
249 * \param output The SHA-384 or SHA-512 checksum result.
250 * \param is384 Determines which function to use.
251 * <ul><li>0: Use SHA-512.</li>
252 * <li>1: Use SHA-384.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000254MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input,
255 size_t ilen,
256 unsigned char output[64],
257 int is384 );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100258
259#undef MBEDTLS_DEPRECATED
260#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Rose Zadik27ff1202018-01-26 11:01:31 +0000261 /**
262 * \brief The SHA-384 or SHA-512 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000264 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266int mbedtls_sha512_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000267
Paul Bakker5121ce52009-01-03 21:22:43 +0000268#ifdef __cplusplus
269}
270#endif
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#endif /* mbedtls_sha512.h */