blob: b1881e183c4351210761c23b19ce4a4876def7eb [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útia2947ac2020-08-19 16:37:36 +020010 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020011 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
12 *
13 * This file is provided under the Apache License 2.0, or the
14 * GNU General Public License v2.0 or later.
15 *
16 * **********
17 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020018 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000030 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020031 * **********
32 *
33 * **********
34 * GNU General Public License v2.0 or later:
35 *
36 * This program is free software; you can redistribute it and/or modify
37 * it under the terms of the GNU General Public License as published by
38 * the Free Software Foundation; either version 2 of the License, or
39 * (at your option) any later version.
40 *
41 * This program is distributed in the hope that it will be useful,
42 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 * GNU General Public License for more details.
45 *
46 * You should have received a copy of the GNU General Public License along
47 * with this program; if not, write to the Free Software Foundation, Inc.,
48 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
49 *
50 * **********
Paul Bakker5121ce52009-01-03 21:22:43 +000051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#ifndef MBEDTLS_SHA256_H
53#define MBEDTLS_SHA256_H
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020056#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020057#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020059#endif
Paul Bakker90995b52013-06-24 19:20:35 +020060
Rich Evans00ab4702015-02-06 13:43:58 +000061#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020062#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000063
Ron Eldor9924bdc2018-10-04 10:59:13 +030064/* MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea381fe82018-01-23 18:16:11 +010065#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
Andres Amaya Garcia0152f1e2018-12-10 10:22:27 +000066#define MBEDTLS_ERR_SHA256_BAD_INPUT_DATA -0x0074 /**< SHA-256 input data was malformed. */
Gilles Peskinea381fe82018-01-23 18:16:11 +010067
Paul Bakker407a0da2013-06-27 14:29:21 +020068#ifdef __cplusplus
69extern "C" {
70#endif
71
Ron Eldorb2aacec2017-05-18 16:53:08 +030072#if !defined(MBEDTLS_SHA256_ALT)
73// Regular implementation
74//
75
Paul Bakker5121ce52009-01-03 21:22:43 +000076/**
Rose Zadik602285e2018-01-26 11:00:39 +000077 * \brief The SHA-256 context structure.
78 *
79 * The structure is used both for SHA-256 and for SHA-224
80 * checksum calculations. The choice between these two is
81 * made in the call to mbedtls_sha256_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000082 */
Dawid Drozd428cc522018-07-24 10:02:47 +020083typedef struct mbedtls_sha256_context
Paul Bakker5121ce52009-01-03 21:22:43 +000084{
Rose Zadik602285e2018-01-26 11:00:39 +000085 uint32_t total[2]; /*!< The number of Bytes processed. */
86 uint32_t state[8]; /*!< The intermediate digest state. */
87 unsigned char buffer[64]; /*!< The data block being processed. */
Rose Zadikbde68b42018-03-27 12:59:13 +010088 int is224; /*!< Determines which function to use:
89 0: Use SHA-256, or 1: Use SHA-224. */
Paul Bakker5121ce52009-01-03 21:22:43 +000090}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Ron Eldorb2aacec2017-05-18 16:53:08 +030093#else /* MBEDTLS_SHA256_ALT */
94#include "sha256_alt.h"
95#endif /* MBEDTLS_SHA256_ALT */
96
Paul Bakker5121ce52009-01-03 21:22:43 +000097/**
Rose Zadik602285e2018-01-26 11:00:39 +000098 * \brief This function initializes a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020099 *
Hanno Becker77886af2018-12-18 14:54:04 +0000100 * \param ctx The SHA-256 context to initialize. This must not be \c NULL.
Paul Bakker5b4af392014-06-26 12:09:34 +0200101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200103
104/**
Rose Zadik602285e2018-01-26 11:00:39 +0000105 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +0200106 *
Hanno Becker77886af2018-12-18 14:54:04 +0000107 * \param ctx The SHA-256 context to clear. This may be \c NULL, in which
108 * case this function returns immediately. If it is not \c NULL,
109 * it must point to an initialized SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +0200110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200112
113/**
Rose Zadik602285e2018-01-26 11:00:39 +0000114 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200115 *
Hanno Becker77886af2018-12-18 14:54:04 +0000116 * \param dst The destination context. This must be initialized.
117 * \param src The context to clone. This must be initialized.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200118 */
119void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
120 const mbedtls_sha256_context *src );
121
122/**
Rose Zadik602285e2018-01-26 11:00:39 +0000123 * \brief This function starts a SHA-224 or SHA-256 checksum
124 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 *
Hanno Becker77886af2018-12-18 14:54:04 +0000126 * \param ctx The context to use. This must be initialized.
127 * \param is224 This determines which function to use. This must be
128 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100129 *
Rose Zadik602285e2018-01-26 11:00:39 +0000130 * \return \c 0 on success.
Hanno Becker77886af2018-12-18 14:54:04 +0000131 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100133int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
135/**
Rose Zadik602285e2018-01-26 11:00:39 +0000136 * \brief This function feeds an input buffer into an ongoing
137 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 *
Hanno Becker77886af2018-12-18 14:54:04 +0000139 * \param ctx The SHA-256 context. This must be initialized
140 * and have a hash operation started.
141 * \param input The buffer holding the data. This must be a readable
142 * buffer of length \p ilen Bytes.
Hanno Beckerfc2a0b22018-12-18 16:31:48 +0000143 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100144 *
Rose Zadik602285e2018-01-26 11:00:39 +0000145 * \return \c 0 on success.
Hanno Becker77886af2018-12-18 14:54:04 +0000146 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100148int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100149 const unsigned char *input,
150 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
152/**
Rose Zadik602285e2018-01-26 11:00:39 +0000153 * \brief This function finishes the SHA-256 operation, and writes
154 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 *
Hanno Becker77886af2018-12-18 14:54:04 +0000156 * \param ctx The SHA-256 context. This must be initialized
157 * and have a hash operation started.
Rose Zadik602285e2018-01-26 11:00:39 +0000158 * \param output The SHA-224 or SHA-256 checksum result.
Hanno Becker77886af2018-12-18 14:54:04 +0000159 * This must be a writable buffer of length \c 32 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100160 *
Rose Zadik602285e2018-01-26 11:00:39 +0000161 * \return \c 0 on success.
Hanno Becker77886af2018-12-18 14:54:04 +0000162 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100164int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100165 unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100167/**
Rose Zadik602285e2018-01-26 11:00:39 +0000168 * \brief This function processes a single data block within
169 * the ongoing SHA-256 computation. This function is for
170 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100171 *
Hanno Becker3f1f4ad2018-12-18 23:19:37 +0000172 * \param ctx The SHA-256 context. This must be initialized.
Hanno Becker77886af2018-12-18 14:54:04 +0000173 * \param data The buffer holding one block of data. This must
174 * be a readable buffer of length \c 64 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100175 *
Rose Zadik602285e2018-01-26 11:00:39 +0000176 * \return \c 0 on success.
Hanno Becker77886af2018-12-18 14:54:04 +0000177 * \return A negative error code on failure.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100178 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100179int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
180 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100181
182#if !defined(MBEDTLS_DEPRECATED_REMOVED)
183#if defined(MBEDTLS_DEPRECATED_WARNING)
184#define MBEDTLS_DEPRECATED __attribute__((deprecated))
185#else
186#define MBEDTLS_DEPRECATED
187#endif
188/**
Rose Zadikbde68b42018-03-27 12:59:13 +0100189 * \brief This function starts a SHA-224 or SHA-256 checksum
190 * calculation.
191 *
Rose Zadik602285e2018-01-26 11:00:39 +0000192 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100193 *
Hanno Becker77886af2018-12-18 14:54:04 +0000194 * \param ctx The context to use. This must be initialized.
195 * \param is224 Determines which function to use. This must be
196 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100197 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000198MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
199 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100200
201/**
Rose Zadik602285e2018-01-26 11:00:39 +0000202 * \brief This function feeds an input buffer into an ongoing
203 * SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100204 *
Rose Zadik602285e2018-01-26 11:00:39 +0000205 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100206 *
Hanno Becker77886af2018-12-18 14:54:04 +0000207 * \param ctx The SHA-256 context to use. This must be
208 * initialized and have a hash operation started.
209 * \param input The buffer holding the data. This must be a readable
210 * buffer of length \p ilen Bytes.
Hanno Beckerfc2a0b22018-12-18 16:31:48 +0000211 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100212 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000213MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
214 const unsigned char *input,
215 size_t ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100216
217/**
Rose Zadik602285e2018-01-26 11:00:39 +0000218 * \brief This function finishes the SHA-256 operation, and writes
219 * the result to the output buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100220 *
Rose Zadik602285e2018-01-26 11:00:39 +0000221 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100222 *
Hanno Becker77886af2018-12-18 14:54:04 +0000223 * \param ctx The SHA-256 context. This must be initialized and
Hanno Beckerfc2a0b22018-12-18 16:31:48 +0000224 * have a hash operation started.
Hanno Becker77886af2018-12-18 14:54:04 +0000225 * \param output The SHA-224 or SHA-256 checksum result. This must be
226 * a writable buffer of length \c 32 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100227 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000228MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
229 unsigned char output[32] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100230
231/**
Rose Zadik602285e2018-01-26 11:00:39 +0000232 * \brief This function processes a single data block within
233 * the ongoing SHA-256 computation. This function is for
234 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100235 *
Rose Zadik602285e2018-01-26 11:00:39 +0000236 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100237 *
Hanno Becker3f1f4ad2018-12-18 23:19:37 +0000238 * \param ctx The SHA-256 context. This must be initialized.
Hanno Becker77886af2018-12-18 14:54:04 +0000239 * \param data The buffer holding one block of data. This must be
240 * a readable buffer of size \c 64 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100241 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000242MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
243 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100244
245#undef MBEDTLS_DEPRECATED
246#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200247
Paul Bakker5121ce52009-01-03 21:22:43 +0000248/**
Rose Zadik602285e2018-01-26 11:00:39 +0000249 * \brief This function calculates the SHA-224 or SHA-256
250 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 *
Rose Zadik602285e2018-01-26 11:00:39 +0000252 * The function allocates the context, performs the
253 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100254 *
Rose Zadik602285e2018-01-26 11:00:39 +0000255 * The SHA-256 result is calculated as
256 * output = SHA-256(input buffer).
257 *
Hanno Becker77886af2018-12-18 14:54:04 +0000258 * \param input The buffer holding the data. This must be a readable
259 * buffer of length \p ilen Bytes.
Hanno Beckerfc2a0b22018-12-18 16:31:48 +0000260 * \param ilen The length of the input data in Bytes.
Hanno Becker77886af2018-12-18 14:54:04 +0000261 * \param output The SHA-224 or SHA-256 checksum result. This must
262 * be a writable buffer of length \c 32 Bytes.
263 * \param is224 Determines which function to use. This must be
264 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100266int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100267 size_t ilen,
268 unsigned char output[32],
269 int is224 );
270
271#if !defined(MBEDTLS_DEPRECATED_REMOVED)
272#if defined(MBEDTLS_DEPRECATED_WARNING)
273#define MBEDTLS_DEPRECATED __attribute__((deprecated))
274#else
275#define MBEDTLS_DEPRECATED
276#endif
Rose Zadik602285e2018-01-26 11:00:39 +0000277
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100278/**
Rose Zadik602285e2018-01-26 11:00:39 +0000279 * \brief This function calculates the SHA-224 or SHA-256 checksum
280 * of a buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100281 *
Rose Zadik602285e2018-01-26 11:00:39 +0000282 * The function allocates the context, performs the
283 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100284 *
Rose Zadik602285e2018-01-26 11:00:39 +0000285 * The SHA-256 result is calculated as
286 * output = SHA-256(input buffer).
287 *
288 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
289 *
Hanno Becker77886af2018-12-18 14:54:04 +0000290 * \param input The buffer holding the data. This must be a readable
291 * buffer of length \p ilen Bytes.
Hanno Beckerfc2a0b22018-12-18 16:31:48 +0000292 * \param ilen The length of the input data in Bytes.
Hanno Becker77886af2018-12-18 14:54:04 +0000293 * \param output The SHA-224 or SHA-256 checksum result. This must be
294 * a writable buffer of length \c 32 Bytes.
Hanno Beckerfc2a0b22018-12-18 16:31:48 +0000295 * \param is224 Determines which function to use. This must be either
Hanno Becker77886af2018-12-18 14:54:04 +0000296 * \c 0 for SHA-256, or \c 1 for SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000298MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
299 size_t ilen,
300 unsigned char output[32],
301 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100302
303#undef MBEDTLS_DEPRECATED
304#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
Ron Eldorfa8f6352017-06-20 15:48:46 +0300306#if defined(MBEDTLS_SELF_TEST)
307
Paul Bakker5121ce52009-01-03 21:22:43 +0000308/**
Rose Zadik602285e2018-01-26 11:00:39 +0000309 * \brief The SHA-224 and SHA-256 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100311 * \return \c 0 on success.
312 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000315
Ron Eldorfa8f6352017-06-20 15:48:46 +0300316#endif /* MBEDTLS_SELF_TEST */
317
Paul Bakker5121ce52009-01-03 21:22:43 +0000318#ifdef __cplusplus
319}
320#endif
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#endif /* mbedtls_sha256.h */