Mbed TLS v4.0.0
 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 
21 #include "mbedtls/platform_util.h"
22 
24 #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080
25 
26 #define MBEDTLS_ERR_MD_BAD_INPUT_DATA PSA_ERROR_INVALID_ARGUMENT
27 
28 #define MBEDTLS_ERR_MD_ALLOC_FAILED PSA_ERROR_INSUFFICIENT_MEMORY
29 
30 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
31 
32 #define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200
33 #endif
34 
35 #ifdef __cplusplus
36 extern "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. */
50 typedef enum {
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  */
102 typedef enum {
106 
110 typedef struct mbedtls_md_context_t {
112  const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
113 
114 #if defined(MBEDTLS_MD_SOME_PSA)
115 
116  mbedtls_md_engine_t MBEDTLS_PRIVATE(engine);
117 #endif
118 
120  void *MBEDTLS_PRIVATE(md_ctx);
121 
122 #if defined(MBEDTLS_MD_C)
123 
124  void *MBEDTLS_PRIVATE(hmac_ctx);
125 #endif
127 
138 
148 
163 
164 
188 int 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 
226 unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
227 
237 static inline unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type)
238 {
240 }
241 
252 
268 
286 int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
287 
307 int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
308 
328 int 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 
348 int mbedtls_md_hmac_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info);
349 
361 const int *mbedtls_md_list(void);
362 
372 const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
373 
383 const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
384 
395 const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
396  const mbedtls_md_context_t *ctx);
397 
398 #if defined(MBEDTLS_FS_IO)
399 
417 int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
418  unsigned char *output);
419 #endif /* MBEDTLS_FS_IO */
420 
440 int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
441  size_t keylen);
442 
463 int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
464  size_t ilen);
465 
485 int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
486 
503 int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
504 
528 int 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 */
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
mbedtls_md_engine_t
Definition: md.h:102
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.
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_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(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_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:237
struct mbedtls_md_context_t mbedtls_md_context_t
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...
struct mbedtls_md_info_t mbedtls_md_info_t
Definition: md.h:95
Build-time configuration info.
mbedtls_md_type_t
Supported message digests.
Definition: md.h:50
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.