blob: e7cb439a280f3bf44262b3e381fb3d01d78d660d [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9 *
10 * This file is provided under the Apache License 2.0, or the
11 * GNU General Public License v2.0 or later.
12 *
13 * **********
14 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020015 *
16 * Licensed under the Apache License, Version 2.0 (the "License"); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
19 *
20 * http://www.apache.org/licenses/LICENSE-2.0
21 *
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000027 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020028 * **********
29 *
30 * **********
31 * GNU General Public License v2.0 or later:
32 *
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2 of the License, or
36 * (at your option) any later version.
37 *
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
42 *
43 * You should have received a copy of the GNU General Public License along
44 * with this program; if not, write to the Free Software Foundation, Inc.,
45 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46 *
47 * **********
48 *
Rose Zadik602285e2018-01-26 11:00:39 +000049 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_SHA256_H
52#define MBEDTLS_SHA256_H
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020055#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakker90995b52013-06-24 19:20:35 +020059
Rich Evans00ab4702015-02-06 13:43:58 +000060#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020061#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000062
Gilles Peskinea381fe82018-01-23 18:16:11 +010063#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if !defined(MBEDTLS_SHA256_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020066// Regular implementation
67//
68
Paul Bakker407a0da2013-06-27 14:29:21 +020069#ifdef __cplusplus
70extern "C" {
71#endif
72
Paul Bakker5121ce52009-01-03 21:22:43 +000073/**
Rose Zadik602285e2018-01-26 11:00:39 +000074 * \brief The SHA-256 context structure.
75 *
76 * The structure is used both for SHA-256 and for SHA-224
77 * checksum calculations. The choice between these two is
78 * made in the call to mbedtls_sha256_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000079 */
80typedef struct
81{
Rose Zadik602285e2018-01-26 11:00:39 +000082 uint32_t total[2]; /*!< The number of Bytes processed. */
83 uint32_t state[8]; /*!< The intermediate digest state. */
84 unsigned char buffer[64]; /*!< The data block being processed. */
85 int is224; /*!< Determines which function to use.
86 <ul><li>0: Use SHA-256.</li>
87 <li>1: Use SHA-224.</li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +000088}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Paul Bakker5121ce52009-01-03 21:22:43 +000091/**
Rose Zadik602285e2018-01-26 11:00:39 +000092 * \brief This function initializes a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020093 *
Rose Zadik602285e2018-01-26 11:00:39 +000094 * \param ctx The SHA-256 context to initialize.
Paul Bakker5b4af392014-06-26 12:09:34 +020095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020097
98/**
Rose Zadik602285e2018-01-26 11:00:39 +000099 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +0200100 *
Rose Zadik602285e2018-01-26 11:00:39 +0000101 * \param ctx The SHA-256 context to clear.
Paul Bakker5b4af392014-06-26 12:09:34 +0200102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +0200104
105/**
Rose Zadik602285e2018-01-26 11:00:39 +0000106 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200107 *
Rose Zadik602285e2018-01-26 11:00:39 +0000108 * \param dst The destination context.
109 * \param src The context to clone.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +0200110 */
111void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
112 const mbedtls_sha256_context *src );
113
114/**
Rose Zadik602285e2018-01-26 11:00:39 +0000115 * \brief This function starts a SHA-224 or SHA-256 checksum
116 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 *
Rose Zadik602285e2018-01-26 11:00:39 +0000118 * \param ctx The context to initialize.
119 * \param is224 Determines which function to use.
120 * <ul><li>0: Use SHA-256.</li>
121 * <li>1: Use SHA-224.</li></ul>
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100122 *
Rose Zadik602285e2018-01-26 11:00:39 +0000123 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100125int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
127/**
Rose Zadik602285e2018-01-26 11:00:39 +0000128 * \brief This function feeds an input buffer into an ongoing
129 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 *
131 * \param ctx SHA-256 context
Andres Amaya Garciaa21247e2017-07-20 14:01:08 +0100132 * \param input buffer holding the data
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 * \param ilen length of the input data
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100134 *
Rose Zadik602285e2018-01-26 11:00:39 +0000135 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100137int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100138 const unsigned char *input,
139 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140
141/**
Rose Zadik602285e2018-01-26 11:00:39 +0000142 * \brief This function finishes the SHA-256 operation, and writes
143 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 *
Rose Zadik602285e2018-01-26 11:00:39 +0000145 * \param ctx The SHA-256 context.
146 * \param output The SHA-224 or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100147 *
Rose Zadik602285e2018-01-26 11:00:39 +0000148 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100150int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100151 unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100153/**
Rose Zadik602285e2018-01-26 11:00:39 +0000154 * \brief This function processes a single data block within
155 * the ongoing SHA-256 computation. This function is for
156 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100157 *
Rose Zadik602285e2018-01-26 11:00:39 +0000158 * \param ctx The SHA-256 context.
159 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100160 *
Rose Zadik602285e2018-01-26 11:00:39 +0000161 * \return \c 0 on success.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100162 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100163int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
164 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100165
166#if !defined(MBEDTLS_DEPRECATED_REMOVED)
167#if defined(MBEDTLS_DEPRECATED_WARNING)
168#define MBEDTLS_DEPRECATED __attribute__((deprecated))
169#else
170#define MBEDTLS_DEPRECATED
171#endif
172/**
Rose Zadik602285e2018-01-26 11:00:39 +0000173 * \brief This function starts a SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100174 *
Rose Zadik602285e2018-01-26 11:00:39 +0000175 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100176 *
Rose Zadik602285e2018-01-26 11:00:39 +0000177 * \param ctx The SHA-256 context to initialize.
178 * \param is224 Determines which function to use.
179 * <ul><li>0: Use SHA-256.</li>
180 * <li>1: Use SHA-224.</li></ul>
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100181 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000182MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
183 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100184
185/**
Rose Zadik602285e2018-01-26 11:00:39 +0000186 * \brief This function feeds an input buffer into an ongoing
187 * SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100188 *
Rose Zadik602285e2018-01-26 11:00:39 +0000189 * \deprecated Superseded by mbedtls_sha256_update_ret() 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 to initialize.
192 * \param input The buffer holding the data.
193 * \param ilen The length of the input data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100194 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000195MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
196 const unsigned char *input,
197 size_t ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100198
199/**
Rose Zadik602285e2018-01-26 11:00:39 +0000200 * \brief This function finishes the SHA-256 operation, and writes
201 * the result to the output buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100202 *
Rose Zadik602285e2018-01-26 11:00:39 +0000203 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100204 *
Rose Zadik602285e2018-01-26 11:00:39 +0000205 * \param ctx The SHA-256 context.
206 * \param output The SHA-224or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100207 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000208MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
209 unsigned char output[32] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100210
211/**
Rose Zadik602285e2018-01-26 11:00:39 +0000212 * \brief This function processes a single data block within
213 * the ongoing SHA-256 computation. This function is for
214 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100215 *
Rose Zadik602285e2018-01-26 11:00:39 +0000216 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100217 *
Rose Zadik602285e2018-01-26 11:00:39 +0000218 * \param ctx The SHA-256 context.
219 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100220 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000221MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
222 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100223
224#undef MBEDTLS_DEPRECATED
225#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200226#ifdef __cplusplus
227}
228#endif
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#else /* MBEDTLS_SHA256_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200231#include "sha256_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#endif /* MBEDTLS_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200233
234#ifdef __cplusplus
235extern "C" {
236#endif
237
Paul Bakker5121ce52009-01-03 21:22:43 +0000238/**
Rose Zadik602285e2018-01-26 11:00:39 +0000239 * \brief This function calculates the SHA-224 or SHA-256
240 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 *
Rose Zadik602285e2018-01-26 11:00:39 +0000242 * The function allocates the context, performs the
243 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100244 *
Rose Zadik602285e2018-01-26 11:00:39 +0000245 * The SHA-256 result is calculated as
246 * output = SHA-256(input buffer).
247 *
248 * \param input The buffer holding the input data.
249 * \param ilen The length of the input data.
250 * \param output The SHA-224 or SHA-256 checksum result.
251 * \param is224 Determines which function to use.
252 * <ul><li>0: Use SHA-256.</li>
253 * <li>1: Use SHA-224.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100255int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100256 size_t ilen,
257 unsigned char output[32],
258 int is224 );
259
260#if !defined(MBEDTLS_DEPRECATED_REMOVED)
261#if defined(MBEDTLS_DEPRECATED_WARNING)
262#define MBEDTLS_DEPRECATED __attribute__((deprecated))
263#else
264#define MBEDTLS_DEPRECATED
265#endif
Rose Zadik602285e2018-01-26 11:00:39 +0000266
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100267/**
Rose Zadik602285e2018-01-26 11:00:39 +0000268 * \brief This function calculates the SHA-224 or SHA-256 checksum
269 * of a buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100270 *
Rose Zadik602285e2018-01-26 11:00:39 +0000271 * The function allocates the context, performs the
272 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100273 *
Rose Zadik602285e2018-01-26 11:00:39 +0000274 * The SHA-256 result is calculated as
275 * output = SHA-256(input buffer).
276 *
277 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
278 *
279 * \param input The buffer holding the data.
280 * \param ilen The length of the input data.
281 * \param output The SHA-224 or SHA-256 checksum result.
282 * \param is224 Determines which function to use.
283 * <ul><li>0: Use SHA-256.</li>
284 * <li>1: Use SHA-224.</li></ul>
Paul Bakker5121ce52009-01-03 21:22:43 +0000285 */
Jaeden Ameroa53ff8d2018-02-19 15:28:08 +0000286MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
287 size_t ilen,
288 unsigned char output[32],
289 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100290
291#undef MBEDTLS_DEPRECATED
292#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
294/**
Rose Zadik602285e2018-01-26 11:00:39 +0000295 * \brief The SHA-224 and SHA-256 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000296 *
Rose Zadik602285e2018-01-26 11:00:39 +0000297 * \return \c 0 on success, or \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000300
Paul Bakker5121ce52009-01-03 21:22:43 +0000301#ifdef __cplusplus
302}
303#endif
304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#endif /* mbedtls_sha256.h */