blob: cccd5c452f9f0b0d7af22abc4a3ea2a765152366 [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 */
Dawid Drozd428cc522018-07-24 10:02:47 +020058typedef struct mbedtls_sha256_context
Paul Bakker5121ce52009-01-03 21:22:43 +000059{
Rose Zadik602285e2018-01-26 11:00:39 +000060 uint32_t total[2]; /*!< The number of Bytes processed. */
61 uint32_t state[8]; /*!< The intermediate digest state. */
62 unsigned char buffer[64]; /*!< The data block being processed. */
Rose Zadikbde68b42018-03-27 12:59:13 +010063 int is224; /*!< Determines which function to use:
64 0: Use SHA-256, or 1: Use SHA-224. */
Paul Bakker5121ce52009-01-03 21:22:43 +000065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Ron Eldorb2aacec2017-05-18 16:53:08 +030068#else /* MBEDTLS_SHA256_ALT */
69#include "sha256_alt.h"
70#endif /* MBEDTLS_SHA256_ALT */
71
Paul Bakker5121ce52009-01-03 21:22:43 +000072/**
Rose Zadik602285e2018-01-26 11:00:39 +000073 * \brief This function initializes a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020074 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050075 * \param ctx The SHA-256 context to initialize. This must not be \c NULL.
Paul Bakker5b4af392014-06-26 12:09:34 +020076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020078
79/**
Rose Zadik602285e2018-01-26 11:00:39 +000080 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020081 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050082 * \param ctx The SHA-256 context to clear. This may be \c NULL, in which
83 * case this function returns immediately. If it is not \c NULL,
84 * it must point to an initialized SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020085 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020087
88/**
Rose Zadik602285e2018-01-26 11:00:39 +000089 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020090 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050091 * \param dst The destination context. This must be initialized.
92 * \param src The context to clone. This must be initialized.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020093 */
94void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
95 const mbedtls_sha256_context *src );
96
97/**
Rose Zadik602285e2018-01-26 11:00:39 +000098 * \brief This function starts a SHA-224 or SHA-256 checksum
99 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * \param ctx The context to use. This must be initialized.
102 * \param is224 This determines which function to use. This must be
103 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100104 *
Rose Zadik602285e2018-01-26 11:00:39 +0000105 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500106 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100108int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
110/**
Rose Zadik602285e2018-01-26 11:00:39 +0000111 * \brief This function feeds an input buffer into an ongoing
112 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500114 * \param ctx The SHA-256 context. This must be initialized
115 * and have a hash operation started.
116 * \param input The buffer holding the data. This must be a readable
117 * buffer of length \p ilen Bytes.
118 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100119 *
Rose Zadik602285e2018-01-26 11:00:39 +0000120 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100123int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100124 const unsigned char *input,
125 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127/**
Rose Zadik602285e2018-01-26 11:00:39 +0000128 * \brief This function finishes the SHA-256 operation, and writes
129 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500131 * \param ctx The SHA-256 context. This must be initialized
132 * and have a hash operation started.
Rose Zadik602285e2018-01-26 11:00:39 +0000133 * \param output The SHA-224 or SHA-256 checksum result.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500134 * This must be a writable buffer of length \c 32 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100135 *
Rose Zadik602285e2018-01-26 11:00:39 +0000136 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100139int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100140 unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100142/**
Rose Zadik602285e2018-01-26 11:00:39 +0000143 * \brief This function processes a single data block within
144 * the ongoing SHA-256 computation. This function is for
145 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100146 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500147 * \param ctx The SHA-256 context. This must be initialized.
148 * \param data The buffer holding one block of data. This must
149 * be a readable buffer of length \c 64 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100150 *
Rose Zadik602285e2018-01-26 11:00:39 +0000151 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500152 * \return A negative error code on failure.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100153 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100154int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
155 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100156
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200157#if !defined(MBEDTLS_DEPRECATED_REMOVED)
158#if defined(MBEDTLS_DEPRECATED_WARNING)
159#define MBEDTLS_DEPRECATED __attribute__((deprecated))
160#else
161#define MBEDTLS_DEPRECATED
162#endif
163/**
164 * \brief This function starts a SHA-224 or SHA-256 checksum
165 * calculation.
166 *
167 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
168 *
169 * \param ctx The context to use. This must be initialized.
170 * \param is224 Determines which function to use. This must be
171 * either \c 0 for SHA-256, or \c 1 for SHA-224.
172 */
173MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
174 int is224 );
175
176/**
177 * \brief This function feeds an input buffer into an ongoing
178 * SHA-256 checksum calculation.
179 *
180 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
181 *
182 * \param ctx The SHA-256 context to use. This must be
183 * initialized and have a hash operation started.
184 * \param input The buffer holding the data. This must be a readable
185 * buffer of length \p ilen Bytes.
186 * \param ilen The length of the input data in Bytes.
187 */
188MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
189 const unsigned char *input,
190 size_t ilen );
191
192/**
193 * \brief This function finishes the SHA-256 operation, and writes
194 * the result to the output buffer.
195 *
196 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
197 *
198 * \param ctx The SHA-256 context. This must be initialized and
199 * have a hash operation started.
200 * \param output The SHA-224 or SHA-256 checksum result. This must be
201 * a writable buffer of length \c 32 Bytes.
202 */
203MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
204 unsigned char output[32] );
205
206/**
207 * \brief This function processes a single data block within
208 * the ongoing SHA-256 computation. This function is for
209 * internal use only.
210 *
211 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
212 *
213 * \param ctx The SHA-256 context. This must be initialized.
214 * \param data The buffer holding one block of data. This must be
215 * a readable buffer of size \c 64 Bytes.
216 */
217MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
218 const unsigned char data[64] );
219
220#undef MBEDTLS_DEPRECATED
221#endif /* !MBEDTLS_DEPRECATED_REMOVED */
222
Paul Bakker5121ce52009-01-03 21:22:43 +0000223/**
Rose Zadik602285e2018-01-26 11:00:39 +0000224 * \brief This function calculates the SHA-224 or SHA-256
225 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 *
Rose Zadik602285e2018-01-26 11:00:39 +0000227 * The function allocates the context, performs the
228 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100229 *
Rose Zadik602285e2018-01-26 11:00:39 +0000230 * The SHA-256 result is calculated as
231 * output = SHA-256(input buffer).
232 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500233 * \param input The buffer holding the data. This must be a readable
234 * buffer of length \p ilen Bytes.
235 * \param ilen The length of the input data in Bytes.
236 * \param output The SHA-224 or SHA-256 checksum result. This must
237 * be a writable buffer of length \c 32 Bytes.
238 * \param is224 Determines which function to use. This must be
239 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100241int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100242 size_t ilen,
243 unsigned char output[32],
244 int is224 );
245
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200246#if !defined(MBEDTLS_DEPRECATED_REMOVED)
247#if defined(MBEDTLS_DEPRECATED_WARNING)
248#define MBEDTLS_DEPRECATED __attribute__((deprecated))
249#else
250#define MBEDTLS_DEPRECATED
251#endif
252
253/**
254 * \brief This function calculates the SHA-224 or SHA-256 checksum
255 * of a buffer.
256 *
257 * The function allocates the context, performs the
258 * calculation, and frees the context.
259 *
260 * The SHA-256 result is calculated as
261 * output = SHA-256(input buffer).
262 *
263 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
264 *
265 * \param input The buffer holding the data. This must be a readable
266 * buffer of length \p ilen Bytes.
267 * \param ilen The length of the input data in Bytes.
268 * \param output The SHA-224 or SHA-256 checksum result. This must be
269 * a writable buffer of length \c 32 Bytes.
270 * \param is224 Determines which function to use. This must be either
271 * \c 0 for SHA-256, or \c 1 for SHA-224.
272 */
273MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
274 size_t ilen,
275 unsigned char output[32],
276 int is224 );
277
278#undef MBEDTLS_DEPRECATED
279#endif /* !MBEDTLS_DEPRECATED_REMOVED */
280
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500281#if defined(MBEDTLS_SELF_TEST)
282
Paul Bakker5121ce52009-01-03 21:22:43 +0000283/**
Rose Zadik602285e2018-01-26 11:00:39 +0000284 * \brief The SHA-224 and SHA-256 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100286 * \return \c 0 on success.
287 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500291#endif /* MBEDTLS_SELF_TEST */
292
Paul Bakker5121ce52009-01-03 21:22:43 +0000293#ifdef __cplusplus
294}
295#endif
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#endif /* mbedtls_sha256.h */