Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [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 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 4 | * \brief The generic message-digest wrapper. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
Darryl Green | a40a101 | 2018-01-05 15:33:17 +0000 | [diff] [blame] | 7 | */ |
| 8 | /* |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 9 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 11 | * |
| 12 | * This file is provided under the Apache License 2.0, or the |
| 13 | * GNU General Public License v2.0 or later. |
| 14 | * |
| 15 | * ********** |
| 16 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 17 | * |
| 18 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 19 | * not use this file except in compliance with the License. |
| 20 | * You may obtain a copy of the License at |
| 21 | * |
| 22 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 23 | * |
| 24 | * Unless required by applicable law or agreed to in writing, software |
| 25 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 26 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 27 | * See the License for the specific language governing permissions and |
| 28 | * limitations under the License. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 29 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 30 | * ********** |
| 31 | * |
| 32 | * ********** |
| 33 | * GNU General Public License v2.0 or later: |
| 34 | * |
| 35 | * This program is free software; you can redistribute it and/or modify |
| 36 | * it under the terms of the GNU General Public License as published by |
| 37 | * the Free Software Foundation; either version 2 of the License, or |
| 38 | * (at your option) any later version. |
| 39 | * |
| 40 | * This program is distributed in the hope that it will be useful, |
| 41 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 42 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 43 | * GNU General Public License for more details. |
| 44 | * |
| 45 | * You should have received a copy of the GNU General Public License along |
| 46 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 47 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 48 | * |
| 49 | * ********** |
| 50 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 51 | * This file is part of Mbed TLS (https://tls.mbed.org) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 52 | */ |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 53 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #ifndef MBEDTLS_MD_H |
| 55 | #define MBEDTLS_MD_H |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 56 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 57 | #include <stddef.h> |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 58 | |
Ron Eldor | f231eaa | 2017-08-22 14:50:14 +0300 | [diff] [blame] | 59 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 60 | #include "config.h" |
| 61 | #else |
| 62 | #include MBEDTLS_CONFIG_FILE |
| 63 | #endif |
| 64 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */ |
| 66 | #define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */ |
| 67 | #define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */ |
| 68 | #define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */ |
Gilles Peskine | 7ecab3d | 2018-01-26 17:56:38 +0100 | [diff] [blame] | 69 | #define MBEDTLS_ERR_MD_HW_ACCEL_FAILED -0x5280 /**< MD hardware accelerator failed. */ |
Paul Bakker | 335db3f | 2011-04-25 15:28:35 +0000 | [diff] [blame] | 70 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 71 | #ifdef __cplusplus |
| 72 | extern "C" { |
| 73 | #endif |
| 74 | |
Hanno Becker | bbca8c5 | 2017-09-25 14:53:51 +0100 | [diff] [blame] | 75 | /** |
| 76 | * \brief Enumeration of supported message digests |
| 77 | * |
| 78 | * \warning MD2, MD4, MD5 and SHA-1 are considered weak message digests and |
| 79 | * their use constitutes a security risk. We recommend considering |
| 80 | * stronger message digests instead. |
| 81 | * |
| 82 | */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 83 | typedef enum { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 84 | MBEDTLS_MD_NONE=0, |
| 85 | MBEDTLS_MD_MD2, |
| 86 | MBEDTLS_MD_MD4, |
| 87 | MBEDTLS_MD_MD5, |
| 88 | MBEDTLS_MD_SHA1, |
| 89 | MBEDTLS_MD_SHA224, |
| 90 | MBEDTLS_MD_SHA256, |
| 91 | MBEDTLS_MD_SHA384, |
| 92 | MBEDTLS_MD_SHA512, |
| 93 | MBEDTLS_MD_RIPEMD160, |
| 94 | } mbedtls_md_type_t; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 95 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | #if defined(MBEDTLS_SHA512_C) |
| 97 | #define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */ |
Paul Bakker | 7db0109 | 2013-09-10 11:10:57 +0200 | [diff] [blame] | 98 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | #define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */ |
Paul Bakker | 7db0109 | 2013-09-10 11:10:57 +0200 | [diff] [blame] | 100 | #endif |
Paul Bakker | 1b57b06 | 2011-01-06 15:48:19 +0000 | [diff] [blame] | 101 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 102 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 103 | * Opaque struct defined in md_internal.h. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 104 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | typedef struct mbedtls_md_info_t mbedtls_md_info_t; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 106 | |
| 107 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 108 | * The generic message-digest context. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 109 | */ |
| 110 | typedef struct { |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 111 | /** Information about the associated message digest. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | const mbedtls_md_info_t *md_info; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 113 | |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 114 | /** The digest-specific context. */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 115 | void *md_ctx; |
Manuel Pégourié-Gonnard | 8379a82 | 2015-03-24 16:48:22 +0100 | [diff] [blame] | 116 | |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 117 | /** The HMAC part of the context. */ |
Manuel Pégourié-Gonnard | dfb3dc8 | 2015-03-25 11:49:07 +0100 | [diff] [blame] | 118 | void *hmac_ctx; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | } mbedtls_md_context_t; |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 120 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 121 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 122 | * \brief This function returns the list of digests supported by the |
| 123 | * generic digest module. |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 124 | * |
Manuel Pégourié-Gonnard | 75036a0 | 2020-05-22 12:12:36 +0200 | [diff] [blame] | 125 | * \note The list starts with the strongest available hashes. |
| 126 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 127 | * \return A statically allocated array of digests. Each element |
| 128 | * in the returned list is an integer belonging to the |
| 129 | * message-digest enumeration #mbedtls_md_type_t. |
| 130 | * The last entry is 0. |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 131 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | const int *mbedtls_md_list( void ); |
Paul Bakker | 72f6266 | 2011-01-16 21:27:44 +0000 | [diff] [blame] | 133 | |
| 134 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 135 | * \brief This function returns the message-digest information |
| 136 | * associated with the given digest name. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 137 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 138 | * \param md_name The name of the digest to search for. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 139 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 140 | * \return The message-digest information associated with \p md_name, |
| 141 | * or NULL if not found. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 142 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 144 | |
| 145 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 146 | * \brief This function returns the message-digest information |
| 147 | * associated with the given digest type. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 148 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 149 | * \param md_type The type of digest to search for. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 150 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 151 | * \return The message-digest information associated with \p md_type, |
| 152 | * or NULL if not found. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 153 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | 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] | 155 | |
| 156 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 157 | * \brief This function initializes a message-digest context without |
| 158 | * binding it to a particular message-digest algorithm. |
| 159 | * |
| 160 | * This function should always be called first. It prepares the |
| 161 | * context for mbedtls_md_setup() for binding it to a |
| 162 | * message-digest algorithm. |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 163 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | void mbedtls_md_init( mbedtls_md_context_t *ctx ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 165 | |
| 166 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 167 | * \brief This function clears the internal structure of \p ctx and |
| 168 | * frees any embedded internal structure, but does not free |
| 169 | * \p ctx itself. |
| 170 | * |
| 171 | * If you have called mbedtls_md_setup() on \p ctx, you must |
| 172 | * call mbedtls_md_free() when you are no longer using the |
| 173 | * context. |
| 174 | * Calling this function if you have previously |
| 175 | * called mbedtls_md_init() and nothing else is optional. |
| 176 | * You must not call this function if you have not called |
| 177 | * mbedtls_md_init(). |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 178 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 179 | void mbedtls_md_free( mbedtls_md_context_t *ctx ); |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | #if ! defined(MBEDTLS_DEPRECATED_REMOVED) |
| 182 | #if defined(MBEDTLS_DEPRECATED_WARNING) |
| 183 | #define MBEDTLS_DEPRECATED __attribute__((deprecated)) |
Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame] | 184 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | #define MBEDTLS_DEPRECATED |
Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame] | 186 | #endif |
| 187 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 188 | * \brief This function selects the message digest algorithm to use, |
| 189 | * and allocates internal structures. |
| 190 | * |
| 191 | * It should be called after mbedtls_md_init() or mbedtls_md_free(). |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | * Makes it necessary to call mbedtls_md_free() later. |
Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame] | 193 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | * \deprecated Superseded by mbedtls_md_setup() in 2.0.0 |
Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame] | 195 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 196 | * \param ctx The context to set up. |
| 197 | * \param md_info The information structure of the message-digest algorithm |
| 198 | * to use. |
Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame] | 199 | * |
Manuel Pégourié-Gonnard | eca510f | 2015-03-26 12:26:34 +0100 | [diff] [blame] | 200 | * \returns \c 0 on success, |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 201 | * #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure, |
| 202 | * #MBEDTLS_ERR_MD_ALLOC_FAILED memory allocation failure. |
Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame] | 203 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED; |
| 205 | #undef MBEDTLS_DEPRECATED |
| 206 | #endif /* MBEDTLS_DEPRECATED_REMOVED */ |
Manuel Pégourié-Gonnard | 147fa09 | 2015-03-25 16:43:14 +0100 | [diff] [blame] | 207 | |
Paul Bakker | 84bbeb5 | 2014-07-01 14:53:22 +0200 | [diff] [blame] | 208 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 209 | * \brief This function selects the message digest algorithm to use, |
| 210 | * and allocates internal structures. |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 211 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 212 | * It should be called after mbedtls_md_init() or |
| 213 | * mbedtls_md_free(). Makes it necessary to call |
| 214 | * mbedtls_md_free() later. |
| 215 | * |
| 216 | * \param ctx The context to set up. |
| 217 | * \param md_info The information structure of the message-digest algorithm |
| 218 | * to use. |
| 219 | * \param hmac <ul><li>0: HMAC is not used. Saves some memory.</li> |
| 220 | * <li>non-zero: HMAC is used with this context.</li></ul> |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 221 | * |
Manuel Pégourié-Gonnard | eca510f | 2015-03-26 12:26:34 +0100 | [diff] [blame] | 222 | * \returns \c 0 on success, |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 223 | * #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure, or |
| 224 | * #MBEDTLS_ERR_MD_ALLOC_FAILED on memory allocation failure. |
Paul Bakker | 562535d | 2011-01-20 16:42:01 +0000 | [diff] [blame] | 225 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | 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] | 227 | |
| 228 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 229 | * \brief This function clones the state of an message-digest |
| 230 | * context. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 231 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 232 | * \note You must call mbedtls_md_setup() on \c dst before calling |
| 233 | * this function. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 234 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 235 | * \note The two contexts must have the same type, |
| 236 | * for example, both are SHA-256. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 237 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 238 | * \warning This function clones the message-digest state, not the |
| 239 | * HMAC state. |
| 240 | * |
| 241 | * \param dst The destination context. |
| 242 | * \param src The context to be cloned. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 243 | * |
| 244 | * \return \c 0 on success, |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 245 | * #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure. |
Manuel Pégourié-Gonnard | 052a6c9 | 2015-07-06 16:06:02 +0200 | [diff] [blame] | 246 | */ |
| 247 | int mbedtls_md_clone( mbedtls_md_context_t *dst, |
| 248 | const mbedtls_md_context_t *src ); |
| 249 | |
| 250 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 251 | * \brief This function extracts the message-digest size from the |
| 252 | * message-digest information structure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 253 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 254 | * \param md_info The information structure of the message-digest algorithm |
| 255 | * to use. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 256 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 257 | * \return The size of the message-digest output in Bytes. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 258 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | 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] | 260 | |
| 261 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 262 | * \brief This function extracts the message-digest type from the |
| 263 | * message-digest information structure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 264 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 265 | * \param md_info The information structure of the message-digest algorithm |
| 266 | * to use. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 267 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 268 | * \return The type of the message digest. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 269 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | 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] | 271 | |
| 272 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 273 | * \brief This function extracts the message-digest name from the |
| 274 | * message-digest information structure. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 275 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 276 | * \param md_info The information structure of the message-digest algorithm |
| 277 | * to use. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 278 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 279 | * \return The name of the message digest. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 280 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 282 | |
| 283 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 284 | * \brief This function starts a message-digest computation. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 285 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 286 | * You must call this function after setting up the context |
| 287 | * with mbedtls_md_setup(), and before passing data with |
| 288 | * mbedtls_md_update(). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 289 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 290 | * \param ctx The generic message-digest context. |
| 291 | * |
| 292 | * \returns \c 0 on success, #MBEDTLS_ERR_MD_BAD_INPUT_DATA if |
| 293 | * parameter verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 294 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 295 | int mbedtls_md_starts( mbedtls_md_context_t *ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 296 | |
| 297 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 298 | * \brief This function feeds an input buffer into an ongoing |
| 299 | * message-digest computation. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 300 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 301 | * You must call mbedtls_md_starts() before calling this |
| 302 | * function. You may call this function multiple times. |
| 303 | * Afterwards, call mbedtls_md_finish(). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 304 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 305 | * \param ctx The generic message-digest context. |
| 306 | * \param input The buffer holding the input data. |
| 307 | * \param ilen The length of the input data. |
| 308 | * |
| 309 | * \returns \c 0 on success, #MBEDTLS_ERR_MD_BAD_INPUT_DATA if |
| 310 | * parameter verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 311 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | 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] | 313 | |
| 314 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 315 | * \brief This function finishes the digest operation, |
| 316 | * and writes the result to the output buffer. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 317 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 318 | * Call this function after a call to mbedtls_md_starts(), |
| 319 | * followed by any number of calls to mbedtls_md_update(). |
| 320 | * Afterwards, you may either clear the context with |
| 321 | * mbedtls_md_free(), or call mbedtls_md_starts() to reuse |
| 322 | * the context for another digest operation with the same |
| 323 | * algorithm. |
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 | * \param ctx The generic message-digest context. |
| 326 | * \param output The buffer for the generic message-digest checksum result. |
| 327 | * |
| 328 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if |
| 329 | * parameter verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 330 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 331 | int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 332 | |
| 333 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 334 | * \brief This function calculates the message-digest of a buffer, |
| 335 | * with respect to a configurable message-digest algorithm |
| 336 | * in a single call. |
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 | * The result is calculated as |
| 339 | * Output = message_digest(input buffer). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 340 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 341 | * \param md_info The information structure of the message-digest algorithm |
| 342 | * to use. |
| 343 | * \param input The buffer holding the data. |
| 344 | * \param ilen The length of the input data. |
| 345 | * \param output The generic message-digest checksum result. |
| 346 | * |
| 347 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if |
| 348 | * parameter verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 349 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 351 | unsigned char *output ); |
| 352 | |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 353 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 354 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 355 | * \brief This function calculates the message-digest checksum |
| 356 | * result of the contents of the provided file. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 357 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 358 | * The result is calculated as |
| 359 | * Output = message_digest(file contents). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 360 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 361 | * \param md_info The information structure of the message-digest algorithm |
| 362 | * to use. |
| 363 | * \param path The input file name. |
| 364 | * \param output The generic message-digest checksum result. |
| 365 | * |
| 366 | * \return \c 0 on success, |
| 367 | * #MBEDTLS_ERR_MD_FILE_IO_ERROR if file input failed, or |
| 368 | * #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 369 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 370 | int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, |
Manuel Pégourié-Gonnard | bfffa90 | 2015-05-28 14:44:00 +0200 | [diff] [blame] | 371 | unsigned char *output ); |
| 372 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 373 | |
| 374 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 375 | * \brief This function sets the HMAC key and prepares to |
| 376 | * authenticate a new message. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 377 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 378 | * Call this function after mbedtls_md_setup(), to use |
| 379 | * the MD context for an HMAC calculation, then call |
| 380 | * mbedtls_md_hmac_update() to provide the input data, and |
| 381 | * mbedtls_md_hmac_finish() to get the HMAC value. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 382 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 383 | * \param ctx The message digest context containing an embedded HMAC |
| 384 | * context. |
| 385 | * \param key The HMAC secret key. |
| 386 | * \param keylen The length of the HMAC key in Bytes. |
| 387 | * |
| 388 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if |
| 389 | * parameter verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 390 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 392 | size_t keylen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 393 | |
| 394 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 395 | * \brief This function feeds an input buffer into an ongoing HMAC |
| 396 | * computation. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 397 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 398 | * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset() |
| 399 | * before calling this function. |
| 400 | * You may call this function multiple times to pass the |
| 401 | * input piecewise. |
| 402 | * Afterwards, call mbedtls_md_hmac_finish(). |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 403 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 404 | * \param ctx The message digest context containing an embedded HMAC |
| 405 | * context. |
| 406 | * \param input The buffer holding the input data. |
| 407 | * \param ilen The length of the input data. |
| 408 | * |
| 409 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if |
| 410 | * parameter verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 411 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 412 | int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 413 | size_t ilen ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 414 | |
| 415 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 416 | * \brief This function finishes the HMAC operation, and writes |
| 417 | * the result to the output buffer. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 418 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 419 | * Call this function after mbedtls_md_hmac_starts() and |
| 420 | * mbedtls_md_hmac_update() to get the HMAC value. Afterwards |
| 421 | * you may either call mbedtls_md_free() to clear the context, |
| 422 | * or call mbedtls_md_hmac_reset() to reuse the context with |
| 423 | * the same HMAC key. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 424 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 425 | * \param ctx The message digest context containing an embedded HMAC |
| 426 | * context. |
| 427 | * \param output The generic HMAC checksum result. |
| 428 | * |
| 429 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if |
| 430 | * parameter verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 431 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 432 | 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] | 433 | |
| 434 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 435 | * \brief This function prepares to authenticate a new message with |
| 436 | * the same key as the previous HMAC operation. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 437 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 438 | * You may call this function after mbedtls_md_hmac_finish(). |
| 439 | * Afterwards call mbedtls_md_hmac_update() to pass the new |
| 440 | * input. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 441 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 442 | * \param ctx The message digest context containing an embedded HMAC |
| 443 | * context. |
| 444 | * |
| 445 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if |
| 446 | * parameter verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 447 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 448 | int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx ); |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 449 | |
| 450 | /** |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 451 | * \brief This function calculates the full generic HMAC |
| 452 | * on the input buffer with the provided key. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 453 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 454 | * The function allocates the context, performs the |
| 455 | * calculation, and frees the context. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 456 | * |
Rose Zadik | 64feefb | 2018-01-25 22:01:10 +0000 | [diff] [blame] | 457 | * The HMAC result is calculated as |
| 458 | * output = generic HMAC(hmac key, input buffer). |
| 459 | * |
| 460 | * \param md_info The information structure of the message-digest algorithm |
| 461 | * to use. |
| 462 | * \param key The HMAC secret key. |
| 463 | * \param keylen The length of the HMAC secret key in Bytes. |
| 464 | * \param input The buffer holding the input data. |
| 465 | * \param ilen The length of the input data. |
| 466 | * \param output The generic HMAC result. |
| 467 | * |
| 468 | * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if |
| 469 | * parameter verification fails. |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 470 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 471 | int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen, |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 472 | const unsigned char *input, size_t ilen, |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 473 | unsigned char *output ); |
| 474 | |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 475 | /* Internal use */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 476 | int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data ); |
Paul Bakker | 1bd3ae8 | 2013-03-13 10:26:44 +0100 | [diff] [blame] | 477 | |
Paul Bakker | 1737385 | 2011-01-06 14:20:01 +0000 | [diff] [blame] | 478 | #ifdef __cplusplus |
| 479 | } |
| 480 | #endif |
| 481 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 482 | #endif /* MBEDTLS_MD_H */ |