Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1 | /** |
Simon Butcher | 5b331b9 | 2016-01-03 16:14:14 +0000 | [diff] [blame] | 2 | * \file md.h |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 3 | * |
Manuel Pégourié-Gonnard | b9b630d | 2023-02-16 19:07:31 +0100 | [diff] [blame] | 4 | * \brief This file contains the generic functions for message-digest |
| 5 | * (hashing) and HMAC. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 6 | * |
| 7 | * \author Adriaan de Jong <dejong@fox-it.com> |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 8 | */ |
| 9 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 10 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 11 | * SPDX-License-Identifier: Apache-2.0 |
| 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 24 | */ |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #ifndef MBEDTLS_MD_H |
| 27 | #define MBEDTLS_MD_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 28 | #include "mbedtls/private_access.h" |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 29 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 30 | #include <stddef.h> |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 31 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 32 | #include "mbedtls/build_info.h" |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 33 | #include "mbedtls/platform_util.h" |
Ron Eldor | f231eaa | 2017-08-22 14:50:14 +0300 | [diff] [blame] | 34 | |
Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 35 | /** The selected feature is not available. */ |
| 36 | #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 |
| 37 | /** Bad input parameters to function. */ |
| 38 | #define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 |
| 39 | /** Failed to allocate memory. */ |
| 40 | #define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 |
| 41 | /** Opening or reading of file failed. */ |
| 42 | #define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 43 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 48 | /** |
Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 49 | * \brief Supported message digests. |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 50 | * |
TRodziewicz | 10e8cf5 | 2021-05-31 17:58:57 +0200 | [diff] [blame] | 51 | * \warning MD5 and SHA-1 are considered weak message digests and |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 52 | * their use constitutes a security risk. We recommend considering |
| 53 | * stronger message digests instead. |
| 54 | * |
| 55 | */ |
Manuel Pégourié-Gonnard | 1f6d2e3 | 2023-06-06 12:34:45 +0200 | [diff] [blame] | 56 | /* Note: these are aligned with the definitions of PSA_ALG_ macros for hashes, |
| 57 | * in order to enable an efficient implementation of conversion functions. |
| 58 | * This is tested by md_to_from_psa() in test_suite_md. */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 59 | typedef enum { |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 60 | MBEDTLS_MD_NONE=0, /**< None. */ |
Manuel Pégourié-Gonnard | 9b76318 | 2023-05-31 10:54:08 +0200 | [diff] [blame] | 61 | MBEDTLS_MD_MD5=0x03, /**< The MD5 message digest. */ |
| 62 | MBEDTLS_MD_RIPEMD160=0x04, /**< The RIPEMD-160 message digest. */ |
| 63 | MBEDTLS_MD_SHA1=0x05, /**< The SHA-1 message digest. */ |
| 64 | MBEDTLS_MD_SHA224=0x08, /**< The SHA-224 message digest. */ |
| 65 | MBEDTLS_MD_SHA256=0x09, /**< The SHA-256 message digest. */ |
| 66 | MBEDTLS_MD_SHA384=0x0a, /**< The SHA-384 message digest. */ |
| 67 | MBEDTLS_MD_SHA512=0x0b, /**< The SHA-512 message digest. */ |
| 68 | MBEDTLS_MD_SHA3_224=0x10, /**< The SHA3-224 message digest. */ |
| 69 | MBEDTLS_MD_SHA3_256=0x11, /**< The SHA3-256 message digest. */ |
| 70 | MBEDTLS_MD_SHA3_384=0x12, /**< The SHA3-384 message digest. */ |
| 71 | MBEDTLS_MD_SHA3_512=0x13, /**< The SHA3-512 message digest. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | } mbedtls_md_type_t; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 73 | |
Dave Rodgman | 9304186 | 2023-06-08 10:13:22 +0100 | [diff] [blame] | 74 | /* Note: this should always be >= PSA_HASH_MAX_SIZE |
| 75 | * in all builds with both CRYPTO_C and MD_LIGHT. |
| 76 | * |
| 77 | * This is to make things easier for modules such as TLS that may define a |
| 78 | * buffer size using MD_MAX_SIZE in a part of the code that's common to PSA |
| 79 | * and legacy, then assume the buffer's size is PSA_HASH_MAX_SIZE in another |
| 80 | * part of the code based on PSA. |
| 81 | */ |
Dave Rodgman | 0442e1b | 2023-06-08 16:03:33 +0100 | [diff] [blame] | 82 | #if defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA3_512) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | #define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */ |
Dave Rodgman | 0442e1b | 2023-06-08 16:03:33 +0100 | [diff] [blame] | 84 | #elif defined(MBEDTLS_MD_CAN_SHA384) || defined(MBEDTLS_MD_CAN_SHA3_384) |
Valerio Setti | d55cb5b | 2022-12-22 14:26:55 +0100 | [diff] [blame] | 85 | #define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */ |
Dave Rodgman | 0442e1b | 2023-06-08 16:03:33 +0100 | [diff] [blame] | 86 | #elif defined(MBEDTLS_MD_CAN_SHA256) || defined(MBEDTLS_MD_CAN_SHA3_256) |
Valerio Setti | d55cb5b | 2022-12-22 14:26:55 +0100 | [diff] [blame] | 87 | #define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */ |
Dave Rodgman | 0442e1b | 2023-06-08 16:03:33 +0100 | [diff] [blame] | 88 | #elif defined(MBEDTLS_MD_CAN_SHA224) || defined(MBEDTLS_MD_CAN_SHA3_224) |
Valerio Setti | d55cb5b | 2022-12-22 14:26:55 +0100 | [diff] [blame] | 89 | #define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */ |
Paul Bakker | 7db0109 | 2013-09-10 11:10:57 +0200 | [diff] [blame] | 90 | #else |
Gilles Peskine | 83d9e09 | 2022-10-22 18:32:43 +0200 | [diff] [blame] | 91 | #define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160 |
| 92 | or smaller (MD5 and earlier) */ |
Paul Bakker | 7db0109 | 2013-09-10 11:10:57 +0200 | [diff] [blame] | 93 | #endif |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 94 | |
Dave Rodgman | ff45d44 | 2023-06-08 10:11:34 +0100 | [diff] [blame] | 95 | #if defined(MBEDTLS_MD_CAN_SHA3_224) |
Pol Henarejos | 4712d4c | 2022-05-20 14:17:14 +0200 | [diff] [blame] | 96 | #define MBEDTLS_MD_MAX_BLOCK_SIZE 144 /* the longest known is SHA3-224 */ |
Dave Rodgman | 0442e1b | 2023-06-08 16:03:33 +0100 | [diff] [blame] | 97 | #elif defined(MBEDTLS_MD_CAN_SHA3_256) |
| 98 | #define MBEDTLS_MD_MAX_BLOCK_SIZE 136 |
Dave Rodgman | f956312 | 2023-06-11 16:04:29 +0100 | [diff] [blame] | 99 | #elif defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA384) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 100 | #define MBEDTLS_MD_MAX_BLOCK_SIZE 128 |
Dave Rodgman | 0442e1b | 2023-06-08 16:03:33 +0100 | [diff] [blame] | 101 | #elif defined(MBEDTLS_MD_CAN_SHA3_384) |
| 102 | #define MBEDTLS_MD_MAX_BLOCK_SIZE 104 |
| 103 | #elif defined(MBEDTLS_MD_CAN_SHA3_512) |
| 104 | #define MBEDTLS_MD_MAX_BLOCK_SIZE 72 |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 105 | #else |
| 106 | #define MBEDTLS_MD_MAX_BLOCK_SIZE 64 |
| 107 | #endif |
| 108 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 109 | /** |
Chris Jones | 3848e31 | 2021-03-11 16:17:59 +0000 | [diff] [blame] | 110 | * Opaque struct. |
| 111 | * |
| 112 | * Constructed using either #mbedtls_md_info_from_string or |
| 113 | * #mbedtls_md_info_from_type. |
| 114 | * |
| 115 | * Fields can be accessed with #mbedtls_md_get_size, |
| 116 | * #mbedtls_md_get_type and #mbedtls_md_get_name. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 117 | */ |
Chris Jones | 3848e31 | 2021-03-11 16:17:59 +0000 | [diff] [blame] | 118 | /* Defined internally in library/md_wrap.h. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | typedef struct mbedtls_md_info_t mbedtls_md_info_t; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 120 | |
| 121 | /** |
Manuel Pégourié-Gonnard | d8ea37f | 2023-03-09 10:46:22 +0100 | [diff] [blame] | 122 | * Used internally to indicate whether a context uses legacy or PSA. |
| 123 | * |
| 124 | * Internal use only. |
| 125 | */ |
| 126 | typedef enum { |
| 127 | MBEDTLS_MD_ENGINE_LEGACY = 0, |
| 128 | MBEDTLS_MD_ENGINE_PSA, |
| 129 | } mbedtls_md_engine_t; |
| 130 | |
| 131 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 132 | * The generic message-digest context. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 133 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 134 | typedef struct mbedtls_md_context_t { |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 135 | /** Information about the associated message digest. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 136 | const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 137 | |
Manuel Pégourié-Gonnard | d8ea37f | 2023-03-09 10:46:22 +0100 | [diff] [blame] | 138 | #if defined(MBEDTLS_MD_SOME_PSA) |
| 139 | /** Are hash operations dispatched to PSA or legacy? */ |
| 140 | mbedtls_md_engine_t MBEDTLS_PRIVATE(engine); |
| 141 | #endif |
| 142 | |
| 143 | /** The digest-specific context (legacy) or the PSA operation. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 144 | void *MBEDTLS_PRIVATE(md_ctx); |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 145 | |
Manuel Pégourié-Gonnard | 39a376a | 2023-03-09 17:21:40 +0100 | [diff] [blame] | 146 | #if defined(MBEDTLS_MD_C) |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 147 | /** The HMAC part of the context. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 148 | void *MBEDTLS_PRIVATE(hmac_ctx); |
Manuel Pégourié-Gonnard | 39a376a | 2023-03-09 17:21:40 +0100 | [diff] [blame] | 149 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | } mbedtls_md_context_t; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 151 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 152 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 153 | * \brief This function returns the message-digest information |
| 154 | * associated with the given digest type. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 155 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 156 | * \param md_type The type of digest to search for. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 157 | * |
Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 158 | * \return The message-digest information associated with \p md_type. |
| 159 | * \return NULL if the associated message-digest information is not found. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 160 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 162 | |
| 163 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 164 | * \brief This function initializes a message-digest context without |
| 165 | * binding it to a particular message-digest algorithm. |
| 166 | * |
| 167 | * This function should always be called first. It prepares the |
| 168 | * context for mbedtls_md_setup() for binding it to a |
| 169 | * message-digest algorithm. |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 170 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | void mbedtls_md_init(mbedtls_md_context_t *ctx); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 172 | |
| 173 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 174 | * \brief This function clears the internal structure of \p ctx and |
| 175 | * frees any embedded internal structure, but does not free |
| 176 | * \p ctx itself. |
| 177 | * |
| 178 | * If you have called mbedtls_md_setup() on \p ctx, you must |
| 179 | * call mbedtls_md_free() when you are no longer using the |
| 180 | * context. |
| 181 | * Calling this function if you have previously |
| 182 | * called mbedtls_md_init() and nothing else is optional. |
| 183 | * You must not call this function if you have not called |
| 184 | * mbedtls_md_init(). |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 185 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | void mbedtls_md_free(mbedtls_md_context_t *ctx); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 187 | |
Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame] | 188 | |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 189 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 190 | * \brief This function selects the message digest algorithm to use, |
| 191 | * and allocates internal structures. |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 192 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 193 | * It should be called after mbedtls_md_init() or |
| 194 | * mbedtls_md_free(). Makes it necessary to call |
| 195 | * mbedtls_md_free() later. |
| 196 | * |
| 197 | * \param ctx The context to set up. |
| 198 | * \param md_info The information structure of the message-digest algorithm |
| 199 | * to use. |
Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 200 | * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory), |
| 201 | * or non-zero: HMAC is used with this context. |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 202 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 203 | * \return \c 0 on success. |
| 204 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 205 | * failure. |
| 206 | * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure. |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 207 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 208 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 209 | int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac); |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 210 | |
| 211 | /** |
Tom Cosgrove | ce7f18c | 2022-07-28 05:50:56 +0100 | [diff] [blame] | 212 | * \brief This function clones the state of a message-digest |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 213 | * context. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 214 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 215 | * \note You must call mbedtls_md_setup() on \c dst before calling |
| 216 | * this function. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 217 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 218 | * \note The two contexts must have the same type, |
| 219 | * for example, both are SHA-256. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 220 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 221 | * \warning This function clones the message-digest state, not the |
| 222 | * HMAC state. |
| 223 | * |
| 224 | * \param dst The destination context. |
| 225 | * \param src The context to be cloned. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 226 | * |
Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 227 | * \return \c 0 on success. |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 228 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure. |
Manuel Pégourié-Gonnard | 9b14639 | 2023-03-09 15:56:14 +0100 | [diff] [blame] | 229 | * \return #MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE if both contexts are |
| 230 | * not using the same engine. This can be avoided by moving |
| 231 | * the call to psa_crypto_init() before the first call to |
| 232 | * mbedtls_md_setup(). |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 233 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 234 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 235 | int mbedtls_md_clone(mbedtls_md_context_t *dst, |
| 236 | const mbedtls_md_context_t *src); |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 237 | |
| 238 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 239 | * \brief This function extracts the message-digest size from the |
| 240 | * message-digest information structure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 241 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 242 | * \param md_info The information structure of the message-digest algorithm |
| 243 | * to use. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 244 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 245 | * \return The size of the message-digest output in Bytes. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 246 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 248 | |
| 249 | /** |
Manuel Pégourié-Gonnard | 1ef26e2 | 2023-01-27 11:47:05 +0100 | [diff] [blame] | 250 | * \brief This function gives the message-digest size associated to |
| 251 | * message-digest type. |
| 252 | * |
| 253 | * \param md_type The message-digest type. |
| 254 | * |
| 255 | * \return The size of the message-digest output in Bytes, |
| 256 | * or 0 if the message-digest type is not known. |
| 257 | */ |
| 258 | static inline unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type) |
| 259 | { |
| 260 | return mbedtls_md_get_size(mbedtls_md_info_from_type(md_type)); |
| 261 | } |
| 262 | |
| 263 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 264 | * \brief This function extracts the message-digest type from the |
| 265 | * message-digest information structure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 266 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 267 | * \param md_info The information structure of the message-digest algorithm |
| 268 | * to use. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 269 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 270 | * \return The type of the message digest. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 271 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 272 | mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 273 | |
| 274 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 275 | * \brief This function starts a message-digest computation. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 276 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 277 | * You must call this function after setting up the context |
| 278 | * with mbedtls_md_setup(), and before passing data with |
| 279 | * mbedtls_md_update(). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 280 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 281 | * \param ctx The generic message-digest context. |
| 282 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 283 | * \return \c 0 on success. |
| 284 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 285 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 286 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 287 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | int mbedtls_md_starts(mbedtls_md_context_t *ctx); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 289 | |
| 290 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 291 | * \brief This function feeds an input buffer into an ongoing |
| 292 | * message-digest computation. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 293 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 294 | * You must call mbedtls_md_starts() before calling this |
| 295 | * function. You may call this function multiple times. |
| 296 | * Afterwards, call mbedtls_md_finish(). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 297 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 298 | * \param ctx The generic message-digest context. |
| 299 | * \param input The buffer holding the input data. |
| 300 | * \param ilen The length of the input data. |
| 301 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 302 | * \return \c 0 on success. |
| 303 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 304 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 305 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 306 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 308 | |
| 309 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 310 | * \brief This function finishes the digest operation, |
| 311 | * and writes the result to the output buffer. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 312 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 313 | * Call this function after a call to mbedtls_md_starts(), |
| 314 | * followed by any number of calls to mbedtls_md_update(). |
| 315 | * Afterwards, you may either clear the context with |
| 316 | * mbedtls_md_free(), or call mbedtls_md_starts() to reuse |
| 317 | * the context for another digest operation with the same |
| 318 | * algorithm. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 319 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 320 | * \param ctx The generic message-digest context. |
| 321 | * \param output The buffer for the generic message-digest checksum result. |
| 322 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 323 | * \return \c 0 on success. |
| 324 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 325 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 326 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 327 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 329 | |
| 330 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 331 | * \brief This function calculates the message-digest of a buffer, |
| 332 | * with respect to a configurable message-digest algorithm |
| 333 | * in a single call. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 334 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 335 | * The result is calculated as |
| 336 | * Output = message_digest(input buffer). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 337 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 338 | * \param md_info The information structure of the message-digest algorithm |
| 339 | * to use. |
| 340 | * \param input The buffer holding the data. |
| 341 | * \param ilen The length of the input data. |
| 342 | * \param output The generic message-digest checksum result. |
| 343 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 344 | * \return \c 0 on success. |
| 345 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 346 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 347 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 348 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, |
| 350 | unsigned char *output); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 351 | |
Manuel Pégourié-Gonnard | 82a4394 | 2023-02-23 09:36:29 +0100 | [diff] [blame] | 352 | /** |
| 353 | * \brief This function returns the list of digests supported by the |
| 354 | * generic digest module. |
| 355 | * |
| 356 | * \note The list starts with the strongest available hashes. |
| 357 | * |
| 358 | * \return A statically allocated array of digests. Each element |
| 359 | * in the returned list is an integer belonging to the |
| 360 | * message-digest enumeration #mbedtls_md_type_t. |
| 361 | * The last entry is 0. |
| 362 | */ |
| 363 | const int *mbedtls_md_list(void); |
| 364 | |
| 365 | /** |
| 366 | * \brief This function returns the message-digest information |
| 367 | * associated with the given digest name. |
| 368 | * |
| 369 | * \param md_name The name of the digest to search for. |
| 370 | * |
| 371 | * \return The message-digest information associated with \p md_name. |
| 372 | * \return NULL if the associated message-digest information is not found. |
| 373 | */ |
| 374 | const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name); |
| 375 | |
| 376 | /** |
Manuel Pégourié-Gonnard | 0fda0d2 | 2023-07-27 12:22:52 +0200 | [diff] [blame] | 377 | * \brief This function returns the name of the message digest for |
| 378 | * the message-digest information structure given. |
Manuel Pégourié-Gonnard | 82a4394 | 2023-02-23 09:36:29 +0100 | [diff] [blame] | 379 | * |
| 380 | * \param md_info The information structure of the message-digest algorithm |
| 381 | * to use. |
| 382 | * |
| 383 | * \return The name of the message digest. |
| 384 | */ |
| 385 | const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info); |
| 386 | |
| 387 | /** |
| 388 | * \brief This function returns the message-digest information |
| 389 | * from the given context. |
| 390 | * |
| 391 | * \param ctx The context from which to extract the information. |
| 392 | * This must be initialized (or \c NULL). |
| 393 | * |
| 394 | * \return The message-digest information associated with \p ctx. |
| 395 | * \return \c NULL if \p ctx is \c NULL. |
| 396 | */ |
| 397 | const mbedtls_md_info_t *mbedtls_md_info_from_ctx( |
| 398 | const mbedtls_md_context_t *ctx); |
| 399 | |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 400 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 401 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 402 | * \brief This function calculates the message-digest checksum |
| 403 | * result of the contents of the provided file. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 404 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 405 | * The result is calculated as |
| 406 | * Output = message_digest(file contents). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 407 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 408 | * \param md_info The information structure of the message-digest algorithm |
| 409 | * to use. |
| 410 | * \param path The input file name. |
| 411 | * \param output The generic message-digest checksum result. |
| 412 | * |
Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 413 | * \return \c 0 on success. |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 414 | * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing |
| 415 | * the file pointed by \p path. |
| 416 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 417 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 418 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 419 | int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path, |
| 420 | unsigned char *output); |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 421 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 422 | |
| 423 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 424 | * \brief This function sets the HMAC key and prepares to |
| 425 | * authenticate a new message. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 426 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 427 | * Call this function after mbedtls_md_setup(), to use |
| 428 | * the MD context for an HMAC calculation, then call |
| 429 | * mbedtls_md_hmac_update() to provide the input data, and |
| 430 | * mbedtls_md_hmac_finish() to get the HMAC value. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 431 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 432 | * \param ctx The message digest context containing an embedded HMAC |
| 433 | * context. |
| 434 | * \param key The HMAC secret key. |
| 435 | * \param keylen The length of the HMAC key in Bytes. |
| 436 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 437 | * \return \c 0 on success. |
| 438 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 439 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 440 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 441 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key, |
| 443 | size_t keylen); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 444 | |
| 445 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 446 | * \brief This function feeds an input buffer into an ongoing HMAC |
| 447 | * computation. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 448 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 449 | * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset() |
| 450 | * before calling this function. |
| 451 | * You may call this function multiple times to pass the |
| 452 | * input piecewise. |
| 453 | * Afterwards, call mbedtls_md_hmac_finish(). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 454 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 455 | * \param ctx The message digest context containing an embedded HMAC |
| 456 | * context. |
| 457 | * \param input The buffer holding the input data. |
| 458 | * \param ilen The length of the input data. |
| 459 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 460 | * \return \c 0 on success. |
| 461 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 462 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 463 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 464 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, |
| 466 | size_t ilen); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 467 | |
| 468 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 469 | * \brief This function finishes the HMAC operation, and writes |
| 470 | * the result to the output buffer. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 471 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 472 | * Call this function after mbedtls_md_hmac_starts() and |
| 473 | * mbedtls_md_hmac_update() to get the HMAC value. Afterwards |
| 474 | * you may either call mbedtls_md_free() to clear the context, |
| 475 | * or call mbedtls_md_hmac_reset() to reuse the context with |
| 476 | * the same HMAC key. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 477 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 478 | * \param ctx The message digest context containing an embedded HMAC |
| 479 | * context. |
| 480 | * \param output The generic HMAC checksum result. |
| 481 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 482 | * \return \c 0 on success. |
| 483 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 484 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 485 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 486 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 487 | int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 488 | |
| 489 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 490 | * \brief This function prepares to authenticate a new message with |
| 491 | * the same key as the previous HMAC operation. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 492 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 493 | * You may call this function after mbedtls_md_hmac_finish(). |
| 494 | * Afterwards call mbedtls_md_hmac_update() to pass the new |
| 495 | * input. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 496 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 497 | * \param ctx The message digest context containing an embedded HMAC |
| 498 | * context. |
| 499 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 500 | * \return \c 0 on success. |
| 501 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 502 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 503 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 504 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 506 | |
| 507 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 508 | * \brief This function calculates the full generic HMAC |
| 509 | * on the input buffer with the provided key. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 510 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 511 | * The function allocates the context, performs the |
| 512 | * calculation, and frees the context. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 513 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 514 | * The HMAC result is calculated as |
| 515 | * output = generic HMAC(hmac key, input buffer). |
| 516 | * |
| 517 | * \param md_info The information structure of the message-digest algorithm |
| 518 | * to use. |
| 519 | * \param key The HMAC secret key. |
| 520 | * \param keylen The length of the HMAC secret key in Bytes. |
| 521 | * \param input The buffer holding the input data. |
| 522 | * \param ilen The length of the input data. |
| 523 | * \param output The generic HMAC result. |
| 524 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 525 | * \return \c 0 on success. |
| 526 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 527 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 528 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 529 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 530 | int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen, |
| 531 | const unsigned char *input, size_t ilen, |
| 532 | unsigned char *output); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 533 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 534 | #ifdef __cplusplus |
| 535 | } |
| 536 | #endif |
| 537 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 538 | #endif /* MBEDTLS_MD_H */ |