Mbed OS Reference
Loading...
Searching...
No Matches
sha256.h
Go to the documentation of this file.
1/**
2 * \file sha256.h
3 *
4 * \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>.
8 */
9/*
10 * Copyright The Mbed TLS Contributors
11 * 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.
24 */
25#ifndef MBEDTLS_SHA256_H
26#define MBEDTLS_SHA256_H
27
28#if !defined(MBEDTLS_CONFIG_FILE)
29#include "mbedtls/config.h"
30#else
31#include MBEDTLS_CONFIG_FILE
32#endif
33
34#include <stddef.h>
35#include <stdint.h>
36
37/**
38 * \addtogroup mbedtls
39 * \{
40 * \defgroup mbedtls_sha256_module SHA-224 and SHA-256
41 * \{
42 */
43
44
45/* MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED is deprecated and should not be used. */
46#define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */
47#define MBEDTLS_ERR_SHA256_BAD_INPUT_DATA -0x0074 /**< SHA-256 input data was malformed. */
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53#if !defined(MBEDTLS_SHA256_ALT)
54// Regular implementation
55//
56
57/**
58 * \brief The SHA-256 context structure.
59 *
60 * The structure is used both for SHA-256 and for SHA-224
61 * checksum calculations. The choice between these two is
62 * made in the call to mbedtls_sha256_starts_ret().
63 */
65{
66 uint32_t total[2]; /*!< The number of Bytes processed. */
67 uint32_t state[8]; /*!< The intermediate digest state. */
68 unsigned char buffer[64]; /*!< The data block being processed. */
69 int is224; /*!< Determines which function to use:
70 0: Use SHA-256, or 1: Use SHA-224. */
71}
73
74#else /* MBEDTLS_SHA256_ALT */
75#include "sha256_alt.h"
76#endif /* MBEDTLS_SHA256_ALT */
77
78/**
79 * \brief This function initializes a SHA-256 context.
80 *
81 * \param ctx The SHA-256 context to initialize. This must not be \c NULL.
82 */
84
85/**
86 * \brief This function clears a SHA-256 context.
87 *
88 * \param ctx The SHA-256 context to clear. This may be \c NULL, in which
89 * case this function returns immediately. If it is not \c NULL,
90 * it must point to an initialized SHA-256 context.
91 */
93
94/**
95 * \brief This function clones the state of a SHA-256 context.
96 *
97 * \param dst The destination context. This must be initialized.
98 * \param src The context to clone. This must be initialized.
99 */
101 const mbedtls_sha256_context *src );
102
103/**
104 * \brief This function starts a SHA-224 or SHA-256 checksum
105 * calculation.
106 *
107 * \param ctx The context to use. This must be initialized.
108 * \param is224 This determines which function to use. This must be
109 * either \c 0 for SHA-256, or \c 1 for SHA-224.
110 *
111 * \return \c 0 on success.
112 * \return A negative error code on failure.
113 */
115
116/**
117 * \brief This function feeds an input buffer into an ongoing
118 * SHA-256 checksum calculation.
119 *
120 * \param ctx The SHA-256 context. This must be initialized
121 * and have a hash operation started.
122 * \param input The buffer holding the data. This must be a readable
123 * buffer of length \p ilen Bytes.
124 * \param ilen The length of the input data in Bytes.
125 *
126 * \return \c 0 on success.
127 * \return A negative error code on failure.
128 */
130 const unsigned char *input,
131 size_t ilen );
132
133/**
134 * \brief This function finishes the SHA-256 operation, and writes
135 * the result to the output buffer.
136 *
137 * \param ctx The SHA-256 context. This must be initialized
138 * and have a hash operation started.
139 * \param output The SHA-224 or SHA-256 checksum result.
140 * This must be a writable buffer of length \c 32 Bytes.
141 *
142 * \return \c 0 on success.
143 * \return A negative error code on failure.
144 */
146 unsigned char output[32] );
147
148/**
149 * \brief This function processes a single data block within
150 * the ongoing SHA-256 computation. This function is for
151 * internal use only.
152 *
153 * \param ctx The SHA-256 context. This must be initialized.
154 * \param data The buffer holding one block of data. This must
155 * be a readable buffer of length \c 64 Bytes.
156 *
157 * \return \c 0 on success.
158 * \return A negative error code on failure.
159 */
161 const unsigned char data[64] );
162
163#if !defined(MBEDTLS_DEPRECATED_REMOVED)
164#if defined(MBEDTLS_DEPRECATED_WARNING)
165#define MBEDTLS_DEPRECATED __attribute__((deprecated))
166#else
167#define MBEDTLS_DEPRECATED
168#endif
169/**
170 * \brief This function starts a SHA-224 or SHA-256 checksum
171 * calculation.
172 *
173 * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0.
174 *
175 * \param ctx The context to use. This must be initialized.
176 * \param is224 Determines which function to use. This must be
177 * either \c 0 for SHA-256, or \c 1 for SHA-224.
178 */
179MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx,
180 int is224 );
181
182/**
183 * \brief This function feeds an input buffer into an ongoing
184 * SHA-256 checksum calculation.
185 *
186 * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0.
187 *
188 * \param ctx The SHA-256 context to use. This must be
189 * initialized and have a hash operation started.
190 * \param input The buffer holding the data. This must be a readable
191 * buffer of length \p ilen Bytes.
192 * \param ilen The length of the input data in Bytes.
193 */
194MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx,
195 const unsigned char *input,
196 size_t ilen );
197
198/**
199 * \brief This function finishes the SHA-256 operation, and writes
200 * the result to the output buffer.
201 *
202 * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0.
203 *
204 * \param ctx The SHA-256 context. This must be initialized and
205 * have a hash operation started.
206 * \param output The SHA-224 or SHA-256 checksum result. This must be
207 * a writable buffer of length \c 32 Bytes.
208 */
209MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx,
210 unsigned char output[32] );
211
212/**
213 * \brief This function processes a single data block within
214 * the ongoing SHA-256 computation. This function is for
215 * internal use only.
216 *
217 * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0.
218 *
219 * \param ctx The SHA-256 context. This must be initialized.
220 * \param data The buffer holding one block of data. This must be
221 * a readable buffer of size \c 64 Bytes.
222 */
223MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx,
224 const unsigned char data[64] );
225
226#undef MBEDTLS_DEPRECATED
227#endif /* !MBEDTLS_DEPRECATED_REMOVED */
228
229/**
230 * \brief This function calculates the SHA-224 or SHA-256
231 * checksum of a buffer.
232 *
233 * The function allocates the context, performs the
234 * calculation, and frees the context.
235 *
236 * The SHA-256 result is calculated as
237 * output = SHA-256(input buffer).
238 *
239 * \param input The buffer holding the data. This must be a readable
240 * buffer of length \p ilen Bytes.
241 * \param ilen The length of the input data in Bytes.
242 * \param output The SHA-224 or SHA-256 checksum result. This must
243 * be a writable buffer of length \c 32 Bytes.
244 * \param is224 Determines which function to use. This must be
245 * either \c 0 for SHA-256, or \c 1 for SHA-224.
246 */
247int mbedtls_sha256_ret( const unsigned char *input,
248 size_t ilen,
249 unsigned char output[32],
250 int is224 );
251
252#if !defined(MBEDTLS_DEPRECATED_REMOVED)
253#if defined(MBEDTLS_DEPRECATED_WARNING)
254#define MBEDTLS_DEPRECATED __attribute__((deprecated))
255#else
256#define MBEDTLS_DEPRECATED
257#endif
258
259/**
260 * \brief This function calculates the SHA-224 or SHA-256 checksum
261 * of a buffer.
262 *
263 * The function allocates the context, performs the
264 * calculation, and frees the context.
265 *
266 * The SHA-256 result is calculated as
267 * output = SHA-256(input buffer).
268 *
269 * \deprecated Superseded by mbedtls_sha256_ret() in 2.7.0.
270 *
271 * \param input The buffer holding the data. This must be a readable
272 * buffer of length \p ilen Bytes.
273 * \param ilen The length of the input data in Bytes.
274 * \param output The SHA-224 or SHA-256 checksum result. This must be
275 * a writable buffer of length \c 32 Bytes.
276 * \param is224 Determines which function to use. This must be either
277 * \c 0 for SHA-256, or \c 1 for SHA-224.
278 */
279MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input,
280 size_t ilen,
281 unsigned char output[32],
282 int is224 );
283
284#undef MBEDTLS_DEPRECATED
285#endif /* !MBEDTLS_DEPRECATED_REMOVED */
286
287#if defined(MBEDTLS_SELF_TEST)
288
289/**
290 * \brief The SHA-224 and SHA-256 checkup routine.
291 *
292 * \return \c 0 on success.
293 * \return \c 1 on failure.
294 */
295int mbedtls_sha256_self_test( int verbose );
296
297#endif /* MBEDTLS_SELF_TEST */
298
299#ifdef __cplusplus
300}
301#endif
302
303/// \}
304/// \}
305
306#endif /* mbedtls_sha256.h */
Configuration options (set of defines)
void mbedtls_sha256_process(mbedtls_sha256_context *ctx, const unsigned char data[64])
This function processes a single data block within the ongoing SHA-256 computation.
void mbedtls_sha256_finish(mbedtls_sha256_context *ctx, unsigned char output[32])
This function finishes the SHA-256 operation, and writes the result to the output buffer.
void mbedtls_sha256(const unsigned char *input, size_t ilen, unsigned char output[32], int is224)
This function calculates the SHA-224 or SHA-256 checksum of a buffer.
void mbedtls_sha256_update(mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing SHA-256 checksum calculation.
void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
This function clears a SHA-256 context.
void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
This function initializes a SHA-256 context.
int mbedtls_internal_sha256_process(mbedtls_sha256_context *ctx, const unsigned char data[64])
This function processes a single data block within the ongoing SHA-256 computation.
void mbedtls_sha256_clone(mbedtls_sha256_context *dst, const mbedtls_sha256_context *src)
This function clones the state of a SHA-256 context.
int mbedtls_sha256_ret(const unsigned char *input, size_t ilen, unsigned char output[32], int is224)
This function calculates the SHA-224 or SHA-256 checksum of a buffer.
int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx, unsigned char output[32])
This function finishes the SHA-256 operation, and writes the result to the output buffer.
void mbedtls_sha256_starts(mbedtls_sha256_context *ctx, int is224)
This function starts a SHA-224 or SHA-256 checksum calculation.
int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224)
This function starts a SHA-224 or SHA-256 checksum calculation.
int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing SHA-256 checksum calculation.
The SHA-256 context structure.
Definition: sha256.h:65
uint32_t total[2]
Definition: sha256.h:66
uint32_t state[8]
Definition: sha256.h:67
unsigned char buffer[64]
Definition: sha256.h:68