blob: bd323dd5b945f327ce96f14f76e5b5ea78b413fe [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
Ron Eldor9924bdc2018-10-04 10:59:13 +030039/* MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea381fe82018-01-23 18:16:11 +010040#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
Andres Amaya Garcia79e593f2018-12-09 20:41:20 +000041#define MBEDTLS_ERR_SHA256_BAD_INPUT_DATA -0x0074 /**< Invalid input data. */
Gilles Peskinea381fe82018-01-23 18:16:11 +010042
Paul Bakker407a0da2013-06-27 14:29:21 +020043#ifdef __cplusplus
44extern "C" {
45#endif
46
Ron Eldorb2aacec2017-05-18 16:53:08 +030047#if !defined(MBEDTLS_SHA256_ALT)
48// Regular implementation
49//
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/**
Rose Zadik602285e2018-01-26 11:00:39 +000052 * \brief The SHA-256 context structure.
53 *
54 * The structure is used both for SHA-256 and for SHA-224
55 * checksum calculations. The choice between these two is
56 * made in the call to mbedtls_sha256_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000057 */
Dawid Drozd428cc522018-07-24 10:02:47 +020058typedef struct mbedtls_sha256_context
Paul Bakker5121ce52009-01-03 21:22:43 +000059{
Rose Zadik602285e2018-01-26 11:00:39 +000060 uint32_t total[2]; /*!< The number of Bytes processed. */
61 uint32_t state[8]; /*!< The intermediate digest state. */
62 unsigned char buffer[64]; /*!< The data block being processed. */
Rose Zadikbde68b42018-03-27 12:59:13 +010063 int is224; /*!< Determines which function to use:
64 0: Use SHA-256, or 1: Use SHA-224. */
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
Ron Eldorb2aacec2017-05-18 16:53:08 +030068#else /* MBEDTLS_SHA256_ALT */
69#include "sha256_alt.h"
70#endif /* MBEDTLS_SHA256_ALT */
71
Paul Bakker5121ce52009-01-03 21:22:43 +000072/**
Rose Zadik602285e2018-01-26 11:00:39 +000073 * \brief This function initializes a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020074 *
Rose Zadik602285e2018-01-26 11:00:39 +000075 * \param ctx The SHA-256 context to initialize.
Paul Bakker5b4af392014-06-26 12:09:34 +020076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020078
79/**
Rose Zadik602285e2018-01-26 11:00:39 +000080 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020081 *
Rose Zadik602285e2018-01-26 11:00:39 +000082 * \param ctx The SHA-256 context to clear.
Paul Bakker5b4af392014-06-26 12:09:34 +020083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020085
86/**
Rose Zadik602285e2018-01-26 11:00:39 +000087 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020088 *
Rose Zadik602285e2018-01-26 11:00:39 +000089 * \param dst The destination context.
90 * \param src The context to clone.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020091 */
92void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
93 const mbedtls_sha256_context *src );
94
95/**
Rose Zadik602285e2018-01-26 11:00:39 +000096 * \brief This function starts a SHA-224 or SHA-256 checksum
97 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000098 *
Rose Zadik602285e2018-01-26 11:00:39 +000099 * \param ctx The context to initialize.
Rose Zadikbde68b42018-03-27 12:59:13 +0100100 * \param is224 Determines which function to use:
101 * 0: Use SHA-256, or 1: Use SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100102 *
Rose Zadik602285e2018-01-26 11:00:39 +0000103 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100105int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107/**
Rose Zadik602285e2018-01-26 11:00:39 +0000108 * \brief This function feeds an input buffer into an ongoing
109 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 *
Rose Zadik6ee22a72018-04-17 10:38:39 +0100111 * \param ctx The SHA-256 context.
Rose Zadikbde68b42018-03-27 12:59:13 +0100112 * \param input The buffer holding the data.
113 * \param ilen The length of the input data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100114 *
Rose Zadik602285e2018-01-26 11:00:39 +0000115 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100117int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100118 const unsigned char *input,
119 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121/**
Rose Zadik602285e2018-01-26 11:00:39 +0000122 * \brief This function finishes the SHA-256 operation, and writes
123 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 *
Rose Zadik602285e2018-01-26 11:00:39 +0000125 * \param ctx The SHA-256 context.
126 * \param output The SHA-224 or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100127 *
Rose Zadik602285e2018-01-26 11:00:39 +0000128 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100130int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100131 unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100133/**
Rose Zadik602285e2018-01-26 11:00:39 +0000134 * \brief This function processes a single data block within
135 * the ongoing SHA-256 computation. This function is for
136 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100137 *
Rose Zadik602285e2018-01-26 11:00:39 +0000138 * \param ctx The SHA-256 context.
139 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100140 *
Rose Zadik602285e2018-01-26 11:00:39 +0000141 * \return \c 0 on success.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100142 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100143int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
144 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100145
146#if !defined(MBEDTLS_DEPRECATED_REMOVED)
147#if defined(MBEDTLS_DEPRECATED_WARNING)
148#define MBEDTLS_DEPRECATED __attribute__((deprecated))
149#else
150#define MBEDTLS_DEPRECATED
151#endif
152/**
Rose Zadikbde68b42018-03-27 12:59:13 +0100153 * \brief This function starts a SHA-224 or SHA-256 checksum
154 * calculation.
155 *
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100156 *
Rose Zadik602285e2018-01-26 11:00:39 +0000157 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100158 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100159 * \param ctx The context to initialize.
160 * \param is224 Determines which function to use:
161 * 0: Use SHA-256, or 1: Use SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100162 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000163MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
164 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100165
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 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000176MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
177 const unsigned char *input,
178 size_t ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100179
180/**
Rose Zadik602285e2018-01-26 11:00:39 +0000181 * \brief This function finishes the SHA-256 operation, and writes
182 * the result to the output buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100183 *
Rose Zadik602285e2018-01-26 11:00:39 +0000184 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100185 *
Rose Zadik602285e2018-01-26 11:00:39 +0000186 * \param ctx The SHA-256 context.
Rose Zadikbde68b42018-03-27 12:59:13 +0100187 * \param output The SHA-224 or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100188 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000189MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
190 unsigned char output[32] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100191
192/**
Rose Zadik602285e2018-01-26 11:00:39 +0000193 * \brief This function processes a single data block within
194 * the ongoing SHA-256 computation. This function is for
195 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100196 *
Rose Zadik602285e2018-01-26 11:00:39 +0000197 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100198 *
Rose Zadik602285e2018-01-26 11:00:39 +0000199 * \param ctx The SHA-256 context.
200 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100201 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000202MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
203 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100204
205#undef MBEDTLS_DEPRECATED
206#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200207
Paul Bakker5121ce52009-01-03 21:22:43 +0000208/**
Rose Zadik602285e2018-01-26 11:00:39 +0000209 * \brief This function calculates the SHA-224 or SHA-256
210 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 *
Rose Zadik602285e2018-01-26 11:00:39 +0000212 * The function allocates the context, performs the
213 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100214 *
Rose Zadik602285e2018-01-26 11:00:39 +0000215 * The SHA-256 result is calculated as
216 * output = SHA-256(input buffer).
217 *
218 * \param input The buffer holding the input data.
219 * \param ilen The length of the input data.
220 * \param output The SHA-224 or SHA-256 checksum result.
Rose Zadikbde68b42018-03-27 12:59:13 +0100221 * \param is224 Determines which function to use:
222 * 0: Use SHA-256, or 1: Use SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100224int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100225 size_t ilen,
226 unsigned char output[32],
227 int is224 );
228
229#if !defined(MBEDTLS_DEPRECATED_REMOVED)
230#if defined(MBEDTLS_DEPRECATED_WARNING)
231#define MBEDTLS_DEPRECATED __attribute__((deprecated))
232#else
233#define MBEDTLS_DEPRECATED
234#endif
Rose Zadik602285e2018-01-26 11:00:39 +0000235
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100236/**
Rose Zadik602285e2018-01-26 11:00:39 +0000237 * \brief This function calculates the SHA-224 or SHA-256 checksum
238 * of a buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100239 *
Rose Zadik602285e2018-01-26 11:00:39 +0000240 * The function allocates the context, performs the
241 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100242 *
Rose Zadik602285e2018-01-26 11:00:39 +0000243 * The SHA-256 result is calculated as
244 * output = SHA-256(input buffer).
245 *
246 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
247 *
248 * \param input The buffer holding the data.
249 * \param ilen The length of the input data.
250 * \param output The SHA-224 or SHA-256 checksum result.
Rose Zadikbde68b42018-03-27 12:59:13 +0100251 * \param is224 Determines which function to use:
252 * 0: Use SHA-256, or 1: Use SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000254MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
255 size_t ilen,
256 unsigned char output[32],
257 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100258
259#undef MBEDTLS_DEPRECATED
260#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
262/**
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
Paul Bakker5121ce52009-01-03 21:22:43 +0000270#ifdef __cplusplus
271}
272#endif
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#endif /* mbedtls_sha256.h */