Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 1 | /** |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 2 | * \file md.h |
| 3 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 4 | * \brief This file contains the generic message-digest wrapper. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 7 | */ |
| 8 | /* |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 9 | * Copyright The Mbed TLS Contributors |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: Apache-2.0 |
| 11 | * |
| 12 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 13 | * not use this file except in compliance with the License. |
| 14 | * You may obtain a copy of the License at |
| 15 | * |
| 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | * |
| 18 | * Unless required by applicable law or agreed to in writing, software |
| 19 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | * See the License for the specific language governing permissions and |
| 22 | * limitations under the License. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 23 | */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 24 | |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 25 | #ifndef MBEDTLS_MD_H |
| 26 | #define MBEDTLS_MD_H |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 27 | #include "mbedtls/private_access.h" |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 28 | |
| 29 | #include <stddef.h> |
| 30 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 31 | #include "mbedtls/build_info.h" |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 32 | |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 33 | #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */ |
| 34 | #define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */ |
| 35 | #define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */ |
| 36 | #define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */ |
| 37 | |
| 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 42 | /** |
| 43 | * \brief Supported message digests. |
| 44 | * |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 45 | * \warning MD5 and SHA-1 are considered weak message digests and |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 46 | * their use constitutes a security risk. We recommend considering |
| 47 | * stronger message digests instead. |
| 48 | * |
| 49 | */ |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 50 | typedef enum { |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 51 | MBEDTLS_MD_NONE=0, /**< None. */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 52 | MBEDTLS_MD_MD5, /**< The MD5 message digest. */ |
| 53 | MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */ |
| 54 | MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */ |
| 55 | MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */ |
| 56 | MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */ |
| 57 | MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */ |
| 58 | MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */ |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 59 | } mbedtls_md_type_t; |
| 60 | |
| 61 | #if defined(MBEDTLS_SHA512_C) |
| 62 | #define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */ |
| 63 | #else |
| 64 | #define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */ |
| 65 | #endif |
| 66 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 67 | #if defined(MBEDTLS_SHA512_C) |
| 68 | #define MBEDTLS_MD_MAX_BLOCK_SIZE 128 |
| 69 | #else |
| 70 | #define MBEDTLS_MD_MAX_BLOCK_SIZE 64 |
| 71 | #endif |
| 72 | |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 73 | /** |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 74 | * Opaque struct. |
| 75 | * |
| 76 | * Constructed using either #mbedtls_md_info_from_string or |
| 77 | * #mbedtls_md_info_from_type. |
| 78 | * |
| 79 | * Fields can be accessed with #mbedtls_md_get_size, |
| 80 | * #mbedtls_md_get_type and #mbedtls_md_get_name. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 81 | */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 82 | /* Defined internally in library/md_wrap.h. */ |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 83 | typedef struct mbedtls_md_info_t mbedtls_md_info_t; |
| 84 | |
| 85 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 86 | * The generic message-digest context. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 87 | */ |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 88 | typedef struct mbedtls_md_context_t |
| 89 | { |
| 90 | /** Information about the associated message digest. */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 91 | const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info); |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 92 | |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 93 | /** The digest-specific context. */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 94 | void *MBEDTLS_PRIVATE(md_ctx); |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 95 | |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 96 | /** The HMAC part of the context. */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 97 | void *MBEDTLS_PRIVATE(hmac_ctx); |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 98 | } mbedtls_md_context_t; |
| 99 | |
| 100 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 101 | * \brief This function returns the list of digests supported by the |
| 102 | * generic digest module. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 103 | * |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 104 | * \note The list starts with the strongest available hashes. |
| 105 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 106 | * \return A statically allocated array of digests. Each element |
| 107 | * in the returned list is an integer belonging to the |
| 108 | * message-digest enumeration #mbedtls_md_type_t. |
| 109 | * The last entry is 0. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 110 | */ |
| 111 | const int *mbedtls_md_list( void ); |
| 112 | |
| 113 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 114 | * \brief This function returns the message-digest information |
| 115 | * associated with the given digest name. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 116 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 117 | * \param md_name The name of the digest to search for. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 118 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 119 | * \return The message-digest information associated with \p md_name. |
| 120 | * \return NULL if the associated message-digest information is not found. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 121 | */ |
| 122 | const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name ); |
| 123 | |
| 124 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 125 | * \brief This function returns the message-digest information |
| 126 | * associated with the given digest type. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 127 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 128 | * \param md_type The type of digest to search for. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 129 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 130 | * \return The message-digest information associated with \p md_type. |
| 131 | * \return NULL if the associated message-digest information is not found. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 132 | */ |
| 133 | const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type ); |
| 134 | |
| 135 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 136 | * \brief This function initializes a message-digest context without |
| 137 | * binding it to a particular message-digest algorithm. |
| 138 | * |
| 139 | * This function should always be called first. It prepares the |
| 140 | * context for mbedtls_md_setup() for binding it to a |
| 141 | * message-digest algorithm. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 142 | */ |
| 143 | void mbedtls_md_init( mbedtls_md_context_t *ctx ); |
| 144 | |
| 145 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 146 | * \brief This function clears the internal structure of \p ctx and |
| 147 | * frees any embedded internal structure, but does not free |
| 148 | * \p ctx itself. |
| 149 | * |
| 150 | * If you have called mbedtls_md_setup() on \p ctx, you must |
| 151 | * call mbedtls_md_free() when you are no longer using the |
| 152 | * context. |
| 153 | * Calling this function if you have previously |
| 154 | * called mbedtls_md_init() and nothing else is optional. |
| 155 | * You must not call this function if you have not called |
| 156 | * mbedtls_md_init(). |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 157 | */ |
| 158 | void mbedtls_md_free( mbedtls_md_context_t *ctx ); |
| 159 | |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 160 | |
| 161 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 162 | * \brief This function selects the message digest algorithm to use, |
| 163 | * and allocates internal structures. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 164 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 165 | * It should be called after mbedtls_md_init() or |
| 166 | * mbedtls_md_free(). Makes it necessary to call |
| 167 | * mbedtls_md_free() later. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 168 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 169 | * \param ctx The context to set up. |
| 170 | * \param md_info The information structure of the message-digest algorithm |
| 171 | * to use. |
| 172 | * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory), |
| 173 | * or non-zero: HMAC is used with this context. |
| 174 | * |
| 175 | * \return \c 0 on success. |
| 176 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 177 | * failure. |
| 178 | * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 179 | */ |
| 180 | int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac ); |
| 181 | |
| 182 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 183 | * \brief This function clones the state of an message-digest |
| 184 | * context. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 185 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 186 | * \note You must call mbedtls_md_setup() on \c dst before calling |
| 187 | * this function. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 188 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 189 | * \note The two contexts must have the same type, |
| 190 | * for example, both are SHA-256. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 191 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 192 | * \warning This function clones the message-digest state, not the |
| 193 | * HMAC state. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 194 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 195 | * \param dst The destination context. |
| 196 | * \param src The context to be cloned. |
| 197 | * |
| 198 | * \return \c 0 on success. |
| 199 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 200 | */ |
| 201 | int mbedtls_md_clone( mbedtls_md_context_t *dst, |
| 202 | const mbedtls_md_context_t *src ); |
| 203 | |
| 204 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 205 | * \brief This function extracts the message-digest size from the |
| 206 | * message-digest information structure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 207 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 208 | * \param md_info The information structure of the message-digest algorithm |
| 209 | * to use. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 210 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 211 | * \return The size of the message-digest output in Bytes. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 212 | */ |
| 213 | unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info ); |
| 214 | |
| 215 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 216 | * \brief This function extracts the message-digest type from the |
| 217 | * message-digest information structure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 218 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 219 | * \param md_info The information structure of the message-digest algorithm |
| 220 | * to use. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 221 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 222 | * \return The type of the message digest. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 223 | */ |
| 224 | mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info ); |
| 225 | |
| 226 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 227 | * \brief This function extracts the message-digest name from the |
| 228 | * message-digest information structure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 229 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 230 | * \param md_info The information structure of the message-digest algorithm |
| 231 | * to use. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 232 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 233 | * \return The name of the message digest. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 234 | */ |
| 235 | const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info ); |
| 236 | |
| 237 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 238 | * \brief This function starts a message-digest computation. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 239 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 240 | * You must call this function after setting up the context |
| 241 | * with mbedtls_md_setup(), and before passing data with |
| 242 | * mbedtls_md_update(). |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 243 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 244 | * \param ctx The generic message-digest context. |
| 245 | * |
| 246 | * \return \c 0 on success. |
| 247 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 248 | * failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 249 | */ |
| 250 | int mbedtls_md_starts( mbedtls_md_context_t *ctx ); |
| 251 | |
| 252 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 253 | * \brief This function feeds an input buffer into an ongoing |
| 254 | * message-digest computation. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 255 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 256 | * You must call mbedtls_md_starts() before calling this |
| 257 | * function. You may call this function multiple times. |
| 258 | * Afterwards, call mbedtls_md_finish(). |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 259 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 260 | * \param ctx The generic message-digest context. |
| 261 | * \param input The buffer holding the input data. |
| 262 | * \param ilen The length of the input data. |
| 263 | * |
| 264 | * \return \c 0 on success. |
| 265 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 266 | * failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 267 | */ |
| 268 | int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen ); |
| 269 | |
| 270 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 271 | * \brief This function finishes the digest operation, |
| 272 | * and writes the result to the output buffer. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 273 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 274 | * Call this function after a call to mbedtls_md_starts(), |
| 275 | * followed by any number of calls to mbedtls_md_update(). |
| 276 | * Afterwards, you may either clear the context with |
| 277 | * mbedtls_md_free(), or call mbedtls_md_starts() to reuse |
| 278 | * the context for another digest operation with the same |
| 279 | * algorithm. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 280 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 281 | * \param ctx The generic message-digest context. |
| 282 | * \param output The buffer for the generic message-digest checksum result. |
| 283 | * |
| 284 | * \return \c 0 on success. |
| 285 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 286 | * failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 287 | */ |
| 288 | int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output ); |
| 289 | |
| 290 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 291 | * \brief This function calculates the message-digest of a buffer, |
| 292 | * with respect to a configurable message-digest algorithm |
| 293 | * in a single call. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 294 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 295 | * The result is calculated as |
| 296 | * Output = message_digest(input buffer). |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 297 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 298 | * \param md_info The information structure of the message-digest algorithm |
| 299 | * to use. |
| 300 | * \param input The buffer holding the data. |
| 301 | * \param ilen The length of the input data. |
| 302 | * \param output The generic message-digest checksum result. |
| 303 | * |
| 304 | * \return \c 0 on success. |
| 305 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 306 | * failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 307 | */ |
| 308 | int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, |
| 309 | unsigned char *output ); |
| 310 | |
| 311 | #if defined(MBEDTLS_FS_IO) |
| 312 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 313 | * \brief This function calculates the message-digest checksum |
| 314 | * result of the contents of the provided file. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 315 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 316 | * The result is calculated as |
| 317 | * Output = message_digest(file contents). |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 318 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 319 | * \param md_info The information structure of the message-digest algorithm |
| 320 | * to use. |
| 321 | * \param path The input file name. |
| 322 | * \param output The generic message-digest checksum result. |
| 323 | * |
| 324 | * \return \c 0 on success. |
| 325 | * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing |
| 326 | * the file pointed by \p path. |
| 327 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 328 | */ |
| 329 | int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, |
| 330 | unsigned char *output ); |
| 331 | #endif /* MBEDTLS_FS_IO */ |
| 332 | |
| 333 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 334 | * \brief This function sets the HMAC key and prepares to |
| 335 | * authenticate a new message. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 336 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 337 | * Call this function after mbedtls_md_setup(), to use |
| 338 | * the MD context for an HMAC calculation, then call |
| 339 | * mbedtls_md_hmac_update() to provide the input data, and |
| 340 | * mbedtls_md_hmac_finish() to get the HMAC value. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 341 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 342 | * \param ctx The message digest context containing an embedded HMAC |
| 343 | * context. |
| 344 | * \param key The HMAC secret key. |
| 345 | * \param keylen The length of the HMAC key in Bytes. |
| 346 | * |
| 347 | * \return \c 0 on success. |
| 348 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 349 | * failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 350 | */ |
| 351 | int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, |
| 352 | size_t keylen ); |
| 353 | |
| 354 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 355 | * \brief This function feeds an input buffer into an ongoing HMAC |
| 356 | * computation. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 357 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 358 | * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset() |
| 359 | * before calling this function. |
| 360 | * You may call this function multiple times to pass the |
| 361 | * input piecewise. |
| 362 | * Afterwards, call mbedtls_md_hmac_finish(). |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 363 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 364 | * \param ctx The message digest context containing an embedded HMAC |
| 365 | * context. |
| 366 | * \param input The buffer holding the input data. |
| 367 | * \param ilen The length of the input data. |
| 368 | * |
| 369 | * \return \c 0 on success. |
| 370 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 371 | * failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 372 | */ |
| 373 | int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, |
| 374 | size_t ilen ); |
| 375 | |
| 376 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 377 | * \brief This function finishes the HMAC operation, and writes |
| 378 | * the result to the output buffer. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 379 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 380 | * Call this function after mbedtls_md_hmac_starts() and |
| 381 | * mbedtls_md_hmac_update() to get the HMAC value. Afterwards |
| 382 | * you may either call mbedtls_md_free() to clear the context, |
| 383 | * or call mbedtls_md_hmac_reset() to reuse the context with |
| 384 | * the same HMAC key. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 385 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 386 | * \param ctx The message digest context containing an embedded HMAC |
| 387 | * context. |
| 388 | * \param output The generic HMAC checksum result. |
| 389 | * |
| 390 | * \return \c 0 on success. |
| 391 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 392 | * failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 393 | */ |
| 394 | int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output); |
| 395 | |
| 396 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 397 | * \brief This function prepares to authenticate a new message with |
| 398 | * the same key as the previous HMAC operation. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 399 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 400 | * You may call this function after mbedtls_md_hmac_finish(). |
| 401 | * Afterwards call mbedtls_md_hmac_update() to pass the new |
| 402 | * input. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 403 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 404 | * \param ctx The message digest context containing an embedded HMAC |
| 405 | * context. |
| 406 | * |
| 407 | * \return \c 0 on success. |
| 408 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 409 | * failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 410 | */ |
| 411 | int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx ); |
| 412 | |
| 413 | /** |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 414 | * \brief This function calculates the full generic HMAC |
| 415 | * on the input buffer with the provided key. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 416 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 417 | * The function allocates the context, performs the |
| 418 | * calculation, and frees the context. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 419 | * |
Fabio Utzig | 3ac36ea | 2018-12-27 12:35:39 -0200 | [diff] [blame] | 420 | * The HMAC result is calculated as |
| 421 | * output = generic HMAC(hmac key, input buffer). |
| 422 | * |
| 423 | * \param md_info The information structure of the message-digest algorithm |
| 424 | * to use. |
| 425 | * \param key The HMAC secret key. |
| 426 | * \param keylen The length of the HMAC secret key in Bytes. |
| 427 | * \param input The buffer holding the input data. |
| 428 | * \param ilen The length of the input data. |
| 429 | * \param output The generic HMAC result. |
| 430 | * |
| 431 | * \return \c 0 on success. |
| 432 | * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 433 | * failure. |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 434 | */ |
| 435 | int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen, |
| 436 | const unsigned char *input, size_t ilen, |
| 437 | unsigned char *output ); |
| 438 | |
| 439 | /* Internal use */ |
| 440 | int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data ); |
| 441 | |
| 442 | #ifdef __cplusplus |
| 443 | } |
| 444 | #endif |
| 445 | |
| 446 | #endif /* MBEDTLS_MD_H */ |