blob: 67a5bd6ce0606d6cb438311348c07803da523d33 [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.
Paul Bakker17373852011-01-06 14:20:01 +00006 *
7 * \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é-Gonnard9d698df2023-03-14 12:24:05 +010039 * - MBEDTLS_MD_xxx_VIA_PSA is defined if the md module may perform xxx via PSA
40 * (see below).
41 * - MBEDTLS_MD_SOME_PSA is defined if at least one algorithm may be performed
42 * via PSA (see below).
43 * - MBEDTLS_MD_SOME_LEGACY is defined if at least one algorithm may be performed
44 * via a direct legacy call (see below).
Gilles Peskine416d0e22022-10-22 18:27:57 +020045 *
46 * The md module performs an algorithm via PSA if there is a PSA hash
Manuel Pégourié-Gonnard9d698df2023-03-14 12:24:05 +010047 * accelerator and the PSA driver subsytem is initialized at the time the
48 * operation is started, and makes a direct legacy call otherwise.
Gilles Peskine416d0e22022-10-22 18:27:57 +020049 */
50
51/* PSA accelerated implementations */
52#if defined(MBEDTLS_PSA_CRYPTO_C)
53#if defined(MBEDTLS_PSA_ACCEL_ALG_MD5)
54#define MBEDTLS_MD_CAN_MD5
55#define MBEDTLS_MD_MD5_VIA_PSA
56#define MBEDTLS_MD_SOME_PSA
57#endif
58#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1)
59#define MBEDTLS_MD_CAN_SHA1
60#define MBEDTLS_MD_SHA1_VIA_PSA
61#define MBEDTLS_MD_SOME_PSA
62#endif
63#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224)
64#define MBEDTLS_MD_CAN_SHA224
65#define MBEDTLS_MD_SHA224_VIA_PSA
66#define MBEDTLS_MD_SOME_PSA
67#endif
68#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256)
69#define MBEDTLS_MD_CAN_SHA256
70#define MBEDTLS_MD_SHA256_VIA_PSA
71#define MBEDTLS_MD_SOME_PSA
72#endif
73#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384)
74#define MBEDTLS_MD_CAN_SHA384
75#define MBEDTLS_MD_SHA384_VIA_PSA
76#define MBEDTLS_MD_SOME_PSA
77#endif
78#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512)
79#define MBEDTLS_MD_CAN_SHA512
80#define MBEDTLS_MD_SHA512_VIA_PSA
81#define MBEDTLS_MD_SOME_PSA
82#endif
83#if defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160)
84#define MBEDTLS_MD_CAN_RIPEMD160
85#define MBEDTLS_MD_RIPEMD160_VIA_PSA
86#define MBEDTLS_MD_SOME_PSA
87#endif
88#endif /* MBEDTLS_PSA_CRYPTO_C */
89
90/* Built-in implementations */
91#if defined(MBEDTLS_MD5_C)
92#define MBEDTLS_MD_CAN_MD5
93#define MBEDTLS_MD_SOME_LEGACY
94#endif
95#if defined(MBEDTLS_SHA1_C)
96#define MBEDTLS_MD_CAN_SHA1
97#define MBEDTLS_MD_SOME_LEGACY
98#endif
99#if defined(MBEDTLS_SHA224_C)
100#define MBEDTLS_MD_CAN_SHA224
101#define MBEDTLS_MD_SOME_LEGACY
102#endif
103#if defined(MBEDTLS_SHA256_C)
104#define MBEDTLS_MD_CAN_SHA256
105#define MBEDTLS_MD_SOME_LEGACY
106#endif
107#if defined(MBEDTLS_SHA384_C)
108#define MBEDTLS_MD_CAN_SHA384
109#define MBEDTLS_MD_SOME_LEGACY
110#endif
111#if defined(MBEDTLS_SHA512_C)
112#define MBEDTLS_MD_CAN_SHA512
113#define MBEDTLS_MD_SOME_LEGACY
114#endif
Dave Rodgman05d71ff2023-06-07 18:02:04 +0100115#if defined(MBEDTLS_SHA3_C)
Dave Rodgmanff45d442023-06-08 10:11:34 +0100116#define MBEDTLS_MD_CAN_SHA3_224
117#define MBEDTLS_MD_CAN_SHA3_256
118#define MBEDTLS_MD_CAN_SHA3_384
119#define MBEDTLS_MD_CAN_SHA3_512
Dave Rodgman05d71ff2023-06-07 18:02:04 +0100120#endif
Gilles Peskine416d0e22022-10-22 18:27:57 +0200121#if defined(MBEDTLS_RIPEMD160_C)
122#define MBEDTLS_MD_CAN_RIPEMD160
123#define MBEDTLS_MD_SOME_LEGACY
124#endif
125
126#endif /* MBEDTLS_MD_LIGHT */
127
Gilles Peskined2971572021-07-26 18:48:10 +0200128/** The selected feature is not available. */
129#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080
130/** Bad input parameters to function. */
131#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100
132/** Failed to allocate memory. */
133#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180
134/** Opening or reading of file failed. */
135#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200
Ron Eldor9924bdc2018-10-04 10:59:13 +0300136
Paul Bakker407a0da2013-06-27 14:29:21 +0200137#ifdef __cplusplus
138extern "C" {
139#endif
140
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100141/**
Rose Zadik8c9c7942018-03-27 11:52:58 +0100142 * \brief Supported message digests.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100143 *
TRodziewicz10e8cf52021-05-31 17:58:57 +0200144 * \warning MD5 and SHA-1 are considered weak message digests and
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100145 * their use constitutes a security risk. We recommend considering
146 * stronger message digests instead.
147 *
148 */
Paul Bakker17373852011-01-06 14:20:01 +0000149typedef enum {
Rose Zadikf3e47362018-04-16 16:31:16 +0100150 MBEDTLS_MD_NONE=0, /**< None. */
Rose Zadikf3e47362018-04-16 16:31:16 +0100151 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
152 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
153 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
154 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
155 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
156 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
Rose Zadik8c9c7942018-03-27 11:52:58 +0100157 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200158 MBEDTLS_MD_SHA3_224, /**< The SHA3-224 message digest. */
159 MBEDTLS_MD_SHA3_256, /**< The SHA3-256 message digest. */
160 MBEDTLS_MD_SHA3_384, /**< The SHA3-384 message digest. */
161 MBEDTLS_MD_SHA3_512, /**< The SHA3-512 message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +0000163
Dave Rodgman93041862023-06-08 10:13:22 +0100164/* Note: this should always be >= PSA_HASH_MAX_SIZE
165 * in all builds with both CRYPTO_C and MD_LIGHT.
166 *
167 * This is to make things easier for modules such as TLS that may define a
168 * buffer size using MD_MAX_SIZE in a part of the code that's common to PSA
169 * and legacy, then assume the buffer's size is PSA_HASH_MAX_SIZE in another
170 * part of the code based on PSA.
171 */
Pol Henarejosd06c6fc2023-05-05 16:01:18 +0200172#if defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_SHA3_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200174#elif defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100175#define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200176#elif defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100177#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200178#elif defined(MBEDTLS_MD_CAN_SHA224)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100179#define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */
Paul Bakker7db01092013-09-10 11:10:57 +0200180#else
Gilles Peskine83d9e092022-10-22 18:32:43 +0200181#define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160
182 or smaller (MD5 and earlier) */
Paul Bakker7db01092013-09-10 11:10:57 +0200183#endif
Paul Bakker1b57b062011-01-06 15:48:19 +0000184
Dave Rodgmanff45d442023-06-08 10:11:34 +0100185#if defined(MBEDTLS_MD_CAN_SHA3_224)
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200186#define MBEDTLS_MD_MAX_BLOCK_SIZE 144 /* the longest known is SHA3-224 */
Pol Henarejosd06c6fc2023-05-05 16:01:18 +0200187#elif defined(MBEDTLS_MD_CAN_SHA512)
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000188#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
189#else
190#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
191#endif
192
Paul Bakker17373852011-01-06 14:20:01 +0000193/**
Chris Jones3848e312021-03-11 16:17:59 +0000194 * Opaque struct.
195 *
196 * Constructed using either #mbedtls_md_info_from_string or
197 * #mbedtls_md_info_from_type.
198 *
199 * Fields can be accessed with #mbedtls_md_get_size,
200 * #mbedtls_md_get_type and #mbedtls_md_get_name.
Paul Bakker17373852011-01-06 14:20:01 +0000201 */
Chris Jones3848e312021-03-11 16:17:59 +0000202/* Defined internally in library/md_wrap.h. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +0000204
205/**
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100206 * Used internally to indicate whether a context uses legacy or PSA.
207 *
208 * Internal use only.
209 */
210typedef enum {
211 MBEDTLS_MD_ENGINE_LEGACY = 0,
212 MBEDTLS_MD_ENGINE_PSA,
213} mbedtls_md_engine_t;
214
215/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000216 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000217 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218typedef struct mbedtls_md_context_t {
Rose Zadik64feefb2018-01-25 22:01:10 +0000219 /** Information about the associated message digest. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200220 const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000221
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100222#if defined(MBEDTLS_MD_SOME_PSA)
223 /** Are hash operations dispatched to PSA or legacy? */
224 mbedtls_md_engine_t MBEDTLS_PRIVATE(engine);
225#endif
226
227 /** The digest-specific context (legacy) or the PSA operation. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200228 void *MBEDTLS_PRIVATE(md_ctx);
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100229
Manuel Pégourié-Gonnard39a376a2023-03-09 17:21:40 +0100230#if defined(MBEDTLS_MD_C)
Rose Zadik64feefb2018-01-25 22:01:10 +0000231 /** The HMAC part of the context. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200232 void *MBEDTLS_PRIVATE(hmac_ctx);
Manuel Pégourié-Gonnard39a376a2023-03-09 17:21:40 +0100233#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000235
Paul Bakker17373852011-01-06 14:20:01 +0000236/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000237 * \brief This function returns the message-digest information
238 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000239 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000240 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000241 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100242 * \return The message-digest information associated with \p md_type.
243 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000244 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
Paul Bakker17373852011-01-06 14:20:01 +0000246
247/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000248 * \brief This function initializes a message-digest context without
249 * binding it to a particular message-digest algorithm.
250 *
251 * This function should always be called first. It prepares the
252 * context for mbedtls_md_setup() for binding it to a
253 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255void mbedtls_md_init(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200256
257/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000258 * \brief This function clears the internal structure of \p ctx and
259 * frees any embedded internal structure, but does not free
260 * \p ctx itself.
261 *
262 * If you have called mbedtls_md_setup() on \p ctx, you must
263 * call mbedtls_md_free() when you are no longer using the
264 * context.
265 * Calling this function if you have previously
266 * called mbedtls_md_init() and nothing else is optional.
267 * You must not call this function if you have not called
268 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200269 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270void mbedtls_md_free(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200271
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100272
Paul Bakker84bbeb52014-07-01 14:53:22 +0200273/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000274 * \brief This function selects the message digest algorithm to use,
275 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000276 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000277 * It should be called after mbedtls_md_init() or
278 * mbedtls_md_free(). Makes it necessary to call
279 * mbedtls_md_free() later.
280 *
281 * \param ctx The context to set up.
282 * \param md_info The information structure of the message-digest algorithm
283 * to use.
Rose Zadik8c9c7942018-03-27 11:52:58 +0100284 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
285 * or non-zero: HMAC is used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000286 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100287 * \return \c 0 on success.
288 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
289 * failure.
290 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000291 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100292MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100293int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
Paul Bakker562535d2011-01-20 16:42:01 +0000294
295/**
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100296 * \brief This function clones the state of a message-digest
Rose Zadik64feefb2018-01-25 22:01:10 +0000297 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200298 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000299 * \note You must call mbedtls_md_setup() on \c dst before calling
300 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200301 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000302 * \note The two contexts must have the same type,
303 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200304 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000305 * \warning This function clones the message-digest state, not the
306 * HMAC state.
307 *
308 * \param dst The destination context.
309 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200310 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100311 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100312 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Manuel Pégourié-Gonnard9b146392023-03-09 15:56:14 +0100313 * \return #MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE if both contexts are
314 * not using the same engine. This can be avoided by moving
315 * the call to psa_crypto_init() before the first call to
316 * mbedtls_md_setup().
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200317 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100318MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100319int mbedtls_md_clone(mbedtls_md_context_t *dst,
320 const mbedtls_md_context_t *src);
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200321
322/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000323 * \brief This function extracts the message-digest size from the
324 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000325 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000326 * \param md_info The information structure of the message-digest algorithm
327 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000328 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000329 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000330 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000332
333/**
Manuel Pégourié-Gonnard1ef26e22023-01-27 11:47:05 +0100334 * \brief This function gives the message-digest size associated to
335 * message-digest type.
336 *
337 * \param md_type The message-digest type.
338 *
339 * \return The size of the message-digest output in Bytes,
340 * or 0 if the message-digest type is not known.
341 */
342static inline unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type)
343{
344 return mbedtls_md_get_size(mbedtls_md_info_from_type(md_type));
345}
346
347/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000348 * \brief This function extracts the message-digest type from the
349 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000350 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000351 * \param md_info The information structure of the message-digest algorithm
352 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000353 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000354 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000355 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100356mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000357
358/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000359 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000360 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000361 * You must call this function after setting up the context
362 * with mbedtls_md_setup(), and before passing data with
363 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000364 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000365 * \param ctx The generic message-digest context.
366 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100367 * \return \c 0 on success.
368 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
369 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000370 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100371MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100372int mbedtls_md_starts(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000373
374/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000375 * \brief This function feeds an input buffer into an ongoing
376 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000377 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000378 * You must call mbedtls_md_starts() before calling this
379 * function. You may call this function multiple times.
380 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000381 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000382 * \param ctx The generic message-digest context.
383 * \param input The buffer holding the input data.
384 * \param ilen The length of the input data.
385 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100386 * \return \c 0 on success.
387 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
388 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000389 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100390MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100391int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000392
393/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000394 * \brief This function finishes the digest operation,
395 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000396 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000397 * Call this function after a call to mbedtls_md_starts(),
398 * followed by any number of calls to mbedtls_md_update().
399 * Afterwards, you may either clear the context with
400 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
401 * the context for another digest operation with the same
402 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000403 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000404 * \param ctx The generic message-digest context.
405 * \param output The buffer for the generic message-digest checksum result.
406 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100407 * \return \c 0 on success.
408 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
409 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000410 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100411MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100412int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000413
414/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000415 * \brief This function calculates the message-digest of a buffer,
416 * with respect to a configurable message-digest algorithm
417 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000418 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000419 * The result is calculated as
420 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000421 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000422 * \param md_info The information structure of the message-digest algorithm
423 * to use.
424 * \param input The buffer holding the data.
425 * \param ilen The length of the input data.
426 * \param output The generic message-digest checksum result.
427 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100428 * \return \c 0 on success.
429 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
430 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000431 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100432MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100433int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
434 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000435
Manuel Pégourié-Gonnard82a43942023-02-23 09:36:29 +0100436/**
437 * \brief This function returns the list of digests supported by the
438 * generic digest module.
439 *
440 * \note The list starts with the strongest available hashes.
441 *
442 * \return A statically allocated array of digests. Each element
443 * in the returned list is an integer belonging to the
444 * message-digest enumeration #mbedtls_md_type_t.
445 * The last entry is 0.
446 */
447const int *mbedtls_md_list(void);
448
449/**
450 * \brief This function returns the message-digest information
451 * associated with the given digest name.
452 *
453 * \param md_name The name of the digest to search for.
454 *
455 * \return The message-digest information associated with \p md_name.
456 * \return NULL if the associated message-digest information is not found.
457 */
458const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
459
460/**
461 * \brief This function extracts the message-digest name from the
462 * message-digest information structure.
463 *
464 * \param md_info The information structure of the message-digest algorithm
465 * to use.
466 *
467 * \return The name of the message digest.
468 */
469const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
470
471/**
472 * \brief This function returns the message-digest information
473 * from the given context.
474 *
475 * \param ctx The context from which to extract the information.
476 * This must be initialized (or \c NULL).
477 *
478 * \return The message-digest information associated with \p ctx.
479 * \return \c NULL if \p ctx is \c NULL.
480 */
481const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
482 const mbedtls_md_context_t *ctx);
483
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200484#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000485/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000486 * \brief This function calculates the message-digest checksum
487 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000488 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000489 * The result is calculated as
490 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000491 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000492 * \param md_info The information structure of the message-digest algorithm
493 * to use.
494 * \param path The input file name.
495 * \param output The generic message-digest checksum result.
496 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100497 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100498 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
499 * the file pointed by \p path.
500 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000501 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100502MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100503int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
504 unsigned char *output);
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200505#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000506
507/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000508 * \brief This function sets the HMAC key and prepares to
509 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000510 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000511 * Call this function after mbedtls_md_setup(), to use
512 * the MD context for an HMAC calculation, then call
513 * mbedtls_md_hmac_update() to provide the input data, and
514 * mbedtls_md_hmac_finish() to get the HMAC value.
Paul Bakker17373852011-01-06 14:20:01 +0000515 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000516 * \param ctx The message digest context containing an embedded HMAC
517 * context.
518 * \param key The HMAC secret key.
519 * \param keylen The length of the HMAC key in Bytes.
520 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100521 * \return \c 0 on success.
522 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
523 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000524 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100525MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100526int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
527 size_t keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000528
529/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000530 * \brief This function feeds an input buffer into an ongoing HMAC
531 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000532 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000533 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
534 * before calling this function.
535 * You may call this function multiple times to pass the
536 * input piecewise.
537 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000538 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000539 * \param ctx The message digest context containing an embedded HMAC
540 * context.
541 * \param input The buffer holding the input data.
542 * \param ilen The length of the input data.
543 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100544 * \return \c 0 on success.
545 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
546 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000547 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100548MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100549int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
550 size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000551
552/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000553 * \brief This function finishes the HMAC operation, and writes
554 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000555 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000556 * Call this function after mbedtls_md_hmac_starts() and
557 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
558 * you may either call mbedtls_md_free() to clear the context,
559 * or call mbedtls_md_hmac_reset() to reuse the context with
560 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000561 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000562 * \param ctx The message digest context containing an embedded HMAC
563 * context.
564 * \param output The generic HMAC checksum result.
565 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100566 * \return \c 0 on success.
567 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
568 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000569 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100570MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100571int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000572
573/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000574 * \brief This function prepares to authenticate a new message with
575 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000576 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000577 * You may call this function after mbedtls_md_hmac_finish().
578 * Afterwards call mbedtls_md_hmac_update() to pass the new
579 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000580 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000581 * \param ctx The message digest context containing an embedded HMAC
582 * context.
583 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100584 * \return \c 0 on success.
585 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
586 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000587 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100588MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100589int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000590
591/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000592 * \brief This function calculates the full generic HMAC
593 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000594 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000595 * The function allocates the context, performs the
596 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000597 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000598 * The HMAC result is calculated as
599 * output = generic HMAC(hmac key, input buffer).
600 *
601 * \param md_info The information structure of the message-digest algorithm
602 * to use.
603 * \param key The HMAC secret key.
604 * \param keylen The length of the HMAC secret key in Bytes.
605 * \param input The buffer holding the input data.
606 * \param ilen The length of the input data.
607 * \param output The generic HMAC result.
608 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100609 * \return \c 0 on success.
610 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
611 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000612 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100613MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100614int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
615 const unsigned char *input, size_t ilen,
616 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000617
Paul Bakker17373852011-01-06 14:20:01 +0000618#ifdef __cplusplus
619}
620#endif
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622#endif /* MBEDTLS_MD_H */