blob: 47a31e83af6914bb5b59e7a492388aab20e7d21e [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 */
41
Paul Bakker407a0da2013-06-27 14:29:21 +020042#ifdef __cplusplus
43extern "C" {
44#endif
45
Ron Eldorb2aacec2017-05-18 16:53:08 +030046#if !defined(MBEDTLS_SHA256_ALT)
47// Regular implementation
48//
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 */
Dawid Drozd428cc522018-07-24 10:02:47 +020057typedef struct mbedtls_sha256_context
Paul Bakker5121ce52009-01-03 21:22:43 +000058{
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. */
Rose Zadikbde68b42018-03-27 12:59:13 +010062 int is224; /*!< Determines which function to use:
63 0: Use SHA-256, or 1: Use SHA-224. */
Paul Bakker5121ce52009-01-03 21:22:43 +000064}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Ron Eldorb2aacec2017-05-18 16:53:08 +030067#else /* MBEDTLS_SHA256_ALT */
68#include "sha256_alt.h"
69#endif /* MBEDTLS_SHA256_ALT */
70
Paul Bakker5121ce52009-01-03 21:22:43 +000071/**
Rose Zadik602285e2018-01-26 11:00:39 +000072 * \brief This function initializes 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 initialize.
Paul Bakker5b4af392014-06-26 12:09:34 +020075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020077
78/**
Rose Zadik602285e2018-01-26 11:00:39 +000079 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020080 *
Rose Zadik602285e2018-01-26 11:00:39 +000081 * \param ctx The SHA-256 context to clear.
Paul Bakker5b4af392014-06-26 12:09:34 +020082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020084
85/**
Rose Zadik602285e2018-01-26 11:00:39 +000086 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020087 *
Rose Zadik602285e2018-01-26 11:00:39 +000088 * \param dst The destination context.
89 * \param src The context to clone.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020090 */
91void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
92 const mbedtls_sha256_context *src );
93
94/**
Rose Zadik602285e2018-01-26 11:00:39 +000095 * \brief This function starts a SHA-224 or SHA-256 checksum
96 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000097 *
Rose Zadik602285e2018-01-26 11:00:39 +000098 * \param ctx The context to initialize.
Rose Zadikbde68b42018-03-27 12:59:13 +010099 * \param is224 Determines which function to use:
100 * 0: Use SHA-256, or 1: Use SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100101 *
Rose Zadik602285e2018-01-26 11:00:39 +0000102 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100104int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
106/**
Rose Zadik602285e2018-01-26 11:00:39 +0000107 * \brief This function feeds an input buffer into an ongoing
108 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 *
Rose Zadik6ee22a72018-04-17 10:38:39 +0100110 * \param ctx The SHA-256 context.
Rose Zadikbde68b42018-03-27 12:59:13 +0100111 * \param input The buffer holding the data.
112 * \param ilen The length of the input data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100113 *
Rose Zadik602285e2018-01-26 11:00:39 +0000114 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100116int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100117 const unsigned char *input,
118 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
120/**
Rose Zadik602285e2018-01-26 11:00:39 +0000121 * \brief This function finishes the SHA-256 operation, and writes
122 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 *
Rose Zadik602285e2018-01-26 11:00:39 +0000124 * \param ctx The SHA-256 context.
125 * \param output The SHA-224 or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100126 *
Rose Zadik602285e2018-01-26 11:00:39 +0000127 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100129int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100130 unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100132/**
Rose Zadik602285e2018-01-26 11:00:39 +0000133 * \brief This function processes a single data block within
134 * the ongoing SHA-256 computation. This function is for
135 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100136 *
Rose Zadik602285e2018-01-26 11:00:39 +0000137 * \param ctx The SHA-256 context.
138 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100139 *
Rose Zadik602285e2018-01-26 11:00:39 +0000140 * \return \c 0 on success.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100141 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100142int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
143 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100144
145#if !defined(MBEDTLS_DEPRECATED_REMOVED)
146#if defined(MBEDTLS_DEPRECATED_WARNING)
147#define MBEDTLS_DEPRECATED __attribute__((deprecated))
148#else
149#define MBEDTLS_DEPRECATED
150#endif
151/**
Rose Zadikbde68b42018-03-27 12:59:13 +0100152 * \brief This function starts a SHA-224 or SHA-256 checksum
153 * calculation.
154 *
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100155 *
Rose Zadik602285e2018-01-26 11:00:39 +0000156 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100157 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100158 * \param ctx The context to initialize.
159 * \param is224 Determines which function to use:
160 * 0: Use SHA-256, or 1: Use SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100161 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000162MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
163 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100164
165/**
Rose Zadik602285e2018-01-26 11:00:39 +0000166 * \brief This function feeds an input buffer into an ongoing
167 * SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100168 *
Rose Zadik602285e2018-01-26 11:00:39 +0000169 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100170 *
Rose Zadik602285e2018-01-26 11:00:39 +0000171 * \param ctx The SHA-256 context to initialize.
172 * \param input The buffer holding the data.
173 * \param ilen The length of the input data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100174 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000175MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
176 const unsigned char *input,
177 size_t ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100178
179/**
Rose Zadik602285e2018-01-26 11:00:39 +0000180 * \brief This function finishes the SHA-256 operation, and writes
181 * the result to the output buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100182 *
Rose Zadik602285e2018-01-26 11:00:39 +0000183 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100184 *
Rose Zadik602285e2018-01-26 11:00:39 +0000185 * \param ctx The SHA-256 context.
Rose Zadikbde68b42018-03-27 12:59:13 +0100186 * \param output The SHA-224 or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100187 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000188MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
189 unsigned char output[32] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100190
191/**
Rose Zadik602285e2018-01-26 11:00:39 +0000192 * \brief This function processes a single data block within
193 * the ongoing SHA-256 computation. This function is for
194 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100195 *
Rose Zadik602285e2018-01-26 11:00:39 +0000196 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100197 *
Rose Zadik602285e2018-01-26 11:00:39 +0000198 * \param ctx The SHA-256 context.
199 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100200 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000201MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
202 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100203
204#undef MBEDTLS_DEPRECATED
205#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200206
Paul Bakker5121ce52009-01-03 21:22:43 +0000207/**
Rose Zadik602285e2018-01-26 11:00:39 +0000208 * \brief This function calculates the SHA-224 or SHA-256
209 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 *
Rose Zadik602285e2018-01-26 11:00:39 +0000211 * The function allocates the context, performs the
212 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100213 *
Rose Zadik602285e2018-01-26 11:00:39 +0000214 * The SHA-256 result is calculated as
215 * output = SHA-256(input buffer).
216 *
217 * \param input The buffer holding the input data.
218 * \param ilen The length of the input data.
219 * \param output The SHA-224 or SHA-256 checksum result.
Rose Zadikbde68b42018-03-27 12:59:13 +0100220 * \param is224 Determines which function to use:
221 * 0: Use SHA-256, or 1: Use SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100223int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100224 size_t ilen,
225 unsigned char output[32],
226 int is224 );
227
228#if !defined(MBEDTLS_DEPRECATED_REMOVED)
229#if defined(MBEDTLS_DEPRECATED_WARNING)
230#define MBEDTLS_DEPRECATED __attribute__((deprecated))
231#else
232#define MBEDTLS_DEPRECATED
233#endif
Rose Zadik602285e2018-01-26 11:00:39 +0000234
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100235/**
Rose Zadik602285e2018-01-26 11:00:39 +0000236 * \brief This function calculates the SHA-224 or SHA-256 checksum
237 * of a buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100238 *
Rose Zadik602285e2018-01-26 11:00:39 +0000239 * The function allocates the context, performs the
240 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100241 *
Rose Zadik602285e2018-01-26 11:00:39 +0000242 * The SHA-256 result is calculated as
243 * output = SHA-256(input buffer).
244 *
245 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
246 *
247 * \param input The buffer holding the data.
248 * \param ilen The length of the input data.
249 * \param output The SHA-224 or SHA-256 checksum result.
Rose Zadikbde68b42018-03-27 12:59:13 +0100250 * \param is224 Determines which function to use:
251 * 0: Use SHA-256, or 1: Use SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000253MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
254 size_t ilen,
255 unsigned char output[32],
256 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100257
258#undef MBEDTLS_DEPRECATED
259#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
261/**
Rose Zadik602285e2018-01-26 11:00:39 +0000262 * \brief The SHA-224 and SHA-256 checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100264 * \return \c 0 on success.
265 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000268
Paul Bakker5121ce52009-01-03 21:22:43 +0000269#ifdef __cplusplus
270}
271#endif
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#endif /* mbedtls_sha256.h */