blob: 353ad7a2cb09aa7f2c349056182f6f02c4f98814 [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 Peskine1990fab2021-07-26 18:48:10 +020064/** SHA-512 hardware accelerator failed */
65#define MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED -0x0039
66/** SHA-512 input data was malformed. */
67#define MBEDTLS_ERR_SHA512_BAD_INPUT_DATA -0x0075
Gilles Peskinea381fe82018-01-23 18:16:11 +010068
Paul Bakker407a0da2013-06-27 14:29:21 +020069#ifdef __cplusplus
70extern "C" {
71#endif
72
Ron Eldorb2aacec2017-05-18 16:53:08 +030073#if !defined(MBEDTLS_SHA512_ALT)
74// Regular implementation
75//
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/**
Rose Zadik27ff1202018-01-26 11:01:31 +000078 * \brief The SHA-512 context structure.
79 *
80 * The structure is used both for SHA-384 and for SHA-512
81 * checksum calculations. The choice between these two is
82 * made in the call to mbedtls_sha512_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000083 */
Dawid Drozd428cc522018-07-24 10:02:47 +020084typedef struct mbedtls_sha512_context
Paul Bakker5121ce52009-01-03 21:22:43 +000085{
Rose Zadik27ff1202018-01-26 11:01:31 +000086 uint64_t total[2]; /*!< The number of Bytes processed. */
87 uint64_t state[8]; /*!< The intermediate digest state. */
88 unsigned char buffer[128]; /*!< The data block being processed. */
Rose Zadik1a6275a2018-03-27 13:03:42 +010089 int is384; /*!< Determines which function to use:
90 0: Use SHA-512, or 1: Use SHA-384. */
Paul Bakker5121ce52009-01-03 21:22:43 +000091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092mbedtls_sha512_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Ron Eldorb2aacec2017-05-18 16:53:08 +030094#else /* MBEDTLS_SHA512_ALT */
95#include "sha512_alt.h"
96#endif /* MBEDTLS_SHA512_ALT */
97
Paul Bakker5121ce52009-01-03 21:22:43 +000098/**
Rose Zadik27ff1202018-01-26 11:01:31 +000099 * \brief This function initializes a SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +0200100 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000101 * \param ctx The SHA-512 context to initialize. This must
102 * not be \c NULL.
Paul Bakker5b4af392014-06-26 12:09:34 +0200103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104void mbedtls_sha512_init( mbedtls_sha512_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200105
106/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000107 * \brief This function clears a SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +0200108 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000109 * \param ctx The SHA-512 context to clear. This may be \c NULL,
110 * in which case this function does nothing. If it
111 * is not \c NULL, it must point to an initialized
112 * SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +0200113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114void mbedtls_sha512_free( mbedtls_sha512_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200115
116/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000117 * \brief This function clones the state of a SHA-512 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200118 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000119 * \param dst The destination context. This must be initialized.
120 * \param src The context to clone. This must be initialized.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200121 */
122void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
123 const mbedtls_sha512_context *src );
124
125/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000126 * \brief This function starts a SHA-384 or SHA-512 checksum
127 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000129 * \param ctx The SHA-512 context to use. This must be initialized.
130 * \param is384 Determines which function to use. This must be
131 * either \c for SHA-512, or \c 1 for SHA-384.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100132 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000133 * \return \c 0 on success.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000134 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100136int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000139 * \brief This function feeds an input buffer into an ongoing
140 * SHA-512 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000142 * \param ctx The SHA-512 context. This must be initialized
143 * and have a hash operation started.
144 * \param input The buffer holding the input data. This must
145 * be a readable buffer of length \p ilen Bytes.
Hanno Beckerca6f4582018-12-18 15:37:22 +0000146 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100147 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000148 * \return \c 0 on success.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000149 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100151int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
Rose Zadik27ff1202018-01-26 11:01:31 +0000152 const unsigned char *input,
153 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
155/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000156 * \brief This function finishes the SHA-512 operation, and writes
Gilles Peskinee3645ee2020-11-22 13:59:43 +0100157 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000159 * \param ctx The SHA-512 context. This must be initialized
160 * and have a hash operation started.
Rose Zadik27ff1202018-01-26 11:01:31 +0000161 * \param output The SHA-384 or SHA-512 checksum result.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000162 * This must be a writable buffer of length \c 64 Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100163 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000164 * \return \c 0 on success.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000165 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100167int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100168 unsigned char output[64] );
169
170/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000171 * \brief This function processes a single data block within
172 * the ongoing SHA-512 computation.
Gilles Peskinee3645ee2020-11-22 13:59:43 +0100173 * This function is for internal use only.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100174 *
Hanno Beckerbb186f82018-12-19 10:27:24 +0000175 * \param ctx The SHA-512 context. This must be initialized.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000176 * \param data The buffer holding one block of data. This
177 * must be a readable buffer of length \c 128 Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100178 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000179 * \return \c 0 on success.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000180 * \return A negative error code on failure.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100181 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100182int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
183 const unsigned char data[128] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100184#if !defined(MBEDTLS_DEPRECATED_REMOVED)
185#if defined(MBEDTLS_DEPRECATED_WARNING)
186#define MBEDTLS_DEPRECATED __attribute__((deprecated))
187#else
188#define MBEDTLS_DEPRECATED
189#endif
190/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000191 * \brief This function starts a SHA-384 or SHA-512 checksum
192 * calculation.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100193 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100194 * \deprecated Superseded by mbedtls_sha512_starts_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100195 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000196 * \param ctx The SHA-512 context to use. This must be initialized.
197 * \param is384 Determines which function to use. This must be either
198 * \c 0 for SHA-512 or \c 1 for SHA-384.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100199 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000200MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
201 int is384 );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100202
203/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000204 * \brief This function feeds an input buffer into an ongoing
205 * SHA-512 checksum calculation.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100206 *
Rose Zadik1a6275a2018-03-27 13:03:42 +0100207 * \deprecated Superseded by mbedtls_sha512_update_ret() in 2.7.0.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100208 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000209 * \param ctx The SHA-512 context. This must be initialized
210 * and have a hash operation started.
211 * \param input The buffer holding the data. This must be a readable
Hanno Beckerca6f4582018-12-18 15:37:22 +0000212 * buffer of length \p ilen Bytes.
213 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100214 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000215MBEDTLS_DEPRECATED void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
216 const unsigned char *input,
217 size_t ilen );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100218
219/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000220 * \brief This function finishes the SHA-512 operation, and writes
221 * the result to the output buffer.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100222 *
Rose Zadik1a6275a2018-03-27 13:03:42 +0100223 * \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.7.0.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100224 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000225 * \param ctx The SHA-512 context. This must be initialized
226 * and have a hash operation started.
227 * \param output The SHA-384 or SHA-512 checksum result. This must
228 * be a writable buffer of size \c 64 Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100229 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000230MBEDTLS_DEPRECATED void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
231 unsigned char output[64] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100232
233/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000234 * \brief This function processes a single data block within
235 * the ongoing SHA-512 computation. This function is for
236 * internal use only.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100237 *
Rose Zadik1a6275a2018-03-27 13:03:42 +0100238 * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.7.0.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100239 *
Hanno Beckerbb186f82018-12-19 10:27:24 +0000240 * \param ctx The SHA-512 context. This must be initialized.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000241 * \param data The buffer holding one block of data. This must be
242 * a readable buffer of length \c 128 Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100243 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000244MBEDTLS_DEPRECATED void mbedtls_sha512_process(
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100245 mbedtls_sha512_context *ctx,
Jaeden Amero041039f2018-02-19 15:28:08 +0000246 const unsigned char data[128] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100247
248#undef MBEDTLS_DEPRECATED
249#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000250
251/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000252 * \brief This function calculates the SHA-512 or SHA-384
253 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000255 * The function allocates the context, performs the
256 * calculation, and frees the context.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100257 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000258 * The SHA-512 result is calculated as
259 * output = SHA-512(input buffer).
260 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000261 * \param input The buffer holding the input data. This must be
Hanno Beckerca6f4582018-12-18 15:37:22 +0000262 * a readable buffer of length \p ilen Bytes.
263 * \param ilen The length of the input data in Bytes.
Rose Zadik27ff1202018-01-26 11:01:31 +0000264 * \param output The SHA-384 or SHA-512 checksum result.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000265 * This must be a writable buffer of length \c 64 Bytes.
266 * \param is384 Determines which function to use. This must be either
267 * \c 0 for SHA-512, or \c 1 for SHA-384.
Rose Zadik27ff1202018-01-26 11:01:31 +0000268 *
269 * \return \c 0 on success.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000270 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100272int mbedtls_sha512_ret( const unsigned char *input,
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100273 size_t ilen,
274 unsigned char output[64],
275 int is384 );
276
277#if !defined(MBEDTLS_DEPRECATED_REMOVED)
278#if defined(MBEDTLS_DEPRECATED_WARNING)
279#define MBEDTLS_DEPRECATED __attribute__((deprecated))
280#else
281#define MBEDTLS_DEPRECATED
282#endif
Ron Eldorfa8f6352017-06-20 15:48:46 +0300283
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100284/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000285 * \brief This function calculates the SHA-512 or SHA-384
286 * checksum of a buffer.
287 *
288 * The function allocates the context, performs the
289 * calculation, and frees the context.
290 *
291 * The SHA-512 result is calculated as
292 * output = SHA-512(input buffer).
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100293 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100294 * \deprecated Superseded by mbedtls_sha512_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100295 *
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000296 * \param input The buffer holding the data. This must be a
Hanno Beckerca6f4582018-12-18 15:37:22 +0000297 * readable buffer of length \p ilen Bytes.
298 * \param ilen The length of the input data in Bytes.
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000299 * \param output The SHA-384 or SHA-512 checksum result. This must
300 * be a writable buffer of length \c 64 Bytes.
Hanno Becker3f2d1ef2018-12-18 18:41:40 +0000301 * \param is384 Determines which function to use. This must be either
Hanno Beckerb5c99f52018-12-18 15:29:32 +0000302 * \c 0 for SHA-512, or \c 1 for SHA-384.
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000304MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input,
305 size_t ilen,
306 unsigned char output[64],
307 int is384 );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100308
309#undef MBEDTLS_DEPRECATED
310#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ron Eldorfa8f6352017-06-20 15:48:46 +0300311
312#if defined(MBEDTLS_SELF_TEST)
313
Rose Zadik27ff1202018-01-26 11:01:31 +0000314 /**
315 * \brief The SHA-384 or SHA-512 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 *
Rose Zadik1a6275a2018-03-27 13:03:42 +0100317 * \return \c 0 on success.
318 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320int mbedtls_sha512_self_test( int verbose );
Ron Eldorfa8f6352017-06-20 15:48:46 +0300321#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000322
Paul Bakker5121ce52009-01-03 21:22:43 +0000323#ifdef __cplusplus
324}
325#endif
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#endif /* mbedtls_sha512.h */