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. |
| 6 | * |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 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 | 416d0e2 | 2022-10-22 18:27:57 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_MD_LIGHT) |
| 36 | |
| 37 | /* |
| 38 | * - MBEDTLS_MD_CAN_xxx is defined if the md module can perform xxx. |
| 39 | * - MBEDTLS_MD_xxx_VIA_PSA is defined if the md module performs xxx via PSA. |
| 40 | * - MBEDTLS_MD_SOME_PSA is defined if at least one algorithm is performed |
| 41 | * via PSA. |
| 42 | * - MBEDTLS_MD_SOME_LEGACY is defined if at least one algorithm is performed |
| 43 | * via a direct legacy call. |
| 44 | * |
| 45 | * The md module performs an algorithm via PSA if there is a PSA hash |
| 46 | * accelerator, and makes a direct legacy call otherwise. |
| 47 | */ |
| 48 | |
| 49 | /* PSA accelerated implementations */ |
| 50 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 51 | #if defined(MBEDTLS_PSA_ACCEL_ALG_MD5) |
| 52 | #define MBEDTLS_MD_CAN_MD5 |
| 53 | #define MBEDTLS_MD_MD5_VIA_PSA |
| 54 | #define MBEDTLS_MD_SOME_PSA |
| 55 | #endif |
| 56 | #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1) |
| 57 | #define MBEDTLS_MD_CAN_SHA1 |
| 58 | #define MBEDTLS_MD_SHA1_VIA_PSA |
| 59 | #define MBEDTLS_MD_SOME_PSA |
| 60 | #endif |
| 61 | #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224) |
| 62 | #define MBEDTLS_MD_CAN_SHA224 |
| 63 | #define MBEDTLS_MD_SHA224_VIA_PSA |
| 64 | #define MBEDTLS_MD_SOME_PSA |
| 65 | #endif |
| 66 | #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256) |
| 67 | #define MBEDTLS_MD_CAN_SHA256 |
| 68 | #define MBEDTLS_MD_SHA256_VIA_PSA |
| 69 | #define MBEDTLS_MD_SOME_PSA |
| 70 | #endif |
| 71 | #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384) |
| 72 | #define MBEDTLS_MD_CAN_SHA384 |
| 73 | #define MBEDTLS_MD_SHA384_VIA_PSA |
| 74 | #define MBEDTLS_MD_SOME_PSA |
| 75 | #endif |
| 76 | #if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512) |
| 77 | #define MBEDTLS_MD_CAN_SHA512 |
| 78 | #define MBEDTLS_MD_SHA512_VIA_PSA |
| 79 | #define MBEDTLS_MD_SOME_PSA |
| 80 | #endif |
| 81 | #if defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160) |
| 82 | #define MBEDTLS_MD_CAN_RIPEMD160 |
| 83 | #define MBEDTLS_MD_RIPEMD160_VIA_PSA |
| 84 | #define MBEDTLS_MD_SOME_PSA |
| 85 | #endif |
| 86 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |
| 87 | |
| 88 | /* Built-in implementations */ |
| 89 | #if defined(MBEDTLS_MD5_C) |
| 90 | #define MBEDTLS_MD_CAN_MD5 |
| 91 | #define MBEDTLS_MD_SOME_LEGACY |
| 92 | #endif |
| 93 | #if defined(MBEDTLS_SHA1_C) |
| 94 | #define MBEDTLS_MD_CAN_SHA1 |
| 95 | #define MBEDTLS_MD_SOME_LEGACY |
| 96 | #endif |
| 97 | #if defined(MBEDTLS_SHA224_C) |
| 98 | #define MBEDTLS_MD_CAN_SHA224 |
| 99 | #define MBEDTLS_MD_SOME_LEGACY |
| 100 | #endif |
| 101 | #if defined(MBEDTLS_SHA256_C) |
| 102 | #define MBEDTLS_MD_CAN_SHA256 |
| 103 | #define MBEDTLS_MD_SOME_LEGACY |
| 104 | #endif |
| 105 | #if defined(MBEDTLS_SHA384_C) |
| 106 | #define MBEDTLS_MD_CAN_SHA384 |
| 107 | #define MBEDTLS_MD_SOME_LEGACY |
| 108 | #endif |
| 109 | #if defined(MBEDTLS_SHA512_C) |
| 110 | #define MBEDTLS_MD_CAN_SHA512 |
| 111 | #define MBEDTLS_MD_SOME_LEGACY |
| 112 | #endif |
| 113 | #if defined(MBEDTLS_RIPEMD160_C) |
| 114 | #define MBEDTLS_MD_CAN_RIPEMD160 |
| 115 | #define MBEDTLS_MD_SOME_LEGACY |
| 116 | #endif |
| 117 | |
| 118 | #endif /* MBEDTLS_MD_LIGHT */ |
| 119 | |
Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 120 | /** The selected feature is not available. */ |
| 121 | #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 |
| 122 | /** Bad input parameters to function. */ |
| 123 | #define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 |
| 124 | /** Failed to allocate memory. */ |
| 125 | #define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 |
| 126 | /** Opening or reading of file failed. */ |
| 127 | #define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 128 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 129 | #ifdef __cplusplus |
| 130 | extern "C" { |
| 131 | #endif |
| 132 | |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 133 | /** |
Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 134 | * \brief Supported message digests. |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 135 | * |
TRodziewicz | 10e8cf5 | 2021-05-31 17:58:57 +0200 | [diff] [blame] | 136 | * \warning MD5 and SHA-1 are considered weak message digests and |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 137 | * their use constitutes a security risk. We recommend considering |
| 138 | * stronger message digests instead. |
| 139 | * |
| 140 | */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 141 | typedef enum { |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 142 | MBEDTLS_MD_NONE=0, /**< None. */ |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 143 | MBEDTLS_MD_MD5, /**< The MD5 message digest. */ |
| 144 | MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */ |
| 145 | MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */ |
| 146 | MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */ |
| 147 | MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */ |
| 148 | MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */ |
Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 149 | MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | } mbedtls_md_type_t; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 151 | |
Gilles Peskine | 83d9e09 | 2022-10-22 18:32:43 +0200 | [diff] [blame] | 152 | #if defined(MBEDTLS_MD_CAN_SHA512) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | #define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */ |
Gilles Peskine | 83d9e09 | 2022-10-22 18:32:43 +0200 | [diff] [blame] | 154 | #elif defined(MBEDTLS_MD_CAN_SHA384) |
Valerio Setti | d55cb5b | 2022-12-22 14:26:55 +0100 | [diff] [blame] | 155 | #define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */ |
Gilles Peskine | 83d9e09 | 2022-10-22 18:32:43 +0200 | [diff] [blame] | 156 | #elif defined(MBEDTLS_MD_CAN_SHA256) |
Valerio Setti | d55cb5b | 2022-12-22 14:26:55 +0100 | [diff] [blame] | 157 | #define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */ |
Gilles Peskine | 83d9e09 | 2022-10-22 18:32:43 +0200 | [diff] [blame] | 158 | #elif defined(MBEDTLS_MD_CAN_SHA224) |
Valerio Setti | d55cb5b | 2022-12-22 14:26:55 +0100 | [diff] [blame] | 159 | #define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */ |
Paul Bakker | 7db0109 | 2013-09-10 11:10:57 +0200 | [diff] [blame] | 160 | #else |
Gilles Peskine | 83d9e09 | 2022-10-22 18:32:43 +0200 | [diff] [blame] | 161 | #define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160 |
| 162 | or smaller (MD5 and earlier) */ |
Paul Bakker | 7db0109 | 2013-09-10 11:10:57 +0200 | [diff] [blame] | 163 | #endif |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 164 | |
Gilles Peskine | 83d9e09 | 2022-10-22 18:32:43 +0200 | [diff] [blame] | 165 | #if defined(MBEDTLS_MD_CAN_SHA512) |
Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 166 | #define MBEDTLS_MD_MAX_BLOCK_SIZE 128 |
| 167 | #else |
| 168 | #define MBEDTLS_MD_MAX_BLOCK_SIZE 64 |
| 169 | #endif |
| 170 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 171 | /** |
Chris Jones | 3848e31 | 2021-03-11 16:17:59 +0000 | [diff] [blame] | 172 | * Opaque struct. |
| 173 | * |
| 174 | * Constructed using either #mbedtls_md_info_from_string or |
| 175 | * #mbedtls_md_info_from_type. |
| 176 | * |
| 177 | * Fields can be accessed with #mbedtls_md_get_size, |
| 178 | * #mbedtls_md_get_type and #mbedtls_md_get_name. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 179 | */ |
Chris Jones | 3848e31 | 2021-03-11 16:17:59 +0000 | [diff] [blame] | 180 | /* Defined internally in library/md_wrap.h. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | typedef struct mbedtls_md_info_t mbedtls_md_info_t; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 182 | |
| 183 | /** |
Manuel Pégourié-Gonnard | d8ea37f | 2023-03-09 10:46:22 +0100 | [diff] [blame] | 184 | * Used internally to indicate whether a context uses legacy or PSA. |
| 185 | * |
| 186 | * Internal use only. |
| 187 | */ |
| 188 | typedef enum { |
| 189 | MBEDTLS_MD_ENGINE_LEGACY = 0, |
| 190 | MBEDTLS_MD_ENGINE_PSA, |
| 191 | } mbedtls_md_engine_t; |
| 192 | |
| 193 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 194 | * The generic message-digest context. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 195 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 196 | typedef struct mbedtls_md_context_t { |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 197 | /** Information about the associated message digest. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 198 | const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 199 | |
Manuel Pégourié-Gonnard | d8ea37f | 2023-03-09 10:46:22 +0100 | [diff] [blame] | 200 | #if defined(MBEDTLS_MD_SOME_PSA) |
| 201 | /** Are hash operations dispatched to PSA or legacy? */ |
| 202 | mbedtls_md_engine_t MBEDTLS_PRIVATE(engine); |
| 203 | #endif |
| 204 | |
| 205 | /** The digest-specific context (legacy) or the PSA operation. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 206 | void *MBEDTLS_PRIVATE(md_ctx); |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 207 | |
Manuel Pégourié-Gonnard | 39a376a | 2023-03-09 17:21:40 +0100 | [diff] [blame^] | 208 | #if defined(MBEDTLS_MD_C) |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 209 | /** The HMAC part of the context. */ |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 210 | void *MBEDTLS_PRIVATE(hmac_ctx); |
Manuel Pégourié-Gonnard | 39a376a | 2023-03-09 17:21:40 +0100 | [diff] [blame^] | 211 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | } mbedtls_md_context_t; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 213 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 214 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 215 | * \brief This function returns the message-digest information |
| 216 | * associated with the given digest type. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 217 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 218 | * \param md_type The type of digest to search for. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 219 | * |
Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 220 | * \return The message-digest information associated with \p md_type. |
| 221 | * \return NULL if the associated message-digest information is not found. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 222 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 223 | 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] | 224 | |
Max Fillinger | 0bb3833 | 2021-12-28 16:32:00 +0100 | [diff] [blame] | 225 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 226 | * \brief This function initializes a message-digest context without |
| 227 | * binding it to a particular message-digest algorithm. |
| 228 | * |
| 229 | * This function should always be called first. It prepares the |
| 230 | * context for mbedtls_md_setup() for binding it to a |
| 231 | * message-digest algorithm. |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 232 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 233 | void mbedtls_md_init(mbedtls_md_context_t *ctx); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 234 | |
| 235 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 236 | * \brief This function clears the internal structure of \p ctx and |
| 237 | * frees any embedded internal structure, but does not free |
| 238 | * \p ctx itself. |
| 239 | * |
| 240 | * If you have called mbedtls_md_setup() on \p ctx, you must |
| 241 | * call mbedtls_md_free() when you are no longer using the |
| 242 | * context. |
| 243 | * Calling this function if you have previously |
| 244 | * called mbedtls_md_init() and nothing else is optional. |
| 245 | * You must not call this function if you have not called |
| 246 | * mbedtls_md_init(). |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 247 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | void mbedtls_md_free(mbedtls_md_context_t *ctx); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 249 | |
Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame] | 250 | |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 251 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 252 | * \brief This function selects the message digest algorithm to use, |
| 253 | * and allocates internal structures. |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 254 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 255 | * It should be called after mbedtls_md_init() or |
| 256 | * mbedtls_md_free(). Makes it necessary to call |
| 257 | * mbedtls_md_free() later. |
| 258 | * |
| 259 | * \param ctx The context to set up. |
| 260 | * \param md_info The information structure of the message-digest algorithm |
| 261 | * to use. |
Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 262 | * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory), |
| 263 | * or non-zero: HMAC is used with this context. |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 264 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 265 | * \return \c 0 on success. |
| 266 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 267 | * failure. |
| 268 | * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure. |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 269 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 270 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | 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] | 272 | |
| 273 | /** |
Tom Cosgrove | ce7f18c | 2022-07-28 05:50:56 +0100 | [diff] [blame] | 274 | * \brief This function clones the state of a message-digest |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 275 | * context. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 276 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 277 | * \note You must call mbedtls_md_setup() on \c dst before calling |
| 278 | * this function. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 279 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 280 | * \note The two contexts must have the same type, |
| 281 | * for example, both are SHA-256. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 282 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 283 | * \warning This function clones the message-digest state, not the |
| 284 | * HMAC state. |
| 285 | * |
| 286 | * \param dst The destination context. |
| 287 | * \param src The context to be cloned. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 288 | * |
Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 289 | * \return \c 0 on success. |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 290 | * \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] | 291 | * \return #MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE if both contexts are |
| 292 | * not using the same engine. This can be avoided by moving |
| 293 | * the call to psa_crypto_init() before the first call to |
| 294 | * mbedtls_md_setup(). |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 295 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 296 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | int mbedtls_md_clone(mbedtls_md_context_t *dst, |
| 298 | const mbedtls_md_context_t *src); |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 299 | |
| 300 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 301 | * \brief This function extracts the message-digest size from the |
| 302 | * message-digest information structure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 303 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 304 | * \param md_info The information structure of the message-digest algorithm |
| 305 | * to use. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 306 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 307 | * \return The size of the message-digest output in Bytes. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 308 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 309 | 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] | 310 | |
| 311 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 312 | * \brief This function extracts the message-digest type from the |
| 313 | * message-digest information structure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 314 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 315 | * \param md_info The information structure of the message-digest algorithm |
| 316 | * to use. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 317 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 318 | * \return The type of the message digest. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 319 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | 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] | 321 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 322 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 323 | * \brief This function starts a message-digest computation. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 324 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 325 | * You must call this function after setting up the context |
| 326 | * with mbedtls_md_setup(), and before passing data with |
| 327 | * mbedtls_md_update(). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 328 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 329 | * \param ctx The generic message-digest context. |
| 330 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 331 | * \return \c 0 on success. |
| 332 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 333 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 334 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 335 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 336 | int mbedtls_md_starts(mbedtls_md_context_t *ctx); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 337 | |
| 338 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 339 | * \brief This function feeds an input buffer into an ongoing |
| 340 | * message-digest computation. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 341 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 342 | * You must call mbedtls_md_starts() before calling this |
| 343 | * function. You may call this function multiple times. |
| 344 | * Afterwards, call mbedtls_md_finish(). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 345 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 346 | * \param ctx The generic message-digest context. |
| 347 | * \param input The buffer holding the input data. |
| 348 | * \param ilen The length of the input data. |
| 349 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 350 | * \return \c 0 on success. |
| 351 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 352 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 353 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 354 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | 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] | 356 | |
| 357 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 358 | * \brief This function finishes the digest operation, |
| 359 | * and writes the result to the output buffer. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 360 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 361 | * Call this function after a call to mbedtls_md_starts(), |
| 362 | * followed by any number of calls to mbedtls_md_update(). |
| 363 | * Afterwards, you may either clear the context with |
| 364 | * mbedtls_md_free(), or call mbedtls_md_starts() to reuse |
| 365 | * the context for another digest operation with the same |
| 366 | * algorithm. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 367 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 368 | * \param ctx The generic message-digest context. |
| 369 | * \param output The buffer for the generic message-digest checksum result. |
| 370 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 371 | * \return \c 0 on success. |
| 372 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 373 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 374 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 375 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 376 | int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 377 | |
| 378 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 379 | * \brief This function calculates the message-digest of a buffer, |
| 380 | * with respect to a configurable message-digest algorithm |
| 381 | * in a single call. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 382 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 383 | * The result is calculated as |
| 384 | * Output = message_digest(input buffer). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 385 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 386 | * \param md_info The information structure of the message-digest algorithm |
| 387 | * to use. |
| 388 | * \param input The buffer holding the data. |
| 389 | * \param ilen The length of the input data. |
| 390 | * \param output The generic message-digest checksum result. |
| 391 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 392 | * \return \c 0 on success. |
| 393 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 394 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 395 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 396 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 397 | int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, |
| 398 | unsigned char *output); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 399 | |
Manuel Pégourié-Gonnard | 82a4394 | 2023-02-23 09:36:29 +0100 | [diff] [blame] | 400 | /** |
| 401 | * \brief This function returns the list of digests supported by the |
| 402 | * generic digest module. |
| 403 | * |
| 404 | * \note The list starts with the strongest available hashes. |
| 405 | * |
| 406 | * \return A statically allocated array of digests. Each element |
| 407 | * in the returned list is an integer belonging to the |
| 408 | * message-digest enumeration #mbedtls_md_type_t. |
| 409 | * The last entry is 0. |
| 410 | */ |
| 411 | const int *mbedtls_md_list(void); |
| 412 | |
| 413 | /** |
| 414 | * \brief This function returns the message-digest information |
| 415 | * associated with the given digest name. |
| 416 | * |
| 417 | * \param md_name The name of the digest to search for. |
| 418 | * |
| 419 | * \return The message-digest information associated with \p md_name. |
| 420 | * \return NULL if the associated message-digest information is not found. |
| 421 | */ |
| 422 | const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name); |
| 423 | |
| 424 | /** |
| 425 | * \brief This function extracts the message-digest name from the |
| 426 | * message-digest information structure. |
| 427 | * |
| 428 | * \param md_info The information structure of the message-digest algorithm |
| 429 | * to use. |
| 430 | * |
| 431 | * \return The name of the message digest. |
| 432 | */ |
| 433 | const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info); |
| 434 | |
| 435 | /** |
| 436 | * \brief This function returns the message-digest information |
| 437 | * from the given context. |
| 438 | * |
| 439 | * \param ctx The context from which to extract the information. |
| 440 | * This must be initialized (or \c NULL). |
| 441 | * |
| 442 | * \return The message-digest information associated with \p ctx. |
| 443 | * \return \c NULL if \p ctx is \c NULL. |
| 444 | */ |
| 445 | const mbedtls_md_info_t *mbedtls_md_info_from_ctx( |
| 446 | const mbedtls_md_context_t *ctx); |
| 447 | |
| 448 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 449 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 450 | * \brief This function calculates the message-digest checksum |
| 451 | * result of the contents of the provided file. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 452 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 453 | * The result is calculated as |
| 454 | * Output = message_digest(file contents). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 455 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 456 | * \param md_info The information structure of the message-digest algorithm |
| 457 | * to use. |
| 458 | * \param path The input file name. |
| 459 | * \param output The generic message-digest checksum result. |
| 460 | * |
Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 461 | * \return \c 0 on success. |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 462 | * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing |
| 463 | * the file pointed by \p path. |
| 464 | * \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] | 465 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 466 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path, |
| 468 | unsigned char *output); |
Manuel Pégourié-Gonnard | 82a4394 | 2023-02-23 09:36:29 +0100 | [diff] [blame] | 469 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 470 | |
| 471 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 472 | * \brief This function sets the HMAC key and prepares to |
| 473 | * authenticate a new message. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 474 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 475 | * Call this function after mbedtls_md_setup(), to use |
| 476 | * the MD context for an HMAC calculation, then call |
| 477 | * mbedtls_md_hmac_update() to provide the input data, and |
| 478 | * mbedtls_md_hmac_finish() to get the HMAC value. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 479 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 480 | * \param ctx The message digest context containing an embedded HMAC |
| 481 | * context. |
| 482 | * \param key The HMAC secret key. |
| 483 | * \param keylen The length of the HMAC key in Bytes. |
| 484 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 485 | * \return \c 0 on success. |
| 486 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 487 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 488 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 489 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key, |
| 491 | size_t keylen); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 492 | |
| 493 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 494 | * \brief This function feeds an input buffer into an ongoing HMAC |
| 495 | * computation. |
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 | * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset() |
| 498 | * before calling this function. |
| 499 | * You may call this function multiple times to pass the |
| 500 | * input piecewise. |
| 501 | * Afterwards, call mbedtls_md_hmac_finish(). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 502 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 503 | * \param ctx The message digest context containing an embedded HMAC |
| 504 | * context. |
| 505 | * \param input The buffer holding the input data. |
| 506 | * \param ilen The length of the input data. |
| 507 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 508 | * \return \c 0 on success. |
| 509 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 510 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 511 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 512 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, |
| 514 | size_t ilen); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 515 | |
| 516 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 517 | * \brief This function finishes the HMAC operation, and writes |
| 518 | * the result to the output buffer. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 519 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 520 | * Call this function after mbedtls_md_hmac_starts() and |
| 521 | * mbedtls_md_hmac_update() to get the HMAC value. Afterwards |
| 522 | * you may either call mbedtls_md_free() to clear the context, |
| 523 | * or call mbedtls_md_hmac_reset() to reuse the context with |
| 524 | * the same HMAC key. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 525 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 526 | * \param ctx The message digest context containing an embedded HMAC |
| 527 | * context. |
| 528 | * \param output The generic HMAC checksum result. |
| 529 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 530 | * \return \c 0 on success. |
| 531 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 532 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 533 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 534 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 535 | 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] | 536 | |
| 537 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 538 | * \brief This function prepares to authenticate a new message with |
| 539 | * the same key as the previous HMAC operation. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 540 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 541 | * You may call this function after mbedtls_md_hmac_finish(). |
| 542 | * Afterwards call mbedtls_md_hmac_update() to pass the new |
| 543 | * input. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 544 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 545 | * \param ctx The message digest context containing an embedded HMAC |
| 546 | * context. |
| 547 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 548 | * \return \c 0 on success. |
| 549 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 550 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 551 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 552 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 553 | int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 554 | |
| 555 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 556 | * \brief This function calculates the full generic HMAC |
| 557 | * on the input buffer with the provided key. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 558 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 559 | * The function allocates the context, performs the |
| 560 | * calculation, and frees the context. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 561 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 562 | * The HMAC result is calculated as |
| 563 | * output = generic HMAC(hmac key, input buffer). |
| 564 | * |
| 565 | * \param md_info The information structure of the message-digest algorithm |
| 566 | * to use. |
| 567 | * \param key The HMAC secret key. |
| 568 | * \param keylen The length of the HMAC secret key in Bytes. |
| 569 | * \param input The buffer holding the input data. |
| 570 | * \param ilen The length of the input data. |
| 571 | * \param output The generic HMAC result. |
| 572 | * |
Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 573 | * \return \c 0 on success. |
| 574 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 575 | * failure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 576 | */ |
Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 577 | MBEDTLS_CHECK_RETURN_TYPICAL |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen, |
| 579 | const unsigned char *input, size_t ilen, |
| 580 | unsigned char *output); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 581 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 582 | #ifdef __cplusplus |
| 583 | } |
| 584 | #endif |
| 585 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 586 | #endif /* MBEDTLS_MD_H */ |