blob: 002fe9d935994392a7aa0dc73cfb38a56951a4e1 [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úti1e148272020-08-07 13:07:28 +02009 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000010 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +000011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#ifndef MBEDTLS_SHA512_H
13#define MBEDTLS_SHA512_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020014#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000015
Bence Szépkútic662b362021-05-27 11:25:03 +020016#include "mbedtls/build_info.h"
Paul Bakker90995b52013-06-24 19:20:35 +020017
Rich Evans00ab4702015-02-06 13:43:58 +000018#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020019#include <stdint.h>
Paul Bakker5121ce52009-01-03 21:22:43 +000020
Gilles Peskined2971572021-07-26 18:48:10 +020021/** SHA-512 input data was malformed. */
22#define MBEDTLS_ERR_SHA512_BAD_INPUT_DATA -0x0075
Gilles Peskinea381fe82018-01-23 18:16:11 +010023
Paul Bakker407a0da2013-06-27 14:29:21 +020024#ifdef __cplusplus
25extern "C" {
26#endif
27
Paul Bakker5121ce52009-01-03 21:22:43 +000028/**
Rose Zadik27ff1202018-01-26 11:01:31 +000029 * \brief The SHA-512 context structure.
30 *
31 * The structure is used both for SHA-384 and for SHA-512
32 * checksum calculations. The choice between these two is
TRodziewicz26371e42021-06-08 16:45:41 +020033 * made in the call to mbedtls_sha512_starts().
Paul Bakker5121ce52009-01-03 21:22:43 +000034 */
Gilles Peskine449bd832023-01-11 14:50:10 +010035typedef struct mbedtls_sha512_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020036 uint64_t MBEDTLS_PRIVATE(total)[2]; /*!< The number of Bytes processed. */
37 uint64_t MBEDTLS_PRIVATE(state)[8]; /*!< The intermediate digest state. */
38 unsigned char MBEDTLS_PRIVATE(buffer)[128]; /*!< The data block being processed. */
Mateusz Starzyk3352a532021-04-06 14:28:22 +020039#if defined(MBEDTLS_SHA384_C)
Mateusz Starzyk846f0212021-05-19 19:44:07 +020040 int MBEDTLS_PRIVATE(is384); /*!< Determines which function to use:
41 0: Use SHA-512, or 1: Use SHA-384. */
Manuel Pégourié-Gonnard3df4e602019-07-17 15:16:14 +020042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000043}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044mbedtls_sha512_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Paul Bakker5121ce52009-01-03 21:22:43 +000046/**
Rose Zadik27ff1202018-01-26 11:01:31 +000047 * \brief This function initializes a SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020048 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050049 * \param ctx The SHA-512 context to initialize. This must
50 * not be \c NULL.
Paul Bakker5b4af392014-06-26 12:09:34 +020051 */
Gilles Peskine449bd832023-01-11 14:50:10 +010052void mbedtls_sha512_init(mbedtls_sha512_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020053
54/**
Rose Zadik27ff1202018-01-26 11:01:31 +000055 * \brief This function clears a SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020056 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050057 * \param ctx The SHA-512 context to clear. This may be \c NULL,
58 * in which case this function does nothing. If it
59 * is not \c NULL, it must point to an initialized
60 * SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020061 */
Gilles Peskine449bd832023-01-11 14:50:10 +010062void mbedtls_sha512_free(mbedtls_sha512_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020063
64/**
Rose Zadik27ff1202018-01-26 11:01:31 +000065 * \brief This function clones the state of a SHA-512 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020066 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050067 * \param dst The destination context. This must be initialized.
68 * \param src The context to clone. This must be initialized.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020069 */
Gilles Peskine449bd832023-01-11 14:50:10 +010070void mbedtls_sha512_clone(mbedtls_sha512_context *dst,
71 const mbedtls_sha512_context *src);
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020072
73/**
Rose Zadik27ff1202018-01-26 11:01:31 +000074 * \brief This function starts a SHA-384 or SHA-512 checksum
75 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000076 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050077 * \param ctx The SHA-512 context to use. This must be initialized.
78 * \param is384 Determines which function to use. This must be
Manuel Pégourié-Gonnard663ee202020-01-07 10:11:22 +010079 * either \c 0 for SHA-512, or \c 1 for SHA-384.
80 *
Valerio Settie2697502022-12-23 14:29:54 +010081 * \note is384 must be defined accordingly to the enabled
82 * MBEDTLS_SHA384_C/MBEDTLS_SHA512_C symbols otherwise the
83 * function will return #MBEDTLS_ERR_SHA512_BAD_INPUT_DATA.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +010084 *
Rose Zadik27ff1202018-01-26 11:01:31 +000085 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050086 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +000087 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088int mbedtls_sha512_starts(mbedtls_sha512_context *ctx, int is384);
Paul Bakker5121ce52009-01-03 21:22:43 +000089
90/**
Rose Zadik27ff1202018-01-26 11:01:31 +000091 * \brief This function feeds an input buffer into an ongoing
92 * SHA-512 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000093 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050094 * \param ctx The SHA-512 context. This must be initialized
95 * and have a hash operation started.
96 * \param input The buffer holding the input data. This must
97 * be a readable buffer of length \p ilen Bytes.
98 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +010099 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000100 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103int mbedtls_sha512_update(mbedtls_sha512_context *ctx,
104 const unsigned char *input,
105 size_t ilen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000108 * \brief This function finishes the SHA-512 operation, and writes
Gilles Peskine383c2452020-11-22 13:59:43 +0100109 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500111 * \param ctx The SHA-512 context. This must be initialized
112 * and have a hash operation started.
Rose Zadik27ff1202018-01-26 11:01:31 +0000113 * \param output The SHA-384 or SHA-512 checksum result.
Gilles Peskinee02e02f2021-05-13 00:22:35 +0200114 * This must be a writable buffer of length \c 64 bytes
Gilles Peskine96d6e082021-05-18 20:06:04 +0200115 * for SHA-512, \c 48 bytes for SHA-384.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100116 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000117 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500118 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120int mbedtls_sha512_finish(mbedtls_sha512_context *ctx,
121 unsigned char *output);
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100122
123/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000124 * \brief This function processes a single data block within
125 * the ongoing SHA-512 computation.
Gilles Peskine383c2452020-11-22 13:59:43 +0100126 * This function is for internal use only.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100127 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500128 * \param ctx The SHA-512 context. This must be initialized.
129 * \param data The buffer holding one block of data. This
130 * must be a readable buffer of length \c 128 Bytes.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100131 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000132 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 * \return A negative error code on failure.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100134 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx,
136 const unsigned char data[128]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
138/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000139 * \brief This function calculates the SHA-512 or SHA-384
140 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000142 * The function allocates the context, performs the
143 * calculation, and frees the context.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100144 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000145 * The SHA-512 result is calculated as
146 * output = SHA-512(input buffer).
147 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500148 * \param input The buffer holding the input data. This must be
149 * a readable buffer of length \p ilen Bytes.
150 * \param ilen The length of the input data in Bytes.
Rose Zadik27ff1202018-01-26 11:01:31 +0000151 * \param output The SHA-384 or SHA-512 checksum result.
Gilles Peskinee02e02f2021-05-13 00:22:35 +0200152 * This must be a writable buffer of length \c 64 bytes
Gilles Peskine96d6e082021-05-18 20:06:04 +0200153 * for SHA-512, \c 48 bytes for SHA-384.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500154 * \param is384 Determines which function to use. This must be either
155 * \c 0 for SHA-512, or \c 1 for SHA-384.
Rose Zadik27ff1202018-01-26 11:01:31 +0000156 *
Valerio Setti43363f52022-12-14 08:53:23 +0100157 * \note is384 must be defined accordingly with the supported
158 * symbols in the config file. If:
159 * - is384 is 0, but \c MBEDTLS_SHA384_C is not defined, or
160 * - is384 is 1, but \c MBEDTLS_SHA512_C is not defined
161 * then the function will return
Manuel Pégourié-Gonnard3a3b5c72020-01-24 10:57:25 +0100162 * #MBEDTLS_ERR_SHA512_BAD_INPUT_DATA.
Manuel Pégourié-Gonnard663ee202020-01-07 10:11:22 +0100163 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000164 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500165 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167int mbedtls_sha512(const unsigned char *input,
168 size_t ilen,
169 unsigned char *output,
170 int is384);
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100171
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500172#if defined(MBEDTLS_SELF_TEST)
173
Valerio Settid10e0a62022-12-22 14:25:26 +0100174#if defined(MBEDTLS_SHA384_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100175/**
Valerio Setti43363f52022-12-14 08:53:23 +0100176 * \brief The SHA-384 checkup routine.
177 *
178 * \return \c 0 on success.
179 * \return \c 1 on failure.
180 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181int mbedtls_sha384_self_test(int verbose);
Valerio Settid10e0a62022-12-22 14:25:26 +0100182#endif /* MBEDTLS_SHA384_C */
Valerio Setti43363f52022-12-14 08:53:23 +0100183
Valerio Settid10e0a62022-12-22 14:25:26 +0100184#if defined(MBEDTLS_SHA512_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100185/**
Valerio Setti43363f52022-12-14 08:53:23 +0100186 * \brief The SHA-512 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 *
Rose Zadik1a6275a2018-03-27 13:03:42 +0100188 * \return \c 0 on success.
189 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100191int mbedtls_sha512_self_test(int verbose);
Valerio Settid10e0a62022-12-22 14:25:26 +0100192#endif /* MBEDTLS_SHA512_C */
193
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500194#endif /* MBEDTLS_SELF_TEST */
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
Paul Bakker5121ce52009-01-03 21:22:43 +0000196#ifdef __cplusplus
197}
198#endif
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#endif /* mbedtls_sha512.h */