blob: de4f8758ed584812f923d810a34f231f7286d857 [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
Dave Rodgman852b6c32023-07-05 19:47:08 +010088#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA3_224)
89#define MBEDTLS_MD_CAN_SHA3_224
90#define MBEDTLS_MD_SHA3_224_VIA_PSA
91#define MBEDTLS_MD_SOME_PSA
92#endif
93#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA3_256)
94#define MBEDTLS_MD_CAN_SHA3_256
95#define MBEDTLS_MD_SHA3_256_VIA_PSA
96#define MBEDTLS_MD_SOME_PSA
97#endif
98#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA3_384)
99#define MBEDTLS_MD_CAN_SHA3_384
100#define MBEDTLS_MD_SHA3_384_VIA_PSA
101#define MBEDTLS_MD_SOME_PSA
102#endif
103#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA3_512)
104#define MBEDTLS_MD_CAN_SHA3_512
105#define MBEDTLS_MD_SHA3_512_VIA_PSA
106#define MBEDTLS_MD_SOME_PSA
107#endif
Gilles Peskine416d0e22022-10-22 18:27:57 +0200108#endif /* MBEDTLS_PSA_CRYPTO_C */
109
110/* Built-in implementations */
111#if defined(MBEDTLS_MD5_C)
112#define MBEDTLS_MD_CAN_MD5
113#define MBEDTLS_MD_SOME_LEGACY
114#endif
115#if defined(MBEDTLS_SHA1_C)
116#define MBEDTLS_MD_CAN_SHA1
117#define MBEDTLS_MD_SOME_LEGACY
118#endif
119#if defined(MBEDTLS_SHA224_C)
120#define MBEDTLS_MD_CAN_SHA224
121#define MBEDTLS_MD_SOME_LEGACY
122#endif
123#if defined(MBEDTLS_SHA256_C)
124#define MBEDTLS_MD_CAN_SHA256
125#define MBEDTLS_MD_SOME_LEGACY
126#endif
127#if defined(MBEDTLS_SHA384_C)
128#define MBEDTLS_MD_CAN_SHA384
129#define MBEDTLS_MD_SOME_LEGACY
130#endif
131#if defined(MBEDTLS_SHA512_C)
132#define MBEDTLS_MD_CAN_SHA512
133#define MBEDTLS_MD_SOME_LEGACY
134#endif
Dave Rodgman05d71ff2023-06-07 18:02:04 +0100135#if defined(MBEDTLS_SHA3_C)
Dave Rodgmanff45d442023-06-08 10:11:34 +0100136#define MBEDTLS_MD_CAN_SHA3_224
137#define MBEDTLS_MD_CAN_SHA3_256
138#define MBEDTLS_MD_CAN_SHA3_384
139#define MBEDTLS_MD_CAN_SHA3_512
Dave Rodgman852b6c32023-07-05 19:47:08 +0100140#define MBEDTLS_MD_SOME_LEGACY
Dave Rodgman05d71ff2023-06-07 18:02:04 +0100141#endif
Gilles Peskine416d0e22022-10-22 18:27:57 +0200142#if defined(MBEDTLS_RIPEMD160_C)
143#define MBEDTLS_MD_CAN_RIPEMD160
144#define MBEDTLS_MD_SOME_LEGACY
145#endif
146
147#endif /* MBEDTLS_MD_LIGHT */
148
Gilles Peskined2971572021-07-26 18:48:10 +0200149/** The selected feature is not available. */
150#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080
151/** Bad input parameters to function. */
152#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100
153/** Failed to allocate memory. */
154#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180
155/** Opening or reading of file failed. */
156#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200
Ron Eldor9924bdc2018-10-04 10:59:13 +0300157
Paul Bakker407a0da2013-06-27 14:29:21 +0200158#ifdef __cplusplus
159extern "C" {
160#endif
161
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100162/**
Rose Zadik8c9c7942018-03-27 11:52:58 +0100163 * \brief Supported message digests.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100164 *
TRodziewicz10e8cf52021-05-31 17:58:57 +0200165 * \warning MD5 and SHA-1 are considered weak message digests and
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100166 * their use constitutes a security risk. We recommend considering
167 * stronger message digests instead.
168 *
169 */
Paul Bakker17373852011-01-06 14:20:01 +0000170typedef enum {
Rose Zadikf3e47362018-04-16 16:31:16 +0100171 MBEDTLS_MD_NONE=0, /**< None. */
Rose Zadikf3e47362018-04-16 16:31:16 +0100172 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
173 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
174 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
175 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
176 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
177 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
Rose Zadik8c9c7942018-03-27 11:52:58 +0100178 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200179 MBEDTLS_MD_SHA3_224, /**< The SHA3-224 message digest. */
180 MBEDTLS_MD_SHA3_256, /**< The SHA3-256 message digest. */
181 MBEDTLS_MD_SHA3_384, /**< The SHA3-384 message digest. */
182 MBEDTLS_MD_SHA3_512, /**< The SHA3-512 message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +0000184
Dave Rodgman93041862023-06-08 10:13:22 +0100185/* Note: this should always be >= PSA_HASH_MAX_SIZE
186 * in all builds with both CRYPTO_C and MD_LIGHT.
187 *
188 * This is to make things easier for modules such as TLS that may define a
189 * buffer size using MD_MAX_SIZE in a part of the code that's common to PSA
190 * and legacy, then assume the buffer's size is PSA_HASH_MAX_SIZE in another
191 * part of the code based on PSA.
192 */
Dave Rodgman0442e1b2023-06-08 16:03:33 +0100193#if defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA3_512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Dave Rodgman0442e1b2023-06-08 16:03:33 +0100195#elif defined(MBEDTLS_MD_CAN_SHA384) || defined(MBEDTLS_MD_CAN_SHA3_384)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100196#define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */
Dave Rodgman0442e1b2023-06-08 16:03:33 +0100197#elif defined(MBEDTLS_MD_CAN_SHA256) || defined(MBEDTLS_MD_CAN_SHA3_256)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100198#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */
Dave Rodgman0442e1b2023-06-08 16:03:33 +0100199#elif defined(MBEDTLS_MD_CAN_SHA224) || defined(MBEDTLS_MD_CAN_SHA3_224)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100200#define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */
Paul Bakker7db01092013-09-10 11:10:57 +0200201#else
Gilles Peskine83d9e092022-10-22 18:32:43 +0200202#define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160
203 or smaller (MD5 and earlier) */
Paul Bakker7db01092013-09-10 11:10:57 +0200204#endif
Paul Bakker1b57b062011-01-06 15:48:19 +0000205
Dave Rodgmanff45d442023-06-08 10:11:34 +0100206#if defined(MBEDTLS_MD_CAN_SHA3_224)
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200207#define MBEDTLS_MD_MAX_BLOCK_SIZE 144 /* the longest known is SHA3-224 */
Dave Rodgman0442e1b2023-06-08 16:03:33 +0100208#elif defined(MBEDTLS_MD_CAN_SHA3_256)
209#define MBEDTLS_MD_MAX_BLOCK_SIZE 136
Dave Rodgmanf9563122023-06-11 16:04:29 +0100210#elif defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA384)
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000211#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
Dave Rodgman0442e1b2023-06-08 16:03:33 +0100212#elif defined(MBEDTLS_MD_CAN_SHA3_384)
213#define MBEDTLS_MD_MAX_BLOCK_SIZE 104
214#elif defined(MBEDTLS_MD_CAN_SHA3_512)
215#define MBEDTLS_MD_MAX_BLOCK_SIZE 72
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000216#else
217#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
218#endif
219
Paul Bakker17373852011-01-06 14:20:01 +0000220/**
Chris Jones3848e312021-03-11 16:17:59 +0000221 * Opaque struct.
222 *
223 * Constructed using either #mbedtls_md_info_from_string or
224 * #mbedtls_md_info_from_type.
225 *
226 * Fields can be accessed with #mbedtls_md_get_size,
227 * #mbedtls_md_get_type and #mbedtls_md_get_name.
Paul Bakker17373852011-01-06 14:20:01 +0000228 */
Chris Jones3848e312021-03-11 16:17:59 +0000229/* Defined internally in library/md_wrap.h. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +0000231
232/**
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100233 * Used internally to indicate whether a context uses legacy or PSA.
234 *
235 * Internal use only.
236 */
237typedef enum {
238 MBEDTLS_MD_ENGINE_LEGACY = 0,
239 MBEDTLS_MD_ENGINE_PSA,
240} mbedtls_md_engine_t;
241
242/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000243 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000244 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245typedef struct mbedtls_md_context_t {
Rose Zadik64feefb2018-01-25 22:01:10 +0000246 /** Information about the associated message digest. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200247 const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000248
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100249#if defined(MBEDTLS_MD_SOME_PSA)
250 /** Are hash operations dispatched to PSA or legacy? */
251 mbedtls_md_engine_t MBEDTLS_PRIVATE(engine);
252#endif
253
254 /** The digest-specific context (legacy) or the PSA operation. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200255 void *MBEDTLS_PRIVATE(md_ctx);
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100256
Manuel Pégourié-Gonnard39a376a2023-03-09 17:21:40 +0100257#if defined(MBEDTLS_MD_C)
Rose Zadik64feefb2018-01-25 22:01:10 +0000258 /** The HMAC part of the context. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200259 void *MBEDTLS_PRIVATE(hmac_ctx);
Manuel Pégourié-Gonnard39a376a2023-03-09 17:21:40 +0100260#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000262
Paul Bakker17373852011-01-06 14:20:01 +0000263/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000264 * \brief This function returns the message-digest information
265 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000266 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000267 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000268 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100269 * \return The message-digest information associated with \p md_type.
270 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000271 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
Paul Bakker17373852011-01-06 14:20:01 +0000273
274/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000275 * \brief This function initializes a message-digest context without
276 * binding it to a particular message-digest algorithm.
277 *
278 * This function should always be called first. It prepares the
279 * context for mbedtls_md_setup() for binding it to a
280 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200281 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100282void mbedtls_md_init(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200283
284/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000285 * \brief This function clears the internal structure of \p ctx and
286 * frees any embedded internal structure, but does not free
287 * \p ctx itself.
288 *
289 * If you have called mbedtls_md_setup() on \p ctx, you must
290 * call mbedtls_md_free() when you are no longer using the
291 * context.
292 * Calling this function if you have previously
293 * called mbedtls_md_init() and nothing else is optional.
294 * You must not call this function if you have not called
295 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200296 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100297void mbedtls_md_free(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200298
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100299
Paul Bakker84bbeb52014-07-01 14:53:22 +0200300/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000301 * \brief This function selects the message digest algorithm to use,
302 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000303 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000304 * It should be called after mbedtls_md_init() or
305 * mbedtls_md_free(). Makes it necessary to call
306 * mbedtls_md_free() later.
307 *
308 * \param ctx The context to set up.
309 * \param md_info The information structure of the message-digest algorithm
310 * to use.
Rose Zadik8c9c7942018-03-27 11:52:58 +0100311 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
312 * or non-zero: HMAC is used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000313 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100314 * \return \c 0 on success.
315 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
316 * failure.
317 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000318 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100319MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100320int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
Paul Bakker562535d2011-01-20 16:42:01 +0000321
322/**
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100323 * \brief This function clones the state of a message-digest
Rose Zadik64feefb2018-01-25 22:01:10 +0000324 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200325 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000326 * \note You must call mbedtls_md_setup() on \c dst before calling
327 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200328 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000329 * \note The two contexts must have the same type,
330 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200331 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000332 * \warning This function clones the message-digest state, not the
333 * HMAC state.
334 *
335 * \param dst The destination context.
336 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200337 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100338 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100339 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Manuel Pégourié-Gonnard9b146392023-03-09 15:56:14 +0100340 * \return #MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE if both contexts are
341 * not using the same engine. This can be avoided by moving
342 * the call to psa_crypto_init() before the first call to
343 * mbedtls_md_setup().
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200344 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100345MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100346int mbedtls_md_clone(mbedtls_md_context_t *dst,
347 const mbedtls_md_context_t *src);
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200348
349/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000350 * \brief This function extracts the message-digest size from the
351 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000352 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000353 * \param md_info The information structure of the message-digest algorithm
354 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000355 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000356 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000359
360/**
Manuel Pégourié-Gonnard1ef26e22023-01-27 11:47:05 +0100361 * \brief This function gives the message-digest size associated to
362 * message-digest type.
363 *
364 * \param md_type The message-digest type.
365 *
366 * \return The size of the message-digest output in Bytes,
367 * or 0 if the message-digest type is not known.
368 */
369static inline unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type)
370{
371 return mbedtls_md_get_size(mbedtls_md_info_from_type(md_type));
372}
373
374/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000375 * \brief This function extracts the message-digest type from the
376 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000377 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000378 * \param md_info The information structure of the message-digest algorithm
379 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000380 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000381 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000382 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100383mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000384
385/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000386 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000387 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000388 * You must call this function after setting up the context
389 * with mbedtls_md_setup(), and before passing data with
390 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000391 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000392 * \param ctx The generic message-digest context.
393 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100394 * \return \c 0 on success.
395 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
396 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000397 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100398MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100399int mbedtls_md_starts(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000400
401/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000402 * \brief This function feeds an input buffer into an ongoing
403 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000404 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000405 * You must call mbedtls_md_starts() before calling this
406 * function. You may call this function multiple times.
407 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000408 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000409 * \param ctx The generic message-digest context.
410 * \param input The buffer holding the input data.
411 * \param ilen The length of the input data.
412 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100413 * \return \c 0 on success.
414 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
415 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000416 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100417MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100418int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000419
420/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000421 * \brief This function finishes the digest operation,
422 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000423 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000424 * Call this function after a call to mbedtls_md_starts(),
425 * followed by any number of calls to mbedtls_md_update().
426 * Afterwards, you may either clear the context with
427 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
428 * the context for another digest operation with the same
429 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000430 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000431 * \param ctx The generic message-digest context.
432 * \param output The buffer for the generic message-digest checksum result.
433 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100434 * \return \c 0 on success.
435 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
436 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000437 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100438MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100439int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000440
441/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000442 * \brief This function calculates the message-digest of a buffer,
443 * with respect to a configurable message-digest algorithm
444 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000445 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000446 * The result is calculated as
447 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000448 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000449 * \param md_info The information structure of the message-digest algorithm
450 * to use.
451 * \param input The buffer holding the data.
452 * \param ilen The length of the input data.
453 * \param output The generic message-digest checksum result.
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(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
461 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000462
Manuel Pégourié-Gonnard82a43942023-02-23 09:36:29 +0100463/**
464 * \brief This function returns the list of digests supported by the
465 * generic digest module.
466 *
467 * \note The list starts with the strongest available hashes.
468 *
469 * \return A statically allocated array of digests. Each element
470 * in the returned list is an integer belonging to the
471 * message-digest enumeration #mbedtls_md_type_t.
472 * The last entry is 0.
473 */
474const int *mbedtls_md_list(void);
475
476/**
477 * \brief This function returns the message-digest information
478 * associated with the given digest name.
479 *
480 * \param md_name The name of the digest to search for.
481 *
482 * \return The message-digest information associated with \p md_name.
483 * \return NULL if the associated message-digest information is not found.
484 */
485const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
486
487/**
488 * \brief This function extracts the message-digest name from the
489 * message-digest information structure.
490 *
491 * \param md_info The information structure of the message-digest algorithm
492 * to use.
493 *
494 * \return The name of the message digest.
495 */
496const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
497
498/**
499 * \brief This function returns the message-digest information
500 * from the given context.
501 *
502 * \param ctx The context from which to extract the information.
503 * This must be initialized (or \c NULL).
504 *
505 * \return The message-digest information associated with \p ctx.
506 * \return \c NULL if \p ctx is \c NULL.
507 */
508const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
509 const mbedtls_md_context_t *ctx);
510
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200511#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000512/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000513 * \brief This function calculates the message-digest checksum
514 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000515 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000516 * The result is calculated as
517 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000518 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000519 * \param md_info The information structure of the message-digest algorithm
520 * to use.
521 * \param path The input file name.
522 * \param output The generic message-digest checksum result.
523 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100524 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100525 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
526 * the file pointed by \p path.
527 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000528 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100529MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100530int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
531 unsigned char *output);
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200532#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000533
534/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000535 * \brief This function sets the HMAC key and prepares to
536 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000537 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000538 * Call this function after mbedtls_md_setup(), to use
539 * the MD context for an HMAC calculation, then call
540 * mbedtls_md_hmac_update() to provide the input data, and
541 * mbedtls_md_hmac_finish() to get the HMAC value.
Paul Bakker17373852011-01-06 14:20:01 +0000542 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000543 * \param ctx The message digest context containing an embedded HMAC
544 * context.
545 * \param key The HMAC secret key.
546 * \param keylen The length of the HMAC key in Bytes.
547 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100548 * \return \c 0 on success.
549 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
550 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000551 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100552MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100553int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
554 size_t keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000555
556/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000557 * \brief This function feeds an input buffer into an ongoing HMAC
558 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000559 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000560 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
561 * before calling this function.
562 * You may call this function multiple times to pass the
563 * input piecewise.
564 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000565 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000566 * \param ctx The message digest context containing an embedded HMAC
567 * context.
568 * \param input The buffer holding the input data.
569 * \param ilen The length of the input data.
570 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100571 * \return \c 0 on success.
572 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
573 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000574 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100575MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100576int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
577 size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000578
579/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000580 * \brief This function finishes the HMAC operation, and writes
581 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000582 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000583 * Call this function after mbedtls_md_hmac_starts() and
584 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
585 * you may either call mbedtls_md_free() to clear the context,
586 * or call mbedtls_md_hmac_reset() to reuse the context with
587 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000588 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000589 * \param ctx The message digest context containing an embedded HMAC
590 * context.
591 * \param output The generic HMAC checksum result.
592 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100593 * \return \c 0 on success.
594 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
595 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000596 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100597MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100598int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000599
600/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000601 * \brief This function prepares to authenticate a new message with
602 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000603 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000604 * You may call this function after mbedtls_md_hmac_finish().
605 * Afterwards call mbedtls_md_hmac_update() to pass the new
606 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000607 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000608 * \param ctx The message digest context containing an embedded HMAC
609 * context.
610 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100611 * \return \c 0 on success.
612 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
613 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000614 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100615MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100616int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000617
618/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000619 * \brief This function calculates the full generic HMAC
620 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000621 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000622 * The function allocates the context, performs the
623 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000624 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000625 * The HMAC result is calculated as
626 * output = generic HMAC(hmac key, input buffer).
627 *
628 * \param md_info The information structure of the message-digest algorithm
629 * to use.
630 * \param key The HMAC secret key.
631 * \param keylen The length of the HMAC secret key in Bytes.
632 * \param input The buffer holding the input data.
633 * \param ilen The length of the input data.
634 * \param output The generic HMAC result.
635 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100636 * \return \c 0 on success.
637 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
638 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000639 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100640MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100641int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
642 const unsigned char *input, size_t ilen,
643 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000644
Paul Bakker17373852011-01-06 14:20:01 +0000645#ifdef __cplusplus
646}
647#endif
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#endif /* MBEDTLS_MD_H */