| 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 | 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 | 
| Dave Rodgman | 05d71ff | 2023-06-07 18:02:04 +0100 | [diff] [blame] | 115 | #if defined(MBEDTLS_SHA3_C) | 
| Dave Rodgman | ff45d44 | 2023-06-08 10:11:34 +0100 | [diff] [blame] | 116 | #define MBEDTLS_MD_CAN_SHA3_224 | 
|  | 117 | #define MBEDTLS_MD_CAN_SHA3_256 | 
|  | 118 | #define MBEDTLS_MD_CAN_SHA3_384 | 
|  | 119 | #define MBEDTLS_MD_CAN_SHA3_512 | 
| Dave Rodgman | 05d71ff | 2023-06-07 18:02:04 +0100 | [diff] [blame] | 120 | #endif | 
| Gilles Peskine | 416d0e2 | 2022-10-22 18:27:57 +0200 | [diff] [blame] | 121 | #if defined(MBEDTLS_RIPEMD160_C) | 
|  | 122 | #define MBEDTLS_MD_CAN_RIPEMD160 | 
|  | 123 | #define MBEDTLS_MD_SOME_LEGACY | 
|  | 124 | #endif | 
|  | 125 |  | 
|  | 126 | #endif /* MBEDTLS_MD_LIGHT */ | 
|  | 127 |  | 
| Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 128 | /** The selected feature is not available. */ | 
|  | 129 | #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE                -0x5080 | 
|  | 130 | /** Bad input parameters to function. */ | 
|  | 131 | #define MBEDTLS_ERR_MD_BAD_INPUT_DATA                     -0x5100 | 
|  | 132 | /** Failed to allocate memory. */ | 
|  | 133 | #define MBEDTLS_ERR_MD_ALLOC_FAILED                       -0x5180 | 
|  | 134 | /** Opening or reading of file failed. */ | 
|  | 135 | #define MBEDTLS_ERR_MD_FILE_IO_ERROR                      -0x5200 | 
| Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 136 |  | 
| Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 137 | #ifdef __cplusplus | 
|  | 138 | extern "C" { | 
|  | 139 | #endif | 
|  | 140 |  | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 141 | /** | 
| Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 142 | * \brief     Supported message digests. | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 143 | * | 
| TRodziewicz | 10e8cf5 | 2021-05-31 17:58:57 +0200 | [diff] [blame] | 144 | * \warning   MD5 and SHA-1 are considered weak message digests and | 
| Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 145 | *            their use constitutes a security risk. We recommend considering | 
|  | 146 | *            stronger message digests instead. | 
|  | 147 | * | 
|  | 148 | */ | 
| Manuel Pégourié-Gonnard | 1f6d2e3 | 2023-06-06 12:34:45 +0200 | [diff] [blame] | 149 | /* Note: these are aligned with the definitions of PSA_ALG_ macros for hashes, | 
|  | 150 | * in order to enable an efficient implementation of conversion functions. | 
|  | 151 | * This is tested by md_to_from_psa() in test_suite_md. */ | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 152 | typedef enum { | 
| Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 153 | MBEDTLS_MD_NONE=0,    /**< None. */ | 
| Manuel Pégourié-Gonnard | 9b76318 | 2023-05-31 10:54:08 +0200 | [diff] [blame] | 154 | MBEDTLS_MD_MD5=0x03,       /**< The MD5 message digest. */ | 
|  | 155 | MBEDTLS_MD_RIPEMD160=0x04, /**< The RIPEMD-160 message digest. */ | 
|  | 156 | MBEDTLS_MD_SHA1=0x05,      /**< The SHA-1 message digest. */ | 
|  | 157 | MBEDTLS_MD_SHA224=0x08,    /**< The SHA-224 message digest. */ | 
|  | 158 | MBEDTLS_MD_SHA256=0x09,    /**< The SHA-256 message digest. */ | 
|  | 159 | MBEDTLS_MD_SHA384=0x0a,    /**< The SHA-384 message digest. */ | 
|  | 160 | MBEDTLS_MD_SHA512=0x0b,    /**< The SHA-512 message digest. */ | 
|  | 161 | MBEDTLS_MD_SHA3_224=0x10,  /**< The SHA3-224 message digest. */ | 
|  | 162 | MBEDTLS_MD_SHA3_256=0x11,  /**< The SHA3-256 message digest. */ | 
|  | 163 | MBEDTLS_MD_SHA3_384=0x12,  /**< The SHA3-384 message digest. */ | 
|  | 164 | MBEDTLS_MD_SHA3_512=0x13,  /**< The SHA3-512 message digest. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | } mbedtls_md_type_t; | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 166 |  | 
| Dave Rodgman | 9304186 | 2023-06-08 10:13:22 +0100 | [diff] [blame] | 167 | /* Note: this should always be >= PSA_HASH_MAX_SIZE | 
|  | 168 | * in all builds with both CRYPTO_C and MD_LIGHT. | 
|  | 169 | * | 
|  | 170 | * This is to make things easier for modules such as TLS that may define a | 
|  | 171 | * buffer size using MD_MAX_SIZE in a part of the code that's common to PSA | 
|  | 172 | * and legacy, then assume the buffer's size is PSA_HASH_MAX_SIZE in another | 
|  | 173 | * part of the code based on PSA. | 
|  | 174 | */ | 
| Dave Rodgman | 0442e1b | 2023-06-08 16:03:33 +0100 | [diff] [blame] | 175 | #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] | 176 | #define MBEDTLS_MD_MAX_SIZE         64  /* longest known is SHA512 */ | 
| Dave Rodgman | 0442e1b | 2023-06-08 16:03:33 +0100 | [diff] [blame] | 177 | #elif defined(MBEDTLS_MD_CAN_SHA384) || defined(MBEDTLS_MD_CAN_SHA3_384) | 
| Valerio Setti | d55cb5b | 2022-12-22 14:26:55 +0100 | [diff] [blame] | 178 | #define MBEDTLS_MD_MAX_SIZE         48  /* longest known is SHA384 */ | 
| Dave Rodgman | 0442e1b | 2023-06-08 16:03:33 +0100 | [diff] [blame] | 179 | #elif defined(MBEDTLS_MD_CAN_SHA256) || defined(MBEDTLS_MD_CAN_SHA3_256) | 
| Valerio Setti | d55cb5b | 2022-12-22 14:26:55 +0100 | [diff] [blame] | 180 | #define MBEDTLS_MD_MAX_SIZE         32  /* longest known is SHA256 */ | 
| Dave Rodgman | 0442e1b | 2023-06-08 16:03:33 +0100 | [diff] [blame] | 181 | #elif defined(MBEDTLS_MD_CAN_SHA224) || defined(MBEDTLS_MD_CAN_SHA3_224) | 
| Valerio Setti | d55cb5b | 2022-12-22 14:26:55 +0100 | [diff] [blame] | 182 | #define MBEDTLS_MD_MAX_SIZE         28  /* longest known is SHA224 */ | 
| Paul Bakker | 7db0109 | 2013-09-10 11:10:57 +0200 | [diff] [blame] | 183 | #else | 
| Gilles Peskine | 83d9e09 | 2022-10-22 18:32:43 +0200 | [diff] [blame] | 184 | #define MBEDTLS_MD_MAX_SIZE         20  /* longest known is SHA1 or RIPE MD-160 | 
|  | 185 | or smaller (MD5 and earlier) */ | 
| Paul Bakker | 7db0109 | 2013-09-10 11:10:57 +0200 | [diff] [blame] | 186 | #endif | 
| Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 187 |  | 
| Dave Rodgman | ff45d44 | 2023-06-08 10:11:34 +0100 | [diff] [blame] | 188 | #if defined(MBEDTLS_MD_CAN_SHA3_224) | 
| Pol Henarejos | 4712d4c | 2022-05-20 14:17:14 +0200 | [diff] [blame] | 189 | #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] | 190 | #elif defined(MBEDTLS_MD_CAN_SHA3_256) | 
|  | 191 | #define MBEDTLS_MD_MAX_BLOCK_SIZE         136 | 
| Dave Rodgman | f956312 | 2023-06-11 16:04:29 +0100 | [diff] [blame] | 192 | #elif defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA384) | 
| Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 193 | #define MBEDTLS_MD_MAX_BLOCK_SIZE         128 | 
| Dave Rodgman | 0442e1b | 2023-06-08 16:03:33 +0100 | [diff] [blame] | 194 | #elif defined(MBEDTLS_MD_CAN_SHA3_384) | 
|  | 195 | #define MBEDTLS_MD_MAX_BLOCK_SIZE         104 | 
|  | 196 | #elif defined(MBEDTLS_MD_CAN_SHA3_512) | 
|  | 197 | #define MBEDTLS_MD_MAX_BLOCK_SIZE         72 | 
| Hanno Becker | 2e24c3b | 2017-12-27 21:28:58 +0000 | [diff] [blame] | 198 | #else | 
|  | 199 | #define MBEDTLS_MD_MAX_BLOCK_SIZE         64 | 
|  | 200 | #endif | 
|  | 201 |  | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 202 | /** | 
| Chris Jones | 3848e31 | 2021-03-11 16:17:59 +0000 | [diff] [blame] | 203 | * Opaque struct. | 
|  | 204 | * | 
|  | 205 | * Constructed using either #mbedtls_md_info_from_string or | 
|  | 206 | * #mbedtls_md_info_from_type. | 
|  | 207 | * | 
|  | 208 | * Fields can be accessed with #mbedtls_md_get_size, | 
|  | 209 | * #mbedtls_md_get_type and #mbedtls_md_get_name. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 210 | */ | 
| Chris Jones | 3848e31 | 2021-03-11 16:17:59 +0000 | [diff] [blame] | 211 | /* Defined internally in library/md_wrap.h. */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | typedef struct mbedtls_md_info_t mbedtls_md_info_t; | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 213 |  | 
|  | 214 | /** | 
| Manuel Pégourié-Gonnard | d8ea37f | 2023-03-09 10:46:22 +0100 | [diff] [blame] | 215 | * Used internally to indicate whether a context uses legacy or PSA. | 
|  | 216 | * | 
|  | 217 | * Internal use only. | 
|  | 218 | */ | 
|  | 219 | typedef enum { | 
|  | 220 | MBEDTLS_MD_ENGINE_LEGACY = 0, | 
|  | 221 | MBEDTLS_MD_ENGINE_PSA, | 
|  | 222 | } mbedtls_md_engine_t; | 
|  | 223 |  | 
|  | 224 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 225 | * The generic message-digest context. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 226 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 227 | typedef struct mbedtls_md_context_t { | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 228 | /** Information about the associated message digest. */ | 
| Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 229 | const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 230 |  | 
| Manuel Pégourié-Gonnard | d8ea37f | 2023-03-09 10:46:22 +0100 | [diff] [blame] | 231 | #if defined(MBEDTLS_MD_SOME_PSA) | 
|  | 232 | /** Are hash operations dispatched to PSA or legacy? */ | 
|  | 233 | mbedtls_md_engine_t MBEDTLS_PRIVATE(engine); | 
|  | 234 | #endif | 
|  | 235 |  | 
|  | 236 | /** The digest-specific context (legacy) or the PSA operation. */ | 
| Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 237 | void *MBEDTLS_PRIVATE(md_ctx); | 
| Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 238 |  | 
| Manuel Pégourié-Gonnard | 39a376a | 2023-03-09 17:21:40 +0100 | [diff] [blame] | 239 | #if defined(MBEDTLS_MD_C) | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 240 | /** The HMAC part of the context. */ | 
| Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 241 | void *MBEDTLS_PRIVATE(hmac_ctx); | 
| Manuel Pégourié-Gonnard | 39a376a | 2023-03-09 17:21:40 +0100 | [diff] [blame] | 242 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | } mbedtls_md_context_t; | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 244 |  | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 245 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 246 | * \brief           This function returns the message-digest information | 
|  | 247 | *                  associated with the given digest type. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 248 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 249 | * \param md_type   The type of digest to search for. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 250 | * | 
| Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 251 | * \return          The message-digest information associated with \p md_type. | 
|  | 252 | * \return          NULL if the associated message-digest information is not found. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 253 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 254 | 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] | 255 |  | 
|  | 256 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 257 | * \brief           This function initializes a message-digest context without | 
|  | 258 | *                  binding it to a particular message-digest algorithm. | 
|  | 259 | * | 
|  | 260 | *                  This function should always be called first. It prepares the | 
|  | 261 | *                  context for mbedtls_md_setup() for binding it to a | 
|  | 262 | *                  message-digest algorithm. | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 263 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 264 | void mbedtls_md_init(mbedtls_md_context_t *ctx); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 265 |  | 
|  | 266 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 267 | * \brief           This function clears the internal structure of \p ctx and | 
|  | 268 | *                  frees any embedded internal structure, but does not free | 
|  | 269 | *                  \p ctx itself. | 
|  | 270 | * | 
|  | 271 | *                  If you have called mbedtls_md_setup() on \p ctx, you must | 
|  | 272 | *                  call mbedtls_md_free() when you are no longer using the | 
|  | 273 | *                  context. | 
|  | 274 | *                  Calling this function if you have previously | 
|  | 275 | *                  called mbedtls_md_init() and nothing else is optional. | 
|  | 276 | *                  You must not call this function if you have not called | 
|  | 277 | *                  mbedtls_md_init(). | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 278 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 279 | void mbedtls_md_free(mbedtls_md_context_t *ctx); | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 280 |  | 
| Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame] | 281 |  | 
| Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 282 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 283 | * \brief           This function selects the message digest algorithm to use, | 
|  | 284 | *                  and allocates internal structures. | 
| Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 285 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 286 | *                  It should be called after mbedtls_md_init() or | 
|  | 287 | *                  mbedtls_md_free(). Makes it necessary to call | 
|  | 288 | *                  mbedtls_md_free() later. | 
|  | 289 | * | 
|  | 290 | * \param ctx       The context to set up. | 
|  | 291 | * \param md_info   The information structure of the message-digest algorithm | 
|  | 292 | *                  to use. | 
| Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 293 | * \param hmac      Defines if HMAC is used. 0: HMAC is not used (saves some memory), | 
|  | 294 | *                  or non-zero: HMAC is used with this context. | 
| Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 295 | * | 
| Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 296 | * \return          \c 0 on success. | 
|  | 297 | * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification | 
|  | 298 | *                  failure. | 
|  | 299 | * \return          #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure. | 
| Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 300 | */ | 
| Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 301 | MBEDTLS_CHECK_RETURN_TYPICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | 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] | 303 |  | 
|  | 304 | /** | 
| Tom Cosgrove | ce7f18c | 2022-07-28 05:50:56 +0100 | [diff] [blame] | 305 | * \brief           This function clones the state of a message-digest | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 306 | *                  context. | 
| Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 307 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 308 | * \note            You must call mbedtls_md_setup() on \c dst before calling | 
|  | 309 | *                  this function. | 
| Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 310 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 311 | * \note            The two contexts must have the same type, | 
|  | 312 | *                  for example, both are SHA-256. | 
| Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 313 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 314 | * \warning         This function clones the message-digest state, not the | 
|  | 315 | *                  HMAC state. | 
|  | 316 | * | 
|  | 317 | * \param dst       The destination context. | 
|  | 318 | * \param src       The context to be cloned. | 
| Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 319 | * | 
| Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 320 | * \return          \c 0 on success. | 
| Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 321 | * \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] | 322 | * \return          #MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE if both contexts are | 
|  | 323 | *                  not using the same engine. This can be avoided by moving | 
|  | 324 | *                  the call to psa_crypto_init() before the first call to | 
|  | 325 | *                  mbedtls_md_setup(). | 
| Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [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_clone(mbedtls_md_context_t *dst, | 
|  | 329 | const mbedtls_md_context_t *src); | 
| Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 330 |  | 
|  | 331 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 332 | * \brief           This function extracts the message-digest size from the | 
|  | 333 | *                  message-digest information structure. | 
| 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 | * \param md_info   The information structure of the message-digest algorithm | 
|  | 336 | *                  to use. | 
| 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 | * \return          The size of the message-digest output in Bytes. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 339 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | 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] | 341 |  | 
|  | 342 | /** | 
| Manuel Pégourié-Gonnard | 1ef26e2 | 2023-01-27 11:47:05 +0100 | [diff] [blame] | 343 | * \brief           This function gives the message-digest size associated to | 
|  | 344 | *                  message-digest type. | 
|  | 345 | * | 
|  | 346 | * \param md_type   The message-digest type. | 
|  | 347 | * | 
|  | 348 | * \return          The size of the message-digest output in Bytes, | 
|  | 349 | *                  or 0 if the message-digest type is not known. | 
|  | 350 | */ | 
|  | 351 | static inline unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type) | 
|  | 352 | { | 
|  | 353 | return mbedtls_md_get_size(mbedtls_md_info_from_type(md_type)); | 
|  | 354 | } | 
|  | 355 |  | 
|  | 356 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 357 | * \brief           This function extracts the message-digest type from the | 
|  | 358 | *                  message-digest information structure. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 359 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 360 | * \param md_info   The information structure of the message-digest algorithm | 
|  | 361 | *                  to use. | 
| 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 | * \return          The type of the message digest. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 364 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | 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] | 366 |  | 
|  | 367 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 368 | * \brief           This function starts a message-digest computation. | 
| 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 | *                  You must call this function after setting up the context | 
|  | 371 | *                  with mbedtls_md_setup(), and before passing data with | 
|  | 372 | *                  mbedtls_md_update(). | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 373 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 374 | * \param ctx       The generic message-digest context. | 
|  | 375 | * | 
| Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 376 | * \return          \c 0 on success. | 
|  | 377 | * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification | 
|  | 378 | *                  failure. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 379 | */ | 
| Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 380 | MBEDTLS_CHECK_RETURN_TYPICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | int mbedtls_md_starts(mbedtls_md_context_t *ctx); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 382 |  | 
|  | 383 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 384 | * \brief           This function feeds an input buffer into an ongoing | 
|  | 385 | *                  message-digest computation. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 386 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 387 | *                  You must call mbedtls_md_starts() before calling this | 
|  | 388 | *                  function. You may call this function multiple times. | 
|  | 389 | *                  Afterwards, call mbedtls_md_finish(). | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 390 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 391 | * \param ctx       The generic message-digest context. | 
|  | 392 | * \param input     The buffer holding the input data. | 
|  | 393 | * \param ilen      The length of the input data. | 
|  | 394 | * | 
| Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 395 | * \return          \c 0 on success. | 
|  | 396 | * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification | 
|  | 397 | *                  failure. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 398 | */ | 
| Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 399 | MBEDTLS_CHECK_RETURN_TYPICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | 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] | 401 |  | 
|  | 402 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 403 | * \brief           This function finishes the digest operation, | 
|  | 404 | *                  and writes the result to the output buffer. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 405 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 406 | *                  Call this function after a call to mbedtls_md_starts(), | 
|  | 407 | *                  followed by any number of calls to mbedtls_md_update(). | 
|  | 408 | *                  Afterwards, you may either clear the context with | 
|  | 409 | *                  mbedtls_md_free(), or call mbedtls_md_starts() to reuse | 
|  | 410 | *                  the context for another digest operation with the same | 
|  | 411 | *                  algorithm. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 412 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 413 | * \param ctx       The generic message-digest context. | 
|  | 414 | * \param output    The buffer for the generic message-digest checksum result. | 
|  | 415 | * | 
| Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 416 | * \return          \c 0 on success. | 
|  | 417 | * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification | 
|  | 418 | *                  failure. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 419 | */ | 
| Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 420 | MBEDTLS_CHECK_RETURN_TYPICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output); | 
| 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 calculates the message-digest of a buffer, | 
|  | 425 | *                 with respect to a configurable message-digest algorithm | 
|  | 426 | *                 in a single call. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 427 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 428 | *                 The result is calculated as | 
|  | 429 | *                 Output = message_digest(input buffer). | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 430 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 431 | * \param md_info  The information structure of the message-digest algorithm | 
|  | 432 | *                 to use. | 
|  | 433 | * \param input    The buffer holding the data. | 
|  | 434 | * \param ilen     The length of the input data. | 
|  | 435 | * \param output   The generic message-digest checksum result. | 
|  | 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(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, | 
|  | 443 | unsigned char *output); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 444 |  | 
| Manuel Pégourié-Gonnard | 82a4394 | 2023-02-23 09:36:29 +0100 | [diff] [blame] | 445 | /** | 
|  | 446 | * \brief           This function returns the list of digests supported by the | 
|  | 447 | *                  generic digest module. | 
|  | 448 | * | 
|  | 449 | * \note            The list starts with the strongest available hashes. | 
|  | 450 | * | 
|  | 451 | * \return          A statically allocated array of digests. Each element | 
|  | 452 | *                  in the returned list is an integer belonging to the | 
|  | 453 | *                  message-digest enumeration #mbedtls_md_type_t. | 
|  | 454 | *                  The last entry is 0. | 
|  | 455 | */ | 
|  | 456 | const int *mbedtls_md_list(void); | 
|  | 457 |  | 
|  | 458 | /** | 
|  | 459 | * \brief           This function returns the message-digest information | 
|  | 460 | *                  associated with the given digest name. | 
|  | 461 | * | 
|  | 462 | * \param md_name   The name of the digest to search for. | 
|  | 463 | * | 
|  | 464 | * \return          The message-digest information associated with \p md_name. | 
|  | 465 | * \return          NULL if the associated message-digest information is not found. | 
|  | 466 | */ | 
|  | 467 | const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name); | 
|  | 468 |  | 
|  | 469 | /** | 
|  | 470 | * \brief           This function extracts the message-digest name from the | 
|  | 471 | *                  message-digest information structure. | 
|  | 472 | * | 
|  | 473 | * \param md_info   The information structure of the message-digest algorithm | 
|  | 474 | *                  to use. | 
|  | 475 | * | 
|  | 476 | * \return          The name of the message digest. | 
|  | 477 | */ | 
|  | 478 | const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info); | 
|  | 479 |  | 
|  | 480 | /** | 
|  | 481 | * \brief           This function returns the message-digest information | 
|  | 482 | *                  from the given context. | 
|  | 483 | * | 
|  | 484 | * \param ctx       The context from which to extract the information. | 
|  | 485 | *                  This must be initialized (or \c NULL). | 
|  | 486 | * | 
|  | 487 | * \return          The message-digest information associated with \p ctx. | 
|  | 488 | * \return          \c NULL if \p ctx is \c NULL. | 
|  | 489 | */ | 
|  | 490 | const mbedtls_md_info_t *mbedtls_md_info_from_ctx( | 
|  | 491 | const mbedtls_md_context_t *ctx); | 
|  | 492 |  | 
| Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 493 | #if defined(MBEDTLS_FS_IO) | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 494 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 495 | * \brief          This function calculates the message-digest checksum | 
|  | 496 | *                 result of the contents of the provided file. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 497 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 498 | *                 The result is calculated as | 
|  | 499 | *                 Output = message_digest(file contents). | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 500 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 501 | * \param md_info  The information structure of the message-digest algorithm | 
|  | 502 | *                 to use. | 
|  | 503 | * \param path     The input file name. | 
|  | 504 | * \param output   The generic message-digest checksum result. | 
|  | 505 | * | 
| Rose Zadik | 8c9c794 | 2018-03-27 11:52:58 +0100 | [diff] [blame] | 506 | * \return         \c 0 on success. | 
| Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 507 | * \return         #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing | 
|  | 508 | *                 the file pointed by \p path. | 
|  | 509 | * \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] | 510 | */ | 
| Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 511 | MBEDTLS_CHECK_RETURN_TYPICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path, | 
|  | 513 | unsigned char *output); | 
| Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 514 | #endif /* MBEDTLS_FS_IO */ | 
| 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 sets the HMAC key and prepares to | 
|  | 518 | *                  authenticate a new message. | 
| 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_setup(), to use | 
|  | 521 | *                  the MD context for an HMAC calculation, then call | 
|  | 522 | *                  mbedtls_md_hmac_update() to provide the input data, and | 
|  | 523 | *                  mbedtls_md_hmac_finish() to get the HMAC value. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 524 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 525 | * \param ctx       The message digest context containing an embedded HMAC | 
|  | 526 | *                  context. | 
|  | 527 | * \param key       The HMAC secret key. | 
|  | 528 | * \param keylen    The length of the HMAC key in Bytes. | 
|  | 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_starts(mbedtls_md_context_t *ctx, const unsigned char *key, | 
|  | 536 | size_t keylen); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 537 |  | 
|  | 538 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 539 | * \brief           This function feeds an input buffer into an ongoing HMAC | 
|  | 540 | *                  computation. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 541 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 542 | *                  Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset() | 
|  | 543 | *                  before calling this function. | 
|  | 544 | *                  You may call this function multiple times to pass the | 
|  | 545 | *                  input piecewise. | 
|  | 546 | *                  Afterwards, call mbedtls_md_hmac_finish(). | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 547 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 548 | * \param ctx       The message digest context containing an embedded HMAC | 
|  | 549 | *                  context. | 
|  | 550 | * \param input     The buffer holding the input data. | 
|  | 551 | * \param ilen      The length of the input data. | 
|  | 552 | * | 
| Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 553 | * \return          \c 0 on success. | 
|  | 554 | * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification | 
|  | 555 | *                  failure. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 556 | */ | 
| Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 557 | MBEDTLS_CHECK_RETURN_TYPICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input, | 
|  | 559 | size_t ilen); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 560 |  | 
|  | 561 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 562 | * \brief           This function finishes the HMAC operation, and writes | 
|  | 563 | *                  the result to the output buffer. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 564 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 565 | *                  Call this function after mbedtls_md_hmac_starts() and | 
|  | 566 | *                  mbedtls_md_hmac_update() to get the HMAC value. Afterwards | 
|  | 567 | *                  you may either call mbedtls_md_free() to clear the context, | 
|  | 568 | *                  or call mbedtls_md_hmac_reset() to reuse the context with | 
|  | 569 | *                  the same HMAC key. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 570 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 571 | * \param ctx       The message digest context containing an embedded HMAC | 
|  | 572 | *                  context. | 
|  | 573 | * \param output    The generic HMAC checksum 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_finish(mbedtls_md_context_t *ctx, unsigned char *output); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 581 |  | 
|  | 582 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 583 | * \brief           This function prepares to authenticate a new message with | 
|  | 584 | *                  the same key as the previous HMAC operation. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 585 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 586 | *                  You may call this function after mbedtls_md_hmac_finish(). | 
|  | 587 | *                  Afterwards call mbedtls_md_hmac_update() to pass the new | 
|  | 588 | *                  input. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 589 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 590 | * \param ctx       The message digest context containing an embedded HMAC | 
|  | 591 | *                  context. | 
|  | 592 | * | 
| Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 593 | * \return          \c 0 on success. | 
|  | 594 | * \return          #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification | 
|  | 595 | *                  failure. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 596 | */ | 
| Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 597 | MBEDTLS_CHECK_RETURN_TYPICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 599 |  | 
|  | 600 | /** | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 601 | * \brief          This function calculates the full generic HMAC | 
|  | 602 | *                 on the input buffer with the provided key. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 603 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 604 | *                 The function allocates the context, performs the | 
|  | 605 | *                 calculation, and frees the context. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 606 | * | 
| Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 607 | *                 The HMAC result is calculated as | 
|  | 608 | *                 output = generic HMAC(hmac key, input buffer). | 
|  | 609 | * | 
|  | 610 | * \param md_info  The information structure of the message-digest algorithm | 
|  | 611 | *                 to use. | 
|  | 612 | * \param key      The HMAC secret key. | 
|  | 613 | * \param keylen   The length of the HMAC secret key in Bytes. | 
|  | 614 | * \param input    The buffer holding the input data. | 
|  | 615 | * \param ilen     The length of the input data. | 
|  | 616 | * \param output   The generic HMAC result. | 
|  | 617 | * | 
| Rose Zadik | f3e4736 | 2018-04-16 16:31:16 +0100 | [diff] [blame] | 618 | * \return         \c 0 on success. | 
|  | 619 | * \return         #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification | 
|  | 620 | *                 failure. | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 621 | */ | 
| Gilles Peskine | ecf6beb | 2021-12-10 21:35:10 +0100 | [diff] [blame] | 622 | MBEDTLS_CHECK_RETURN_TYPICAL | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 623 | int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen, | 
|  | 624 | const unsigned char *input, size_t ilen, | 
|  | 625 | unsigned char *output); | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 626 |  | 
| Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 627 | #ifdef __cplusplus | 
|  | 628 | } | 
|  | 629 | #endif | 
|  | 630 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 631 | #endif /* MBEDTLS_MD_H */ |