blob: f9349e1d85ab1452ea8bfbc7b145693542d2d4d1 [file] [log] [blame]
Gilles Peskine449bd832023-01-11 14:50:10 +01001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file md.h
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +01004 * \brief This file contains the generic functions for message-digest
5 * (hashing) and HMAC.
6 *
7 * Availability of function in this modules is controled by two
8 * feature macros:
9 * - MBEDTLS_MD_C enables the whole module;
10 * - MBEDTLS_MD_LIGHT enables only functions for hashing an accessing
11 * some hash metadata; is it automatically set whenever MBEDTLS_MD_C
12 * is set.
Paul Bakker17373852011-01-06 14:20:01 +000013 *
14 * \author Adriaan de Jong <dejong@fox-it.com>
Darryl Greena40a1012018-01-05 15:33:17 +000015 */
16/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020017 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020018 * SPDX-License-Identifier: Apache-2.0
19 *
20 * Licensed under the Apache License, Version 2.0 (the "License"); you may
21 * not use this file except in compliance with the License.
22 * You may obtain a copy of the License at
23 *
24 * http://www.apache.org/licenses/LICENSE-2.0
25 *
26 * Unless required by applicable law or agreed to in writing, software
27 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
28 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29 * See the License for the specific language governing permissions and
30 * limitations under the License.
Paul Bakker17373852011-01-06 14:20:01 +000031 */
Rose Zadik64feefb2018-01-25 22:01:10 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#ifndef MBEDTLS_MD_H
34#define MBEDTLS_MD_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020035#include "mbedtls/private_access.h"
Paul Bakker17373852011-01-06 14:20:01 +000036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000038
Bence Szépkútic662b362021-05-27 11:25:03 +020039#include "mbedtls/build_info.h"
Gilles Peskineecf6beb2021-12-10 21:35:10 +010040#include "mbedtls/platform_util.h"
Ron Eldorf231eaa2017-08-22 14:50:14 +030041
Gilles Peskined2971572021-07-26 18:48:10 +020042/** The selected feature is not available. */
43#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080
44/** Bad input parameters to function. */
45#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100
46/** Failed to allocate memory. */
47#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180
48/** Opening or reading of file failed. */
49#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200
Ron Eldor9924bdc2018-10-04 10:59:13 +030050
Paul Bakker407a0da2013-06-27 14:29:21 +020051#ifdef __cplusplus
52extern "C" {
53#endif
54
Hanno Beckerbbca8c52017-09-25 14:53:51 +010055/**
Rose Zadik8c9c7942018-03-27 11:52:58 +010056 * \brief Supported message digests.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010057 *
TRodziewicz10e8cf52021-05-31 17:58:57 +020058 * \warning MD5 and SHA-1 are considered weak message digests and
Hanno Beckerbbca8c52017-09-25 14:53:51 +010059 * their use constitutes a security risk. We recommend considering
60 * stronger message digests instead.
61 *
62 */
Paul Bakker17373852011-01-06 14:20:01 +000063typedef enum {
Rose Zadikf3e47362018-04-16 16:31:16 +010064 MBEDTLS_MD_NONE=0, /**< None. */
Rose Zadikf3e47362018-04-16 16:31:16 +010065 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
66 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
67 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
68 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
69 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
70 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
Rose Zadik8c9c7942018-03-27 11:52:58 +010071 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +000073
Valerio Settid55cb5b2022-12-22 14:26:55 +010074#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Valerio Settid55cb5b2022-12-22 14:26:55 +010076#elif defined(MBEDTLS_SHA384_C)
77#define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */
78#elif defined(MBEDTLS_SHA256_C)
79#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */
80#elif defined(MBEDTLS_SHA224_C)
81#define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */
Paul Bakker7db01092013-09-10 11:10:57 +020082#else
Valerio Settid55cb5b2022-12-22 14:26:55 +010083#define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160 */
Paul Bakker7db01092013-09-10 11:10:57 +020084#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000085
Hanno Becker2e24c3b2017-12-27 21:28:58 +000086#if defined(MBEDTLS_SHA512_C)
87#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
88#else
89#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
90#endif
91
Paul Bakker17373852011-01-06 14:20:01 +000092/**
Chris Jones3848e312021-03-11 16:17:59 +000093 * Opaque struct.
94 *
95 * Constructed using either #mbedtls_md_info_from_string or
96 * #mbedtls_md_info_from_type.
97 *
98 * Fields can be accessed with #mbedtls_md_get_size,
99 * #mbedtls_md_get_type and #mbedtls_md_get_name.
Paul Bakker17373852011-01-06 14:20:01 +0000100 */
Chris Jones3848e312021-03-11 16:17:59 +0000101/* Defined internally in library/md_wrap.h. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +0000103
104/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000105 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107typedef struct mbedtls_md_context_t {
Rose Zadik64feefb2018-01-25 22:01:10 +0000108 /** Information about the associated message digest. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200109 const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000110
Rose Zadik64feefb2018-01-25 22:01:10 +0000111 /** The digest-specific context. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200112 void *MBEDTLS_PRIVATE(md_ctx);
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100113
Rose Zadik64feefb2018-01-25 22:01:10 +0000114 /** The HMAC part of the context. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200115 void *MBEDTLS_PRIVATE(hmac_ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000117
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100118#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +0000119/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000120 * \brief This function returns the list of digests supported by the
121 * generic digest module.
Paul Bakker72f62662011-01-16 21:27:44 +0000122 *
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200123 * \note The list starts with the strongest available hashes.
124 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000125 * \return A statically allocated array of digests. Each element
126 * in the returned list is an integer belonging to the
127 * message-digest enumeration #mbedtls_md_type_t.
128 * The last entry is 0.
Paul Bakker72f62662011-01-16 21:27:44 +0000129 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130const int *mbedtls_md_list(void);
Paul Bakker72f62662011-01-16 21:27:44 +0000131
132/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000133 * \brief This function returns the message-digest information
134 * associated with the given digest name.
Paul Bakker17373852011-01-06 14:20:01 +0000135 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000136 * \param md_name The name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000137 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100138 * \return The message-digest information associated with \p md_name.
139 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000140 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100141const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100142#endif /* MBEDTLS_MD_C */
Paul Bakker17373852011-01-06 14:20:01 +0000143
144/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000145 * \brief This function returns the message-digest information
146 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000147 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000148 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000149 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100150 * \return The message-digest information associated with \p md_type.
151 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000152 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
Paul Bakker17373852011-01-06 14:20:01 +0000154
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100155#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +0000156/**
Max Fillinger0bb38332021-12-28 16:32:00 +0100157 * \brief This function returns the message-digest information
158 * from the given context.
159 *
160 * \param ctx The context from which to extract the information.
161 * This must be initialized (or \c NULL).
162 *
163 * \return The message-digest information associated with \p ctx.
164 * \return \c NULL if \p ctx is \c NULL.
165 */
166const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 const mbedtls_md_context_t *ctx);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100168#endif /* MBEDTLS_MD_C */
Max Fillinger0bb38332021-12-28 16:32:00 +0100169
Max Fillinger0bb38332021-12-28 16:32:00 +0100170/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000171 * \brief This function initializes a message-digest context without
172 * binding it to a particular message-digest algorithm.
173 *
174 * This function should always be called first. It prepares the
175 * context for mbedtls_md_setup() for binding it to a
176 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200177 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178void mbedtls_md_init(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200179
180/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000181 * \brief This function clears the internal structure of \p ctx and
182 * frees any embedded internal structure, but does not free
183 * \p ctx itself.
184 *
185 * If you have called mbedtls_md_setup() on \p ctx, you must
186 * call mbedtls_md_free() when you are no longer using the
187 * context.
188 * Calling this function if you have previously
189 * called mbedtls_md_init() and nothing else is optional.
190 * You must not call this function if you have not called
191 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200192 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193void mbedtls_md_free(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200194
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100195
Paul Bakker84bbeb52014-07-01 14:53:22 +0200196/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000197 * \brief This function selects the message digest algorithm to use,
198 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000199 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000200 * It should be called after mbedtls_md_init() or
201 * mbedtls_md_free(). Makes it necessary to call
202 * mbedtls_md_free() later.
203 *
204 * \param ctx The context to set up.
205 * \param md_info The information structure of the message-digest algorithm
206 * to use.
Rose Zadik8c9c7942018-03-27 11:52:58 +0100207 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
208 * or non-zero: HMAC is used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000209 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100210 * \return \c 0 on success.
211 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
212 * failure.
213 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000214 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100215MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100216int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
Paul Bakker562535d2011-01-20 16:42:01 +0000217
218/**
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100219 * \brief This function clones the state of a message-digest
Rose Zadik64feefb2018-01-25 22:01:10 +0000220 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200221 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000222 * \note You must call mbedtls_md_setup() on \c dst before calling
223 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200224 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000225 * \note The two contexts must have the same type,
226 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200227 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000228 * \warning This function clones the message-digest state, not the
229 * HMAC state.
230 *
231 * \param dst The destination context.
232 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200233 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100234 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100235 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200236 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100237MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100238int mbedtls_md_clone(mbedtls_md_context_t *dst,
239 const mbedtls_md_context_t *src);
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200240
241/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000242 * \brief This function extracts the message-digest size from the
243 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000244 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000245 * \param md_info The information structure of the message-digest algorithm
246 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000247 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000248 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000249 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100250unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000251
252/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000253 * \brief This function extracts the message-digest type from the
254 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000255 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000256 * \param md_info The information structure of the message-digest algorithm
257 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000258 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000259 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000260 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000262
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100263#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +0000264/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000265 * \brief This function extracts the message-digest name from the
266 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000267 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000268 * \param md_info The information structure of the message-digest algorithm
269 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000270 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000271 * \return The name of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100274#endif /* MBEDTLS_MD_C */
Paul Bakker17373852011-01-06 14:20:01 +0000275
276/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000277 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000278 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000279 * You must call this function after setting up the context
280 * with mbedtls_md_setup(), and before passing data with
281 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000282 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000283 * \param ctx The generic message-digest context.
284 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100285 * \return \c 0 on success.
286 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
287 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000288 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100289MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100290int mbedtls_md_starts(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000291
292/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000293 * \brief This function feeds an input buffer into an ongoing
294 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000295 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000296 * You must call mbedtls_md_starts() before calling this
297 * function. You may call this function multiple times.
298 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000299 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000300 * \param ctx The generic message-digest context.
301 * \param input The buffer holding the input data.
302 * \param ilen The length of the input data.
303 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100304 * \return \c 0 on success.
305 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
306 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000307 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100308MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100309int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000310
311/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000312 * \brief This function finishes the digest operation,
313 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000314 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000315 * Call this function after a call to mbedtls_md_starts(),
316 * followed by any number of calls to mbedtls_md_update().
317 * Afterwards, you may either clear the context with
318 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
319 * the context for another digest operation with the same
320 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000321 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000322 * \param ctx The generic message-digest context.
323 * \param output The buffer for the generic message-digest checksum result.
324 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100325 * \return \c 0 on success.
326 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
327 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000328 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100329MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100330int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000331
332/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000333 * \brief This function calculates the message-digest of a buffer,
334 * with respect to a configurable message-digest algorithm
335 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000336 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000337 * The result is calculated as
338 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000339 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000340 * \param md_info The information structure of the message-digest algorithm
341 * to use.
342 * \param input The buffer holding the data.
343 * \param ilen The length of the input data.
344 * \param output The generic message-digest checksum result.
345 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100346 * \return \c 0 on success.
347 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
348 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000349 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100350MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100351int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
352 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000353
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100354#if defined(MBEDTLS_FS_IO) && defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +0000355/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000356 * \brief This function calculates the message-digest checksum
357 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000358 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000359 * The result is calculated as
360 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000361 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000362 * \param md_info The information structure of the message-digest algorithm
363 * to use.
364 * \param path The input file name.
365 * \param output The generic message-digest checksum result.
366 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100367 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100368 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
369 * the file pointed by \p path.
370 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000371 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100372MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100373int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
374 unsigned char *output);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100375#endif /* MBEDTLS_FS_IO && MBEDTLS_MD_C */
Paul Bakker17373852011-01-06 14:20:01 +0000376
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100377#if defined(MBEDTLS_MD_C)
Paul Bakker17373852011-01-06 14:20:01 +0000378/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000379 * \brief This function sets the HMAC key and prepares to
380 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000381 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000382 * Call this function after mbedtls_md_setup(), to use
383 * the MD context for an HMAC calculation, then call
384 * mbedtls_md_hmac_update() to provide the input data, and
385 * mbedtls_md_hmac_finish() to get the HMAC value.
Paul Bakker17373852011-01-06 14:20:01 +0000386 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000387 * \param ctx The message digest context containing an embedded HMAC
388 * context.
389 * \param key The HMAC secret key.
390 * \param keylen The length of the HMAC key in Bytes.
391 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100392 * \return \c 0 on success.
393 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
394 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000395 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100396MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100397int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
398 size_t keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000399
400/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000401 * \brief This function feeds an input buffer into an ongoing HMAC
402 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000403 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000404 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
405 * before calling this function.
406 * You may call this function multiple times to pass the
407 * input piecewise.
408 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000409 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000410 * \param ctx The message digest context containing an embedded HMAC
411 * context.
412 * \param input The buffer holding the input data.
413 * \param ilen The length of the input data.
414 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100415 * \return \c 0 on success.
416 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
417 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000418 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100419MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100420int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
421 size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000422
423/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000424 * \brief This function finishes the HMAC operation, and writes
425 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000426 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000427 * Call this function after mbedtls_md_hmac_starts() and
428 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
429 * you may either call mbedtls_md_free() to clear the context,
430 * or call mbedtls_md_hmac_reset() to reuse the context with
431 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000432 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000433 * \param ctx The message digest context containing an embedded HMAC
434 * context.
435 * \param output The generic HMAC checksum result.
436 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100437 * \return \c 0 on success.
438 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
439 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000440 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100441MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100442int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000443
444/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000445 * \brief This function prepares to authenticate a new message with
446 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000447 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000448 * You may call this function after mbedtls_md_hmac_finish().
449 * Afterwards call mbedtls_md_hmac_update() to pass the new
450 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000451 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000452 * \param ctx The message digest context containing an embedded HMAC
453 * context.
454 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100455 * \return \c 0 on success.
456 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
457 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000458 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100459MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100460int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000461
462/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000463 * \brief This function calculates the full generic HMAC
464 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000465 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000466 * The function allocates the context, performs the
467 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000468 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000469 * The HMAC result is calculated as
470 * output = generic HMAC(hmac key, input buffer).
471 *
472 * \param md_info The information structure of the message-digest algorithm
473 * to use.
474 * \param key The HMAC secret key.
475 * \param keylen The length of the HMAC secret key in Bytes.
476 * \param input The buffer holding the input data.
477 * \param ilen The length of the input data.
478 * \param output The generic HMAC result.
479 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100480 * \return \c 0 on success.
481 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
482 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000483 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100484MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100485int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
486 const unsigned char *input, size_t ilen,
487 unsigned char *output);
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +0100488#endif /* MBEDTLS_MD_C */
Paul Bakker17373852011-01-06 14:20:01 +0000489
Paul Bakker17373852011-01-06 14:20:01 +0000490#ifdef __cplusplus
491}
492#endif
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#endif /* MBEDTLS_MD_H */