blob: 6305f8c05c5d4f0323316824ec83ab0932f976fb [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)
Jaeden Amero203fdf72019-07-04 20:01:14 +010031#include "mbedtls/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
Jaeden Amero59ebddf2019-07-10 11:03:03 +010036#include "mbedtls/platform_util.h"
37
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020039#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000040
Ron Eldor9924bdc2018-10-04 10:59:13 +030041/* MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea381fe82018-01-23 18:16:11 +010042#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050043#define MBEDTLS_ERR_SHA256_BAD_INPUT_DATA -0x0074 /**< SHA-256 input data was malformed. */
Gilles Peskinea381fe82018-01-23 18:16:11 +010044
Paul Bakker407a0da2013-06-27 14:29:21 +020045#ifdef __cplusplus
46extern "C" {
47#endif
48
Ron Eldorb2aacec2017-05-18 16:53:08 +030049#if !defined(MBEDTLS_SHA256_ALT)
50// Regular implementation
51//
52
Paul Bakker5121ce52009-01-03 21:22:43 +000053/**
Rose Zadik602285e2018-01-26 11:00:39 +000054 * \brief The SHA-256 context structure.
55 *
56 * The structure is used both for SHA-256 and for SHA-224
57 * checksum calculations. The choice between these two is
58 * made in the call to mbedtls_sha256_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000059 */
Dawid Drozd428cc522018-07-24 10:02:47 +020060typedef struct mbedtls_sha256_context
Paul Bakker5121ce52009-01-03 21:22:43 +000061{
Rose Zadik602285e2018-01-26 11:00:39 +000062 uint32_t total[2]; /*!< The number of Bytes processed. */
63 uint32_t state[8]; /*!< The intermediate digest state. */
64 unsigned char buffer[64]; /*!< The data block being processed. */
Rose Zadikbde68b42018-03-27 12:59:13 +010065 int is224; /*!< Determines which function to use:
66 0: Use SHA-256, or 1: Use SHA-224. */
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 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050077 * \param ctx The SHA-256 context to initialize. This must not be \c NULL.
Paul Bakker5b4af392014-06-26 12:09:34 +020078 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +010079MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020081
82/**
Rose Zadik602285e2018-01-26 11:00:39 +000083 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020084 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050085 * \param ctx The SHA-256 context to clear. This may be \c NULL, in which
86 * case this function returns immediately. If it is not \c NULL,
87 * it must point to an initialized SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020088 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +010089MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020091
92/**
Rose Zadik602285e2018-01-26 11:00:39 +000093 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020094 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050095 * \param dst The destination context. This must be initialized.
96 * \param src The context to clone. This must be initialized.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020097 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +010098MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020099void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
100 const mbedtls_sha256_context *src );
101
102/**
Rose Zadik602285e2018-01-26 11:00:39 +0000103 * \brief This function starts a SHA-224 or SHA-256 checksum
104 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500106 * \param ctx The context to use. This must be initialized.
107 * \param is224 This determines which function to use. This must be
108 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100109 *
Rose Zadik602285e2018-01-26 11:00:39 +0000110 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500111 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100113MBEDTLS_DEPRECATED
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100114int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116/**
Rose Zadik602285e2018-01-26 11:00:39 +0000117 * \brief This function feeds an input buffer into an ongoing
118 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500120 * \param ctx The SHA-256 context. This must be initialized
121 * and have a hash operation started.
122 * \param input The buffer holding the data. This must be a readable
123 * buffer of length \p ilen Bytes.
124 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100125 *
Rose Zadik602285e2018-01-26 11:00:39 +0000126 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500127 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100129MBEDTLS_DEPRECATED
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100130int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100131 const unsigned char *input,
132 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133
134/**
Rose Zadik602285e2018-01-26 11:00:39 +0000135 * \brief This function finishes the SHA-256 operation, and writes
136 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500138 * \param ctx The SHA-256 context. This must be initialized
139 * and have a hash operation started.
Rose Zadik602285e2018-01-26 11:00:39 +0000140 * \param output The SHA-224 or SHA-256 checksum result.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500141 * This must be a writable buffer of length \c 32 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100142 *
Rose Zadik602285e2018-01-26 11:00:39 +0000143 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500144 * \return A negative error code on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100146MBEDTLS_DEPRECATED
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100147int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100148 unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100150/**
Rose Zadik602285e2018-01-26 11:00:39 +0000151 * \brief This function processes a single data block within
152 * the ongoing SHA-256 computation. This function is for
153 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100154 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500155 * \param ctx The SHA-256 context. This must be initialized.
156 * \param data The buffer holding one block of data. This must
157 * be a readable buffer of length \c 64 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100158 *
Rose Zadik602285e2018-01-26 11:00:39 +0000159 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500160 * \return A negative error code on failure.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100161 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100162int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
163 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100164
165#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100166/**
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 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500172 * \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.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100175 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000176MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
177 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100178
179/**
Rose Zadik602285e2018-01-26 11:00:39 +0000180 * \brief This function feeds an input buffer into an ongoing
181 * SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100182 *
Rose Zadik602285e2018-01-26 11:00:39 +0000183 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100184 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500185 * \param ctx The SHA-256 context to use. This must be
186 * initialized and have a hash operation started.
187 * \param input The buffer holding the data. This must be a readable
188 * buffer of length \p ilen Bytes.
189 * \param ilen The length of the input data in Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100190 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000191MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
192 const unsigned char *input,
193 size_t ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100194
195/**
Rose Zadik602285e2018-01-26 11:00:39 +0000196 * \brief This function finishes the SHA-256 operation, and writes
197 * the result to the output buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100198 *
Rose Zadik602285e2018-01-26 11:00:39 +0000199 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100200 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500201 * \param ctx The SHA-256 context. This must be initialized and
202 * have a hash operation started.
203 * \param output The SHA-224 or SHA-256 checksum result. This must be
204 * a writable buffer of length \c 32 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100205 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000206MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
207 unsigned char output[32] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100208
209/**
Rose Zadik602285e2018-01-26 11:00:39 +0000210 * \brief This function processes a single data block within
211 * the ongoing SHA-256 computation. This function is for
212 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100213 *
Rose Zadik602285e2018-01-26 11:00:39 +0000214 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100215 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216 * \param ctx The SHA-256 context. This must be initialized.
217 * \param data The buffer holding one block of data. This must be
218 * a readable buffer of size \c 64 Bytes.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100219 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000220MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
221 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100222#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200223
Paul Bakker5121ce52009-01-03 21:22:43 +0000224/**
Rose Zadik602285e2018-01-26 11:00:39 +0000225 * \brief This function calculates the SHA-224 or SHA-256
226 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 *
Rose Zadik602285e2018-01-26 11:00:39 +0000228 * The function allocates the context, performs the
229 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100230 *
Rose Zadik602285e2018-01-26 11:00:39 +0000231 * The SHA-256 result is calculated as
232 * output = SHA-256(input buffer).
233 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500234 * \param input The buffer holding the data. This must be a readable
235 * buffer of length \p ilen Bytes.
236 * \param ilen The length of the input data in Bytes.
237 * \param output The SHA-224 or SHA-256 checksum result. This must
238 * be a writable buffer of length \c 32 Bytes.
239 * \param is224 Determines which function to use. This must be
240 * either \c 0 for SHA-256, or \c 1 for SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100242MBEDTLS_DEPRECATED
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100243int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100244 size_t ilen,
245 unsigned char output[32],
246 int is224 );
247
248#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100249/**
Rose Zadik602285e2018-01-26 11:00:39 +0000250 * \brief This function calculates the SHA-224 or SHA-256 checksum
251 * of a buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100252 *
Rose Zadik602285e2018-01-26 11:00:39 +0000253 * The function allocates the context, performs the
254 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100255 *
Rose Zadik602285e2018-01-26 11:00:39 +0000256 * The SHA-256 result is calculated as
257 * output = SHA-256(input buffer).
258 *
259 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
260 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500261 * \param input The buffer holding the data. This must be a readable
262 * buffer of length \p ilen Bytes.
263 * \param ilen The length of the input data in Bytes.
264 * \param output The SHA-224 or SHA-256 checksum result. This must be
265 * a writable buffer of length \c 32 Bytes.
266 * \param is224 Determines which function to use. This must be either
267 * \c 0 for SHA-256, or \c 1 for SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000269MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
270 size_t ilen,
271 unsigned char output[32],
272 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100273#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500275#if defined(MBEDTLS_SELF_TEST)
276
Paul Bakker5121ce52009-01-03 21:22:43 +0000277/**
Rose Zadik602285e2018-01-26 11:00:39 +0000278 * \brief The SHA-224 and SHA-256 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100280 * \return \c 0 on success.
281 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100283MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286#endif /* MBEDTLS_SELF_TEST */
287
Paul Bakker5121ce52009-01-03 21:22:43 +0000288#ifdef __cplusplus
289}
290#endif
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#endif /* mbedtls_sha256.h */