]>
Commit | Line | Data |
---|---|---|
700d8687 OM |
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 (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved | |
11 | * SPDX-License-Identifier: GPL-2.0 | |
12 | * | |
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. | |
17 | * | |
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. | |
22 | * | |
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. | |
26 | * | |
27 | * This file is part of Mbed TLS (https://tls.mbed.org) | |
28 | */ | |
29 | #ifndef MBEDTLS_SHA256_H | |
30 | #define MBEDTLS_SHA256_H | |
31 | ||
32 | #if !defined(MBEDTLS_CONFIG_FILE) | |
33 | #include "config.h" | |
34 | #else | |
35 | #include MBEDTLS_CONFIG_FILE | |
36 | #endif | |
37 | ||
38 | #include <stddef.h> | |
39 | #include <stdint.h> | |
40 | ||
41 | #define MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED -0x0037 /**< SHA-256 hardware accelerator failed */ | |
42 | ||
43 | #ifdef __cplusplus | |
44 | extern "C" { | |
45 | #endif | |
46 | ||
47 | #if !defined(MBEDTLS_SHA256_ALT) | |
48 | // Regular implementation | |
49 | // | |
50 | ||
51 | /** | |
52 | * \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(). | |
57 | */ | |
58 | typedef struct mbedtls_sha256_context | |
59 | { | |
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. */ | |
65 | } | |
66 | mbedtls_sha256_context; | |
67 | ||
68 | #else /* MBEDTLS_SHA256_ALT */ | |
69 | #include "sha256_alt.h" | |
70 | #endif /* MBEDTLS_SHA256_ALT */ | |
71 | ||
72 | /** | |
73 | * \brief This function initializes a SHA-256 context. | |
74 | * | |
75 | * \param ctx The SHA-256 context to initialize. | |
76 | */ | |
77 | void mbedtls_sha256_init( mbedtls_sha256_context *ctx ); | |
78 | ||
79 | /** | |
80 | * \brief This function clears a SHA-256 context. | |
81 | * | |
82 | * \param ctx The SHA-256 context to clear. | |
83 | */ | |
84 | void mbedtls_sha256_free( mbedtls_sha256_context *ctx ); | |
85 | ||
86 | /** | |
87 | * \brief This function clones the state of a SHA-256 context. | |
88 | * | |
89 | * \param dst The destination context. | |
90 | * \param src The context to clone. | |
91 | */ | |
92 | void mbedtls_sha256_clone( mbedtls_sha256_context *dst, | |
93 | const mbedtls_sha256_context *src ); | |
94 | ||
95 | /** | |
96 | * \brief This function starts a SHA-224 or SHA-256 checksum | |
97 | * calculation. | |
98 | * | |
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. | |
102 | * | |
103 | * \return \c 0 on success. | |
104 | */ | |
105 | int mbedtls_sha256_starts_ret( mbedtls_sha256_context *ctx, int is224 ); | |
106 | ||
107 | /** | |
108 | * \brief This function feeds an input buffer into an ongoing | |
109 | * SHA-256 checksum calculation. | |
110 | * | |
111 | * \param ctx The SHA-256 context. | |
112 | * \param input The buffer holding the data. | |
113 | * \param ilen The length of the input data. | |
114 | * | |
115 | * \return \c 0 on success. | |
116 | */ | |
117 | int mbedtls_sha256_update_ret( mbedtls_sha256_context *ctx, | |
118 | const unsigned char *input, | |
119 | size_t ilen ); | |
120 | ||
121 | /** | |
122 | * \brief This function finishes the SHA-256 operation, and writes | |
123 | * the result to the output buffer. | |
124 | * | |
125 | * \param ctx The SHA-256 context. | |
126 | * \param output The SHA-224 or SHA-256 checksum result. | |
127 | * | |
128 | * \return \c 0 on success. | |
129 | */ | |
130 | int mbedtls_sha256_finish_ret( mbedtls_sha256_context *ctx, | |
131 | unsigned char output[32] ); | |
132 | ||
133 | /** | |
134 | * \brief This function processes a single data block within | |
135 | * the ongoing SHA-256 computation. This function is for | |
136 | * internal use only. | |
137 | * | |
138 | * \param ctx The SHA-256 context. | |
139 | * \param data The buffer holding one block of data. | |
140 | * | |
141 | * \return \c 0 on success. | |
142 | */ | |
143 | int mbedtls_internal_sha256_process( mbedtls_sha256_context *ctx, | |
144 | const unsigned char data[64] ); | |
145 | ||
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 | /** | |
153 | * \brief This function starts a SHA-224 or SHA-256 checksum | |
154 | * calculation. | |
155 | * | |
156 | * | |
157 | * \deprecated Superseded by mbedtls_sha256_starts_ret() in 2.7.0. | |
158 | * | |
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. | |
162 | */ | |
163 | MBEDTLS_DEPRECATED void mbedtls_sha256_starts( mbedtls_sha256_context *ctx, | |
164 | int is224 ); | |
165 | ||
166 | /** | |
167 | * \brief This function feeds an input buffer into an ongoing | |
168 | * SHA-256 checksum calculation. | |
169 | * | |
170 | * \deprecated Superseded by mbedtls_sha256_update_ret() in 2.7.0. | |
171 | * | |
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. | |
175 | */ | |
176 | MBEDTLS_DEPRECATED void mbedtls_sha256_update( mbedtls_sha256_context *ctx, | |
177 | const unsigned char *input, | |
178 | size_t ilen ); | |
179 | ||
180 | /** | |
181 | * \brief This function finishes the SHA-256 operation, and writes | |
182 | * the result to the output buffer. | |
183 | * | |
184 | * \deprecated Superseded by mbedtls_sha256_finish_ret() in 2.7.0. | |
185 | * | |
186 | * \param ctx The SHA-256 context. | |
187 | * \param output The SHA-224 or SHA-256 checksum result. | |
188 | */ | |
189 | MBEDTLS_DEPRECATED void mbedtls_sha256_finish( mbedtls_sha256_context *ctx, | |
190 | unsigned char output[32] ); | |
191 | ||
192 | /** | |
193 | * \brief This function processes a single data block within | |
194 | * the ongoing SHA-256 computation. This function is for | |
195 | * internal use only. | |
196 | * | |
197 | * \deprecated Superseded by mbedtls_internal_sha256_process() in 2.7.0. | |
198 | * | |
199 | * \param ctx The SHA-256 context. | |
200 | * \param data The buffer holding one block of data. | |
201 | */ | |
202 | MBEDTLS_DEPRECATED void mbedtls_sha256_process( mbedtls_sha256_context *ctx, | |
203 | const unsigned char data[64] ); | |
204 | ||
205 | #undef MBEDTLS_DEPRECATED | |
206 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | |
207 | ||
208 | /** | |
209 | * \brief This function calculates the SHA-224 or SHA-256 | |
210 | * checksum of a buffer. | |
211 | * | |
212 | * The function allocates the context, performs the | |
213 | * calculation, and frees the context. | |
214 | * | |
215 | * 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. | |
221 | * \param is224 Determines which function to use: | |
222 | * 0: Use SHA-256, or 1: Use SHA-224. | |
223 | */ | |
224 | int mbedtls_sha256_ret( const unsigned char *input, | |
225 | 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 | |
235 | ||
236 | /** | |
237 | * \brief This function calculates the SHA-224 or SHA-256 checksum | |
238 | * of a buffer. | |
239 | * | |
240 | * The function allocates the context, performs the | |
241 | * calculation, and frees the context. | |
242 | * | |
243 | * 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. | |
251 | * \param is224 Determines which function to use: | |
252 | * 0: Use SHA-256, or 1: Use SHA-224. | |
253 | */ | |
254 | MBEDTLS_DEPRECATED void mbedtls_sha256( const unsigned char *input, | |
255 | size_t ilen, | |
256 | unsigned char output[32], | |
257 | int is224 ); | |
258 | ||
259 | #undef MBEDTLS_DEPRECATED | |
260 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ | |
261 | ||
262 | /** | |
263 | * \brief The SHA-224 and SHA-256 checkup routine. | |
264 | * | |
265 | * \return \c 0 on success. | |
266 | * \return \c 1 on failure. | |
267 | */ | |
268 | int mbedtls_sha256_self_test( int verbose ); | |
269 | ||
270 | #ifdef __cplusplus | |
271 | } | |
272 | #endif | |
273 | ||
274 | #endif /* mbedtls_sha256.h */ |