blob: 8d7a1f733e275459025c2c9571925fb1ebca0562 [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 Zadik602285e2018-01-26 11:00:39 +00004 * \brief The SHA-224 and SHA-256 cryptographic hash function.
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Rose Zadik602285e2018-01-26 11:00:39 +00007 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000021 *
Rose Zadik602285e2018-01-26 11:00:39 +000022 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_SHA256_H
25#define MBEDTLS_SHA256_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000035
Gilles Peskinea381fe82018-01-23 18:16:11 +010036#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
37
Paul Bakker407a0da2013-06-27 14:29:21 +020038#ifdef __cplusplus
39extern "C" {
40#endif
41
Ron Eldorb2aacec2017-05-18 16:53:08 +030042#if !defined(MBEDTLS_SHA256_ALT)
43// Regular implementation
44//
45
Paul Bakker5121ce52009-01-03 21:22:43 +000046/**
Rose Zadik602285e2018-01-26 11:00:39 +000047 * \brief The SHA-256 context structure.
48 *
49 * The structure is used both for SHA-256 and for SHA-224
50 * checksum calculations. The choice between these two is
51 * made in the call to mbedtls_sha256_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000052 */
53typedef struct
54{
Rose Zadik602285e2018-01-26 11:00:39 +000055 uint32_t total[2]; /*!< The number of Bytes processed. */
56 uint32_t state[8]; /*!< The intermediate digest state. */
57 unsigned char buffer[64]; /*!< The data block being processed. */
58 int is224; /*!< Determines which function to use.
59 <ul><li>0: Use SHA-256.</li>
60 <li>1: Use SHA-224.</li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +000061}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Ron Eldorb2aacec2017-05-18 16:53:08 +030064#else /* MBEDTLS_SHA256_ALT */
65#include "sha256_alt.h"
66#endif /* MBEDTLS_SHA256_ALT */
67
Paul Bakker5121ce52009-01-03 21:22:43 +000068/**
Rose Zadik602285e2018-01-26 11:00:39 +000069 * \brief This function initializes a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020070 *
Rose Zadik602285e2018-01-26 11:00:39 +000071 * \param ctx The SHA-256 context to initialize.
Paul Bakker5b4af392014-06-26 12:09:34 +020072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020074
75/**
Rose Zadik602285e2018-01-26 11:00:39 +000076 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020077 *
Rose Zadik602285e2018-01-26 11:00:39 +000078 * \param ctx The SHA-256 context to clear.
Paul Bakker5b4af392014-06-26 12:09:34 +020079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020081
82/**
Rose Zadik602285e2018-01-26 11:00:39 +000083 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020084 *
Rose Zadik602285e2018-01-26 11:00:39 +000085 * \param dst The destination context.
86 * \param src The context to clone.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020087 */
88void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
89 const mbedtls_sha256_context *src );
90
91/**
Rose Zadik602285e2018-01-26 11:00:39 +000092 * \brief This function starts a SHA-224 or SHA-256 checksum
93 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000094 *
Rose Zadik602285e2018-01-26 11:00:39 +000095 * \param ctx The context to initialize.
96 * \param is224 Determines which function to use.
97 * <ul><li>0: Use SHA-256.</li>
98 * <li>1: Use SHA-224.</li></ul>
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +010099 *
Rose Zadik602285e2018-01-26 11:00:39 +0000100 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100102int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
104/**
Rose Zadik602285e2018-01-26 11:00:39 +0000105 * \brief This function feeds an input buffer into an ongoing
106 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 *
108 * \param ctx SHA-256 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100109 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 * \param ilen length of the input data
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100111 *
Rose Zadik602285e2018-01-26 11:00:39 +0000112 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100114int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100115 const unsigned char *input,
116 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
118/**
Rose Zadik602285e2018-01-26 11:00:39 +0000119 * \brief This function finishes the SHA-256 operation, and writes
120 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 *
Rose Zadik602285e2018-01-26 11:00:39 +0000122 * \param ctx The SHA-256 context.
123 * \param output The SHA-224 or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100124 *
Rose Zadik602285e2018-01-26 11:00:39 +0000125 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100127int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100128 unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100130/**
Rose Zadik602285e2018-01-26 11:00:39 +0000131 * \brief This function processes a single data block within
132 * the ongoing SHA-256 computation. This function is for
133 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100134 *
Rose Zadik602285e2018-01-26 11:00:39 +0000135 * \param ctx The SHA-256 context.
136 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100137 *
Rose Zadik602285e2018-01-26 11:00:39 +0000138 * \return \c 0 on success.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100139 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100140int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
141 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100142
143#if !defined(MBEDTLS_DEPRECATED_REMOVED)
144#if defined(MBEDTLS_DEPRECATED_WARNING)
145#define MBEDTLS_DEPRECATED __attribute__((deprecated))
146#else
147#define MBEDTLS_DEPRECATED
148#endif
149/**
Rose Zadik602285e2018-01-26 11:00:39 +0000150 * \brief This function starts a SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100151 *
Rose Zadik602285e2018-01-26 11:00:39 +0000152 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100153 *
Rose Zadik602285e2018-01-26 11:00:39 +0000154 * \param ctx The SHA-256 context to initialize.
155 * \param is224 Determines which function to use.
156 * <ul><li>0: Use SHA-256.</li>
157 * <li>1: Use SHA-224.</li></ul>
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100158 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000159MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
160 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100161
162/**
Rose Zadik602285e2018-01-26 11:00:39 +0000163 * \brief This function feeds an input buffer into an ongoing
164 * SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100165 *
Rose Zadik602285e2018-01-26 11:00:39 +0000166 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100167 *
Rose Zadik602285e2018-01-26 11:00:39 +0000168 * \param ctx The SHA-256 context to initialize.
169 * \param input The buffer holding the data.
170 * \param ilen The length of the input data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100171 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000172MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
173 const unsigned char *input,
174 size_t ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100175
176/**
Rose Zadik602285e2018-01-26 11:00:39 +0000177 * \brief This function finishes the SHA-256 operation, and writes
178 * the result to the output buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100179 *
Rose Zadik602285e2018-01-26 11:00:39 +0000180 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100181 *
Rose Zadik602285e2018-01-26 11:00:39 +0000182 * \param ctx The SHA-256 context.
183 * \param output The SHA-224or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100184 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000185MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
186 unsigned char output[32] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100187
188/**
Rose Zadik602285e2018-01-26 11:00:39 +0000189 * \brief This function processes a single data block within
190 * the ongoing SHA-256 computation. This function is for
191 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100192 *
Rose Zadik602285e2018-01-26 11:00:39 +0000193 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100194 *
Rose Zadik602285e2018-01-26 11:00:39 +0000195 * \param ctx The SHA-256 context.
196 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100197 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000198MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
199 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100200
201#undef MBEDTLS_DEPRECATED
202#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200203
Paul Bakker5121ce52009-01-03 21:22:43 +0000204/**
Rose Zadik602285e2018-01-26 11:00:39 +0000205 * \brief This function calculates the SHA-224 or SHA-256
206 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 *
Rose Zadik602285e2018-01-26 11:00:39 +0000208 * The function allocates the context, performs the
209 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100210 *
Rose Zadik602285e2018-01-26 11:00:39 +0000211 * The SHA-256 result is calculated as
212 * output = SHA-256(input buffer).
213 *
214 * \param input The buffer holding the input data.
215 * \param ilen The length of the input data.
216 * \param output The SHA-224 or SHA-256 checksum result.
217 * \param is224 Determines which function to use.
218 * <ul><li>0: Use SHA-256.</li>
219 * <li>1: Use SHA-224.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100221int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100222 size_t ilen,
223 unsigned char output[32],
224 int is224 );
225
226#if !defined(MBEDTLS_DEPRECATED_REMOVED)
227#if defined(MBEDTLS_DEPRECATED_WARNING)
228#define MBEDTLS_DEPRECATED __attribute__((deprecated))
229#else
230#define MBEDTLS_DEPRECATED
231#endif
Rose Zadik602285e2018-01-26 11:00:39 +0000232
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100233/**
Rose Zadik602285e2018-01-26 11:00:39 +0000234 * \brief This function calculates the SHA-224 or SHA-256 checksum
235 * of a buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100236 *
Rose Zadik602285e2018-01-26 11:00:39 +0000237 * The function allocates the context, performs the
238 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100239 *
Rose Zadik602285e2018-01-26 11:00:39 +0000240 * The SHA-256 result is calculated as
241 * output = SHA-256(input buffer).
242 *
243 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
244 *
245 * \param input The buffer holding the data.
246 * \param ilen The length of the input data.
247 * \param output The SHA-224 or SHA-256 checksum result.
248 * \param is224 Determines which function to use.
249 * <ul><li>0: Use SHA-256.</li>
250 * <li>1: Use SHA-224.</li></ul>
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
260/**
Rose Zadik602285e2018-01-26 11:00:39 +0000261 * \brief The SHA-224 and SHA-256 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 *
Rose Zadik602285e2018-01-26 11:00:39 +0000263 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
Paul Bakker5121ce52009-01-03 21:22:43 +0000267#ifdef __cplusplus
268}
269#endif
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271#endif /* mbedtls_sha256.h */