blob: 00bd17d0cf8e392746a5656412f6c5249449fb16 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020011 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#ifndef MBEDTLS_SHA256_H
26#define MBEDTLS_SHA256_H
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010029#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#endif
Paul Bakker90995b52013-06-24 19:20:35 +020033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020035#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000036
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +020037/* MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020038/** SHA-256 hardware accelerator failed */
39#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037
40/** SHA-256 input data was malformed. */
41#define MBEDTLS_ERR_SHA256_BAD_INPUT_DATA -0x0074
Gilles Peskinea381fe82018-01-23 18:16:11 +010042
Paul Bakker407a0da2013-06-27 14:29:21 +020043#ifdef __cplusplus
44extern "C" {
45#endif
46
Ron Eldorb2aacec2017-05-18 16:53:08 +030047#if !defined(MBEDTLS_SHA256_ALT)
48// Regular implementation
49//
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/**
Rose Zadik602285e2018-01-26 11:00:39 +000052 * \brief The SHA-256 context structure.
53 *
54 * The structure is used both for SHA-256 and for SHA-224
55 * checksum calculations. The choice between these two is
56 * made in the call to mbedtls_sha256_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000057 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010058typedef struct mbedtls_sha256_context {
Rose Zadik602285e2018-01-26 11:00:39 +000059 uint32_t total[2]; /*!< The number of Bytes processed. */
60 uint32_t state[8]; /*!< The intermediate digest state. */
61 unsigned char buffer[64]; /*!< The data block being processed. */
Rose Zadikbde68b42018-03-27 12:59:13 +010062 int is224; /*!< Determines which function to use:
63 0: Use SHA-256, or 1: Use SHA-224. */
Paul Bakker5121ce52009-01-03 21:22:43 +000064}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Ron Eldorb2aacec2017-05-18 16:53:08 +030067#else /* MBEDTLS_SHA256_ALT */
68#include "sha256_alt.h"
69#endif /* MBEDTLS_SHA256_ALT */
70
Paul Bakker5121ce52009-01-03 21:22:43 +000071/**
Rose Zadik602285e2018-01-26 11:00:39 +000072 * \brief This function initializes a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020073 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050074 * \param ctx The SHA-256 context to initialize. This must not be \c NULL.
Paul Bakker5b4af392014-06-26 12:09:34 +020075 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076void mbedtls_sha256_init(mbedtls_sha256_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020077
78/**
Rose Zadik602285e2018-01-26 11:00:39 +000079 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020080 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050081 * \param ctx The SHA-256 context to clear. This may be \c NULL, in which
82 * case this function returns immediately. If it is not \c NULL,
83 * it must point to an initialized SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020084 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085void mbedtls_sha256_free(mbedtls_sha256_context *ctx);
Paul Bakker5b4af392014-06-26 12:09:34 +020086
87/**
Rose Zadik602285e2018-01-26 11:00:39 +000088 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020089 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050090 * \param dst The destination context. This must be initialized.
91 * \param src The context to clone. This must be initialized.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020092 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093void mbedtls_sha256_clone(mbedtls_sha256_context *dst,
94 const mbedtls_sha256_context *src);
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020095
96/**
Rose Zadik602285e2018-01-26 11:00:39 +000097 * \brief This function starts a SHA-224 or SHA-256 checksum
98 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000099 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500100 * \param ctx The context to use. This must be initialized.
101 * \param is224 This determines which function to use. This must be
102 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100103 *
Rose Zadik602285e2018-01-26 11:00:39 +0000104 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500105 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224);
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109/**
Rose Zadik602285e2018-01-26 11:00:39 +0000110 * \brief This function feeds an input buffer into an ongoing
111 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500113 * \param ctx The SHA-256 context. This must be initialized
114 * and have a hash operation started.
115 * \param input The buffer holding the data. This must be a readable
116 * buffer of length \p ilen Bytes.
117 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100118 *
Rose Zadik602285e2018-01-26 11:00:39 +0000119 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500120 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx,
123 const unsigned char *input,
124 size_t ilen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
126/**
Rose Zadik602285e2018-01-26 11:00:39 +0000127 * \brief This function finishes the SHA-256 operation, and writes
128 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500130 * \param ctx The SHA-256 context. This must be initialized
131 * and have a hash operation started.
Rose Zadik602285e2018-01-26 11:00:39 +0000132 * \param output The SHA-224 or SHA-256 checksum result.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 * This must be a writable buffer of length \c 32 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100134 *
Rose Zadik602285e2018-01-26 11:00:39 +0000135 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500136 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx,
139 unsigned char output[32]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100141/**
Rose Zadik602285e2018-01-26 11:00:39 +0000142 * \brief This function processes a single data block within
143 * the ongoing SHA-256 computation. This function is for
144 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100145 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500146 * \param ctx The SHA-256 context. This must be initialized.
147 * \param data The buffer holding one block of data. This must
148 * be a readable buffer of length \c 64 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100149 *
Rose Zadik602285e2018-01-26 11:00:39 +0000150 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500151 * \return A negative error code on failure.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100152 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx,
154 const unsigned char data[64]);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100155
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200156#if !defined(MBEDTLS_DEPRECATED_REMOVED)
157#if defined(MBEDTLS_DEPRECATED_WARNING)
158#define MBEDTLS_DEPRECATED __attribute__((deprecated))
159#else
160#define MBEDTLS_DEPRECATED
161#endif
162/**
163 * \brief This function starts a SHA-224 or SHA-256 checksum
164 * calculation.
165 *
166 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
167 *
168 * \param ctx The context to use. This must be initialized.
169 * \param is224 Determines which function to use. This must be
170 * either \c 0 for SHA-256, or \c 1 for SHA-224.
171 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172MBEDTLS_DEPRECATED void mbedtls_sha256_starts(mbedtls_sha256_context *ctx,
173 int is224);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200174
175/**
176 * \brief This function feeds an input buffer into an ongoing
177 * SHA-256 checksum calculation.
178 *
179 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
180 *
181 * \param ctx The SHA-256 context to use. This must be
182 * initialized and have a hash operation started.
183 * \param input The buffer holding the data. This must be a readable
184 * buffer of length \p ilen Bytes.
185 * \param ilen The length of the input data in Bytes.
186 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187MBEDTLS_DEPRECATED void mbedtls_sha256_update(mbedtls_sha256_context *ctx,
188 const unsigned char *input,
189 size_t ilen);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200190
191/**
192 * \brief This function finishes the SHA-256 operation, and writes
193 * the result to the output buffer.
194 *
195 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
196 *
197 * \param ctx The SHA-256 context. This must be initialized and
198 * have a hash operation started.
199 * \param output The SHA-224 or SHA-256 checksum result. This must be
200 * a writable buffer of length \c 32 Bytes.
201 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100202MBEDTLS_DEPRECATED void mbedtls_sha256_finish(mbedtls_sha256_context *ctx,
203 unsigned char output[32]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200204
205/**
206 * \brief This function processes a single data block within
207 * the ongoing SHA-256 computation. This function is for
208 * internal use only.
209 *
210 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
211 *
212 * \param ctx The SHA-256 context. This must be initialized.
213 * \param data The buffer holding one block of data. This must be
214 * a readable buffer of size \c 64 Bytes.
215 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216MBEDTLS_DEPRECATED void mbedtls_sha256_process(mbedtls_sha256_context *ctx,
217 const unsigned char data[64]);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200218
219#undef MBEDTLS_DEPRECATED
220#endif /* !MBEDTLS_DEPRECATED_REMOVED */
221
Paul Bakker5121ce52009-01-03 21:22:43 +0000222/**
Rose Zadik602285e2018-01-26 11:00:39 +0000223 * \brief This function calculates the SHA-224 or SHA-256
224 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 *
Rose Zadik602285e2018-01-26 11:00:39 +0000226 * The function allocates the context, performs the
227 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100228 *
Rose Zadik602285e2018-01-26 11:00:39 +0000229 * The SHA-256 result is calculated as
230 * output = SHA-256(input buffer).
231 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500232 * \param input The buffer holding the data. This must be a readable
233 * buffer of length \p ilen Bytes.
234 * \param ilen The length of the input data in Bytes.
235 * \param output The SHA-224 or SHA-256 checksum result. This must
236 * be a writable buffer of length \c 32 Bytes.
237 * \param is224 Determines which function to use. This must be
238 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Tom Cosgrove8100bf52021-11-22 15:35:58 +0000239 *
240 * \return \c 0 on success.
241 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243int mbedtls_sha256_ret(const unsigned char *input,
244 size_t ilen,
245 unsigned char output[32],
246 int is224);
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100247
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200248#if !defined(MBEDTLS_DEPRECATED_REMOVED)
249#if defined(MBEDTLS_DEPRECATED_WARNING)
250#define MBEDTLS_DEPRECATED __attribute__((deprecated))
251#else
252#define MBEDTLS_DEPRECATED
253#endif
254
255/**
256 * \brief This function calculates the SHA-224 or SHA-256 checksum
257 * of a buffer.
258 *
259 * The function allocates the context, performs the
260 * calculation, and frees the context.
261 *
262 * The SHA-256 result is calculated as
263 * output = SHA-256(input buffer).
264 *
265 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
266 *
267 * \param input The buffer holding the data. This must be a readable
268 * buffer of length \p ilen Bytes.
269 * \param ilen The length of the input data in Bytes.
270 * \param output The SHA-224 or SHA-256 checksum result. This must be
271 * a writable buffer of length \c 32 Bytes.
272 * \param is224 Determines which function to use. This must be either
273 * \c 0 for SHA-256, or \c 1 for SHA-224.
274 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100275MBEDTLS_DEPRECATED void mbedtls_sha256(const unsigned char *input,
276 size_t ilen,
277 unsigned char output[32],
278 int is224);
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200279
280#undef MBEDTLS_DEPRECATED
281#endif /* !MBEDTLS_DEPRECATED_REMOVED */
282
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500283#if defined(MBEDTLS_SELF_TEST)
284
Paul Bakker5121ce52009-01-03 21:22:43 +0000285/**
Rose Zadik602285e2018-01-26 11:00:39 +0000286 * \brief The SHA-224 and SHA-256 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000287 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100288 * \return \c 0 on success.
289 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291int mbedtls_sha256_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500293#endif /* MBEDTLS_SELF_TEST */
294
Paul Bakker5121ce52009-01-03 21:22:43 +0000295#ifdef __cplusplus
296}
297#endif
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#endif /* mbedtls_sha256.h */