Mbed OS Reference
Loading...
Searching...
No Matches
sha512.h
Go to the documentation of this file.
1/**
2 * \file sha512.h
3 * \brief This file contains SHA-384 and SHA-512 definitions and functions.
4 *
5 * 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>.
7 */
8/*
9 * Copyright The Mbed TLS Contributors
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24#ifndef MBEDTLS_SHA512_H
25#define MBEDTLS_SHA512_H
26
27#if !defined(MBEDTLS_CONFIG_FILE)
28#include "mbedtls/config.h"
29#else
30#include MBEDTLS_CONFIG_FILE
31#endif
32
33#include <stddef.h>
34#include <stdint.h>
35
36/**
37 * \addtogroup mbedtls
38 * \{
39 * \defgroup mbedtls_sha512_module SHA-384 and SHA-512
40 * \{
41 */
42
43/* MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED is deprecated and should not be used. */
44#define MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED -0x0039 /**< SHA-512 hardware accelerator failed */
45#define MBEDTLS_ERR_SHA512_BAD_INPUT_DATA -0x0075 /**< SHA-512 input data was malformed. */
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
51#if !defined(MBEDTLS_SHA512_ALT)
52// Regular implementation
53//
54
55/**
56 * \brief The SHA-512 context structure.
57 *
58 * The structure is used both for SHA-384 and for SHA-512
59 * checksum calculations. The choice between these two is
60 * made in the call to mbedtls_sha512_starts_ret().
61 */
63{
64 uint64_t total[2]; /*!< The number of Bytes processed. */
65 uint64_t state[8]; /*!< The intermediate digest state. */
66 unsigned char buffer[128]; /*!< The data block being processed. */
67#if !defined(MBEDTLS_SHA512_NO_SHA384)
68 int is384; /*!< Determines which function to use:
69 0: Use SHA-512, or 1: Use SHA-384. */
70#endif
71}
73
74#else /* MBEDTLS_SHA512_ALT */
75#include "sha512_alt.h"
76#endif /* MBEDTLS_SHA512_ALT */
77
78/**
79 * \brief This function initializes a SHA-512 context.
80 *
81 * \param ctx The SHA-512 context to initialize. This must
82 * not be \c NULL.
83 */
85
86/**
87 * \brief This function clears a SHA-512 context.
88 *
89 * \param ctx The SHA-512 context to clear. This may be \c NULL,
90 * in which case this function does nothing. If it
91 * is not \c NULL, it must point to an initialized
92 * SHA-512 context.
93 */
95
96/**
97 * \brief This function clones the state of a SHA-512 context.
98 *
99 * \param dst The destination context. This must be initialized.
100 * \param src The context to clone. This must be initialized.
101 */
103 const mbedtls_sha512_context *src );
104
105/**
106 * \brief This function starts a SHA-384 or SHA-512 checksum
107 * calculation.
108 *
109 * \param ctx The SHA-512 context to use. This must be initialized.
110 * \param is384 Determines which function to use. This must be
111 * either \c 0 for SHA-512, or \c 1 for SHA-384.
112 *
113 * \note When \c MBEDTLS_SHA512_NO_SHA384 is defined, \p is384 must
114 * be \c 0, or the function will return
115 * #MBEDTLS_ERR_SHA512_BAD_INPUT_DATA.
116 *
117 * \return \c 0 on success.
118 * \return A negative error code on failure.
119 */
121
122/**
123 * \brief This function feeds an input buffer into an ongoing
124 * SHA-512 checksum calculation.
125 *
126 * \param ctx The SHA-512 context. This must be initialized
127 * and have a hash operation started.
128 * \param input The buffer holding the input data. This must
129 * be a readable buffer of length \p ilen Bytes.
130 * \param ilen The length of the input data in Bytes.
131 *
132 * \return \c 0 on success.
133 * \return A negative error code on failure.
134 */
136 const unsigned char *input,
137 size_t ilen );
138
139/**
140 * \brief This function finishes the SHA-512 operation, and writes
141 * the result to the output buffer.
142 *
143 * \param ctx The SHA-512 context. This must be initialized
144 * and have a hash operation started.
145 * \param output The SHA-384 or SHA-512 checksum result.
146 * This must be a writable buffer of length \c 64 Bytes.
147 *
148 * \return \c 0 on success.
149 * \return A negative error code on failure.
150 */
152 unsigned char output[64] );
153
154/**
155 * \brief This function processes a single data block within
156 * the ongoing SHA-512 computation.
157 * This function is for internal use only.
158 *
159 * \param ctx The SHA-512 context. This must be initialized.
160 * \param data The buffer holding one block of data. This
161 * must be a readable buffer of length \c 128 Bytes.
162 *
163 * \return \c 0 on success.
164 * \return A negative error code on failure.
165 */
167 const unsigned char data[128] );
168#if !defined(MBEDTLS_DEPRECATED_REMOVED)
169#if defined(MBEDTLS_DEPRECATED_WARNING)
170#define MBEDTLS_DEPRECATED __attribute__((deprecated))
171#else
172#define MBEDTLS_DEPRECATED
173#endif
174/**
175 * \brief This function starts a SHA-384 or SHA-512 checksum
176 * calculation.
177 *
178 * \deprecated Superseded by mbedtls_sha512_starts_ret() in 2.7.0
179 *
180 * \param ctx The SHA-512 context to use. This must be initialized.
181 * \param is384 Determines which function to use. This must be either
182 * \c 0 for SHA-512 or \c 1 for SHA-384.
183 *
184 * \note When \c MBEDTLS_SHA512_NO_SHA384 is defined, \p is384 must
185 * be \c 0, or the function will fail to work.
186 */
187MBEDTLS_DEPRECATED void mbedtls_sha512_starts( mbedtls_sha512_context *ctx,
188 int is384 );
189
190/**
191 * \brief This function feeds an input buffer into an ongoing
192 * SHA-512 checksum calculation.
193 *
194 * \deprecated Superseded by mbedtls_sha512_update_ret() in 2.7.0.
195 *
196 * \param ctx The SHA-512 context. This must be initialized
197 * and have a hash operation started.
198 * \param input The buffer holding the data. This must be a readable
199 * buffer of length \p ilen Bytes.
200 * \param ilen The length of the input data in Bytes.
201 */
202MBEDTLS_DEPRECATED void mbedtls_sha512_update( mbedtls_sha512_context *ctx,
203 const unsigned char *input,
204 size_t ilen );
205
206/**
207 * \brief This function finishes the SHA-512 operation, and writes
208 * the result to the output buffer.
209 *
210 * \deprecated Superseded by mbedtls_sha512_finish_ret() in 2.7.0.
211 *
212 * \param ctx The SHA-512 context. This must be initialized
213 * and have a hash operation started.
214 * \param output The SHA-384 or SHA-512 checksum result. This must
215 * be a writable buffer of size \c 64 Bytes.
216 */
217MBEDTLS_DEPRECATED void mbedtls_sha512_finish( mbedtls_sha512_context *ctx,
218 unsigned char output[64] );
219
220/**
221 * \brief This function processes a single data block within
222 * the ongoing SHA-512 computation. This function is for
223 * internal use only.
224 *
225 * \deprecated Superseded by mbedtls_internal_sha512_process() in 2.7.0.
226 *
227 * \param ctx The SHA-512 context. This must be initialized.
228 * \param data The buffer holding one block of data. This must be
229 * a readable buffer of length \c 128 Bytes.
230 */
231MBEDTLS_DEPRECATED void mbedtls_sha512_process(
233 const unsigned char data[128] );
234
235#undef MBEDTLS_DEPRECATED
236#endif /* !MBEDTLS_DEPRECATED_REMOVED */
237
238/**
239 * \brief This function calculates the SHA-512 or SHA-384
240 * checksum of a buffer.
241 *
242 * The function allocates the context, performs the
243 * calculation, and frees the context.
244 *
245 * The SHA-512 result is calculated as
246 * output = SHA-512(input buffer).
247 *
248 * \param input The buffer holding the input data. This must be
249 * a readable buffer of length \p ilen Bytes.
250 * \param ilen The length of the input data in Bytes.
251 * \param output The SHA-384 or SHA-512 checksum result.
252 * This must be a writable buffer of length \c 64 Bytes.
253 * \param is384 Determines which function to use. This must be either
254 * \c 0 for SHA-512, or \c 1 for SHA-384.
255 *
256 * \note When \c MBEDTLS_SHA512_NO_SHA384 is defined, \p is384 must
257 * be \c 0, or the function will return
258 * #MBEDTLS_ERR_SHA512_BAD_INPUT_DATA.
259 *
260 * \return \c 0 on success.
261 * \return A negative error code on failure.
262 */
263int mbedtls_sha512_ret( const unsigned char *input,
264 size_t ilen,
265 unsigned char output[64],
266 int is384 );
267
268#if !defined(MBEDTLS_DEPRECATED_REMOVED)
269#if defined(MBEDTLS_DEPRECATED_WARNING)
270#define MBEDTLS_DEPRECATED __attribute__((deprecated))
271#else
272#define MBEDTLS_DEPRECATED
273#endif
274
275/**
276 * \brief This function calculates the SHA-512 or SHA-384
277 * checksum of a buffer.
278 *
279 * The function allocates the context, performs the
280 * calculation, and frees the context.
281 *
282 * The SHA-512 result is calculated as
283 * output = SHA-512(input buffer).
284 *
285 * \deprecated Superseded by mbedtls_sha512_ret() in 2.7.0
286 *
287 * \param input The buffer holding the data. This must be a
288 * readable buffer of length \p ilen Bytes.
289 * \param ilen The length of the input data in Bytes.
290 * \param output The SHA-384 or SHA-512 checksum result. This must
291 * be a writable buffer of length \c 64 Bytes.
292 * \param is384 Determines which function to use. This must be either
293 * \c 0 for SHA-512, or \c 1 for SHA-384.
294 *
295 * \note When \c MBEDTLS_SHA512_NO_SHA384 is defined, \p is384 must
296 * be \c 0, or the function will fail to work.
297 */
298MBEDTLS_DEPRECATED void mbedtls_sha512( const unsigned char *input,
299 size_t ilen,
300 unsigned char output[64],
301 int is384 );
302
303#undef MBEDTLS_DEPRECATED
304#endif /* !MBEDTLS_DEPRECATED_REMOVED */
305
306#if defined(MBEDTLS_SELF_TEST)
307
308 /**
309 * \brief The SHA-384 or SHA-512 checkup routine.
310 *
311 * \return \c 0 on success.
312 * \return \c 1 on failure.
313 */
314int mbedtls_sha512_self_test( int verbose );
315#endif /* MBEDTLS_SELF_TEST */
316
317#ifdef __cplusplus
318}
319#endif
320
321/// \}
322/// \}
323
324#endif /* mbedtls_sha512.h */
Configuration options (set of defines)
int mbedtls_sha512_ret(const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
This function calculates the SHA-512 or SHA-384 checksum of a buffer.
void mbedtls_sha512_starts(mbedtls_sha512_context *ctx, int is384)
This function starts a SHA-384 or SHA-512 checksum calculation.
int mbedtls_internal_sha512_process(mbedtls_sha512_context *ctx, const unsigned char data[128])
This function processes a single data block within the ongoing SHA-512 computation.
void mbedtls_sha512_process(mbedtls_sha512_context *ctx, const unsigned char data[128])
This function processes a single data block within the ongoing SHA-512 computation.
void mbedtls_sha512_clone(mbedtls_sha512_context *dst, const mbedtls_sha512_context *src)
This function clones the state of a SHA-512 context.
void mbedtls_sha512_update(mbedtls_sha512_context *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing SHA-512 checksum calculation.
void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
This function clears a SHA-512 context.
void mbedtls_sha512(const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
This function calculates the SHA-512 or SHA-384 checksum of a buffer.
int mbedtls_sha512_starts_ret(mbedtls_sha512_context *ctx, int is384)
This function starts a SHA-384 or SHA-512 checksum calculation.
void mbedtls_sha512_finish(mbedtls_sha512_context *ctx, unsigned char output[64])
This function finishes the SHA-512 operation, and writes the result to the output buffer.
int mbedtls_sha512_update_ret(mbedtls_sha512_context *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing SHA-512 checksum calculation.
int mbedtls_sha512_finish_ret(mbedtls_sha512_context *ctx, unsigned char output[64])
This function finishes the SHA-512 operation, and writes the result to the output buffer.
void mbedtls_sha512_init(mbedtls_sha512_context *ctx)
This function initializes a SHA-512 context.
The SHA-512 context structure.
Definition: sha512.h:63
unsigned char buffer[128]
Definition: sha512.h:66
uint64_t state[8]
Definition: sha512.h:65
uint64_t total[2]
Definition: sha512.h:64