blob: 7affb1be84fa16c39fc0823bbe606391a4bea7d9 [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
Gilles Peskinea381fe82018-01-23 18:16:11 +010039#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if !defined(MBEDTLS_SHA256_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020042// Regular implementation
43//
44
Paul Bakker407a0da2013-06-27 14:29:21 +020045#ifdef __cplusplus
46extern "C" {
47#endif
48
Paul Bakker5121ce52009-01-03 21:22:43 +000049/**
Rose Zadik602285e2018-01-26 11:00:39 +000050 * \brief The SHA-256 context structure.
51 *
52 * The structure is used both for SHA-256 and for SHA-224
53 * checksum calculations. The choice between these two is
54 * made in the call to mbedtls_sha256_starts_ret().
Paul Bakker5121ce52009-01-03 21:22:43 +000055 */
56typedef struct
57{
Rose Zadik602285e2018-01-26 11:00:39 +000058 uint32_t total[2]; /*!< The number of Bytes processed. */
59 uint32_t state[8]; /*!< The intermediate digest state. */
60 unsigned char buffer[64]; /*!< The data block being processed. */
Rose Zadikbde68b42018-03-27 12:59:13 +010061 int is224; /*!< Determines which function to use:
62 0: Use SHA-256, or 1: Use SHA-224. */
Paul Bakker5121ce52009-01-03 21:22:43 +000063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064mbedtls_sha256_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Paul Bakker5121ce52009-01-03 21:22:43 +000066/**
Rose Zadik602285e2018-01-26 11:00:39 +000067 * \brief This function initializes a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020068 *
Rose Zadik602285e2018-01-26 11:00:39 +000069 * \param ctx The SHA-256 context to initialize.
Paul Bakker5b4af392014-06-26 12:09:34 +020070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071void mbedtls_sha256_init( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020072
73/**
Rose Zadik602285e2018-01-26 11:00:39 +000074 * \brief This function clears a SHA-256 context.
Paul Bakker5b4af392014-06-26 12:09:34 +020075 *
Rose Zadik602285e2018-01-26 11:00:39 +000076 * \param ctx The SHA-256 context to clear.
Paul Bakker5b4af392014-06-26 12:09:34 +020077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078void mbedtls_sha256_free( mbedtls_sha256_context *ctx );
Paul Bakker5b4af392014-06-26 12:09:34 +020079
80/**
Rose Zadik602285e2018-01-26 11:00:39 +000081 * \brief This function clones the state of a SHA-256 context.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020082 *
Rose Zadik602285e2018-01-26 11:00:39 +000083 * \param dst The destination context.
84 * \param src The context to clone.
Manuel Pégourié-Gonnard16d412f2015-07-06 15:26:26 +020085 */
86void mbedtls_sha256_clone( mbedtls_sha256_context *dst,
87 const mbedtls_sha256_context *src );
88
89/**
Rose Zadik602285e2018-01-26 11:00:39 +000090 * \brief This function starts a SHA-224 or SHA-256 checksum
91 * calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +000092 *
Rose Zadik602285e2018-01-26 11:00:39 +000093 * \param ctx The context to initialize.
Rose Zadikbde68b42018-03-27 12:59:13 +010094 * \param is224 Determines which function to use:
95 * 0: Use SHA-256, or 1: Use SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +010096 *
Rose Zadik602285e2018-01-26 11:00:39 +000097 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000098 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010099int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101/**
Rose Zadik602285e2018-01-26 11:00:39 +0000102 * \brief This function feeds an input buffer into an ongoing
103 * SHA-256 checksum calculation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100105 * \param ctx The SHA-256 context to initialize.
106 * \param input The buffer holding the data.
107 * \param ilen The length of the input data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100108 *
Rose Zadik602285e2018-01-26 11:00:39 +0000109 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100111int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100112 const unsigned char *input,
113 size_t ilen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
115/**
Rose Zadik602285e2018-01-26 11:00:39 +0000116 * \brief This function finishes the SHA-256 operation, and writes
117 * the result to the output buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 *
Rose Zadik602285e2018-01-26 11:00:39 +0000119 * \param ctx The SHA-256 context.
120 * \param output The SHA-224 or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100121 *
Rose Zadik602285e2018-01-26 11:00:39 +0000122 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100124int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100125 unsigned char output[32] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100127/**
Rose Zadik602285e2018-01-26 11:00:39 +0000128 * \brief This function processes a single data block within
129 * the ongoing SHA-256 computation. This function is for
130 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100131 *
Rose Zadik602285e2018-01-26 11:00:39 +0000132 * \param ctx The SHA-256 context.
133 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100134 *
Rose Zadik602285e2018-01-26 11:00:39 +0000135 * \return \c 0 on success.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100136 */
Andres Amaya Garciacccfe082017-06-28 10:36:39 +0100137int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx,
138 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100139
140#if !defined(MBEDTLS_DEPRECATED_REMOVED)
141#if defined(MBEDTLS_DEPRECATED_WARNING)
142#define MBEDTLS_DEPRECATED __attribute__((deprecated))
143#else
144#define MBEDTLS_DEPRECATED
145#endif
146/**
Rose Zadikbde68b42018-03-27 12:59:13 +0100147 * \brief This function starts a SHA-224 or SHA-256 checksum
148 * calculation.
149 *
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100150 *
Rose Zadik602285e2018-01-26 11:00:39 +0000151 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100152 *
Rose Zadikbde68b42018-03-27 12:59:13 +0100153 * \param ctx The context to initialize.
154 * \param is224 Determines which function to use:
155 * 0: Use SHA-256, or 1: Use SHA-224.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100156 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000157MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
158 int is224 );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100159
160/**
Rose Zadik602285e2018-01-26 11:00:39 +0000161 * \brief This function feeds an input buffer into an ongoing
162 * SHA-256 checksum calculation.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100163 *
Rose Zadik602285e2018-01-26 11:00:39 +0000164 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100165 *
Rose Zadik602285e2018-01-26 11:00:39 +0000166 * \param ctx The SHA-256 context to initialize.
167 * \param input The buffer holding the data.
168 * \param ilen The length of the input data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100169 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000170MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
171 const unsigned char *input,
172 size_t ilen );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100173
174/**
Rose Zadik602285e2018-01-26 11:00:39 +0000175 * \brief This function finishes the SHA-256 operation, and writes
176 * the result to the output buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100177 *
Rose Zadik602285e2018-01-26 11:00:39 +0000178 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100179 *
Rose Zadik602285e2018-01-26 11:00:39 +0000180 * \param ctx The SHA-256 context.
Rose Zadikbde68b42018-03-27 12:59:13 +0100181 * \param output The SHA-224 or SHA-256 checksum result.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100182 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000183MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
184 unsigned char output[32] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100185
186/**
Rose Zadik602285e2018-01-26 11:00:39 +0000187 * \brief This function processes a single data block within
188 * the ongoing SHA-256 computation. This function is for
189 * internal use only.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100190 *
Rose Zadik602285e2018-01-26 11:00:39 +0000191 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100192 *
Rose Zadik602285e2018-01-26 11:00:39 +0000193 * \param ctx The SHA-256 context.
194 * \param data The buffer holding one block of data.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100195 */
Jaeden Amero041039f2018-02-19 15:28:08 +0000196MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
197 const unsigned char data[64] );
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100198
199#undef MBEDTLS_DEPRECATED
200#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker90995b52013-06-24 19:20:35 +0200201#ifdef __cplusplus
202}
203#endif
204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#else /* MBEDTLS_SHA256_ALT */
Paul Bakkerd2681d82013-06-30 14:49:12 +0200206#include "sha256_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#endif /* MBEDTLS_SHA256_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200208
209#ifdef __cplusplus
210extern "C" {
211#endif
212
Paul Bakker5121ce52009-01-03 21:22:43 +0000213/**
Rose Zadik602285e2018-01-26 11:00:39 +0000214 * \brief This function calculates the SHA-224 or SHA-256
215 * checksum of a buffer.
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 *
Rose Zadik602285e2018-01-26 11:00:39 +0000217 * The function allocates the context, performs the
218 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100219 *
Rose Zadik602285e2018-01-26 11:00:39 +0000220 * The SHA-256 result is calculated as
221 * output = SHA-256(input buffer).
222 *
223 * \param input The buffer holding the input data.
224 * \param ilen The length of the input data.
225 * \param output The SHA-224 or SHA-256 checksum result.
Rose Zadikbde68b42018-03-27 12:59:13 +0100226 * \param is224 Determines which function to use:
227 * 0: Use SHA-256, or 1: Use SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100229int mbedtls_sha256_ret( const unsigned char *input,
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100230 size_t ilen,
231 unsigned char output[32],
232 int is224 );
233
234#if !defined(MBEDTLS_DEPRECATED_REMOVED)
235#if defined(MBEDTLS_DEPRECATED_WARNING)
236#define MBEDTLS_DEPRECATED __attribute__((deprecated))
237#else
238#define MBEDTLS_DEPRECATED
239#endif
Rose Zadik602285e2018-01-26 11:00:39 +0000240
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100241/**
Rose Zadik602285e2018-01-26 11:00:39 +0000242 * \brief This function calculates the SHA-224 or SHA-256 checksum
243 * of a buffer.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100244 *
Rose Zadik602285e2018-01-26 11:00:39 +0000245 * The function allocates the context, performs the
246 * calculation, and frees the context.
Andres Amaya Garcia72a7f532017-05-02 11:38:47 +0100247 *
Rose Zadik602285e2018-01-26 11:00:39 +0000248 * The SHA-256 result is calculated as
249 * output = SHA-256(input buffer).
250 *
251 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
252 *
253 * \param input The buffer holding the data.
254 * \param ilen The length of the input data.
255 * \param output The SHA-224 or SHA-256 checksum result.
Rose Zadikbde68b42018-03-27 12:59:13 +0100256 * \param is224 Determines which function to use:
257 * 0: Use SHA-256, or 1: Use SHA-224.
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 */
Jaeden Amero041039f2018-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 Zadikbde68b42018-03-27 12:59:13 +0100270 * \return \c 0 on success.
271 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273int mbedtls_sha256_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
Paul Bakker5121ce52009-01-03 21:22:43 +0000275#ifdef __cplusplus
276}
277#endif
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#endif /* mbedtls_sha256.h */