Mbed OS Reference
Loading...
Searching...
No Matches
md.h
Go to the documentation of this file.
1 /**
2 * \file md.h
3 *
4 * \brief This file contains the generic message-digest wrapper.
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 */
8/*
9 * Copyright The Mbed TLS Contributors
10 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24
25#ifndef MBEDTLS_MD_H
26#define MBEDTLS_MD_H
27
28#include <stddef.h>
29
30#if !defined(MBEDTLS_CONFIG_FILE)
31#include "mbedtls/config.h"
32#else
33#include MBEDTLS_CONFIG_FILE
34#endif
35
36/**
37 * \addtogroup mbedtls
38 * \{
39 * \defgroup mbedtls_md_module Message Digest (MD)
40 * \{
41 */
42
43
44#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
45#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
46#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
47#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
48
49/* MBEDTLS_ERR_MD_HW_ACCEL_FAILED is deprecated and should not be used. */
50#define MBEDTLS_ERR_MD_HW_ACCEL_FAILED -0x5280 /**< MD hardware accelerator failed. */
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
56/**
57 * \brief Supported message digests.
58 *
59 * \warning MD2, MD4, MD5 and SHA-1 are considered weak message digests and
60 * their use constitutes a security risk. We recommend considering
61 * stronger message digests instead.
62 *
63 */
64typedef enum {
65 MBEDTLS_MD_NONE=0, /**< None. */
66 MBEDTLS_MD_MD2, /**< The MD2 message digest. */
67 MBEDTLS_MD_MD4, /**< The MD4 message digest. */
68 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
69 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
70 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
71 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
72 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
73 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
74 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
76
77#if defined(MBEDTLS_SHA512_C)
78#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
79#else
80#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
81#endif
82
83#if defined(MBEDTLS_SHA512_C)
84#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
85#else
86#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
87#endif
88
89/**
90 * Opaque struct defined in md_internal.h.
91 */
93
94/**
95 * The generic message-digest context.
96 */
98{
99 /** Information about the associated message digest. */
101
102 /** The digest-specific context. */
103 void *md_ctx;
104
105 /** The HMAC part of the context. */
106 void *hmac_ctx;
108
109/**
110 * \brief This function returns the list of digests supported by the
111 * generic digest module.
112 *
113 * \note The list starts with the strongest available hashes.
114 *
115 * \return A statically allocated array of digests. Each element
116 * in the returned list is an integer belonging to the
117 * message-digest enumeration #mbedtls_md_type_t.
118 * The last entry is 0.
119 */
120const int *mbedtls_md_list( void );
121
122/**
123 * \brief This function returns the message-digest information
124 * associated with the given digest name.
125 *
126 * \param md_name The name of the digest to search for.
127 *
128 * \return The message-digest information associated with \p md_name.
129 * \return NULL if the associated message-digest information is not found.
130 */
131const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
132
133/**
134 * \brief This function returns the message-digest information
135 * associated with the given digest type.
136 *
137 * \param md_type The type of digest to search for.
138 *
139 * \return The message-digest information associated with \p md_type.
140 * \return NULL if the associated message-digest information is not found.
141 */
143
144/**
145 * \brief This function initializes a message-digest context without
146 * binding it to a particular message-digest algorithm.
147 *
148 * This function should always be called first. It prepares the
149 * context for mbedtls_md_setup() for binding it to a
150 * message-digest algorithm.
151 */
153
154/**
155 * \brief This function clears the internal structure of \p ctx and
156 * frees any embedded internal structure, but does not free
157 * \p ctx itself.
158 *
159 * If you have called mbedtls_md_setup() on \p ctx, you must
160 * call mbedtls_md_free() when you are no longer using the
161 * context.
162 * Calling this function if you have previously
163 * called mbedtls_md_init() and nothing else is optional.
164 * You must not call this function if you have not called
165 * mbedtls_md_init().
166 */
168
169#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
170#if defined(MBEDTLS_DEPRECATED_WARNING)
171#define MBEDTLS_DEPRECATED __attribute__((deprecated))
172#else
173#define MBEDTLS_DEPRECATED
174#endif
175/**
176 * \brief This function selects the message digest algorithm to use,
177 * and allocates internal structures.
178 *
179 * It should be called after mbedtls_md_init() or mbedtls_md_free().
180 * Makes it necessary to call mbedtls_md_free() later.
181 *
182 * \deprecated Superseded by mbedtls_md_setup() in 2.0.0
183 *
184 * \param ctx The context to set up.
185 * \param md_info The information structure of the message-digest algorithm
186 * to use.
187 *
188 * \return \c 0 on success.
189 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
190 * failure.
191 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
192 */
193int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED;
194#undef MBEDTLS_DEPRECATED
195#endif /* MBEDTLS_DEPRECATED_REMOVED */
196
197/**
198 * \brief This function selects the message digest algorithm to use,
199 * and allocates internal structures.
200 *
201 * It should be called after mbedtls_md_init() or
202 * mbedtls_md_free(). Makes it necessary to call
203 * mbedtls_md_free() later.
204 *
205 * \param ctx The context to set up.
206 * \param md_info The information structure of the message-digest algorithm
207 * to use.
208 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
209 * or non-zero: HMAC is used with this context.
210 *
211 * \return \c 0 on success.
212 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
213 * failure.
214 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
215 */
216int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
217
218/**
219 * \brief This function clones the state of an message-digest
220 * context.
221 *
222 * \note You must call mbedtls_md_setup() on \c dst before calling
223 * this function.
224 *
225 * \note The two contexts must have the same type,
226 * for example, both are SHA-256.
227 *
228 * \warning This function clones the message-digest state, not the
229 * HMAC state.
230 *
231 * \param dst The destination context.
232 * \param src The context to be cloned.
233 *
234 * \return \c 0 on success.
235 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
236 */
238 const mbedtls_md_context_t *src );
239
240/**
241 * \brief This function extracts the message-digest size from the
242 * message-digest information structure.
243 *
244 * \param md_info The information structure of the message-digest algorithm
245 * to use.
246 *
247 * \return The size of the message-digest output in Bytes.
248 */
249unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
250
251/**
252 * \brief This function extracts the message-digest type from the
253 * message-digest information structure.
254 *
255 * \param md_info The information structure of the message-digest algorithm
256 * to use.
257 *
258 * \return The type of the message digest.
259 */
261
262/**
263 * \brief This function extracts the message-digest name from the
264 * message-digest information structure.
265 *
266 * \param md_info The information structure of the message-digest algorithm
267 * to use.
268 *
269 * \return The name of the message digest.
270 */
271const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
272
273/**
274 * \brief This function starts a message-digest computation.
275 *
276 * You must call this function after setting up the context
277 * with mbedtls_md_setup(), and before passing data with
278 * mbedtls_md_update().
279 *
280 * \param ctx The generic message-digest context.
281 *
282 * \return \c 0 on success.
283 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
284 * failure.
285 */
287
288/**
289 * \brief This function feeds an input buffer into an ongoing
290 * message-digest computation.
291 *
292 * You must call mbedtls_md_starts() before calling this
293 * function. You may call this function multiple times.
294 * Afterwards, call mbedtls_md_finish().
295 *
296 * \param ctx The generic message-digest context.
297 * \param input The buffer holding the input data.
298 * \param ilen The length of the input data.
299 *
300 * \return \c 0 on success.
301 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
302 * failure.
303 */
304int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
305
306/**
307 * \brief This function finishes the digest operation,
308 * and writes the result to the output buffer.
309 *
310 * Call this function after a call to mbedtls_md_starts(),
311 * followed by any number of calls to mbedtls_md_update().
312 * Afterwards, you may either clear the context with
313 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
314 * the context for another digest operation with the same
315 * algorithm.
316 *
317 * \param ctx The generic message-digest context.
318 * \param output The buffer for the generic message-digest checksum result.
319 *
320 * \return \c 0 on success.
321 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
322 * failure.
323 */
324int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
325
326/**
327 * \brief This function calculates the message-digest of a buffer,
328 * with respect to a configurable message-digest algorithm
329 * in a single call.
330 *
331 * The result is calculated as
332 * Output = message_digest(input buffer).
333 *
334 * \param md_info The information structure of the message-digest algorithm
335 * to use.
336 * \param input The buffer holding the data.
337 * \param ilen The length of the input data.
338 * \param output The generic message-digest checksum result.
339 *
340 * \return \c 0 on success.
341 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
342 * failure.
343 */
344int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
345 unsigned char *output );
346
347#if defined(MBEDTLS_FS_IO)
348/**
349 * \brief This function calculates the message-digest checksum
350 * result of the contents of the provided file.
351 *
352 * The result is calculated as
353 * Output = message_digest(file contents).
354 *
355 * \param md_info The information structure of the message-digest algorithm
356 * to use.
357 * \param path The input file name.
358 * \param output The generic message-digest checksum result.
359 *
360 * \return \c 0 on success.
361 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
362 * the file pointed by \p path.
363 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
364 */
365int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
366 unsigned char *output );
367#endif /* MBEDTLS_FS_IO */
368
369/**
370 * \brief This function sets the HMAC key and prepares to
371 * authenticate a new message.
372 *
373 * Call this function after mbedtls_md_setup(), to use
374 * the MD context for an HMAC calculation, then call
375 * mbedtls_md_hmac_update() to provide the input data, and
376 * mbedtls_md_hmac_finish() to get the HMAC value.
377 *
378 * \param ctx The message digest context containing an embedded HMAC
379 * context.
380 * \param key The HMAC secret key.
381 * \param keylen The length of the HMAC key in Bytes.
382 *
383 * \return \c 0 on success.
384 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
385 * failure.
386 */
387int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
388 size_t keylen );
389
390/**
391 * \brief This function feeds an input buffer into an ongoing HMAC
392 * computation.
393 *
394 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
395 * before calling this function.
396 * You may call this function multiple times to pass the
397 * input piecewise.
398 * Afterwards, call mbedtls_md_hmac_finish().
399 *
400 * \param ctx The message digest context containing an embedded HMAC
401 * context.
402 * \param input The buffer holding the input data.
403 * \param ilen The length of the input data.
404 *
405 * \return \c 0 on success.
406 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
407 * failure.
408 */
409int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
410 size_t ilen );
411
412/**
413 * \brief This function finishes the HMAC operation, and writes
414 * the result to the output buffer.
415 *
416 * Call this function after mbedtls_md_hmac_starts() and
417 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
418 * you may either call mbedtls_md_free() to clear the context,
419 * or call mbedtls_md_hmac_reset() to reuse the context with
420 * the same HMAC key.
421 *
422 * \param ctx The message digest context containing an embedded HMAC
423 * context.
424 * \param output The generic HMAC checksum result.
425 *
426 * \return \c 0 on success.
427 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
428 * failure.
429 */
430int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
431
432/**
433 * \brief This function prepares to authenticate a new message with
434 * the same key as the previous HMAC operation.
435 *
436 * You may call this function after mbedtls_md_hmac_finish().
437 * Afterwards call mbedtls_md_hmac_update() to pass the new
438 * input.
439 *
440 * \param ctx The message digest context containing an embedded HMAC
441 * context.
442 *
443 * \return \c 0 on success.
444 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
445 * failure.
446 */
448
449/**
450 * \brief This function calculates the full generic HMAC
451 * on the input buffer with the provided key.
452 *
453 * The function allocates the context, performs the
454 * calculation, and frees the context.
455 *
456 * The HMAC result is calculated as
457 * output = generic HMAC(hmac key, input buffer).
458 *
459 * \param md_info The information structure of the message-digest algorithm
460 * to use.
461 * \param key The HMAC secret key.
462 * \param keylen The length of the HMAC secret key in Bytes.
463 * \param input The buffer holding the input data.
464 * \param ilen The length of the input data.
465 * \param output The generic HMAC result.
466 *
467 * \return \c 0 on success.
468 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
469 * failure.
470 */
471int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
472 const unsigned char *input, size_t ilen,
473 unsigned char *output );
474
475/* Internal use */
476int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
477
478/// \}
479/// \}
480
481#ifdef __cplusplus
482}
483#endif
484
485#endif /* MBEDTLS_MD_H */
Configuration options (set of defines)
const int * mbedtls_md_list(void)
This function returns the list of digests supported by the generic digest module.
int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac)
This function selects the message digest algorithm to use, and allocates internal structures.
mbedtls_md_type_t
Supported message digests.
Definition: md.h:64
int mbedtls_md_starts(mbedtls_md_context_t *ctx)
This function starts a message-digest computation.
int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen, const unsigned char *input, size_t ilen, unsigned char *output)
This function calculates the full generic HMAC on the input buffer with the provided key.
int mbedtls_md_init_ctx(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info)
This function selects the message digest algorithm to use, and allocates internal structures.
int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
This function calculates the message-digest of a buffer, with respect to a configurable message-diges...
int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx)
This function prepares to authenticate a new message with the same key as the previous HMAC operation...
const char * mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
This function extracts the message-digest name from the message-digest information structure.
int mbedtls_md_clone(mbedtls_md_context_t *dst, const mbedtls_md_context_t *src)
This function clones the state of an message-digest context.
mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info)
This function extracts the message-digest type from the message-digest information structure.
int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output)
This function finishes the HMAC operation, and writes the result to the output buffer.
int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing message-digest computation.
int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing HMAC computation.
int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key, size_t keylen)
This function sets the HMAC key and prepares to authenticate a new message.
const mbedtls_md_info_t * mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
This function returns the message-digest information associated with the given digest type.
void mbedtls_md_init(mbedtls_md_context_t *ctx)
This function initializes a message-digest context without binding it to a particular message-digest ...
int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output)
This function finishes the digest operation, and writes the result to the output buffer.
const mbedtls_md_info_t * mbedtls_md_info_from_string(const char *md_name)
This function returns the message-digest information associated with the given digest name.
unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info)
This function extracts the message-digest size from the message-digest information structure.
void mbedtls_md_free(mbedtls_md_context_t *ctx)
This function clears the internal structure of ctx and frees any embedded internal structure,...
@ MBEDTLS_MD_SHA512
The SHA-512 message digest.
Definition: md.h:73
@ MBEDTLS_MD_MD5
The MD5 message digest.
Definition: md.h:68
@ MBEDTLS_MD_RIPEMD160
The RIPEMD-160 message digest.
Definition: md.h:74
@ MBEDTLS_MD_SHA384
The SHA-384 message digest.
Definition: md.h:72
@ MBEDTLS_MD_NONE
None.
Definition: md.h:65
@ MBEDTLS_MD_SHA256
The SHA-256 message digest.
Definition: md.h:71
@ MBEDTLS_MD_SHA224
The SHA-224 message digest.
Definition: md.h:70
@ MBEDTLS_MD_SHA1
The SHA-1 message digest.
Definition: md.h:69
@ MBEDTLS_MD_MD4
The MD4 message digest.
Definition: md.h:67
@ MBEDTLS_MD_MD2
The MD2 message digest.
Definition: md.h:66
The generic message-digest context.
Definition: md.h:98
void * hmac_ctx
The HMAC part of the context.
Definition: md.h:106
const mbedtls_md_info_t * md_info
Information about the associated message digest.
Definition: md.h:100
void * md_ctx
The digest-specific context.
Definition: md.h:103
Message digest information.
Definition: md_internal.h:46