blob: ffb16c277a0ec17d1f08be967c3f615c79eed7fa [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if !defined(MBEDTLS_SHA256_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020039// Regular implementation
40//
41
Paul Bakker407a0da2013-06-27 14:29:21 +020042#ifdef __cplusplus
43extern "C" {
44#endif
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
Paul Bakker5121ce52009-01-03 21:22:43 +000064/**
Rose Zadik602285e2018-01-26 11:00:39 +000065 * \brief This function initializes a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020066 *
Rose Zadik602285e2018-01-26 11:00:39 +000067 * \param ctx The SHA-256 context to initialize.
Paul Bakker5b4af392014-06-26 12:09:34 +020068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020070
71/**
Rose Zadik602285e2018-01-26 11:00:39 +000072 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020073 *
Rose Zadik602285e2018-01-26 11:00:39 +000074 * \param ctx The SHA-256 context to clear.
Paul Bakker5b4af392014-06-26 12:09:34 +020075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020077
78/**
Rose Zadik602285e2018-01-26 11:00:39 +000079 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020080 *
Rose Zadik602285e2018-01-26 11:00:39 +000081 * \param dst The destination context.
82 * \param src The context to clone.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020083 */
84void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
85 const mbedtls_sha256_context *src );
86
87/**
Rose Zadik602285e2018-01-26 11:00:39 +000088 * \brief This function starts a SHA-224 or SHA-256 checksum
89 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000090 *
Rose Zadik602285e2018-01-26 11:00:39 +000091 * \param ctx The context to initialize.
92 * \param is224 Determines which function to use.
93 * <ul><li>0: Use SHA-256.</li>
94 * <li>1: Use SHA-224.</li></ul>
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +010095 *
Rose Zadik602285e2018-01-26 11:00:39 +000096 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000097 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010098int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +000099
100/**
Rose Zadik602285e2018-01-26 11:00:39 +0000101 * \brief This function feeds an input buffer into an ongoing
102 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 *
104 * \param ctx SHA-256 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100105 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 * \param ilen length of the input data
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100107 *
Rose Zadik602285e2018-01-26 11:00:39 +0000108 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100110int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100111 const unsigned char *input,
112 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114/**
Rose Zadik602285e2018-01-26 11:00:39 +0000115 * \brief This function finishes the SHA-256 operation, and writes
116 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 *
Rose Zadik602285e2018-01-26 11:00:39 +0000118 * \param ctx The SHA-256 context.
119 * \param output The SHA-224 or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100120 *
Rose Zadik602285e2018-01-26 11:00:39 +0000121 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100123int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100124 unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100126/**
Rose Zadik602285e2018-01-26 11:00:39 +0000127 * \brief This function processes a single data block within
128 * the ongoing SHA-256 computation. This function is for
129 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100130 *
Rose Zadik602285e2018-01-26 11:00:39 +0000131 * \param ctx The SHA-256 context.
132 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100133 *
Rose Zadik602285e2018-01-26 11:00:39 +0000134 * \return \c 0 on success.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100135 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100136int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
137 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100138
139#if !defined(MBEDTLS_DEPRECATED_REMOVED)
140#if defined(MBEDTLS_DEPRECATED_WARNING)
141#define MBEDTLS_DEPRECATED __attribute__((deprecated))
142#else
143#define MBEDTLS_DEPRECATED
144#endif
145/**
Rose Zadik602285e2018-01-26 11:00:39 +0000146 * \brief This function starts a SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100147 *
Rose Zadik602285e2018-01-26 11:00:39 +0000148 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100149 *
Rose Zadik602285e2018-01-26 11:00:39 +0000150 * \param ctx The SHA-256 context to initialize.
151 * \param is224 Determines which function to use.
152 * <ul><li>0: Use SHA-256.</li>
153 * <li>1: Use SHA-224.</li></ul>
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100154 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000155MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
156 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100157
158/**
Rose Zadik602285e2018-01-26 11:00:39 +0000159 * \brief This function feeds an input buffer into an ongoing
160 * SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100161 *
Rose Zadik602285e2018-01-26 11:00:39 +0000162 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100163 *
Rose Zadik602285e2018-01-26 11:00:39 +0000164 * \param ctx The SHA-256 context to initialize.
165 * \param input The buffer holding the data.
166 * \param ilen The length of the input data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100167 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000168MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
169 const unsigned char *input,
170 size_t ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100171
172/**
Rose Zadik602285e2018-01-26 11:00:39 +0000173 * \brief This function finishes the SHA-256 operation, and writes
174 * the result to the output buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100175 *
Rose Zadik602285e2018-01-26 11:00:39 +0000176 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100177 *
Rose Zadik602285e2018-01-26 11:00:39 +0000178 * \param ctx The SHA-256 context.
179 * \param output The SHA-224or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100180 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000181MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
182 unsigned char output[32] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100183
184/**
Rose Zadik602285e2018-01-26 11:00:39 +0000185 * \brief This function processes a single data block within
186 * the ongoing SHA-256 computation. This function is for
187 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100188 *
Rose Zadik602285e2018-01-26 11:00:39 +0000189 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100190 *
Rose Zadik602285e2018-01-26 11:00:39 +0000191 * \param ctx The SHA-256 context.
192 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100193 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000194MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
195 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100196
197#undef MBEDTLS_DEPRECATED
198#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200199#ifdef __cplusplus
200}
201#endif
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#else /* MBEDTLS_SHA256_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200204#include "sha256_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#endif /* MBEDTLS_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200206
207#ifdef __cplusplus
208extern "C" {
209#endif
210
Paul Bakker5121ce52009-01-03 21:22:43 +0000211/**
Rose Zadik602285e2018-01-26 11:00:39 +0000212 * \brief This function calculates the SHA-224 or SHA-256
213 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 *
Rose Zadik602285e2018-01-26 11:00:39 +0000215 * The function allocates the context, performs the
216 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100217 *
Rose Zadik602285e2018-01-26 11:00:39 +0000218 * The SHA-256 result is calculated as
219 * output = SHA-256(input buffer).
220 *
221 * \param input The buffer holding the input data.
222 * \param ilen The length of the input data.
223 * \param output The SHA-224 or SHA-256 checksum result.
224 * \param is224 Determines which function to use.
225 * <ul><li>0: Use SHA-256.</li>
226 * <li>1: Use SHA-224.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100228int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100229 size_t ilen,
230 unsigned char output[32],
231 int is224 );
232
233#if !defined(MBEDTLS_DEPRECATED_REMOVED)
234#if defined(MBEDTLS_DEPRECATED_WARNING)
235#define MBEDTLS_DEPRECATED __attribute__((deprecated))
236#else
237#define MBEDTLS_DEPRECATED
238#endif
Rose Zadik602285e2018-01-26 11:00:39 +0000239
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100240/**
Rose Zadik602285e2018-01-26 11:00:39 +0000241 * \brief This function calculates the SHA-224 or SHA-256 checksum
242 * of a buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100243 *
Rose Zadik602285e2018-01-26 11:00:39 +0000244 * The function allocates the context, performs the
245 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100246 *
Rose Zadik602285e2018-01-26 11:00:39 +0000247 * The SHA-256 result is calculated as
248 * output = SHA-256(input buffer).
249 *
250 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
251 *
252 * \param input The buffer holding the data.
253 * \param ilen The length of the input data.
254 * \param output The SHA-224 or SHA-256 checksum result.
255 * \param is224 Determines which function to use.
256 * <ul><li>0: Use SHA-256.</li>
257 * <li>1: Use SHA-224.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000259MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
260 size_t ilen,
261 unsigned char output[32],
262 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100263
264#undef MBEDTLS_DEPRECATED
265#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000266
267/**
Rose Zadik602285e2018-01-26 11:00:39 +0000268 * \brief The SHA-224 and SHA-256 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 *
Rose Zadik602285e2018-01-26 11:00:39 +0000270 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
Paul Bakker5121ce52009-01-03 21:22:43 +0000274#ifdef __cplusplus
275}
276#endif
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#endif /* mbedtls_sha256.h */