blob: d4c3e6468a744ea6c8bc1be8f9d6efd782d35ec7 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file sha256.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadikbde68b42018-03-27 12:59:13 +01004 * \brief This file contains SHA-224 and SHA-256 definitions and functions.
5 *
6 * The Secure Hash Algorithms 224 and 256 (SHA-224 and SHA-256) cryptographic
7 * hash functions are defined in <em>FIPS 180-4: Secure Hash Standard (SHS)</em>.
Darryl Greena40a1012018-01-05 15:33:17 +00008 */
9/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020010 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +000011 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +000012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013#ifndef MBEDTLS_SHA256_H
14#define MBEDTLS_SHA256_H
Paul Bakker5121ce52009-01-03 21:22:43 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010017#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020018#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020020#endif
Paul Bakker90995b52013-06-24 19:20:35 +020021
Rich Evans00ab4702015-02-06 13:43:58 +000022#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020023#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000024
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020025/* MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020026/** SHA-256 hardware accelerator failed */
27#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037
28/** SHA-256 input data was malformed. */
29#define MBEDTLS_ERR_SHA256_BAD_INPUT_DATA -0x0074
Gilles Peskinea381fe82018-01-23 18:16:11 +010030
Paul Bakker407a0da2013-06-27 14:29:21 +020031#ifdef __cplusplus
32extern "C" {
33#endif
34
Ron Eldorb2aacec2017-05-18 16:53:08 +030035#if !defined(MBEDTLS_SHA256_ALT)
36// Regular implementation
37//
38
Paul Bakker5121ce52009-01-03 21:22:43 +000039/**
Rose Zadik602285e2018-01-26 11:00:39 +000040 * \brief The SHA-256 context structure.
41 *
42 * The structure is used both for SHA-256 and for SHA-224
43 * checksum calculations. The choice between these two is
44 * made in the call to mbedtls_sha256_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000045 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010046typedef struct mbedtls_sha256_context {
Rose Zadik602285e2018-01-26 11:00:39 +000047 uint32_t total[2]; /*!< The number of Bytes processed. */
48 uint32_t state[8]; /*!< The intermediate digest state. */
49 unsigned char buffer[64]; /*!< The data block being processed. */
Rose Zadikbde68b42018-03-27 12:59:13 +010050 int is224; /*!< Determines which function to use:
51 0: Use SHA-256, or 1: Use SHA-224. */
Paul Bakker5121ce52009-01-03 21:22:43 +000052}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Ron Eldorb2aacec2017-05-18 16:53:08 +030055#else /* MBEDTLS_SHA256_ALT */
56#include "sha256_alt.h"
57#endif /* MBEDTLS_SHA256_ALT */
58
Paul Bakker5121ce52009-01-03 21:22:43 +000059/**
Rose Zadik602285e2018-01-26 11:00:39 +000060 * \brief This function initializes a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020061 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050062 * \param ctx The SHA-256 context to initialize. This must not be \c NULL.
Paul Bakker5b4af392014-06-26 12:09:34 +020063 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064void mbedtls_sha256_init(mbedtls_sha256_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020065
66/**
Rose Zadik602285e2018-01-26 11:00:39 +000067 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020068 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050069 * \param ctx The SHA-256 context to clear. This may be \c NULL, in which
70 * case this function returns immediately. If it is not \c NULL,
71 * it must point to an initialized SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020072 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073void mbedtls_sha256_free(mbedtls_sha256_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020074
75/**
Rose Zadik602285e2018-01-26 11:00:39 +000076 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020077 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050078 * \param dst The destination context. This must be initialized.
79 * \param src The context to clone. This must be initialized.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020080 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
82 const mbedtls_sha256_context *src);
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020083
84/**
Rose Zadik602285e2018-01-26 11:00:39 +000085 * \brief This function starts a SHA-224 or SHA-256 checksum
86 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000087 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050088 * \param ctx The context to use. This must be initialized.
89 * \param is224 This determines which function to use. This must be
90 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +010091 *
Rose Zadik602285e2018-01-26 11:00:39 +000092 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +000094 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224);
Paul Bakker5121ce52009-01-03 21:22:43 +000096
97/**
Rose Zadik602285e2018-01-26 11:00:39 +000098 * \brief This function feeds an input buffer into an ongoing
99 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * \param ctx The SHA-256 context. This must be initialized
102 * and have a hash operation started.
103 * \param input The buffer holding the data. This must be a readable
104 * buffer of length \p ilen Bytes.
105 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100106 *
Rose Zadik602285e2018-01-26 11:00:39 +0000107 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500108 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx,
111 const unsigned char *input,
112 size_t ilen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114/**
Rose Zadik602285e2018-01-26 11:00:39 +0000115 * \brief This function finishes the SHA-256 operation, and writes
116 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500118 * \param ctx The SHA-256 context. This must be initialized
119 * and have a hash operation started.
Rose Zadik602285e2018-01-26 11:00:39 +0000120 * \param output The SHA-224 or SHA-256 checksum result.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121 * This must be a writable buffer of length \c 32 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100122 *
Rose Zadik602285e2018-01-26 11:00:39 +0000123 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx,
127 unsigned char output[32]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100129/**
Rose Zadik602285e2018-01-26 11:00:39 +0000130 * \brief This function processes a single data block within
131 * the ongoing SHA-256 computation. This function is for
132 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100133 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500134 * \param ctx The SHA-256 context. This must be initialized.
135 * \param data The buffer holding one block of data. This must
136 * be a readable buffer of length \c 64 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100137 *
Rose Zadik602285e2018-01-26 11:00:39 +0000138 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500139 * \return A negative error code on failure.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100140 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
142 const unsigned char data[64]);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100143
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200144#if !defined(MBEDTLS_DEPRECATED_REMOVED)
145#if defined(MBEDTLS_DEPRECATED_WARNING)
146#define MBEDTLS_DEPRECATED __attribute__((deprecated))
147#else
148#define MBEDTLS_DEPRECATED
149#endif
150/**
151 * \brief This function starts a SHA-224 or SHA-256 checksum
152 * calculation.
153 *
154 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
155 *
156 * \param ctx The context to use. This must be initialized.
157 * \param is224 Determines which function to use. This must be
158 * either \c 0 for SHA-256, or \c 1 for SHA-224.
159 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160MBEDTLS_DEPRECATED void mbedtls_sha256_starts(mbedtls_sha256_context *ctx,
161 int is224);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200162
163/**
164 * \brief This function feeds an input buffer into an ongoing
165 * SHA-256 checksum calculation.
166 *
167 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
168 *
169 * \param ctx The SHA-256 context to use. This must be
170 * initialized and have a hash operation started.
171 * \param input The buffer holding the data. This must be a readable
172 * buffer of length \p ilen Bytes.
173 * \param ilen The length of the input data in Bytes.
174 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175MBEDTLS_DEPRECATED void mbedtls_sha256_update(mbedtls_sha256_context *ctx,
176 const unsigned char *input,
177 size_t ilen);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200178
179/**
180 * \brief This function finishes the SHA-256 operation, and writes
181 * the result to the output buffer.
182 *
183 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
184 *
185 * \param ctx The SHA-256 context. This must be initialized and
186 * have a hash operation started.
187 * \param output The SHA-224 or SHA-256 checksum result. This must be
188 * a writable buffer of length \c 32 Bytes.
189 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190MBEDTLS_DEPRECATED void mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
191 unsigned char output[32]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200192
193/**
194 * \brief This function processes a single data block within
195 * the ongoing SHA-256 computation. This function is for
196 * internal use only.
197 *
198 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
199 *
200 * \param ctx The SHA-256 context. This must be initialized.
201 * \param data The buffer holding one block of data. This must be
202 * a readable buffer of size \c 64 Bytes.
203 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204MBEDTLS_DEPRECATED void mbedtls_sha256_process(mbedtls_sha256_context *ctx,
205 const unsigned char data[64]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200206
207#undef MBEDTLS_DEPRECATED
208#endif /* !MBEDTLS_DEPRECATED_REMOVED */
209
Paul Bakker5121ce52009-01-03 21:22:43 +0000210/**
Rose Zadik602285e2018-01-26 11:00:39 +0000211 * \brief This function calculates the SHA-224 or SHA-256
212 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 *
Rose Zadik602285e2018-01-26 11:00:39 +0000214 * The function allocates the context, performs the
215 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100216 *
Rose Zadik602285e2018-01-26 11:00:39 +0000217 * The SHA-256 result is calculated as
218 * output = SHA-256(input buffer).
219 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500220 * \param input The buffer holding the data. This must be a readable
221 * buffer of length \p ilen Bytes.
222 * \param ilen The length of the input data in Bytes.
223 * \param output The SHA-224 or SHA-256 checksum result. This must
224 * be a writable buffer of length \c 32 Bytes.
225 * \param is224 Determines which function to use. This must be
226 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Tom Cosgrove8100bf52021-11-22 15:35:58 +0000227 *
228 * \return \c 0 on success.
229 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231int mbedtls_sha256_ret(const unsigned char *input,
232 size_t ilen,
233 unsigned char output[32],
234 int is224);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100235
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200236#if !defined(MBEDTLS_DEPRECATED_REMOVED)
237#if defined(MBEDTLS_DEPRECATED_WARNING)
238#define MBEDTLS_DEPRECATED __attribute__((deprecated))
239#else
240#define MBEDTLS_DEPRECATED
241#endif
242
243/**
244 * \brief This function calculates the SHA-224 or SHA-256 checksum
245 * of a buffer.
246 *
247 * The function allocates the context, performs the
248 * calculation, and frees the context.
249 *
250 * The SHA-256 result is calculated as
251 * output = SHA-256(input buffer).
252 *
253 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
254 *
255 * \param input The buffer holding the data. This must be a readable
256 * buffer of length \p ilen Bytes.
257 * \param ilen The length of the input data in Bytes.
258 * \param output The SHA-224 or SHA-256 checksum result. This must be
259 * a writable buffer of length \c 32 Bytes.
260 * \param is224 Determines which function to use. This must be either
261 * \c 0 for SHA-256, or \c 1 for SHA-224.
262 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263MBEDTLS_DEPRECATED void mbedtls_sha256(const unsigned char *input,
264 size_t ilen,
265 unsigned char output[32],
266 int is224);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200267
268#undef MBEDTLS_DEPRECATED
269#endif /* !MBEDTLS_DEPRECATED_REMOVED */
270
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500271#if defined(MBEDTLS_SELF_TEST)
272
Paul Bakker5121ce52009-01-03 21:22:43 +0000273/**
Rose Zadik602285e2018-01-26 11:00:39 +0000274 * \brief The SHA-224 and SHA-256 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100276 * \return \c 0 on success.
277 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100279int mbedtls_sha256_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000280
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500281#endif /* MBEDTLS_SELF_TEST */
282
Paul Bakker5121ce52009-01-03 21:22:43 +0000283#ifdef __cplusplus
284}
285#endif
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#endif /* mbedtls_sha256.h */