4 * \brief This file contains SHA-224 and SHA-256 definitions and functions.
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>.
10 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
11 * SPDX-License-Identifier: GPL-2.0
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * This file is part of Mbed TLS (https://tls.mbed.org)
29 #ifndef MBEDTLS_SHA256_H
30 #define MBEDTLS_SHA256_H
32 #if !defined(MBEDTLS_CONFIG_FILE)
35 #include MBEDTLS_CONFIG_FILE
41 #define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
47 #if !defined(MBEDTLS_SHA256_ALT)
48 // Regular implementation
52 * \brief The SHA-256 context structure.
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().
58 typedef struct mbedtls_sha256_context
60 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. */
63 int is224
; /*!< Determines which function to use:
64 0: Use SHA-256, or 1: Use SHA-224. */
66 mbedtls_sha256_context
;
68 #else /* MBEDTLS_SHA256_ALT */
69 #include "sha256_alt.h"
70 #endif /* MBEDTLS_SHA256_ALT */
73 * \brief This function initializes a SHA-256 context.
75 * \param ctx The SHA-256 context to initialize.
77 void mbedtls_sha256_init( mbedtls_sha256_context
*ctx
);
80 * \brief This function clears a SHA-256 context.
82 * \param ctx The SHA-256 context to clear.
84 void mbedtls_sha256_free( mbedtls_sha256_context
*ctx
);
87 * \brief This function clones the state of a SHA-256 context.
89 * \param dst The destination context.
90 * \param src The context to clone.
92 void mbedtls_sha256_clone( mbedtls_sha256_context
*dst
,
93 const mbedtls_sha256_context
*src
);
96 * \brief This function starts a SHA-224 or SHA-256 checksum
99 * \param ctx The context to initialize.
100 * \param is224 Determines which function to use:
101 * 0: Use SHA-256, or 1: Use SHA-224.
103 * \return \c 0 on success.
105 int mbedtls_sha256_starts_ret( mbedtls_sha256_context
*ctx
, int is224
);
108 * \brief This function feeds an input buffer into an ongoing
109 * SHA-256 checksum calculation.
111 * \param ctx The SHA-256 context.
112 * \param input The buffer holding the data.
113 * \param ilen The length of the input data.
115 * \return \c 0 on success.
117 int mbedtls_sha256_update_ret( mbedtls_sha256_context
*ctx
,
118 const unsigned char *input
,
122 * \brief This function finishes the SHA-256 operation, and writes
123 * the result to the output buffer.
125 * \param ctx The SHA-256 context.
126 * \param output The SHA-224 or SHA-256 checksum result.
128 * \return \c 0 on success.
130 int mbedtls_sha256_finish_ret( mbedtls_sha256_context
*ctx
,
131 unsigned char output
[32] );
134 * \brief This function processes a single data block within
135 * the ongoing SHA-256 computation. This function is for
138 * \param ctx The SHA-256 context.
139 * \param data The buffer holding one block of data.
141 * \return \c 0 on success.
143 int mbedtls_internal_sha256_process( mbedtls_sha256_context
*ctx
,
144 const unsigned char data
[64] );
146 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
147 #if defined(MBEDTLS_DEPRECATED_WARNING)
148 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
150 #define MBEDTLS_DEPRECATED
153 * \brief This function starts a SHA-224 or SHA-256 checksum
157 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
159 * \param ctx The context to initialize.
160 * \param is224 Determines which function to use:
161 * 0: Use SHA-256, or 1: Use SHA-224.
163 MBEDTLS_DEPRECATED
void mbedtls_sha256_starts( mbedtls_sha256_context
*ctx
,
167 * \brief This function feeds an input buffer into an ongoing
168 * SHA-256 checksum calculation.
170 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
172 * \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.
176 MBEDTLS_DEPRECATED
void mbedtls_sha256_update( mbedtls_sha256_context
*ctx
,
177 const unsigned char *input
,
181 * \brief This function finishes the SHA-256 operation, and writes
182 * the result to the output buffer.
184 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
186 * \param ctx The SHA-256 context.
187 * \param output The SHA-224 or SHA-256 checksum result.
189 MBEDTLS_DEPRECATED
void mbedtls_sha256_finish( mbedtls_sha256_context
*ctx
,
190 unsigned char output
[32] );
193 * \brief This function processes a single data block within
194 * the ongoing SHA-256 computation. This function is for
197 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
199 * \param ctx The SHA-256 context.
200 * \param data The buffer holding one block of data.
202 MBEDTLS_DEPRECATED
void mbedtls_sha256_process( mbedtls_sha256_context
*ctx
,
203 const unsigned char data
[64] );
205 #undef MBEDTLS_DEPRECATED
206 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
209 * \brief This function calculates the SHA-224 or SHA-256
210 * checksum of a buffer.
212 * The function allocates the context, performs the
213 * calculation, and frees the context.
215 * The SHA-256 result is calculated as
216 * output = SHA-256(input buffer).
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.
221 * \param is224 Determines which function to use:
222 * 0: Use SHA-256, or 1: Use SHA-224.
224 int mbedtls_sha256_ret( const unsigned char *input
,
226 unsigned char output
[32],
229 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
230 #if defined(MBEDTLS_DEPRECATED_WARNING)
231 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
233 #define MBEDTLS_DEPRECATED
237 * \brief This function calculates the SHA-224 or SHA-256 checksum
240 * The function allocates the context, performs the
241 * calculation, and frees the context.
243 * The SHA-256 result is calculated as
244 * output = SHA-256(input buffer).
246 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
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.
251 * \param is224 Determines which function to use:
252 * 0: Use SHA-256, or 1: Use SHA-224.
254 MBEDTLS_DEPRECATED
void mbedtls_sha256( const unsigned char *input
,
256 unsigned char output
[32],
259 #undef MBEDTLS_DEPRECATED
260 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
263 * \brief The SHA-224 and SHA-256 checkup routine.
265 * \return \c 0 on success.
266 * \return \c 1 on failure.
268 int mbedtls_sha256_self_test( int verbose
);
274 #endif /* mbedtls_sha256.h */