blob: de19dc249ad4f3d300c37f4aea5b8a39a5d57add [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
Gilles Peskinea381fe82018-01-23 18:16:11 +010039#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
40
Paul Bakker407a0da2013-06-27 14:29:21 +020041#ifdef __cplusplus
42extern "C" {
43#endif
44
Ron Eldorb2aacec2017-05-18 16:53:08 +030045#if !defined(MBEDTLS_SHA256_ALT)
46// Regular implementation
47//
48
Paul Bakker5121ce52009-01-03 21:22:43 +000049/**
Rose Zadik602285e2018-01-26 11:00:39 +000050 * \brief The SHA-256 context structure.
51 *
52 * The structure is used both for SHA-256 and for SHA-224
53 * checksum calculations. The choice between these two is
54 * made in the call to mbedtls_sha256_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000055 */
56typedef struct
57{
Rose Zadik602285e2018-01-26 11:00:39 +000058 uint32_t total[2]; /*!< The number of Bytes processed. */
59 uint32_t state[8]; /*!< The intermediate digest state. */
60 unsigned char buffer[64]; /*!< The data block being processed. */
Rose Zadikbde68b42018-03-27 12:59:13 +010061 int is224; /*!< Determines which function to use:
62 0: Use SHA-256, or 1: Use SHA-224. */
Paul Bakker5121ce52009-01-03 21:22:43 +000063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Ron Eldorb2aacec2017-05-18 16:53:08 +030066#else /* MBEDTLS_SHA256_ALT */
67#include "sha256_alt.h"
68#endif /* MBEDTLS_SHA256_ALT */
69
Paul Bakker5121ce52009-01-03 21:22:43 +000070/**
Rose Zadik602285e2018-01-26 11:00:39 +000071 * \brief This function initializes a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020072 *
Rose Zadik602285e2018-01-26 11:00:39 +000073 * \param ctx The SHA-256 context to initialize.
Paul Bakker5b4af392014-06-26 12:09:34 +020074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020076
77/**
Rose Zadik602285e2018-01-26 11:00:39 +000078 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020079 *
Rose Zadik602285e2018-01-26 11:00:39 +000080 * \param ctx The SHA-256 context to clear.
Paul Bakker5b4af392014-06-26 12:09:34 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020083
84/**
Rose Zadik602285e2018-01-26 11:00:39 +000085 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020086 *
Rose Zadik602285e2018-01-26 11:00:39 +000087 * \param dst The destination context.
88 * \param src The context to clone.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020089 */
90void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
91 const mbedtls_sha256_context *src );
92
93/**
Rose Zadik602285e2018-01-26 11:00:39 +000094 * \brief This function starts a SHA-224 or SHA-256 checksum
95 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000096 *
Rose Zadik602285e2018-01-26 11:00:39 +000097 * \param ctx The context to initialize.
Rose Zadikbde68b42018-03-27 12:59:13 +010098 * \param is224 Determines which function to use:
99 * 0: Use SHA-256, or 1: Use SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100100 *
Rose Zadik602285e2018-01-26 11:00:39 +0000101 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000102 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100103int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105/**
Rose Zadik602285e2018-01-26 11:00:39 +0000106 * \brief This function feeds an input buffer into an ongoing
107 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 *
Rose Zadik6ee22a72018-04-17 10:38:39 +0100109 * \param ctx The SHA-256 context.
Rose Zadikbde68b42018-03-27 12:59:13 +0100110 * \param input The buffer holding the data.
111 * \param ilen The length of the input data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100112 *
Rose Zadik602285e2018-01-26 11:00:39 +0000113 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100115int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100116 const unsigned char *input,
117 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
119/**
Rose Zadik602285e2018-01-26 11:00:39 +0000120 * \brief This function finishes the SHA-256 operation, and writes
121 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 *
Rose Zadik602285e2018-01-26 11:00:39 +0000123 * \param ctx The SHA-256 context.
124 * \param output The SHA-224 or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100125 *
Rose Zadik602285e2018-01-26 11:00:39 +0000126 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100128int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100129 unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100131/**
Rose Zadik602285e2018-01-26 11:00:39 +0000132 * \brief This function processes a single data block within
133 * the ongoing SHA-256 computation. This function is for
134 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100135 *
Rose Zadik602285e2018-01-26 11:00:39 +0000136 * \param ctx The SHA-256 context.
137 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100138 *
Rose Zadik602285e2018-01-26 11:00:39 +0000139 * \return \c 0 on success.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100140 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100141int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
142 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100143
144#if !defined(MBEDTLS_DEPRECATED_REMOVED)
145#if defined(MBEDTLS_DEPRECATED_WARNING)
146#define MBEDTLS_DEPRECATED __attribute__((deprecated))
147#else
148#define MBEDTLS_DEPRECATED
149#endif
150/**
Rose Zadikbde68b42018-03-27 12:59:13 +0100151 * \brief This function starts a SHA-224 or SHA-256 checksum
152 * calculation.
153 *
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100154 *
Rose Zadik602285e2018-01-26 11:00:39 +0000155 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100156 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100157 * \param ctx The context to initialize.
158 * \param is224 Determines which function to use:
159 * 0: Use SHA-256, or 1: Use SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100160 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000161MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
162 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100163
164/**
Rose Zadik602285e2018-01-26 11:00:39 +0000165 * \brief This function feeds an input buffer into an ongoing
166 * SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100167 *
Rose Zadik602285e2018-01-26 11:00:39 +0000168 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100169 *
Rose Zadik602285e2018-01-26 11:00:39 +0000170 * \param ctx The SHA-256 context to initialize.
171 * \param input The buffer holding the data.
172 * \param ilen The length of the input data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100173 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000174MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
175 const unsigned char *input,
176 size_t ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100177
178/**
Rose Zadik602285e2018-01-26 11:00:39 +0000179 * \brief This function finishes the SHA-256 operation, and writes
180 * the result to the output buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100181 *
Rose Zadik602285e2018-01-26 11:00:39 +0000182 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100183 *
Rose Zadik602285e2018-01-26 11:00:39 +0000184 * \param ctx The SHA-256 context.
Rose Zadikbde68b42018-03-27 12:59:13 +0100185 * \param output The SHA-224 or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100186 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000187MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
188 unsigned char output[32] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100189
190/**
Rose Zadik602285e2018-01-26 11:00:39 +0000191 * \brief This function processes a single data block within
192 * the ongoing SHA-256 computation. This function is for
193 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100194 *
Rose Zadik602285e2018-01-26 11:00:39 +0000195 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100196 *
Rose Zadik602285e2018-01-26 11:00:39 +0000197 * \param ctx The SHA-256 context.
198 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100199 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000200MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
201 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100202
203#undef MBEDTLS_DEPRECATED
204#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200205
Paul Bakker5121ce52009-01-03 21:22:43 +0000206/**
Rose Zadik602285e2018-01-26 11:00:39 +0000207 * \brief This function calculates the SHA-224 or SHA-256
208 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 *
Rose Zadik602285e2018-01-26 11:00:39 +0000210 * The function allocates the context, performs the
211 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100212 *
Rose Zadik602285e2018-01-26 11:00:39 +0000213 * The SHA-256 result is calculated as
214 * output = SHA-256(input buffer).
215 *
216 * \param input The buffer holding the input data.
217 * \param ilen The length of the input data.
218 * \param output The SHA-224 or SHA-256 checksum result.
Rose Zadikbde68b42018-03-27 12:59:13 +0100219 * \param is224 Determines which function to use:
220 * 0: Use SHA-256, or 1: Use SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100222int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100223 size_t ilen,
224 unsigned char output[32],
225 int is224 );
226
227#if !defined(MBEDTLS_DEPRECATED_REMOVED)
228#if defined(MBEDTLS_DEPRECATED_WARNING)
229#define MBEDTLS_DEPRECATED __attribute__((deprecated))
230#else
231#define MBEDTLS_DEPRECATED
232#endif
Rose Zadik602285e2018-01-26 11:00:39 +0000233
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100234/**
Rose Zadik602285e2018-01-26 11:00:39 +0000235 * \brief This function calculates the SHA-224 or SHA-256 checksum
236 * of a buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100237 *
Rose Zadik602285e2018-01-26 11:00:39 +0000238 * The function allocates the context, performs the
239 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100240 *
Rose Zadik602285e2018-01-26 11:00:39 +0000241 * The SHA-256 result is calculated as
242 * output = SHA-256(input buffer).
243 *
244 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
245 *
246 * \param input The buffer holding the data.
247 * \param ilen The length of the input data.
248 * \param output The SHA-224 or SHA-256 checksum result.
Rose Zadikbde68b42018-03-27 12:59:13 +0100249 * \param is224 Determines which function to use:
250 * 0: Use SHA-256, or 1: Use SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000252MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
253 size_t ilen,
254 unsigned char output[32],
255 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100256
257#undef MBEDTLS_DEPRECATED
258#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000259
Ron Eldorfa8f6352017-06-20 15:48:46 +0300260#if defined(MBEDTLS_SELF_TEST)
261
Paul Bakker5121ce52009-01-03 21:22:43 +0000262/**
Rose Zadik602285e2018-01-26 11:00:39 +0000263 * \brief The SHA-224 and SHA-256 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100265 * \return \c 0 on success.
266 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
Ron Eldorfa8f6352017-06-20 15:48:46 +0300270#endif /* MBEDTLS_SELF_TEST */
271
Paul Bakker5121ce52009-01-03 21:22:43 +0000272#ifdef __cplusplus
273}
274#endif
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#endif /* mbedtls_sha256.h */