Mbed OS Reference
Loading...
Searching...
No Matches
mbedtls/include/mbedtls/sha1.h
Go to the documentation of this file.
1/**
2 * \file sha1.h
3 *
4 * \brief This file contains SHA-1 definitions and functions.
5 *
6 * The Secure Hash Algorithm 1 (SHA-1) cryptographic hash function is defined in
7 * <em>FIPS 180-4: Secure Hash Standard (SHS)</em>.
8 *
9 * \warning SHA-1 is considered a weak message digest and its use constitutes
10 * a security risk. We recommend considering stronger message
11 * digests instead.
12 */
13/*
14 * Copyright The Mbed TLS Contributors
15 * SPDX-License-Identifier: Apache-2.0
16 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
28 */
29#ifndef MBEDTLS_SHA1_H
30#define MBEDTLS_SHA1_H
31
32#if !defined(MBEDTLS_CONFIG_FILE)
33#include "mbedtls/config.h"
34#else
35#include MBEDTLS_CONFIG_FILE
36#endif
37
38#include <stddef.h>
39#include <stdint.h>
40
41/**
42 * \addtogroup mbedtls
43 * \{
44 * \defgroup mbedtls_sha1_module SHA-1
45 * \{
46 */
47
48/* MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED is deprecated and should not be used. */
49#define MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED -0x0035 /**< SHA-1 hardware accelerator failed */
50#define MBEDTLS_ERR_SHA1_BAD_INPUT_DATA -0x0073 /**< SHA-1 input data was malformed. */
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56#if !defined(MBEDTLS_SHA1_ALT)
57// Regular implementation
58//
59
60/**
61 * \brief The SHA-1 context structure.
62 *
63 * \warning SHA-1 is considered a weak message digest and its use
64 * constitutes a security risk. We recommend considering
65 * stronger message digests instead.
66 *
67 */
69{
70 uint32_t total[2]; /*!< The number of Bytes processed. */
71 uint32_t state[5]; /*!< The intermediate digest state. */
72 unsigned char buffer[64]; /*!< The data block being processed. */
73}
75
76#else /* MBEDTLS_SHA1_ALT */
77#include "sha1_alt.h"
78#endif /* MBEDTLS_SHA1_ALT */
79
80/**
81 * \brief This function initializes a SHA-1 context.
82 *
83 * \warning SHA-1 is considered a weak message digest and its use
84 * constitutes a security risk. We recommend considering
85 * stronger message digests instead.
86 *
87 * \param ctx The SHA-1 context to initialize.
88 * This must not be \c NULL.
89 *
90 */
92
93/**
94 * \brief This function clears a SHA-1 context.
95 *
96 * \warning SHA-1 is considered a weak message digest and its use
97 * constitutes a security risk. We recommend considering
98 * stronger message digests instead.
99 *
100 * \param ctx The SHA-1 context to clear. This may be \c NULL,
101 * in which case this function does nothing. If it is
102 * not \c NULL, it must point to an initialized
103 * SHA-1 context.
104 *
105 */
107
108/**
109 * \brief This function clones the state of a SHA-1 context.
110 *
111 * \warning SHA-1 is considered a weak message digest and its use
112 * constitutes a security risk. We recommend considering
113 * stronger message digests instead.
114 *
115 * \param dst The SHA-1 context to clone to. This must be initialized.
116 * \param src The SHA-1 context to clone from. This must be initialized.
117 *
118 */
120 const mbedtls_sha1_context *src );
121
122/**
123 * \brief This function starts a SHA-1 checksum calculation.
124 *
125 * \warning SHA-1 is considered a weak message digest and its use
126 * constitutes a security risk. We recommend considering
127 * stronger message digests instead.
128 *
129 * \param ctx The SHA-1 context to initialize. This must be initialized.
130 *
131 * \return \c 0 on success.
132 * \return A negative error code on failure.
133 *
134 */
136
137/**
138 * \brief This function feeds an input buffer into an ongoing SHA-1
139 * checksum calculation.
140 *
141 * \warning SHA-1 is considered a weak message digest and its use
142 * constitutes a security risk. We recommend considering
143 * stronger message digests instead.
144 *
145 * \param ctx The SHA-1 context. This must be initialized
146 * and have a hash operation started.
147 * \param input The buffer holding the input data.
148 * This must be a readable buffer of length \p ilen Bytes.
149 * \param ilen The length of the input data \p input in Bytes.
150 *
151 * \return \c 0 on success.
152 * \return A negative error code on failure.
153 */
155 const unsigned char *input,
156 size_t ilen );
157
158/**
159 * \brief This function finishes the SHA-1 operation, and writes
160 * the result to the output buffer.
161 *
162 * \warning SHA-1 is considered a weak message digest and its use
163 * constitutes a security risk. We recommend considering
164 * stronger message digests instead.
165 *
166 * \param ctx The SHA-1 context to use. This must be initialized and
167 * have a hash operation started.
168 * \param output The SHA-1 checksum result. This must be a writable
169 * buffer of length \c 20 Bytes.
170 *
171 * \return \c 0 on success.
172 * \return A negative error code on failure.
173 */
175 unsigned char output[20] );
176
177/**
178 * \brief SHA-1 process data block (internal use only).
179 *
180 * \warning SHA-1 is considered a weak message digest and its use
181 * constitutes a security risk. We recommend considering
182 * stronger message digests instead.
183 *
184 * \param ctx The SHA-1 context to use. This must be initialized.
185 * \param data The data block being processed. This must be a
186 * readable buffer of length \c 64 Bytes.
187 *
188 * \return \c 0 on success.
189 * \return A negative error code on failure.
190 *
191 */
193 const unsigned char data[64] );
194
195#if !defined(MBEDTLS_DEPRECATED_REMOVED)
196#if defined(MBEDTLS_DEPRECATED_WARNING)
197#define MBEDTLS_DEPRECATED __attribute__((deprecated))
198#else
199#define MBEDTLS_DEPRECATED
200#endif
201/**
202 * \brief This function starts a SHA-1 checksum calculation.
203 *
204 * \warning SHA-1 is considered a weak message digest and its use
205 * constitutes a security risk. We recommend considering
206 * stronger message digests instead.
207 *
208 * \deprecated Superseded by mbedtls_sha1_starts_ret() in 2.7.0.
209 *
210 * \param ctx The SHA-1 context to initialize. This must be initialized.
211 *
212 */
213MBEDTLS_DEPRECATED void mbedtls_sha1_starts( mbedtls_sha1_context *ctx );
214
215/**
216 * \brief This function feeds an input buffer into an ongoing SHA-1
217 * checksum calculation.
218 *
219 * \warning SHA-1 is considered a weak message digest and its use
220 * constitutes a security risk. We recommend considering
221 * stronger message digests instead.
222 *
223 * \deprecated Superseded by mbedtls_sha1_update_ret() in 2.7.0.
224 *
225 * \param ctx The SHA-1 context. This must be initialized and
226 * have a hash operation started.
227 * \param input The buffer holding the input data.
228 * This must be a readable buffer of length \p ilen Bytes.
229 * \param ilen The length of the input data \p input in Bytes.
230 *
231 */
232MBEDTLS_DEPRECATED void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
233 const unsigned char *input,
234 size_t ilen );
235
236/**
237 * \brief This function finishes the SHA-1 operation, and writes
238 * the result to the output buffer.
239 *
240 * \warning SHA-1 is considered a weak message digest and its use
241 * constitutes a security risk. We recommend considering
242 * stronger message digests instead.
243 *
244 * \deprecated Superseded by mbedtls_sha1_finish_ret() in 2.7.0.
245 *
246 * \param ctx The SHA-1 context. This must be initialized and
247 * have a hash operation started.
248 * \param output The SHA-1 checksum result.
249 * This must be a writable buffer of length \c 20 Bytes.
250 */
251MBEDTLS_DEPRECATED void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
252 unsigned char output[20] );
253
254/**
255 * \brief SHA-1 process data block (internal use only).
256 *
257 * \warning SHA-1 is considered a weak message digest and its use
258 * constitutes a security risk. We recommend considering
259 * stronger message digests instead.
260 *
261 * \deprecated Superseded by mbedtls_internal_sha1_process() in 2.7.0.
262 *
263 * \param ctx The SHA-1 context. This must be initialized.
264 * \param data The data block being processed.
265 * This must be a readable buffer of length \c 64 bytes.
266 *
267 */
268MBEDTLS_DEPRECATED void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
269 const unsigned char data[64] );
270
271#undef MBEDTLS_DEPRECATED
272#endif /* !MBEDTLS_DEPRECATED_REMOVED */
273
274/**
275 * \brief This function calculates the SHA-1 checksum of a buffer.
276 *
277 * The function allocates the context, performs the
278 * calculation, and frees the context.
279 *
280 * The SHA-1 result is calculated as
281 * output = SHA-1(input buffer).
282 *
283 * \warning SHA-1 is considered a weak message digest and its use
284 * constitutes a security risk. We recommend considering
285 * stronger message digests instead.
286 *
287 * \param input The buffer holding the input data.
288 * This must be a readable buffer of length \p ilen Bytes.
289 * \param ilen The length of the input data \p input in Bytes.
290 * \param output The SHA-1 checksum result.
291 * This must be a writable buffer of length \c 20 Bytes.
292 *
293 * \return \c 0 on success.
294 * \return A negative error code on failure.
295 *
296 */
297int mbedtls_sha1_ret( const unsigned char *input,
298 size_t ilen,
299 unsigned char output[20] );
300
301#if !defined(MBEDTLS_DEPRECATED_REMOVED)
302#if defined(MBEDTLS_DEPRECATED_WARNING)
303#define MBEDTLS_DEPRECATED __attribute__((deprecated))
304#else
305#define MBEDTLS_DEPRECATED
306#endif
307/**
308 * \brief This function calculates the SHA-1 checksum of a buffer.
309 *
310 * The function allocates the context, performs the
311 * calculation, and frees the context.
312 *
313 * The SHA-1 result is calculated as
314 * output = SHA-1(input buffer).
315 *
316 * \warning SHA-1 is considered a weak message digest and its use
317 * constitutes a security risk. We recommend considering
318 * stronger message digests instead.
319 *
320 * \deprecated Superseded by mbedtls_sha1_ret() in 2.7.0
321 *
322 * \param input The buffer holding the input data.
323 * This must be a readable buffer of length \p ilen Bytes.
324 * \param ilen The length of the input data \p input in Bytes.
325 * \param output The SHA-1 checksum result. This must be a writable
326 * buffer of size \c 20 Bytes.
327 *
328 */
329MBEDTLS_DEPRECATED void mbedtls_sha1( const unsigned char *input,
330 size_t ilen,
331 unsigned char output[20] );
332
333#undef MBEDTLS_DEPRECATED
334#endif /* !MBEDTLS_DEPRECATED_REMOVED */
335
336#if defined(MBEDTLS_SELF_TEST)
337
338/**
339 * \brief The SHA-1 checkup routine.
340 *
341 * \warning SHA-1 is considered a weak message digest and its use
342 * constitutes a security risk. We recommend considering
343 * stronger message digests instead.
344 *
345 * \return \c 0 on success.
346 * \return \c 1 on failure.
347 *
348 */
349int mbedtls_sha1_self_test( int verbose );
350
351#endif /* MBEDTLS_SELF_TEST */
352
353#ifdef __cplusplus
354}
355#endif
356
357/// \}
358/// \}
359
360#endif /* mbedtls_sha1.h */
Configuration options (set of defines)
void mbedtls_sha1_free(mbedtls_sha1_context *ctx)
This function clears a SHA-1 context.
void mbedtls_sha1_clone(mbedtls_sha1_context *dst, const mbedtls_sha1_context *src)
This function clones the state of a SHA-1 context.
int mbedtls_sha1_ret(const unsigned char *input, size_t ilen, unsigned char output[20])
This function calculates the SHA-1 checksum of a buffer.
void mbedtls_sha1_update(mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing SHA-1 checksum calculation.
int mbedtls_sha1_update_ret(mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing SHA-1 checksum calculation.
int mbedtls_internal_sha1_process(mbedtls_sha1_context *ctx, const unsigned char data[64])
SHA-1 process data block (internal use only).
void mbedtls_sha1_process(mbedtls_sha1_context *ctx, const unsigned char data[64])
SHA-1 process data block (internal use only).
int mbedtls_sha1_starts_ret(mbedtls_sha1_context *ctx)
This function starts a SHA-1 checksum calculation.
void mbedtls_sha1_starts(mbedtls_sha1_context *ctx)
This function starts a SHA-1 checksum calculation.
int mbedtls_sha1_finish_ret(mbedtls_sha1_context *ctx, unsigned char output[20])
This function finishes the SHA-1 operation, and writes the result to the output buffer.
void mbedtls_sha1_finish(mbedtls_sha1_context *ctx, unsigned char output[20])
This function finishes the SHA-1 operation, and writes the result to the output buffer.
void mbedtls_sha1_init(mbedtls_sha1_context *ctx)
This function initializes a SHA-1 context.
void mbedtls_sha1(const unsigned char *input, size_t ilen, unsigned char output[20])
This function calculates the SHA-1 checksum of a buffer.
The SHA-1 context structure.