blob: 42aa988c607b1d8892ec229b4d9edab621a1bf82 [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/*
Rose Zadik602285e2018-01-26 11:00:39 +000010 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
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 Bakkerb96f1542010-07-18 20:36:00 +000024 *
Rose Zadik602285e2018-01-26 11:00:39 +000025 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#ifndef MBEDTLS_SHA256_H
28#define MBEDTLS_SHA256_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker90995b52013-06-24 19:20:35 +020035
Rich Evans00ab4702015-02-06 13:43:58 +000036#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020037#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000038
Ron Eldor9924bdc2018-10-04 10:59:13 +030039/* MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea381fe82018-01-23 18:16:11 +010040#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
Andres Amaya Garcia0152f1e2018-12-10 10:22:27 +000041#define MBEDTLS_ERR_SHA256_BAD_INPUT_DATA -0x0074 /**< SHA-256 input data was malformed. */
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. */
Manuel Pégourié-Gonnard8463d292019-07-16 14:39:55 +020063#if !defined(MBEDTLS_SHA256_NO_SHA224)
Rose Zadikbde68b42018-03-27 12:59:13 +010064 int is224; /*!< Determines which function to use:
65 0: Use SHA-256, or 1: Use SHA-224. */
Manuel Pégourié-Gonnard8463d292019-07-16 14:39:55 +020066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Ron Eldorb2aacec2017-05-18 16:53:08 +030070#else /* MBEDTLS_SHA256_ALT */
71#include "sha256_alt.h"
72#endif /* MBEDTLS_SHA256_ALT */
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/**
Rose Zadik602285e2018-01-26 11:00:39 +000075 * \brief This function initializes a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020076 *
Hanno Becker77886af2018-12-18 14:54:04 +000077 * \param ctx The SHA-256 context to initialize. This must not be \c NULL.
Paul Bakker5b4af392014-06-26 12:09:34 +020078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020080
81/**
Rose Zadik602285e2018-01-26 11:00:39 +000082 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020083 *
Hanno Becker77886af2018-12-18 14:54:04 +000084 * \param ctx The SHA-256 context to clear. This may be \c NULL, in which
85 * case this function returns immediately. If it is not \c NULL,
86 * it must point to an initialized SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020089
90/**
Rose Zadik602285e2018-01-26 11:00:39 +000091 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020092 *
Hanno Becker77886af2018-12-18 14:54:04 +000093 * \param dst The destination context. This must be initialized.
94 * \param src The context to clone. This must be initialized.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020095 */
96void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
97 const mbedtls_sha256_context *src );
98
99/**
Rose Zadik602285e2018-01-26 11:00:39 +0000100 * \brief This function starts a SHA-224 or SHA-256 checksum
101 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 *
Hanno Becker77886af2018-12-18 14:54:04 +0000103 * \param ctx The context to use. This must be initialized.
104 * \param is224 This determines which function to use. This must be
105 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Manuel Pégourié-Gonnardefd34482019-09-02 14:41:19 +0200106 * If #MBEDTLS_SHA256_NO_SHA224 is defined, this must be \c 0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100107 *
Rose Zadik602285e2018-01-26 11:00:39 +0000108 * \return \c 0 on success.
Hanno Becker77886af2018-12-18 14:54:04 +0000109 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100111int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113/**
Rose Zadik602285e2018-01-26 11:00:39 +0000114 * \brief This function feeds an input buffer into an ongoing
115 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 *
Hanno Becker77886af2018-12-18 14:54:04 +0000117 * \param ctx The SHA-256 context. This must be initialized
118 * and have a hash operation started.
119 * \param input The buffer holding the data. This must be a readable
120 * buffer of length \p ilen Bytes.
Andrzej Kurek89173262020-07-15 18:51:40 -0400121 * \param ilen The length of the input data in Bytes. At most UINT32_MAX.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100122 *
Rose Zadik602285e2018-01-26 11:00:39 +0000123 * \return \c 0 on success.
Hanno Becker77886af2018-12-18 14:54:04 +0000124 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100126int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100127 const unsigned char *input,
128 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
130/**
Rose Zadik602285e2018-01-26 11:00:39 +0000131 * \brief This function finishes the SHA-256 operation, and writes
132 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 *
Hanno Becker77886af2018-12-18 14:54:04 +0000134 * \param ctx The SHA-256 context. This must be initialized
135 * and have a hash operation started.
Rose Zadik602285e2018-01-26 11:00:39 +0000136 * \param output The SHA-224 or SHA-256 checksum result.
Hanno Becker77886af2018-12-18 14:54:04 +0000137 * This must be a writable buffer of length \c 32 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100138 *
Rose Zadik602285e2018-01-26 11:00:39 +0000139 * \return \c 0 on success.
Hanno Becker77886af2018-12-18 14:54:04 +0000140 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100142int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100143 unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100145/**
Rose Zadik602285e2018-01-26 11:00:39 +0000146 * \brief This function processes a single data block within
147 * the ongoing SHA-256 computation. This function is for
148 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100149 *
Hanno Becker3f1f4ad2018-12-18 23:19:37 +0000150 * \param ctx The SHA-256 context. This must be initialized.
Hanno Becker77886af2018-12-18 14:54:04 +0000151 * \param data The buffer holding one block of data. This must
152 * be a readable buffer of length \c 64 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100153 *
Rose Zadik602285e2018-01-26 11:00:39 +0000154 * \return \c 0 on success.
Hanno Becker77886af2018-12-18 14:54:04 +0000155 * \return A negative error code on failure.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100156 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100157int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
158 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100159
160#if !defined(MBEDTLS_DEPRECATED_REMOVED)
161#if defined(MBEDTLS_DEPRECATED_WARNING)
162#define MBEDTLS_DEPRECATED __attribute__((deprecated))
163#else
164#define MBEDTLS_DEPRECATED
165#endif
166/**
Rose Zadikbde68b42018-03-27 12:59:13 +0100167 * \brief This function starts a SHA-224 or SHA-256 checksum
168 * calculation.
169 *
Rose Zadik602285e2018-01-26 11:00:39 +0000170 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100171 *
Hanno Becker77886af2018-12-18 14:54:04 +0000172 * \param ctx The context to use. This must be initialized.
173 * \param is224 Determines which function to use. This must be
174 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Manuel Pégourié-Gonnardefd34482019-09-02 14:41:19 +0200175 * If #MBEDTLS_SHA256_NO_SHA224 is defined, this must be \c 0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100176 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000177MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
178 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100179
180/**
Rose Zadik602285e2018-01-26 11:00:39 +0000181 * \brief This function feeds an input buffer into an ongoing
182 * SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100183 *
Rose Zadik602285e2018-01-26 11:00:39 +0000184 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100185 *
Hanno Becker77886af2018-12-18 14:54:04 +0000186 * \param ctx The SHA-256 context to use. This must be
187 * initialized and have a hash operation started.
188 * \param input The buffer holding the data. This must be a readable
189 * buffer of length \p ilen Bytes.
Hanno Beckerfc2a0b22018-12-18 16:31:48 +0000190 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100191 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000192MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
193 const unsigned char *input,
194 size_t ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100195
196/**
Rose Zadik602285e2018-01-26 11:00:39 +0000197 * \brief This function finishes the SHA-256 operation, and writes
198 * the result to the output buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100199 *
Rose Zadik602285e2018-01-26 11:00:39 +0000200 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100201 *
Hanno Becker77886af2018-12-18 14:54:04 +0000202 * \param ctx The SHA-256 context. This must be initialized and
Hanno Beckerfc2a0b22018-12-18 16:31:48 +0000203 * have a hash operation started.
Hanno Becker77886af2018-12-18 14:54:04 +0000204 * \param output The SHA-224 or SHA-256 checksum result. This must be
205 * a writable buffer of length \c 32 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100206 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000207MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
208 unsigned char output[32] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100209
210/**
Rose Zadik602285e2018-01-26 11:00:39 +0000211 * \brief This function processes a single data block within
212 * the ongoing SHA-256 computation. This function is for
213 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100214 *
Rose Zadik602285e2018-01-26 11:00:39 +0000215 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100216 *
Hanno Becker3f1f4ad2018-12-18 23:19:37 +0000217 * \param ctx The SHA-256 context. This must be initialized.
Hanno Becker77886af2018-12-18 14:54:04 +0000218 * \param data The buffer holding one block of data. This must be
219 * a readable buffer of size \c 64 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100220 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000221MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
222 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100223
224#undef MBEDTLS_DEPRECATED
225#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200226
Paul Bakker5121ce52009-01-03 21:22:43 +0000227/**
Rose Zadik602285e2018-01-26 11:00:39 +0000228 * \brief This function calculates the SHA-224 or SHA-256
229 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 *
Rose Zadik602285e2018-01-26 11:00:39 +0000231 * The function allocates the context, performs the
232 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100233 *
Rose Zadik602285e2018-01-26 11:00:39 +0000234 * The SHA-256 result is calculated as
235 * output = SHA-256(input buffer).
236 *
Hanno Becker77886af2018-12-18 14:54:04 +0000237 * \param input The buffer holding the data. This must be a readable
238 * buffer of length \p ilen Bytes.
Hanno Beckerfc2a0b22018-12-18 16:31:48 +0000239 * \param ilen The length of the input data in Bytes.
Hanno Becker77886af2018-12-18 14:54:04 +0000240 * \param output The SHA-224 or SHA-256 checksum result. This must
241 * be a writable buffer of length \c 32 Bytes.
242 * \param is224 Determines which function to use. This must be
243 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Manuel Pégourié-Gonnardefd34482019-09-02 14:41:19 +0200244 * If #MBEDTLS_SHA256_NO_SHA224 is defined, this must be \c 0.
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100246int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100247 size_t ilen,
248 unsigned char output[32],
249 int is224 );
250
251#if !defined(MBEDTLS_DEPRECATED_REMOVED)
252#if defined(MBEDTLS_DEPRECATED_WARNING)
253#define MBEDTLS_DEPRECATED __attribute__((deprecated))
254#else
255#define MBEDTLS_DEPRECATED
256#endif
Rose Zadik602285e2018-01-26 11:00:39 +0000257
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100258/**
Rose Zadik602285e2018-01-26 11:00:39 +0000259 * \brief This function calculates the SHA-224 or SHA-256 checksum
260 * of a buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100261 *
Rose Zadik602285e2018-01-26 11:00:39 +0000262 * The function allocates the context, performs the
263 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100264 *
Rose Zadik602285e2018-01-26 11:00:39 +0000265 * The SHA-256 result is calculated as
266 * output = SHA-256(input buffer).
267 *
268 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
269 *
Hanno Becker77886af2018-12-18 14:54:04 +0000270 * \param input The buffer holding the data. This must be a readable
271 * buffer of length \p ilen Bytes.
Hanno Beckerfc2a0b22018-12-18 16:31:48 +0000272 * \param ilen The length of the input data in Bytes.
Hanno Becker77886af2018-12-18 14:54:04 +0000273 * \param output The SHA-224 or SHA-256 checksum result. This must be
274 * a writable buffer of length \c 32 Bytes.
Hanno Beckerfc2a0b22018-12-18 16:31:48 +0000275 * \param is224 Determines which function to use. This must be either
Hanno Becker77886af2018-12-18 14:54:04 +0000276 * \c 0 for SHA-256, or \c 1 for SHA-224.
Manuel Pégourié-Gonnardefd34482019-09-02 14:41:19 +0200277 * If #MBEDTLS_SHA256_NO_SHA224 is defined, this must be \c 0.
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000279MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
280 size_t ilen,
281 unsigned char output[32],
282 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100283
284#undef MBEDTLS_DEPRECATED
285#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
Ron Eldorfa8f6352017-06-20 15:48:46 +0300287#if defined(MBEDTLS_SELF_TEST)
288
Paul Bakker5121ce52009-01-03 21:22:43 +0000289/**
Rose Zadik602285e2018-01-26 11:00:39 +0000290 * \brief The SHA-224 and SHA-256 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100292 * \return \c 0 on success.
293 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Ron Eldorfa8f6352017-06-20 15:48:46 +0300297#endif /* MBEDTLS_SELF_TEST */
298
Paul Bakker5121ce52009-01-03 21:22:43 +0000299#ifdef __cplusplus
300}
301#endif
302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#endif /* mbedtls_sha256.h */