Mbed TLS v3.6.3
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 
20 #include "mbedtls/build_info.h"
21 #include "mbedtls/platform_util.h"
22 
24 #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080
25 
26 #define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100
27 
28 #define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180
29 
30 #define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
44 /* Note: these are aligned with the definitions of PSA_ALG_ macros for hashes,
45  * in order to enable an efficient implementation of conversion functions.
46  * This is tested by md_to_from_psa() in test_suite_md. */
47 typedef enum {
61 
62 /* Note: this should always be >= PSA_HASH_MAX_SIZE
63  * in all builds with both CRYPTO_C and MD_LIGHT.
64  *
65  * This is to make things easier for modules such as TLS that may define a
66  * buffer size using MD_MAX_SIZE in a part of the code that's common to PSA
67  * and legacy, then assume the buffer's size is PSA_HASH_MAX_SIZE in another
68  * part of the code based on PSA.
69  */
70 #if defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA3_512)
71 #define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
72 #elif defined(MBEDTLS_MD_CAN_SHA384) || defined(MBEDTLS_MD_CAN_SHA3_384)
73 #define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */
74 #elif defined(MBEDTLS_MD_CAN_SHA256) || defined(MBEDTLS_MD_CAN_SHA3_256)
75 #define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */
76 #elif defined(MBEDTLS_MD_CAN_SHA224) || defined(MBEDTLS_MD_CAN_SHA3_224)
77 #define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */
78 #else
79 #define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160
80  or smaller (MD5 and earlier) */
81 #endif
82 
83 #if defined(MBEDTLS_MD_CAN_SHA3_224)
84 #define MBEDTLS_MD_MAX_BLOCK_SIZE 144 /* the longest known is SHA3-224 */
85 #elif defined(MBEDTLS_MD_CAN_SHA3_256)
86 #define MBEDTLS_MD_MAX_BLOCK_SIZE 136
87 #elif defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA384)
88 #define MBEDTLS_MD_MAX_BLOCK_SIZE 128
89 #elif defined(MBEDTLS_MD_CAN_SHA3_384)
90 #define MBEDTLS_MD_MAX_BLOCK_SIZE 104
91 #elif defined(MBEDTLS_MD_CAN_SHA3_512)
92 #define MBEDTLS_MD_MAX_BLOCK_SIZE 72
93 #else
94 #define MBEDTLS_MD_MAX_BLOCK_SIZE 64
95 #endif
96 
106 /* Defined internally in library/md_wrap.h. */
108 
114 typedef enum {
118 
122 typedef struct mbedtls_md_context_t {
125 
126 #if defined(MBEDTLS_MD_SOME_PSA)
127 
129 #endif
130 
132  void *MBEDTLS_PRIVATE(md_ctx);
133 
134 #if defined(MBEDTLS_MD_C)
135 
136  void *MBEDTLS_PRIVATE(hmac_ctx);
137 #endif
139 
150 
160 
175 
176 
197 int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
198 
224  const mbedtls_md_context_t *src);
225 
235 unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
236 
246 static inline unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type)
247 {
249 }
250 
261 
277 
295 int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
296 
316 int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
317 
337 int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
338  unsigned char *output);
339 
351 const int *mbedtls_md_list(void);
352 
362 const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
363 
373 const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
374 
386  const mbedtls_md_context_t *ctx);
387 
388 #if defined(MBEDTLS_FS_IO)
389 
407 int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
408  unsigned char *output);
409 #endif /* MBEDTLS_FS_IO */
410 
430 int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
431  size_t keylen);
432 
453 int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
454  size_t ilen);
455 
475 int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
476 
494 
518 int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
519  const unsigned char *input, size_t ilen,
520  unsigned char *output);
521 
522 #ifdef __cplusplus
523 }
524 #endif
525 
526 #endif /* MBEDTLS_MD_H */
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...
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_starts(mbedtls_md_context_t *ctx)
This function starts a message-digest computation.
#define MBEDTLS_CHECK_RETURN_TYPICAL
Definition: platform_util.h:97
const mbedtls_md_info_t * mbedtls_md_info_from_ctx(const mbedtls_md_context_t *ctx)
This function returns the message-digest information from the given context.
mbedtls_md_engine_t
Definition: md.h:114
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.
void mbedtls_md_free(mbedtls_md_context_t *ctx)
This function clears the internal structure of ctx and frees any embedded internal structure...
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_clone(mbedtls_md_context_t *dst, const mbedtls_md_context_t *src)
This function clones the state of a message-digest context.
#define MBEDTLS_PRIVATE(member)
Common and shared functions used by multiple modules in the Mbed TLS library.
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...
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_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...
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(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...
Macro wrapper for struct's members.
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.
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:246
const char * mbedtls_md_get_name(const mbedtls_md_info_t *md_info)
This function returns the name of the message digest for the message-digest information structure giv...
Build-time configuration info.
struct mbedtls_md_context_t mbedtls_md_context_t
const int * mbedtls_md_list(void)
This function returns the list of digests supported by the generic digest module. ...
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...
int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path, unsigned char *output)
This function calculates the message-digest checksum result of the contents of the provided file...
struct mbedtls_md_info_t mbedtls_md_info_t
Definition: md.h:107
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.
mbedtls_md_type_t
Supported message digests.
Definition: md.h:47
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...
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.