blob: 9ff78ecf41801eba4b9c95fad41fd2088be740cf [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file sha512.h
Rose Zadik1a6275a2018-03-27 13:03:42 +01003 * \brief This file contains SHA-384 and SHA-512 definitions and functions.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00004 *
Rose Zadik1a6275a2018-03-27 13:03:42 +01005 * The Secure Hash Algorithms 384 and 512 (SHA-384 and SHA-512) cryptographic
6 * hash functions are defined in <em>FIPS 180-4: Secure Hash Standard (SHS)</em>.
Darryl Greena40a1012018-01-05 15:33:17 +00007 */
8/*
Bence Szépkútia2947ac2020-08-19 16:37:36 +02009 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020010 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
11 *
12 * This file is provided under the Apache License 2.0, or the
13 * GNU General Public License v2.0 or later.
14 *
15 * **********
16 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020017 *
18 * Licensed under the Apache License, Version 2.0 (the "License"); you may
19 * not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000029 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020030 * **********
31 *
32 * **********
33 * GNU General Public License v2.0 or later:
34 *
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License along
46 * with this program; if not, write to the Free Software Foundation, Inc.,
47 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
48 *
49 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_SHA512_H
52#define MBEDTLS_SHA512_H
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020055#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakker90995b52013-06-24 19:20:35 +020059
Rich Evans00ab4702015-02-06 13:43:58 +000060#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020061#include <stdint.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Ron Eldor9924bdc2018-10-04 10:59:13 +030063/* MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea381fe82018-01-23 18:16:11 +010064#define MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED -0x0039 /**< SHA-512 hardware accelerator failed */
Andres Amaya Garciaff1052e2018-12-10 10:28:10 +000065#define MBEDTLS_ERR_SHA512_BAD_INPUT_DATA -0x0075 /**< SHA-512 input data was malformed. */
Gilles Peskinea381fe82018-01-23 18:16:11 +010066
Paul Bakker407a0da2013-06-27 14:29:21 +020067#ifdef __cplusplus
68extern "C" {
69#endif
70
Ron Eldorb2aacec2017-05-18 16:53:08 +030071#if !defined(MBEDTLS_SHA512_ALT)
72// Regular implementation
73//
74
Paul Bakker5121ce52009-01-03 21:22:43 +000075/**
Rose Zadik27ff1202018-01-26 11:01:31 +000076 * \brief The SHA-512 context structure.
77 *
78 * The structure is used both for SHA-384 and for SHA-512
79 * checksum calculations. The choice between these two is
80 * made in the call to mbedtls_sha512_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000081 */
Dawid Drozd428cc522018-07-24 10:02:47 +020082typedef struct mbedtls_sha512_context
Paul Bakker5121ce52009-01-03 21:22:43 +000083{
Rose Zadik27ff1202018-01-26 11:01:31 +000084 uint64_t total[2]; /*!< The number of Bytes processed. */
85 uint64_t state[8]; /*!< The intermediate digest state. */
86 unsigned char buffer[128]; /*!< The data block being processed. */
Rose Zadik1a6275a2018-03-27 13:03:42 +010087 int is384; /*!< Determines which function to use:
88 0: Use SHA-512, or 1: Use SHA-384. */
Paul Bakker5121ce52009-01-03 21:22:43 +000089}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090mbedtls_sha512_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Ron Eldorb2aacec2017-05-18 16:53:08 +030092#else /* MBEDTLS_SHA512_ALT */
93#include "sha512_alt.h"
94#endif /* MBEDTLS_SHA512_ALT */
95
Paul Bakker5121ce52009-01-03 21:22:43 +000096/**
Rose Zadik27ff1202018-01-26 11:01:31 +000097 * \brief This function initializes a SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020098 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +000099 * \param ctx The SHA-512 context to initialize. This must
100 * not be \c NULL.
Paul Bakker5b4af392014-06-26 12:09:34 +0200101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102void mbedtls_sha512_init( mbedtls_sha512_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200103
104/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000105 * \brief This function clears a SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +0200106 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000107 * \param ctx The SHA-512 context to clear. This may be \c NULL,
108 * in which case this function does nothing. If it
109 * is not \c NULL, it must point to an initialized
110 * SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +0200111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112void mbedtls_sha512_free( mbedtls_sha512_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200113
114/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000115 * \brief This function clones the state of a SHA-512 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200116 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000117 * \param dst The destination context. This must be initialized.
118 * \param src The context to clone. This must be initialized.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200119 */
120void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
121 const mbedtls_sha512_context *src );
122
123/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000124 * \brief This function starts a SHA-384 or SHA-512 checksum
125 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000127 * \param ctx The SHA-512 context to use. This must be initialized.
128 * \param is384 Determines which function to use. This must be
129 * either \c for SHA-512, or \c 1 for SHA-384.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100130 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000131 * \return \c 0 on success.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000132 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100134int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
136/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000137 * \brief This function feeds an input buffer into an ongoing
138 * SHA-512 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000140 * \param ctx The SHA-512 context. This must be initialized
141 * and have a hash operation started.
142 * \param input The buffer holding the input data. This must
143 * be a readable buffer of length \p ilen Bytes.
Hanno Beckerca6f4582018-12-18 15:37:22 +0000144 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100145 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000146 * \return \c 0 on success.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000147 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100149int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
Rose Zadik27ff1202018-01-26 11:01:31 +0000150 const unsigned char *input,
151 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
153/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000154 * \brief This function finishes the SHA-512 operation, and writes
155 * the result to the output buffer. This function is for
156 * internal use only.
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000158 * \param ctx The SHA-512 context. This must be initialized
159 * and have a hash operation started.
Rose Zadik27ff1202018-01-26 11:01:31 +0000160 * \param output The SHA-384 or SHA-512 checksum result.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000161 * This must be a writable buffer of length \c 64 Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100162 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000163 * \return \c 0 on success.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000164 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100166int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100167 unsigned char output[64] );
168
169/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000170 * \brief This function processes a single data block within
171 * the ongoing SHA-512 computation.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100172 *
Hanno Beckerbb186f82018-12-19 10:27:24 +0000173 * \param ctx The SHA-512 context. This must be initialized.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000174 * \param data The buffer holding one block of data. This
175 * must be a readable buffer of length \c 128 Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100176 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000177 * \return \c 0 on success.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000178 * \return A negative error code on failure.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100179 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100180int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
181 const unsigned char data[128] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100182#if !defined(MBEDTLS_DEPRECATED_REMOVED)
183#if defined(MBEDTLS_DEPRECATED_WARNING)
184#define MBEDTLS_DEPRECATED __attribute__((deprecated))
185#else
186#define MBEDTLS_DEPRECATED
187#endif
188/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000189 * \brief This function starts a SHA-384 or SHA-512 checksum
190 * calculation.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100191 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100192 * \deprecated Superseded by mbedtls_sha512_starts_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100193 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000194 * \param ctx The SHA-512 context to use. This must be initialized.
195 * \param is384 Determines which function to use. This must be either
196 * \c 0 for SHA-512 or \c 1 for SHA-384.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100197 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000198MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
199 int is384 );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100200
201/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000202 * \brief This function feeds an input buffer into an ongoing
203 * SHA-512 checksum calculation.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100204 *
Rose Zadik1a6275a2018-03-27 13:03:42 +0100205 * \deprecated Superseded by mbedtls_sha512_update_ret() in 2.7.0.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100206 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000207 * \param ctx The SHA-512 context. This must be initialized
208 * and have a hash operation started.
209 * \param input The buffer holding the data. This must be a readable
Hanno Beckerca6f4582018-12-18 15:37:22 +0000210 * buffer of length \p ilen Bytes.
211 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100212 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000213MBEDTLS_DEPRECATED void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
214 const unsigned char *input,
215 size_t ilen );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100216
217/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000218 * \brief This function finishes the SHA-512 operation, and writes
219 * the result to the output buffer.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100220 *
Rose Zadik1a6275a2018-03-27 13:03:42 +0100221 * \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.7.0.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100222 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000223 * \param ctx The SHA-512 context. This must be initialized
224 * and have a hash operation started.
225 * \param output The SHA-384 or SHA-512 checksum result. This must
226 * be a writable buffer of size \c 64 Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100227 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000228MBEDTLS_DEPRECATED void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
229 unsigned char output[64] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100230
231/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000232 * \brief This function processes a single data block within
233 * the ongoing SHA-512 computation. This function is for
234 * internal use only.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100235 *
Rose Zadik1a6275a2018-03-27 13:03:42 +0100236 * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.7.0.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100237 *
Hanno Beckerbb186f82018-12-19 10:27:24 +0000238 * \param ctx The SHA-512 context. This must be initialized.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000239 * \param data The buffer holding one block of data. This must be
240 * a readable buffer of length \c 128 Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100241 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000242MBEDTLS_DEPRECATED void mbedtls_sha512_process(
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100243 mbedtls_sha512_context *ctx,
Jaeden Amero041039f2018-02-19 15:28:08 +0000244 const unsigned char data[128] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100245
246#undef MBEDTLS_DEPRECATED
247#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
249/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000250 * \brief This function calculates the SHA-512 or SHA-384
251 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000253 * The function allocates the context, performs the
254 * calculation, and frees the context.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100255 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000256 * The SHA-512 result is calculated as
257 * output = SHA-512(input buffer).
258 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000259 * \param input The buffer holding the input data. This must be
Hanno Beckerca6f4582018-12-18 15:37:22 +0000260 * a readable buffer of length \p ilen Bytes.
261 * \param ilen The length of the input data in Bytes.
Rose Zadik27ff1202018-01-26 11:01:31 +0000262 * \param output The SHA-384 or SHA-512 checksum result.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000263 * This must be a writable buffer of length \c 64 Bytes.
264 * \param is384 Determines which function to use. This must be either
265 * \c 0 for SHA-512, or \c 1 for SHA-384.
Rose Zadik27ff1202018-01-26 11:01:31 +0000266 *
267 * \return \c 0 on success.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000268 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100270int mbedtls_sha512_ret( const unsigned char *input,
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100271 size_t ilen,
272 unsigned char output[64],
273 int is384 );
274
275#if !defined(MBEDTLS_DEPRECATED_REMOVED)
276#if defined(MBEDTLS_DEPRECATED_WARNING)
277#define MBEDTLS_DEPRECATED __attribute__((deprecated))
278#else
279#define MBEDTLS_DEPRECATED
280#endif
Ron Eldorfa8f6352017-06-20 15:48:46 +0300281
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100282/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000283 * \brief This function calculates the SHA-512 or SHA-384
284 * checksum of a buffer.
285 *
286 * The function allocates the context, performs the
287 * calculation, and frees the context.
288 *
289 * The SHA-512 result is calculated as
290 * output = SHA-512(input buffer).
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100291 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100292 * \deprecated Superseded by mbedtls_sha512_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100293 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000294 * \param input The buffer holding the data. This must be a
Hanno Beckerca6f4582018-12-18 15:37:22 +0000295 * readable buffer of length \p ilen Bytes.
296 * \param ilen The length of the input data in Bytes.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000297 * \param output The SHA-384 or SHA-512 checksum result. This must
298 * be a writable buffer of length \c 64 Bytes.
Hanno Becker3f2d1ef2018-12-18 18:41:40 +0000299 * \param is384 Determines which function to use. This must be either
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000300 * \c 0 for SHA-512, or \c 1 for SHA-384.
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000302MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input,
303 size_t ilen,
304 unsigned char output[64],
305 int is384 );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100306
307#undef MBEDTLS_DEPRECATED
308#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ron Eldorfa8f6352017-06-20 15:48:46 +0300309
310#if defined(MBEDTLS_SELF_TEST)
311
Rose Zadik27ff1202018-01-26 11:01:31 +0000312 /**
313 * \brief The SHA-384 or SHA-512 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 *
Rose Zadik1a6275a2018-03-27 13:03:42 +0100315 * \return \c 0 on success.
316 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318int mbedtls_sha512_self_test( int verbose );
Ron Eldorfa8f6352017-06-20 15:48:46 +0300319#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Paul Bakker5121ce52009-01-03 21:22:43 +0000321#ifdef __cplusplus
322}
323#endif
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#endif /* mbedtls_sha512.h */