blob: a2b6e11644dbb90e05c1cfdff8fda49c117b281a [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
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +010038#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
39 !defined(inline) && !defined(__cplusplus)
40#define inline __inline
41#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_SHA256_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020043// Regular implementation
44//
45
Paul Bakker407a0da2013-06-27 14:29:21 +020046#ifdef __cplusplus
47extern "C" {
48#endif
49
Paul Bakker5121ce52009-01-03 21:22:43 +000050/**
Rose Zadik602285e2018-01-26 11:00:39 +000051 * \brief The SHA-256 context structure.
52 *
53 * The structure is used both for SHA-256 and for SHA-224
54 * checksum calculations. The choice between these two is
55 * made in the call to mbedtls_sha256_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000056 */
57typedef struct
58{
Rose Zadik602285e2018-01-26 11:00:39 +000059 uint32_t total[2]; /*!< The number of Bytes processed. */
60 uint32_t state[8]; /*!< The intermediate digest state. */
61 unsigned char buffer[64]; /*!< The data block being processed. */
62 int is224; /*!< Determines which function to use.
63 <ul><li>0: Use SHA-256.</li>
64 <li>1: Use SHA-224.</li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +000065}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000067
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 */
159MBEDTLS_DEPRECATED static inline void mbedtls_sha256_starts(
160 mbedtls_sha256_context *ctx,
161 int is224 )
162{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100163 mbedtls_sha256_starts_ret( ctx, is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100164}
165
166/**
Rose Zadik602285e2018-01-26 11:00:39 +0000167 * \brief This function feeds an input buffer into an ongoing
168 * SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100169 *
Rose Zadik602285e2018-01-26 11:00:39 +0000170 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100171 *
Rose Zadik602285e2018-01-26 11:00:39 +0000172 * \param ctx The SHA-256 context to initialize.
173 * \param input The buffer holding the data.
174 * \param ilen The length of the input data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100175 */
176MBEDTLS_DEPRECATED static inline void mbedtls_sha256_update(
177 mbedtls_sha256_context *ctx,
178 const unsigned char *input,
179 size_t ilen )
180{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100181 mbedtls_sha256_update_ret( ctx, input, ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100182}
183
184/**
Rose Zadik602285e2018-01-26 11:00:39 +0000185 * \brief This function finishes the SHA-256 operation, and writes
186 * the result to the output buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100187 *
Rose Zadik602285e2018-01-26 11:00:39 +0000188 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100189 *
Rose Zadik602285e2018-01-26 11:00:39 +0000190 * \param ctx The SHA-256 context.
191 * \param output The SHA-224or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100192 */
193MBEDTLS_DEPRECATED static inline void mbedtls_sha256_finish(
194 mbedtls_sha256_context *ctx,
195 unsigned char output[32] )
196{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100197 mbedtls_sha256_finish_ret( ctx, output );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100198}
199
200/**
Rose Zadik602285e2018-01-26 11:00:39 +0000201 * \brief This function processes a single data block within
202 * the ongoing SHA-256 computation. This function is for
203 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100204 *
Rose Zadik602285e2018-01-26 11:00:39 +0000205 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100206 *
Rose Zadik602285e2018-01-26 11:00:39 +0000207 * \param ctx The SHA-256 context.
208 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100209 */
210MBEDTLS_DEPRECATED static inline void mbedtls_sha256_process(
211 mbedtls_sha256_context *ctx,
212 const unsigned char data[64] )
213{
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100214 mbedtls_internal_sha256_process( ctx, data );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100215}
216
217#undef MBEDTLS_DEPRECATED
218#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200219#ifdef __cplusplus
220}
221#endif
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#else /* MBEDTLS_SHA256_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200224#include "sha256_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#endif /* MBEDTLS_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200226
227#ifdef __cplusplus
228extern "C" {
229#endif
230
Paul Bakker5121ce52009-01-03 21:22:43 +0000231/**
Rose Zadik602285e2018-01-26 11:00:39 +0000232 * \brief This function calculates the SHA-224 or SHA-256
233 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 *
Rose Zadik602285e2018-01-26 11:00:39 +0000235 * The function allocates the context, performs the
236 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100237 *
Rose Zadik602285e2018-01-26 11:00:39 +0000238 * The SHA-256 result is calculated as
239 * output = SHA-256(input buffer).
240 *
241 * \param input The buffer holding the input data.
242 * \param ilen The length of the input data.
243 * \param output The SHA-224 or SHA-256 checksum result.
244 * \param is224 Determines which function to use.
245 * <ul><li>0: Use SHA-256.</li>
246 * <li>1: Use SHA-224.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100248int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100249 size_t ilen,
250 unsigned char output[32],
251 int is224 );
252
253#if !defined(MBEDTLS_DEPRECATED_REMOVED)
254#if defined(MBEDTLS_DEPRECATED_WARNING)
255#define MBEDTLS_DEPRECATED __attribute__((deprecated))
256#else
257#define MBEDTLS_DEPRECATED
258#endif
Rose Zadik602285e2018-01-26 11:00:39 +0000259
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100260/**
Rose Zadik602285e2018-01-26 11:00:39 +0000261 * \brief This function calculates the SHA-224 or SHA-256 checksum
262 * of a buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100263 *
Rose Zadik602285e2018-01-26 11:00:39 +0000264 * The function allocates the context, performs the
265 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100266 *
Rose Zadik602285e2018-01-26 11:00:39 +0000267 * The SHA-256 result is calculated as
268 * output = SHA-256(input buffer).
269 *
270 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
271 *
272 * \param input The buffer holding the data.
273 * \param ilen The length of the input data.
274 * \param output The SHA-224 or SHA-256 checksum result.
275 * \param is224 Determines which function to use.
276 * <ul><li>0: Use SHA-256.</li>
277 * <li>1: Use SHA-224.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000278 */
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100279MBEDTLS_DEPRECATED static inline void mbedtls_sha256(
280 const unsigned char *input,
281 size_t ilen,
282 unsigned char output[32],
283 int is224 )
284{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100285 mbedtls_sha256_ret( input, ilen, output, is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100286}
287
288#undef MBEDTLS_DEPRECATED
289#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
291/**
Rose Zadik602285e2018-01-26 11:00:39 +0000292 * \brief The SHA-224 and SHA-256 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 *
Rose Zadik602285e2018-01-26 11:00:39 +0000294 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297
Paul Bakker5121ce52009-01-03 21:22:43 +0000298#ifdef __cplusplus
299}
300#endif
301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* mbedtls_sha256.h */