xref: /optee_os/lib/libmbedtls/mbedtls/include/mbedtls/md.h (revision b0563631928755fe864b97785160fb3088e9efdc)
1817466cbSJens Wiklander /**
2817466cbSJens Wiklander  * \file md.h
3817466cbSJens Wiklander  *
432b31808SJens Wiklander  * \brief   This file contains the generic functions for message-digest
532b31808SJens Wiklander  *          (hashing) and HMAC.
6817466cbSJens Wiklander  *
7817466cbSJens Wiklander  * \author Adriaan de Jong <dejong@fox-it.com>
83d3b0591SJens Wiklander  */
93d3b0591SJens Wiklander /*
107901324dSJerome Forissier  *  Copyright The Mbed TLS Contributors
11*b0563631STom Van Eyck  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
12817466cbSJens Wiklander  */
133d3b0591SJens Wiklander 
14817466cbSJens Wiklander #ifndef MBEDTLS_MD_H
15817466cbSJens Wiklander #define MBEDTLS_MD_H
1632b31808SJens Wiklander #include "mbedtls/private_access.h"
17817466cbSJens Wiklander 
18817466cbSJens Wiklander #include <stddef.h>
19817466cbSJens Wiklander 
2032b31808SJens Wiklander #include "mbedtls/build_info.h"
21039e02dfSJerome Forissier #include "mbedtls/platform_util.h"
223d3b0591SJens Wiklander 
23039e02dfSJerome Forissier /** The selected feature is not available. */
24039e02dfSJerome Forissier #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE                -0x5080
25039e02dfSJerome Forissier /** Bad input parameters to function. */
26039e02dfSJerome Forissier #define MBEDTLS_ERR_MD_BAD_INPUT_DATA                     -0x5100
27039e02dfSJerome Forissier /** Failed to allocate memory. */
28039e02dfSJerome Forissier #define MBEDTLS_ERR_MD_ALLOC_FAILED                       -0x5180
29039e02dfSJerome Forissier /** Opening or reading of file failed. */
30039e02dfSJerome Forissier #define MBEDTLS_ERR_MD_FILE_IO_ERROR                      -0x5200
31817466cbSJens Wiklander 
32817466cbSJens Wiklander #ifdef __cplusplus
33817466cbSJens Wiklander extern "C" {
34817466cbSJens Wiklander #endif
35817466cbSJens Wiklander 
363d3b0591SJens Wiklander /**
373d3b0591SJens Wiklander  * \brief     Supported message digests.
383d3b0591SJens Wiklander  *
3932b31808SJens Wiklander  * \warning   MD5 and SHA-1 are considered weak message digests and
403d3b0591SJens Wiklander  *            their use constitutes a security risk. We recommend considering
413d3b0591SJens Wiklander  *            stronger message digests instead.
423d3b0591SJens Wiklander  *
433d3b0591SJens Wiklander  */
44*b0563631STom Van Eyck /* Note: these are aligned with the definitions of PSA_ALG_ macros for hashes,
45*b0563631STom Van Eyck  * in order to enable an efficient implementation of conversion functions.
46*b0563631STom Van Eyck  * This is tested by md_to_from_psa() in test_suite_md. */
47817466cbSJens Wiklander typedef enum {
483d3b0591SJens Wiklander     MBEDTLS_MD_NONE=0,    /**< None. */
49*b0563631STom Van Eyck     MBEDTLS_MD_MD5=0x03,       /**< The MD5 message digest. */
50*b0563631STom Van Eyck     MBEDTLS_MD_RIPEMD160=0x04, /**< The RIPEMD-160 message digest. */
51*b0563631STom Van Eyck     MBEDTLS_MD_SHA1=0x05,      /**< The SHA-1 message digest. */
52*b0563631STom Van Eyck     MBEDTLS_MD_SHA224=0x08,    /**< The SHA-224 message digest. */
53*b0563631STom Van Eyck     MBEDTLS_MD_SHA256=0x09,    /**< The SHA-256 message digest. */
54*b0563631STom Van Eyck     MBEDTLS_MD_SHA384=0x0a,    /**< The SHA-384 message digest. */
55*b0563631STom Van Eyck     MBEDTLS_MD_SHA512=0x0b,    /**< The SHA-512 message digest. */
56*b0563631STom Van Eyck     MBEDTLS_MD_SHA3_224=0x10,  /**< The SHA3-224 message digest. */
57*b0563631STom Van Eyck     MBEDTLS_MD_SHA3_256=0x11,  /**< The SHA3-256 message digest. */
58*b0563631STom Van Eyck     MBEDTLS_MD_SHA3_384=0x12,  /**< The SHA3-384 message digest. */
59*b0563631STom Van Eyck     MBEDTLS_MD_SHA3_512=0x13,  /**< The SHA3-512 message digest. */
60817466cbSJens Wiklander } mbedtls_md_type_t;
61817466cbSJens Wiklander 
62*b0563631STom Van Eyck /* Note: this should always be >= PSA_HASH_MAX_SIZE
63*b0563631STom Van Eyck  * in all builds with both CRYPTO_C and MD_LIGHT.
64*b0563631STom Van Eyck  *
65*b0563631STom Van Eyck  * This is to make things easier for modules such as TLS that may define a
66*b0563631STom Van Eyck  * buffer size using MD_MAX_SIZE in a part of the code that's common to PSA
67*b0563631STom Van Eyck  * and legacy, then assume the buffer's size is PSA_HASH_MAX_SIZE in another
68*b0563631STom Van Eyck  * part of the code based on PSA.
69*b0563631STom Van Eyck  */
70*b0563631STom Van Eyck #if defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA3_512)
71817466cbSJens Wiklander #define MBEDTLS_MD_MAX_SIZE         64  /* longest known is SHA512 */
72*b0563631STom Van Eyck #elif defined(MBEDTLS_MD_CAN_SHA384) || defined(MBEDTLS_MD_CAN_SHA3_384)
7332b31808SJens Wiklander #define MBEDTLS_MD_MAX_SIZE         48  /* longest known is SHA384 */
74*b0563631STom Van Eyck #elif defined(MBEDTLS_MD_CAN_SHA256) || defined(MBEDTLS_MD_CAN_SHA3_256)
7532b31808SJens Wiklander #define MBEDTLS_MD_MAX_SIZE         32  /* longest known is SHA256 */
76*b0563631STom Van Eyck #elif defined(MBEDTLS_MD_CAN_SHA224) || defined(MBEDTLS_MD_CAN_SHA3_224)
7732b31808SJens Wiklander #define MBEDTLS_MD_MAX_SIZE         28  /* longest known is SHA224 */
78817466cbSJens Wiklander #else
7932b31808SJens Wiklander #define MBEDTLS_MD_MAX_SIZE         20  /* longest known is SHA1 or RIPE MD-160
8032b31808SJens Wiklander                                            or smaller (MD5 and earlier) */
81817466cbSJens Wiklander #endif
82817466cbSJens Wiklander 
83*b0563631STom Van Eyck #if defined(MBEDTLS_MD_CAN_SHA3_224)
84*b0563631STom Van Eyck #define MBEDTLS_MD_MAX_BLOCK_SIZE         144 /* the longest known is SHA3-224 */
85*b0563631STom Van Eyck #elif defined(MBEDTLS_MD_CAN_SHA3_256)
86*b0563631STom Van Eyck #define MBEDTLS_MD_MAX_BLOCK_SIZE         136
87*b0563631STom Van Eyck #elif defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA384)
8811fa71b9SJerome Forissier #define MBEDTLS_MD_MAX_BLOCK_SIZE         128
89*b0563631STom Van Eyck #elif defined(MBEDTLS_MD_CAN_SHA3_384)
90*b0563631STom Van Eyck #define MBEDTLS_MD_MAX_BLOCK_SIZE         104
91*b0563631STom Van Eyck #elif defined(MBEDTLS_MD_CAN_SHA3_512)
92*b0563631STom Van Eyck #define MBEDTLS_MD_MAX_BLOCK_SIZE         72
9311fa71b9SJerome Forissier #else
9411fa71b9SJerome Forissier #define MBEDTLS_MD_MAX_BLOCK_SIZE         64
9511fa71b9SJerome Forissier #endif
9611fa71b9SJerome Forissier 
97817466cbSJens Wiklander /**
9832b31808SJens Wiklander  * Opaque struct.
9932b31808SJens Wiklander  *
10032b31808SJens Wiklander  * Constructed using either #mbedtls_md_info_from_string or
10132b31808SJens Wiklander  * #mbedtls_md_info_from_type.
10232b31808SJens Wiklander  *
10332b31808SJens Wiklander  * Fields can be accessed with #mbedtls_md_get_size,
10432b31808SJens Wiklander  * #mbedtls_md_get_type and #mbedtls_md_get_name.
105817466cbSJens Wiklander  */
10632b31808SJens Wiklander /* Defined internally in library/md_wrap.h. */
107817466cbSJens Wiklander typedef struct mbedtls_md_info_t mbedtls_md_info_t;
108817466cbSJens Wiklander 
109817466cbSJens Wiklander /**
11032b31808SJens Wiklander  * Used internally to indicate whether a context uses legacy or PSA.
11132b31808SJens Wiklander  *
11232b31808SJens Wiklander  * Internal use only.
11332b31808SJens Wiklander  */
11432b31808SJens Wiklander typedef enum {
11532b31808SJens Wiklander     MBEDTLS_MD_ENGINE_LEGACY = 0,
11632b31808SJens Wiklander     MBEDTLS_MD_ENGINE_PSA,
11732b31808SJens Wiklander } mbedtls_md_engine_t;
11832b31808SJens Wiklander 
11932b31808SJens Wiklander /**
1203d3b0591SJens Wiklander  * The generic message-digest context.
121817466cbSJens Wiklander  */
12232b31808SJens Wiklander typedef struct mbedtls_md_context_t {
1233d3b0591SJens Wiklander     /** Information about the associated message digest. */
12432b31808SJens Wiklander     const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
125817466cbSJens Wiklander 
12632b31808SJens Wiklander #if defined(MBEDTLS_MD_SOME_PSA)
12732b31808SJens Wiklander     /** Are hash operations dispatched to PSA or legacy? */
12832b31808SJens Wiklander     mbedtls_md_engine_t MBEDTLS_PRIVATE(engine);
12932b31808SJens Wiklander #endif
130817466cbSJens Wiklander 
13132b31808SJens Wiklander     /** The digest-specific context (legacy) or the PSA operation. */
13232b31808SJens Wiklander     void *MBEDTLS_PRIVATE(md_ctx);
13332b31808SJens Wiklander 
13432b31808SJens Wiklander #if defined(MBEDTLS_MD_C)
1353d3b0591SJens Wiklander     /** The HMAC part of the context. */
13632b31808SJens Wiklander     void *MBEDTLS_PRIVATE(hmac_ctx);
13732b31808SJens Wiklander #endif
138817466cbSJens Wiklander } mbedtls_md_context_t;
139817466cbSJens Wiklander 
140817466cbSJens Wiklander /**
1413d3b0591SJens Wiklander  * \brief           This function returns the message-digest information
1423d3b0591SJens Wiklander  *                  associated with the given digest type.
143817466cbSJens Wiklander  *
1443d3b0591SJens Wiklander  * \param md_type   The type of digest to search for.
145817466cbSJens Wiklander  *
1463d3b0591SJens Wiklander  * \return          The message-digest information associated with \p md_type.
1473d3b0591SJens Wiklander  * \return          NULL if the associated message-digest information is not found.
148817466cbSJens Wiklander  */
149817466cbSJens Wiklander const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
150817466cbSJens Wiklander 
151817466cbSJens Wiklander /**
1523d3b0591SJens Wiklander  * \brief           This function initializes a message-digest context without
1533d3b0591SJens Wiklander  *                  binding it to a particular message-digest algorithm.
1543d3b0591SJens Wiklander  *
1553d3b0591SJens Wiklander  *                  This function should always be called first. It prepares the
1563d3b0591SJens Wiklander  *                  context for mbedtls_md_setup() for binding it to a
1573d3b0591SJens Wiklander  *                  message-digest algorithm.
158817466cbSJens Wiklander  */
159817466cbSJens Wiklander void mbedtls_md_init(mbedtls_md_context_t *ctx);
160817466cbSJens Wiklander 
161817466cbSJens Wiklander /**
1623d3b0591SJens Wiklander  * \brief           This function clears the internal structure of \p ctx and
1633d3b0591SJens Wiklander  *                  frees any embedded internal structure, but does not free
1643d3b0591SJens Wiklander  *                  \p ctx itself.
1653d3b0591SJens Wiklander  *
1663d3b0591SJens Wiklander  *                  If you have called mbedtls_md_setup() on \p ctx, you must
1673d3b0591SJens Wiklander  *                  call mbedtls_md_free() when you are no longer using the
1683d3b0591SJens Wiklander  *                  context.
1693d3b0591SJens Wiklander  *                  Calling this function if you have previously
1703d3b0591SJens Wiklander  *                  called mbedtls_md_init() and nothing else is optional.
1713d3b0591SJens Wiklander  *                  You must not call this function if you have not called
1723d3b0591SJens Wiklander  *                  mbedtls_md_init().
173817466cbSJens Wiklander  */
174817466cbSJens Wiklander void mbedtls_md_free(mbedtls_md_context_t *ctx);
175817466cbSJens Wiklander 
176817466cbSJens Wiklander 
177817466cbSJens Wiklander /**
1783d3b0591SJens Wiklander  * \brief           This function selects the message digest algorithm to use,
1793d3b0591SJens Wiklander  *                  and allocates internal structures.
180817466cbSJens Wiklander  *
1813d3b0591SJens Wiklander  *                  It should be called after mbedtls_md_init() or
1823d3b0591SJens Wiklander  *                  mbedtls_md_free(). Makes it necessary to call
1833d3b0591SJens Wiklander  *                  mbedtls_md_free() later.
184817466cbSJens Wiklander  *
1853d3b0591SJens Wiklander  * \param ctx       The context to set up.
1863d3b0591SJens Wiklander  * \param md_info   The information structure of the message-digest algorithm
1873d3b0591SJens Wiklander  *                  to use.
1883d3b0591SJens Wiklander  * \param hmac      Defines if HMAC is used. 0: HMAC is not used (saves some memory),
1893d3b0591SJens Wiklander  *                  or non-zero: HMAC is used with this context.
1903d3b0591SJens Wiklander  *
1913d3b0591SJens Wiklander  * \return          \c 0 on success.
1923d3b0591SJens Wiklander  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
1933d3b0591SJens Wiklander  *                  failure.
1943d3b0591SJens Wiklander  * \return          #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
195817466cbSJens Wiklander  */
196039e02dfSJerome Forissier MBEDTLS_CHECK_RETURN_TYPICAL
197817466cbSJens Wiklander int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
198817466cbSJens Wiklander 
199817466cbSJens Wiklander /**
20032b31808SJens Wiklander  * \brief           This function clones the state of a message-digest
2013d3b0591SJens Wiklander  *                  context.
202817466cbSJens Wiklander  *
2033d3b0591SJens Wiklander  * \note            You must call mbedtls_md_setup() on \c dst before calling
2043d3b0591SJens Wiklander  *                  this function.
205817466cbSJens Wiklander  *
2063d3b0591SJens Wiklander  * \note            The two contexts must have the same type,
2073d3b0591SJens Wiklander  *                  for example, both are SHA-256.
208817466cbSJens Wiklander  *
2093d3b0591SJens Wiklander  * \warning         This function clones the message-digest state, not the
2103d3b0591SJens Wiklander  *                  HMAC state.
211817466cbSJens Wiklander  *
2123d3b0591SJens Wiklander  * \param dst       The destination context.
2133d3b0591SJens Wiklander  * \param src       The context to be cloned.
2143d3b0591SJens Wiklander  *
2153d3b0591SJens Wiklander  * \return          \c 0 on success.
2163d3b0591SJens Wiklander  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
21732b31808SJens Wiklander  * \return          #MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE if both contexts are
21832b31808SJens Wiklander  *                  not using the same engine. This can be avoided by moving
21932b31808SJens Wiklander  *                  the call to psa_crypto_init() before the first call to
22032b31808SJens Wiklander  *                  mbedtls_md_setup().
221817466cbSJens Wiklander  */
222039e02dfSJerome Forissier MBEDTLS_CHECK_RETURN_TYPICAL
223817466cbSJens Wiklander int mbedtls_md_clone(mbedtls_md_context_t *dst,
224817466cbSJens Wiklander                      const mbedtls_md_context_t *src);
225817466cbSJens Wiklander 
226817466cbSJens Wiklander /**
2273d3b0591SJens Wiklander  * \brief           This function extracts the message-digest size from the
2283d3b0591SJens Wiklander  *                  message-digest information structure.
229817466cbSJens Wiklander  *
2303d3b0591SJens Wiklander  * \param md_info   The information structure of the message-digest algorithm
2313d3b0591SJens Wiklander  *                  to use.
232817466cbSJens Wiklander  *
2333d3b0591SJens Wiklander  * \return          The size of the message-digest output in Bytes.
234817466cbSJens Wiklander  */
235817466cbSJens Wiklander unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
236817466cbSJens Wiklander 
237817466cbSJens Wiklander /**
238*b0563631STom Van Eyck  * \brief           This function gives the message-digest size associated to
239*b0563631STom Van Eyck  *                  message-digest type.
240*b0563631STom Van Eyck  *
241*b0563631STom Van Eyck  * \param md_type   The message-digest type.
242*b0563631STom Van Eyck  *
243*b0563631STom Van Eyck  * \return          The size of the message-digest output in Bytes,
244*b0563631STom Van Eyck  *                  or 0 if the message-digest type is not known.
245*b0563631STom Van Eyck  */
mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type)246*b0563631STom Van Eyck static inline unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type)
247*b0563631STom Van Eyck {
248*b0563631STom Van Eyck     return mbedtls_md_get_size(mbedtls_md_info_from_type(md_type));
249*b0563631STom Van Eyck }
250*b0563631STom Van Eyck 
251*b0563631STom Van Eyck /**
2523d3b0591SJens Wiklander  * \brief           This function extracts the message-digest type from the
2533d3b0591SJens Wiklander  *                  message-digest information structure.
254817466cbSJens Wiklander  *
2553d3b0591SJens Wiklander  * \param md_info   The information structure of the message-digest algorithm
2563d3b0591SJens Wiklander  *                  to use.
257817466cbSJens Wiklander  *
2583d3b0591SJens Wiklander  * \return          The type of the message digest.
259817466cbSJens Wiklander  */
260817466cbSJens Wiklander mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
261817466cbSJens Wiklander 
262817466cbSJens Wiklander /**
2633d3b0591SJens Wiklander  * \brief           This function starts a message-digest computation.
264817466cbSJens Wiklander  *
2653d3b0591SJens Wiklander  *                  You must call this function after setting up the context
2663d3b0591SJens Wiklander  *                  with mbedtls_md_setup(), and before passing data with
2673d3b0591SJens Wiklander  *                  mbedtls_md_update().
268817466cbSJens Wiklander  *
2693d3b0591SJens Wiklander  * \param ctx       The generic message-digest context.
2703d3b0591SJens Wiklander  *
2713d3b0591SJens Wiklander  * \return          \c 0 on success.
2723d3b0591SJens Wiklander  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
2733d3b0591SJens Wiklander  *                  failure.
274817466cbSJens Wiklander  */
275039e02dfSJerome Forissier MBEDTLS_CHECK_RETURN_TYPICAL
276817466cbSJens Wiklander int mbedtls_md_starts(mbedtls_md_context_t *ctx);
277817466cbSJens Wiklander 
278817466cbSJens Wiklander /**
2793d3b0591SJens Wiklander  * \brief           This function feeds an input buffer into an ongoing
2803d3b0591SJens Wiklander  *                  message-digest computation.
281817466cbSJens Wiklander  *
2823d3b0591SJens Wiklander  *                  You must call mbedtls_md_starts() before calling this
2833d3b0591SJens Wiklander  *                  function. You may call this function multiple times.
2843d3b0591SJens Wiklander  *                  Afterwards, call mbedtls_md_finish().
285817466cbSJens Wiklander  *
2863d3b0591SJens Wiklander  * \param ctx       The generic message-digest context.
2873d3b0591SJens Wiklander  * \param input     The buffer holding the input data.
2883d3b0591SJens Wiklander  * \param ilen      The length of the input data.
2893d3b0591SJens Wiklander  *
2903d3b0591SJens Wiklander  * \return          \c 0 on success.
2913d3b0591SJens Wiklander  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
2923d3b0591SJens Wiklander  *                  failure.
293817466cbSJens Wiklander  */
294039e02dfSJerome Forissier MBEDTLS_CHECK_RETURN_TYPICAL
295817466cbSJens Wiklander int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
296817466cbSJens Wiklander 
297817466cbSJens Wiklander /**
2983d3b0591SJens Wiklander  * \brief           This function finishes the digest operation,
2993d3b0591SJens Wiklander  *                  and writes the result to the output buffer.
300817466cbSJens Wiklander  *
3013d3b0591SJens Wiklander  *                  Call this function after a call to mbedtls_md_starts(),
3023d3b0591SJens Wiklander  *                  followed by any number of calls to mbedtls_md_update().
3033d3b0591SJens Wiklander  *                  Afterwards, you may either clear the context with
3043d3b0591SJens Wiklander  *                  mbedtls_md_free(), or call mbedtls_md_starts() to reuse
3053d3b0591SJens Wiklander  *                  the context for another digest operation with the same
3063d3b0591SJens Wiklander  *                  algorithm.
307817466cbSJens Wiklander  *
3083d3b0591SJens Wiklander  * \param ctx       The generic message-digest context.
3093d3b0591SJens Wiklander  * \param output    The buffer for the generic message-digest checksum result.
3103d3b0591SJens Wiklander  *
3113d3b0591SJens Wiklander  * \return          \c 0 on success.
3123d3b0591SJens Wiklander  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
3133d3b0591SJens Wiklander  *                  failure.
314817466cbSJens Wiklander  */
315039e02dfSJerome Forissier MBEDTLS_CHECK_RETURN_TYPICAL
316817466cbSJens Wiklander int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
317817466cbSJens Wiklander 
318817466cbSJens Wiklander /**
3193d3b0591SJens Wiklander  * \brief          This function calculates the message-digest of a buffer,
3203d3b0591SJens Wiklander  *                 with respect to a configurable message-digest algorithm
3213d3b0591SJens Wiklander  *                 in a single call.
322817466cbSJens Wiklander  *
3233d3b0591SJens Wiklander  *                 The result is calculated as
3243d3b0591SJens Wiklander  *                 Output = message_digest(input buffer).
325817466cbSJens Wiklander  *
3263d3b0591SJens Wiklander  * \param md_info  The information structure of the message-digest algorithm
3273d3b0591SJens Wiklander  *                 to use.
3283d3b0591SJens Wiklander  * \param input    The buffer holding the data.
3293d3b0591SJens Wiklander  * \param ilen     The length of the input data.
3303d3b0591SJens Wiklander  * \param output   The generic message-digest checksum result.
3313d3b0591SJens Wiklander  *
3323d3b0591SJens Wiklander  * \return         \c 0 on success.
3333d3b0591SJens Wiklander  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
3343d3b0591SJens Wiklander  *                 failure.
335817466cbSJens Wiklander  */
336039e02dfSJerome Forissier MBEDTLS_CHECK_RETURN_TYPICAL
337817466cbSJens Wiklander int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
338817466cbSJens Wiklander                unsigned char *output);
339817466cbSJens Wiklander 
34032b31808SJens Wiklander /**
34132b31808SJens Wiklander  * \brief           This function returns the list of digests supported by the
34232b31808SJens Wiklander  *                  generic digest module.
34332b31808SJens Wiklander  *
34432b31808SJens Wiklander  * \note            The list starts with the strongest available hashes.
34532b31808SJens Wiklander  *
34632b31808SJens Wiklander  * \return          A statically allocated array of digests. Each element
34732b31808SJens Wiklander  *                  in the returned list is an integer belonging to the
34832b31808SJens Wiklander  *                  message-digest enumeration #mbedtls_md_type_t.
34932b31808SJens Wiklander  *                  The last entry is 0.
35032b31808SJens Wiklander  */
35132b31808SJens Wiklander const int *mbedtls_md_list(void);
35232b31808SJens Wiklander 
35332b31808SJens Wiklander /**
35432b31808SJens Wiklander  * \brief           This function returns the message-digest information
35532b31808SJens Wiklander  *                  associated with the given digest name.
35632b31808SJens Wiklander  *
35732b31808SJens Wiklander  * \param md_name   The name of the digest to search for.
35832b31808SJens Wiklander  *
35932b31808SJens Wiklander  * \return          The message-digest information associated with \p md_name.
36032b31808SJens Wiklander  * \return          NULL if the associated message-digest information is not found.
36132b31808SJens Wiklander  */
36232b31808SJens Wiklander const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
36332b31808SJens Wiklander 
36432b31808SJens Wiklander /**
365*b0563631STom Van Eyck  * \brief           This function returns the name of the message digest for
366*b0563631STom Van Eyck  *                  the message-digest information structure given.
36732b31808SJens Wiklander  *
36832b31808SJens Wiklander  * \param md_info   The information structure of the message-digest algorithm
36932b31808SJens Wiklander  *                  to use.
37032b31808SJens Wiklander  *
37132b31808SJens Wiklander  * \return          The name of the message digest.
37232b31808SJens Wiklander  */
37332b31808SJens Wiklander const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
37432b31808SJens Wiklander 
37532b31808SJens Wiklander /**
37632b31808SJens Wiklander  * \brief           This function returns the message-digest information
37732b31808SJens Wiklander  *                  from the given context.
37832b31808SJens Wiklander  *
37932b31808SJens Wiklander  * \param ctx       The context from which to extract the information.
38032b31808SJens Wiklander  *                  This must be initialized (or \c NULL).
38132b31808SJens Wiklander  *
38232b31808SJens Wiklander  * \return          The message-digest information associated with \p ctx.
38332b31808SJens Wiklander  * \return          \c NULL if \p ctx is \c NULL.
38432b31808SJens Wiklander  */
38532b31808SJens Wiklander const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
38632b31808SJens Wiklander     const mbedtls_md_context_t *ctx);
38732b31808SJens Wiklander 
388817466cbSJens Wiklander #if defined(MBEDTLS_FS_IO)
389817466cbSJens Wiklander /**
3903d3b0591SJens Wiklander  * \brief          This function calculates the message-digest checksum
3913d3b0591SJens Wiklander  *                 result of the contents of the provided file.
392817466cbSJens Wiklander  *
3933d3b0591SJens Wiklander  *                 The result is calculated as
3943d3b0591SJens Wiklander  *                 Output = message_digest(file contents).
395817466cbSJens Wiklander  *
3963d3b0591SJens Wiklander  * \param md_info  The information structure of the message-digest algorithm
3973d3b0591SJens Wiklander  *                 to use.
3983d3b0591SJens Wiklander  * \param path     The input file name.
3993d3b0591SJens Wiklander  * \param output   The generic message-digest checksum result.
4003d3b0591SJens Wiklander  *
4013d3b0591SJens Wiklander  * \return         \c 0 on success.
4023d3b0591SJens Wiklander  * \return         #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
4033d3b0591SJens Wiklander  *                 the file pointed by \p path.
4043d3b0591SJens Wiklander  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
405817466cbSJens Wiklander  */
406039e02dfSJerome Forissier MBEDTLS_CHECK_RETURN_TYPICAL
407817466cbSJens Wiklander int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
408817466cbSJens Wiklander                     unsigned char *output);
409817466cbSJens Wiklander #endif /* MBEDTLS_FS_IO */
410817466cbSJens Wiklander 
411817466cbSJens Wiklander /**
4123d3b0591SJens Wiklander  * \brief           This function sets the HMAC key and prepares to
4133d3b0591SJens Wiklander  *                  authenticate a new message.
414817466cbSJens Wiklander  *
4153d3b0591SJens Wiklander  *                  Call this function after mbedtls_md_setup(), to use
4163d3b0591SJens Wiklander  *                  the MD context for an HMAC calculation, then call
4173d3b0591SJens Wiklander  *                  mbedtls_md_hmac_update() to provide the input data, and
4183d3b0591SJens Wiklander  *                  mbedtls_md_hmac_finish() to get the HMAC value.
419817466cbSJens Wiklander  *
4203d3b0591SJens Wiklander  * \param ctx       The message digest context containing an embedded HMAC
4213d3b0591SJens Wiklander  *                  context.
4223d3b0591SJens Wiklander  * \param key       The HMAC secret key.
4233d3b0591SJens Wiklander  * \param keylen    The length of the HMAC key in Bytes.
4243d3b0591SJens Wiklander  *
4253d3b0591SJens Wiklander  * \return          \c 0 on success.
4263d3b0591SJens Wiklander  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
4273d3b0591SJens Wiklander  *                  failure.
428817466cbSJens Wiklander  */
429039e02dfSJerome Forissier MBEDTLS_CHECK_RETURN_TYPICAL
430817466cbSJens Wiklander int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
431817466cbSJens Wiklander                            size_t keylen);
432817466cbSJens Wiklander 
433817466cbSJens Wiklander /**
4343d3b0591SJens Wiklander  * \brief           This function feeds an input buffer into an ongoing HMAC
4353d3b0591SJens Wiklander  *                  computation.
436817466cbSJens Wiklander  *
4373d3b0591SJens Wiklander  *                  Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
4383d3b0591SJens Wiklander  *                  before calling this function.
4393d3b0591SJens Wiklander  *                  You may call this function multiple times to pass the
4403d3b0591SJens Wiklander  *                  input piecewise.
4413d3b0591SJens Wiklander  *                  Afterwards, call mbedtls_md_hmac_finish().
442817466cbSJens Wiklander  *
4433d3b0591SJens Wiklander  * \param ctx       The message digest context containing an embedded HMAC
4443d3b0591SJens Wiklander  *                  context.
4453d3b0591SJens Wiklander  * \param input     The buffer holding the input data.
4463d3b0591SJens Wiklander  * \param ilen      The length of the input data.
4473d3b0591SJens Wiklander  *
4483d3b0591SJens Wiklander  * \return          \c 0 on success.
4493d3b0591SJens Wiklander  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
4503d3b0591SJens Wiklander  *                  failure.
451817466cbSJens Wiklander  */
452039e02dfSJerome Forissier MBEDTLS_CHECK_RETURN_TYPICAL
453817466cbSJens Wiklander int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
454817466cbSJens Wiklander                            size_t ilen);
455817466cbSJens Wiklander 
456817466cbSJens Wiklander /**
4573d3b0591SJens Wiklander  * \brief           This function finishes the HMAC operation, and writes
4583d3b0591SJens Wiklander  *                  the result to the output buffer.
459817466cbSJens Wiklander  *
4603d3b0591SJens Wiklander  *                  Call this function after mbedtls_md_hmac_starts() and
4613d3b0591SJens Wiklander  *                  mbedtls_md_hmac_update() to get the HMAC value. Afterwards
4623d3b0591SJens Wiklander  *                  you may either call mbedtls_md_free() to clear the context,
4633d3b0591SJens Wiklander  *                  or call mbedtls_md_hmac_reset() to reuse the context with
4643d3b0591SJens Wiklander  *                  the same HMAC key.
465817466cbSJens Wiklander  *
4663d3b0591SJens Wiklander  * \param ctx       The message digest context containing an embedded HMAC
4673d3b0591SJens Wiklander  *                  context.
4683d3b0591SJens Wiklander  * \param output    The generic HMAC checksum result.
4693d3b0591SJens Wiklander  *
4703d3b0591SJens Wiklander  * \return          \c 0 on success.
4713d3b0591SJens Wiklander  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
4723d3b0591SJens Wiklander  *                  failure.
473817466cbSJens Wiklander  */
474039e02dfSJerome Forissier MBEDTLS_CHECK_RETURN_TYPICAL
475817466cbSJens Wiklander int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
476817466cbSJens Wiklander 
477817466cbSJens Wiklander /**
4783d3b0591SJens Wiklander  * \brief           This function prepares to authenticate a new message with
4793d3b0591SJens Wiklander  *                  the same key as the previous HMAC operation.
480817466cbSJens Wiklander  *
4813d3b0591SJens Wiklander  *                  You may call this function after mbedtls_md_hmac_finish().
4823d3b0591SJens Wiklander  *                  Afterwards call mbedtls_md_hmac_update() to pass the new
4833d3b0591SJens Wiklander  *                  input.
484817466cbSJens Wiklander  *
4853d3b0591SJens Wiklander  * \param ctx       The message digest context containing an embedded HMAC
4863d3b0591SJens Wiklander  *                  context.
4873d3b0591SJens Wiklander  *
4883d3b0591SJens Wiklander  * \return          \c 0 on success.
4893d3b0591SJens Wiklander  * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
4903d3b0591SJens Wiklander  *                  failure.
491817466cbSJens Wiklander  */
492039e02dfSJerome Forissier MBEDTLS_CHECK_RETURN_TYPICAL
493817466cbSJens Wiklander int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
494817466cbSJens Wiklander 
495817466cbSJens Wiklander /**
4963d3b0591SJens Wiklander  * \brief          This function calculates the full generic HMAC
4973d3b0591SJens Wiklander  *                 on the input buffer with the provided key.
498817466cbSJens Wiklander  *
4993d3b0591SJens Wiklander  *                 The function allocates the context, performs the
5003d3b0591SJens Wiklander  *                 calculation, and frees the context.
501817466cbSJens Wiklander  *
5023d3b0591SJens Wiklander  *                 The HMAC result is calculated as
5033d3b0591SJens Wiklander  *                 output = generic HMAC(hmac key, input buffer).
5043d3b0591SJens Wiklander  *
5053d3b0591SJens Wiklander  * \param md_info  The information structure of the message-digest algorithm
5063d3b0591SJens Wiklander  *                 to use.
5073d3b0591SJens Wiklander  * \param key      The HMAC secret key.
5083d3b0591SJens Wiklander  * \param keylen   The length of the HMAC secret key in Bytes.
5093d3b0591SJens Wiklander  * \param input    The buffer holding the input data.
5103d3b0591SJens Wiklander  * \param ilen     The length of the input data.
5113d3b0591SJens Wiklander  * \param output   The generic HMAC result.
5123d3b0591SJens Wiklander  *
5133d3b0591SJens Wiklander  * \return         \c 0 on success.
5143d3b0591SJens Wiklander  * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
5153d3b0591SJens Wiklander  *                 failure.
516817466cbSJens Wiklander  */
517039e02dfSJerome Forissier MBEDTLS_CHECK_RETURN_TYPICAL
518817466cbSJens Wiklander int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
519817466cbSJens Wiklander                     const unsigned char *input, size_t ilen,
520817466cbSJens Wiklander                     unsigned char *output);
521817466cbSJens Wiklander 
522817466cbSJens Wiklander #ifdef __cplusplus
523817466cbSJens Wiklander }
524817466cbSJens Wiklander #endif
525817466cbSJens Wiklander 
526817466cbSJens Wiklander #endif /* MBEDTLS_MD_H */
527