blob: a5dfb68420c37398c148cd7ec17d36417e42b956 [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 *
Paul Bakker17373852011-01-06 14:20:01 +00007 * \author Adriaan de Jong <dejong@fox-it.com>
Darryl Greena40a1012018-01-05 15:33:17 +00008 */
9/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020010 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020011 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker17373852011-01-06 14:20:01 +000024 */
Rose Zadik64feefb2018-01-25 22:01:10 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#ifndef MBEDTLS_MD_H
27#define MBEDTLS_MD_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020028#include "mbedtls/private_access.h"
Paul Bakker17373852011-01-06 14:20:01 +000029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000031
Bence Szépkútic662b362021-05-27 11:25:03 +020032#include "mbedtls/build_info.h"
Gilles Peskineecf6beb2021-12-10 21:35:10 +010033#include "mbedtls/platform_util.h"
Ron Eldorf231eaa2017-08-22 14:50:14 +030034
Gilles Peskine416d0e22022-10-22 18:27:57 +020035#if defined(MBEDTLS_MD_LIGHT)
36
37/*
38 * - MBEDTLS_MD_CAN_xxx is defined if the md module can perform xxx.
Manuel Pégourié-Gonnarda9ab4a22023-03-14 10:51:15 +010039 * - MBEDTLS_MD_xxx_VIA_PSA is defined if the md module performs xxx via PSA
40 * (when PSA Crypto is initialized).
Gilles Peskine416d0e22022-10-22 18:27:57 +020041 * - MBEDTLS_MD_SOME_PSA is defined if at least one algorithm is performed
42 * via PSA.
43 * - MBEDTLS_MD_SOME_LEGACY is defined if at least one algorithm is performed
44 * via a direct legacy call.
45 *
46 * The md module performs an algorithm via PSA if there is a PSA hash
47 * accelerator, and makes a direct legacy call otherwise.
48 */
49
50/* PSA accelerated implementations */
51#if defined(MBEDTLS_PSA_CRYPTO_C)
52#if defined(MBEDTLS_PSA_ACCEL_ALG_MD5)
53#define MBEDTLS_MD_CAN_MD5
54#define MBEDTLS_MD_MD5_VIA_PSA
55#define MBEDTLS_MD_SOME_PSA
56#endif
57#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1)
58#define MBEDTLS_MD_CAN_SHA1
59#define MBEDTLS_MD_SHA1_VIA_PSA
60#define MBEDTLS_MD_SOME_PSA
61#endif
62#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224)
63#define MBEDTLS_MD_CAN_SHA224
64#define MBEDTLS_MD_SHA224_VIA_PSA
65#define MBEDTLS_MD_SOME_PSA
66#endif
67#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256)
68#define MBEDTLS_MD_CAN_SHA256
69#define MBEDTLS_MD_SHA256_VIA_PSA
70#define MBEDTLS_MD_SOME_PSA
71#endif
72#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384)
73#define MBEDTLS_MD_CAN_SHA384
74#define MBEDTLS_MD_SHA384_VIA_PSA
75#define MBEDTLS_MD_SOME_PSA
76#endif
77#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512)
78#define MBEDTLS_MD_CAN_SHA512
79#define MBEDTLS_MD_SHA512_VIA_PSA
80#define MBEDTLS_MD_SOME_PSA
81#endif
82#if defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160)
83#define MBEDTLS_MD_CAN_RIPEMD160
84#define MBEDTLS_MD_RIPEMD160_VIA_PSA
85#define MBEDTLS_MD_SOME_PSA
86#endif
87#endif /* MBEDTLS_PSA_CRYPTO_C */
88
89/* Built-in implementations */
90#if defined(MBEDTLS_MD5_C)
91#define MBEDTLS_MD_CAN_MD5
92#define MBEDTLS_MD_SOME_LEGACY
93#endif
94#if defined(MBEDTLS_SHA1_C)
95#define MBEDTLS_MD_CAN_SHA1
96#define MBEDTLS_MD_SOME_LEGACY
97#endif
98#if defined(MBEDTLS_SHA224_C)
99#define MBEDTLS_MD_CAN_SHA224
100#define MBEDTLS_MD_SOME_LEGACY
101#endif
102#if defined(MBEDTLS_SHA256_C)
103#define MBEDTLS_MD_CAN_SHA256
104#define MBEDTLS_MD_SOME_LEGACY
105#endif
106#if defined(MBEDTLS_SHA384_C)
107#define MBEDTLS_MD_CAN_SHA384
108#define MBEDTLS_MD_SOME_LEGACY
109#endif
110#if defined(MBEDTLS_SHA512_C)
111#define MBEDTLS_MD_CAN_SHA512
112#define MBEDTLS_MD_SOME_LEGACY
113#endif
114#if defined(MBEDTLS_RIPEMD160_C)
115#define MBEDTLS_MD_CAN_RIPEMD160
116#define MBEDTLS_MD_SOME_LEGACY
117#endif
118
119#endif /* MBEDTLS_MD_LIGHT */
120
Gilles Peskined2971572021-07-26 18:48:10 +0200121/** The selected feature is not available. */
122#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080
123/** Bad input parameters to function. */
124#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100
125/** Failed to allocate memory. */
126#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180
127/** Opening or reading of file failed. */
128#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200
Ron Eldor9924bdc2018-10-04 10:59:13 +0300129
Paul Bakker407a0da2013-06-27 14:29:21 +0200130#ifdef __cplusplus
131extern "C" {
132#endif
133
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100134/**
Rose Zadik8c9c7942018-03-27 11:52:58 +0100135 * \brief Supported message digests.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100136 *
TRodziewicz10e8cf52021-05-31 17:58:57 +0200137 * \warning MD5 and SHA-1 are considered weak message digests and
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100138 * their use constitutes a security risk. We recommend considering
139 * stronger message digests instead.
140 *
141 */
Paul Bakker17373852011-01-06 14:20:01 +0000142typedef enum {
Rose Zadikf3e47362018-04-16 16:31:16 +0100143 MBEDTLS_MD_NONE=0, /**< None. */
Rose Zadikf3e47362018-04-16 16:31:16 +0100144 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
145 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
146 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
147 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
148 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
149 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
Rose Zadik8c9c7942018-03-27 11:52:58 +0100150 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +0000152
Gilles Peskine83d9e092022-10-22 18:32:43 +0200153#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200155#elif defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100156#define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200157#elif defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100158#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200159#elif defined(MBEDTLS_MD_CAN_SHA224)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100160#define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */
Paul Bakker7db01092013-09-10 11:10:57 +0200161#else
Gilles Peskine83d9e092022-10-22 18:32:43 +0200162#define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160
163 or smaller (MD5 and earlier) */
Paul Bakker7db01092013-09-10 11:10:57 +0200164#endif
Paul Bakker1b57b062011-01-06 15:48:19 +0000165
Gilles Peskine83d9e092022-10-22 18:32:43 +0200166#if defined(MBEDTLS_MD_CAN_SHA512)
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000167#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
168#else
169#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
170#endif
171
Paul Bakker17373852011-01-06 14:20:01 +0000172/**
Chris Jones3848e312021-03-11 16:17:59 +0000173 * Opaque struct.
174 *
175 * Constructed using either #mbedtls_md_info_from_string or
176 * #mbedtls_md_info_from_type.
177 *
178 * Fields can be accessed with #mbedtls_md_get_size,
179 * #mbedtls_md_get_type and #mbedtls_md_get_name.
Paul Bakker17373852011-01-06 14:20:01 +0000180 */
Chris Jones3848e312021-03-11 16:17:59 +0000181/* Defined internally in library/md_wrap.h. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +0000183
184/**
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100185 * Used internally to indicate whether a context uses legacy or PSA.
186 *
187 * Internal use only.
188 */
189typedef enum {
190 MBEDTLS_MD_ENGINE_LEGACY = 0,
191 MBEDTLS_MD_ENGINE_PSA,
192} mbedtls_md_engine_t;
193
194/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000195 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000196 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197typedef struct mbedtls_md_context_t {
Rose Zadik64feefb2018-01-25 22:01:10 +0000198 /** Information about the associated message digest. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200199 const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000200
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100201#if defined(MBEDTLS_MD_SOME_PSA)
202 /** Are hash operations dispatched to PSA or legacy? */
203 mbedtls_md_engine_t MBEDTLS_PRIVATE(engine);
204#endif
205
206 /** The digest-specific context (legacy) or the PSA operation. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200207 void *MBEDTLS_PRIVATE(md_ctx);
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100208
Manuel Pégourié-Gonnard39a376a2023-03-09 17:21:40 +0100209#if defined(MBEDTLS_MD_C)
Rose Zadik64feefb2018-01-25 22:01:10 +0000210 /** The HMAC part of the context. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200211 void *MBEDTLS_PRIVATE(hmac_ctx);
Manuel Pégourié-Gonnard39a376a2023-03-09 17:21:40 +0100212#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000214
Paul Bakker17373852011-01-06 14:20:01 +0000215/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000216 * \brief This function returns the message-digest information
217 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000218 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000219 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000220 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100221 * \return The message-digest information associated with \p md_type.
222 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000223 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
Paul Bakker17373852011-01-06 14:20:01 +0000225
Max Fillinger0bb38332021-12-28 16:32:00 +0100226/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000227 * \brief This function initializes a message-digest context without
228 * binding it to a particular message-digest algorithm.
229 *
230 * This function should always be called first. It prepares the
231 * context for mbedtls_md_setup() for binding it to a
232 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200233 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100234void mbedtls_md_init(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200235
236/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000237 * \brief This function clears the internal structure of \p ctx and
238 * frees any embedded internal structure, but does not free
239 * \p ctx itself.
240 *
241 * If you have called mbedtls_md_setup() on \p ctx, you must
242 * call mbedtls_md_free() when you are no longer using the
243 * context.
244 * Calling this function if you have previously
245 * called mbedtls_md_init() and nothing else is optional.
246 * You must not call this function if you have not called
247 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200248 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100249void mbedtls_md_free(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200250
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100251
Paul Bakker84bbeb52014-07-01 14:53:22 +0200252/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000253 * \brief This function selects the message digest algorithm to use,
254 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000255 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000256 * It should be called after mbedtls_md_init() or
257 * mbedtls_md_free(). Makes it necessary to call
258 * mbedtls_md_free() later.
259 *
260 * \param ctx The context to set up.
261 * \param md_info The information structure of the message-digest algorithm
262 * to use.
Rose Zadik8c9c7942018-03-27 11:52:58 +0100263 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
264 * or non-zero: HMAC is used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000265 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100266 * \return \c 0 on success.
267 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
268 * failure.
269 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000270 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100271MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100272int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
Paul Bakker562535d2011-01-20 16:42:01 +0000273
274/**
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100275 * \brief This function clones the state of a message-digest
Rose Zadik64feefb2018-01-25 22:01:10 +0000276 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200277 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000278 * \note You must call mbedtls_md_setup() on \c dst before calling
279 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200280 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000281 * \note The two contexts must have the same type,
282 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200283 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000284 * \warning This function clones the message-digest state, not the
285 * HMAC state.
286 *
287 * \param dst The destination context.
288 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200289 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100290 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100291 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Manuel Pégourié-Gonnard9b146392023-03-09 15:56:14 +0100292 * \return #MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE if both contexts are
293 * not using the same engine. This can be avoided by moving
294 * the call to psa_crypto_init() before the first call to
295 * mbedtls_md_setup().
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200296 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100297MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100298int mbedtls_md_clone(mbedtls_md_context_t *dst,
299 const mbedtls_md_context_t *src);
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200300
301/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000302 * \brief This function extracts the message-digest size from the
303 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000304 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000305 * \param md_info The information structure of the message-digest algorithm
306 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000307 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000308 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000309 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000311
312/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000313 * \brief This function extracts the message-digest type from the
314 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000315 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000316 * \param md_info The information structure of the message-digest algorithm
317 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000318 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000319 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000320 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000322
Paul Bakker17373852011-01-06 14:20:01 +0000323/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000324 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000325 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000326 * You must call this function after setting up the context
327 * with mbedtls_md_setup(), and before passing data with
328 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000329 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000330 * \param ctx The generic message-digest context.
331 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100332 * \return \c 0 on success.
333 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
334 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000335 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100336MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100337int mbedtls_md_starts(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000338
339/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000340 * \brief This function feeds an input buffer into an ongoing
341 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000342 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000343 * You must call mbedtls_md_starts() before calling this
344 * function. You may call this function multiple times.
345 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000346 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000347 * \param ctx The generic message-digest context.
348 * \param input The buffer holding the input data.
349 * \param ilen The length of the input data.
350 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100351 * \return \c 0 on success.
352 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
353 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000354 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100355MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100356int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000357
358/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000359 * \brief This function finishes the digest operation,
360 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000361 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000362 * Call this function after a call to mbedtls_md_starts(),
363 * followed by any number of calls to mbedtls_md_update().
364 * Afterwards, you may either clear the context with
365 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
366 * the context for another digest operation with the same
367 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000368 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000369 * \param ctx The generic message-digest context.
370 * \param output The buffer for the generic message-digest checksum result.
371 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100372 * \return \c 0 on success.
373 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
374 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000375 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100376MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100377int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000378
379/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000380 * \brief This function calculates the message-digest of a buffer,
381 * with respect to a configurable message-digest algorithm
382 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000383 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000384 * The result is calculated as
385 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000386 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000387 * \param md_info The information structure of the message-digest algorithm
388 * to use.
389 * \param input The buffer holding the data.
390 * \param ilen The length of the input data.
391 * \param output The generic message-digest checksum result.
392 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100393 * \return \c 0 on success.
394 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
395 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000396 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100397MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100398int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
399 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000400
Manuel Pégourié-Gonnard82a43942023-02-23 09:36:29 +0100401/**
402 * \brief This function returns the list of digests supported by the
403 * generic digest module.
404 *
405 * \note The list starts with the strongest available hashes.
406 *
407 * \return A statically allocated array of digests. Each element
408 * in the returned list is an integer belonging to the
409 * message-digest enumeration #mbedtls_md_type_t.
410 * The last entry is 0.
411 */
412const int *mbedtls_md_list(void);
413
414/**
415 * \brief This function returns the message-digest information
416 * associated with the given digest name.
417 *
418 * \param md_name The name of the digest to search for.
419 *
420 * \return The message-digest information associated with \p md_name.
421 * \return NULL if the associated message-digest information is not found.
422 */
423const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
424
425/**
426 * \brief This function extracts the message-digest name from the
427 * message-digest information structure.
428 *
429 * \param md_info The information structure of the message-digest algorithm
430 * to use.
431 *
432 * \return The name of the message digest.
433 */
434const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
435
436/**
437 * \brief This function returns the message-digest information
438 * from the given context.
439 *
440 * \param ctx The context from which to extract the information.
441 * This must be initialized (or \c NULL).
442 *
443 * \return The message-digest information associated with \p ctx.
444 * \return \c NULL if \p ctx is \c NULL.
445 */
446const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
447 const mbedtls_md_context_t *ctx);
448
449#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000450/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000451 * \brief This function calculates the message-digest checksum
452 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000453 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000454 * The result is calculated as
455 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000456 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000457 * \param md_info The information structure of the message-digest algorithm
458 * to use.
459 * \param path The input file name.
460 * \param output The generic message-digest checksum result.
461 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100462 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100463 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
464 * the file pointed by \p path.
465 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000466 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100467MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100468int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
469 unsigned char *output);
Manuel Pégourié-Gonnard82a43942023-02-23 09:36:29 +0100470#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000471
472/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000473 * \brief This function sets the HMAC key and prepares to
474 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000475 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000476 * Call this function after mbedtls_md_setup(), to use
477 * the MD context for an HMAC calculation, then call
478 * mbedtls_md_hmac_update() to provide the input data, and
479 * mbedtls_md_hmac_finish() to get the HMAC value.
Paul Bakker17373852011-01-06 14:20:01 +0000480 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000481 * \param ctx The message digest context containing an embedded HMAC
482 * context.
483 * \param key The HMAC secret key.
484 * \param keylen The length of the HMAC key in Bytes.
485 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100486 * \return \c 0 on success.
487 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
488 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000489 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100490MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100491int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
492 size_t keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000493
494/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000495 * \brief This function feeds an input buffer into an ongoing HMAC
496 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000497 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000498 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
499 * before calling this function.
500 * You may call this function multiple times to pass the
501 * input piecewise.
502 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000503 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000504 * \param ctx The message digest context containing an embedded HMAC
505 * context.
506 * \param input The buffer holding the input data.
507 * \param ilen The length of the input data.
508 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100509 * \return \c 0 on success.
510 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
511 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000512 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100513MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100514int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
515 size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000516
517/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000518 * \brief This function finishes the HMAC operation, and writes
519 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000520 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000521 * Call this function after mbedtls_md_hmac_starts() and
522 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
523 * you may either call mbedtls_md_free() to clear the context,
524 * or call mbedtls_md_hmac_reset() to reuse the context with
525 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000526 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000527 * \param ctx The message digest context containing an embedded HMAC
528 * context.
529 * \param output The generic HMAC checksum result.
530 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100531 * \return \c 0 on success.
532 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
533 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000534 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100535MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100536int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000537
538/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000539 * \brief This function prepares to authenticate a new message with
540 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000541 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000542 * You may call this function after mbedtls_md_hmac_finish().
543 * Afterwards call mbedtls_md_hmac_update() to pass the new
544 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000545 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000546 * \param ctx The message digest context containing an embedded HMAC
547 * context.
548 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100549 * \return \c 0 on success.
550 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
551 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000552 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100553MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100554int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000555
556/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000557 * \brief This function calculates the full generic HMAC
558 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000559 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000560 * The function allocates the context, performs the
561 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000562 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000563 * The HMAC result is calculated as
564 * output = generic HMAC(hmac key, input buffer).
565 *
566 * \param md_info The information structure of the message-digest algorithm
567 * to use.
568 * \param key The HMAC secret key.
569 * \param keylen The length of the HMAC secret key in Bytes.
570 * \param input The buffer holding the input data.
571 * \param ilen The length of the input data.
572 * \param output The generic HMAC result.
573 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100574 * \return \c 0 on success.
575 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
576 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000577 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100578MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100579int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
580 const unsigned char *input, size_t ilen,
581 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000582
Paul Bakker17373852011-01-06 14:20:01 +0000583#ifdef __cplusplus
584}
585#endif
586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#endif /* MBEDTLS_MD_H */