Mbed TLS v4.1.1
Loading...
Searching...
No Matches
md.h
Go to the documentation of this file.
1
9/*
10 * Copyright The Mbed TLS Contributors
11 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
12 */
13
14#ifndef MBEDTLS_MD_H
15#define MBEDTLS_MD_H
16#include "mbedtls/private_access.h"
17
18#include <stddef.h>
19
22
24#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080
26#define MBEDTLS_ERR_MD_BAD_INPUT_DATA PSA_ERROR_INVALID_ARGUMENT
28#define MBEDTLS_ERR_MD_ALLOC_FAILED PSA_ERROR_INSUFFICIENT_MEMORY
29
30#if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
32#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200
33#endif
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
47/* Note: these are aligned with the definitions of PSA_ALG_ macros for hashes,
48 * in order to enable an efficient implementation of conversion functions.
49 * This is tested by md_to_from_psa() in test_suite_md. */
64
65/* Note: this should always be >= PSA_HASH_MAX_SIZE
66 * in all builds with both CRYPTO_C and MD_LIGHT.
67 *
68 * This is to make things easier for modules such as TLS that may define a
69 * buffer size using MD_MAX_SIZE in a part of the code that's common to PSA
70 * and legacy, then assume the buffer's size is PSA_HASH_MAX_SIZE in another
71 * part of the code based on PSA.
72 */
73#if defined(PSA_WANT_ALG_SHA_512) || defined(PSA_WANT_ALG_SHA3_512)
74#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
75#elif defined(PSA_WANT_ALG_SHA_384) || defined(PSA_WANT_ALG_SHA3_384)
76#define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */
77#elif defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA3_256)
78#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */
79#elif defined(PSA_WANT_ALG_SHA_224) || defined(PSA_WANT_ALG_SHA3_224)
80#define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */
81#else
82#define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160
83 or smaller (MD5 and earlier) */
84#endif
85
94/* Defined internally in library/md_wrap.h. */
96
97/*
98 * Used internally to indicate whether a context uses legacy or PSA.
99 *
100 * Internal use only.
101 */
106
110typedef struct mbedtls_md_context_t {
112 const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
113
114#if defined(MBEDTLS_MD_SOME_PSA)
116 mbedtls_md_engine_t MBEDTLS_PRIVATE(engine);
117#endif
118
120 void *MBEDTLS_PRIVATE(md_ctx);
121
122#if defined(MBEDTLS_MD_C)
124 void *MBEDTLS_PRIVATE(hmac_ctx);
125#endif
127
138
148
163
164
188int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
189
215 const mbedtls_md_context_t *src);
216
226unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
227
237static inline unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type)
238{
240}
241
252
268
286int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
287
307int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
308
328int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
329 unsigned char *output);
330
331#if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
332
348int mbedtls_md_hmac_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info);
349
361const int *mbedtls_md_list(void);
362
372const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
373
383const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
384
395const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
396 const mbedtls_md_context_t *ctx);
397
398#if defined(MBEDTLS_FS_IO)
417int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
418 unsigned char *output);
419#endif /* MBEDTLS_FS_IO */
420
440int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
441 size_t keylen);
442
463int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
464 size_t ilen);
465
485int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
486
503int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
504
528int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
529 const unsigned char *input, size_t ilen,
530 unsigned char *output);
531
532#endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */
533
534#ifdef __cplusplus
535}
536#endif
537
538#endif /* MBEDTLS_MD_H */
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:50
@ MBEDTLS_MD_SHA512
Definition md.h:58
@ MBEDTLS_MD_MD5
Definition md.h:52
@ MBEDTLS_MD_RIPEMD160
Definition md.h:53
@ MBEDTLS_MD_SHA384
Definition md.h:57
@ MBEDTLS_MD_SHA3_384
Definition md.h:61
@ MBEDTLS_MD_NONE
Definition md.h:51
@ MBEDTLS_MD_SHA3_512
Definition md.h:62
@ MBEDTLS_MD_SHA256
Definition md.h:56
@ MBEDTLS_MD_SHA224
Definition md.h:55
@ MBEDTLS_MD_SHA1
Definition md.h:54
@ MBEDTLS_MD_SHA3_224
Definition md.h:59
@ MBEDTLS_MD_SHA3_256
Definition md.h:60
int mbedtls_md_starts(mbedtls_md_context_t *ctx)
This function starts a message-digest computation.
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...
struct mbedtls_md_info_t mbedtls_md_info_t
Definition md.h:95
int mbedtls_md_clone(mbedtls_md_context_t *dst, const mbedtls_md_context_t *src)
This function clones the state of a 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_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.
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.
static unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type)
This function gives the message-digest size associated to message-digest type.
Definition md.h:237
mbedtls_md_engine_t
Definition md.h:102
@ MBEDTLS_MD_ENGINE_LEGACY
Definition md.h:103
@ MBEDTLS_MD_ENGINE_PSA
Definition md.h:104
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.
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,...
Common and shared functions used by multiple modules in the Mbed TLS library.
#define MBEDTLS_CHECK_RETURN_TYPICAL
Build-time configuration info.