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