blob: 8404a2d5992443b7ec14589d4d1fb18f9229b470 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_SHA512_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020039// Regular implementation
40//
41
Paul Bakker407a0da2013-06-27 14:29:21 +020042#ifdef __cplusplus
43extern "C" {
44#endif
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
Paul Bakker5121ce52009-01-03 21:22:43 +000064/**
Rose Zadik27ff1202018-01-26 11:01:31 +000065 * \brief This function initializes a SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020066 *
Rose Zadik27ff1202018-01-26 11:01:31 +000067 * \param ctx The SHA-512 context to initialize.
Paul Bakker5b4af392014-06-26 12:09:34 +020068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069void mbedtls_sha512_init( mbedtls_sha512_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020070
71/**
Rose Zadik27ff1202018-01-26 11:01:31 +000072 * \brief This function clears a SHA-512 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020073 *
Rose Zadik27ff1202018-01-26 11:01:31 +000074 * \param ctx The SHA-512 context to clear.
Paul Bakker5b4af392014-06-26 12:09:34 +020075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076void mbedtls_sha512_free( mbedtls_sha512_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020077
78/**
Rose Zadik27ff1202018-01-26 11:01:31 +000079 * \brief This function clones the state of a SHA-512 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020080 *
Rose Zadik27ff1202018-01-26 11:01:31 +000081 * \param dst The destination context.
82 * \param src The context to clone.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020083 */
84void mbedtls_sha512_clone( mbedtls_sha512_context *dst,
85 const mbedtls_sha512_context *src );
86
87/**
Rose Zadik27ff1202018-01-26 11:01:31 +000088 * \brief This function starts a SHA-384 or SHA-512 checksum
89 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000090 *
Rose Zadik27ff1202018-01-26 11:01:31 +000091 * \param ctx The SHA-512 context to initialize.
92 * \param is384 Determines which function to use.
93 * <ul><li>0: Use SHA-512.</li>
94 * <li>1: Use SHA-384.</li></ul>
Andres Amaya Garcia614c6892017-05-02 12:07:26 +010095 *
Rose Zadik27ff1202018-01-26 11:01:31 +000096 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000097 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010098int mbedtls_sha512_starts_ret( mbedtls_sha512_context *ctx, int is384 );
Paul Bakker5121ce52009-01-03 21:22:43 +000099
100/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000101 * \brief This function feeds an input buffer into an ongoing
102 * SHA-512 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000104 * \param ctx The SHA-512 context.
105 * \param input The buffer holding the input data.
106 * \param ilen The length of the input data.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100107 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000108 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100110int mbedtls_sha512_update_ret( mbedtls_sha512_context *ctx,
Rose Zadik27ff1202018-01-26 11:01:31 +0000111 const unsigned char *input,
112 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000115 * \brief This function finishes the SHA-512 operation, and writes
116 * the result to the output buffer. This function is for
117 * internal use only.
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000119 * \param ctx The SHA-512 context.
120 * \param output The SHA-384 or SHA-512 checksum result.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100121 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000122 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100124int mbedtls_sha512_finish_ret( mbedtls_sha512_context *ctx,
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100125 unsigned char output[64] );
126
127/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000128 * \brief This function processes a single data block within
129 * the ongoing SHA-512 computation.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100130 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000131 * \param ctx The SHA-512 context.
132 * \param data The buffer holding one block of data.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100133 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000134 * \return \c 0 on success.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100135 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100136int mbedtls_internal_sha512_process( mbedtls_sha512_context *ctx,
137 const unsigned char data[128] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100138#if !defined(MBEDTLS_DEPRECATED_REMOVED)
139#if defined(MBEDTLS_DEPRECATED_WARNING)
140#define MBEDTLS_DEPRECATED __attribute__((deprecated))
141#else
142#define MBEDTLS_DEPRECATED
143#endif
144/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000145 * \brief This function starts a SHA-384 or SHA-512 checksum
146 * calculation.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100147 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100148 * \deprecated Superseded by mbedtls_sha512_starts_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100149 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000150 * \param ctx The SHA-512 context to initialize.
151 * \param is384 Determines which function to use.
152 * <ul><li>0: Use SHA-512.</li>
153 * <li>1: Use SHA-384.</li></ul>
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100154 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000155MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
156 int is384 );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100157
158/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000159 * \brief This function feeds an input buffer into an ongoing
160 * SHA-512 checksum calculation.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100161 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100162 * \deprecated Superseded by mbedtls_sha512_update_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100163 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000164 * \param ctx The SHA-512 context.
165 * \param input The buffer holding the data.
166 * \param ilen The length of the input data.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100167 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000168MBEDTLS_DEPRECATED void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
169 const unsigned char *input,
170 size_t ilen );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100171
172/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000173 * \brief This function finishes the SHA-512 operation, and writes
174 * the result to the output buffer.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100175 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100176 * \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100177 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000178 * \param ctx The SHA-512 context.
179 * \param output The SHA-384 or SHA-512 checksum result.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100180 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000181MBEDTLS_DEPRECATED void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
182 unsigned char output[64] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100183
184/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000185 * \brief This function processes a single data block within
186 * the ongoing SHA-512 computation. This function is for
187 * internal use only.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100188 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100189 * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100190 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000191 * \param ctx The SHA-512 context.
192 * \param data The buffer holding one block of data.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100193 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000194MBEDTLS_DEPRECATED void mbedtls_sha512_process(
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100195 mbedtls_sha512_context *ctx,
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000196 const unsigned char data[128] );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100197
198#undef MBEDTLS_DEPRECATED
199#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
Paul Bakker90995b52013-06-24 19:20:35 +0200201#ifdef __cplusplus
202}
203#endif
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#else /* MBEDTLS_SHA512_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200206#include "sha512_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#endif /* MBEDTLS_SHA512_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200208
209#ifdef __cplusplus
210extern "C" {
211#endif
212
Paul Bakker5121ce52009-01-03 21:22:43 +0000213/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000214 * \brief This function calculates the SHA-512 or SHA-384
215 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000217 * The function allocates the context, performs the
218 * calculation, and frees the context.
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100219 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000220 * The SHA-512 result is calculated as
221 * output = SHA-512(input buffer).
222 *
223 * \param input The buffer holding the input data.
224 * \param ilen The length of the input data.
225 * \param output The SHA-384 or SHA-512 checksum result.
226 * \param is384 Determines which function to use.
227 * <ul><li>0: Use SHA-512.</li>
228 * <li>1: Use SHA-384.</li></ul>
229 *
230 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100232int mbedtls_sha512_ret( const unsigned char *input,
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100233 size_t ilen,
234 unsigned char output[64],
235 int is384 );
236
237#if !defined(MBEDTLS_DEPRECATED_REMOVED)
238#if defined(MBEDTLS_DEPRECATED_WARNING)
239#define MBEDTLS_DEPRECATED __attribute__((deprecated))
240#else
241#define MBEDTLS_DEPRECATED
242#endif
243/**
Rose Zadik27ff1202018-01-26 11:01:31 +0000244 * \brief This function calculates the SHA-512 or SHA-384
245 * checksum of a buffer.
246 *
247 * The function allocates the context, performs the
248 * calculation, and frees the context.
249 *
250 * The SHA-512 result is calculated as
251 * output = SHA-512(input buffer).
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100252 *
Gilles Peskine3e28d702018-01-22 12:18:59 +0100253 * \deprecated Superseded by mbedtls_sha512_ret() in 2.7.0
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100254 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000255 * \param input The buffer holding the data.
256 * \param ilen The length of the input data.
257 * \param output The SHA-384 or SHA-512 checksum result.
258 * \param is384 Determines which function to use.
259 * <ul><li>0: Use SHA-512.</li>
260 * <li>1: Use SHA-384.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000262MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input,
263 size_t ilen,
264 unsigned char output[64],
265 int is384 );
Andres Amaya Garcia614c6892017-05-02 12:07:26 +0100266
267#undef MBEDTLS_DEPRECATED
268#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Rose Zadik27ff1202018-01-26 11:01:31 +0000269 /**
270 * \brief The SHA-384 or SHA-512 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 *
Rose Zadik27ff1202018-01-26 11:01:31 +0000272 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274int mbedtls_sha512_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
Paul Bakker5121ce52009-01-03 21:22:43 +0000276#ifdef __cplusplus
277}
278#endif
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#endif /* mbedtls_sha512.h */