blob: c9a7858f3229e54f798c478ffdc55829c2bc14c8 [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 */
Manuel Pégourié-Gonnard1f6d2e32023-06-06 12:34:45 +0200170/* Note: these are aligned with the definitions of PSA_ALG_ macros for hashes,
171 * in order to enable an efficient implementation of conversion functions.
172 * This is tested by md_to_from_psa() in test_suite_md. */
Paul Bakker17373852011-01-06 14:20:01 +0000173typedef enum {
Rose Zadikf3e47362018-04-16 16:31:16 +0100174 MBEDTLS_MD_NONE=0, /**< None. */
Manuel Pégourié-Gonnard9b763182023-05-31 10:54:08 +0200175 MBEDTLS_MD_MD5=0x03, /**< The MD5 message digest. */
176 MBEDTLS_MD_RIPEMD160=0x04, /**< The RIPEMD-160 message digest. */
177 MBEDTLS_MD_SHA1=0x05, /**< The SHA-1 message digest. */
178 MBEDTLS_MD_SHA224=0x08, /**< The SHA-224 message digest. */
179 MBEDTLS_MD_SHA256=0x09, /**< The SHA-256 message digest. */
180 MBEDTLS_MD_SHA384=0x0a, /**< The SHA-384 message digest. */
181 MBEDTLS_MD_SHA512=0x0b, /**< The SHA-512 message digest. */
182 MBEDTLS_MD_SHA3_224=0x10, /**< The SHA3-224 message digest. */
183 MBEDTLS_MD_SHA3_256=0x11, /**< The SHA3-256 message digest. */
184 MBEDTLS_MD_SHA3_384=0x12, /**< The SHA3-384 message digest. */
185 MBEDTLS_MD_SHA3_512=0x13, /**< The SHA3-512 message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +0000187
Dave Rodgman93041862023-06-08 10:13:22 +0100188/* Note: this should always be >= PSA_HASH_MAX_SIZE
189 * in all builds with both CRYPTO_C and MD_LIGHT.
190 *
191 * This is to make things easier for modules such as TLS that may define a
192 * buffer size using MD_MAX_SIZE in a part of the code that's common to PSA
193 * and legacy, then assume the buffer's size is PSA_HASH_MAX_SIZE in another
194 * part of the code based on PSA.
195 */
Dave Rodgman0442e1b2023-06-08 16:03:33 +0100196#if defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA3_512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Dave Rodgman0442e1b2023-06-08 16:03:33 +0100198#elif defined(MBEDTLS_MD_CAN_SHA384) || defined(MBEDTLS_MD_CAN_SHA3_384)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100199#define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */
Dave Rodgman0442e1b2023-06-08 16:03:33 +0100200#elif defined(MBEDTLS_MD_CAN_SHA256) || defined(MBEDTLS_MD_CAN_SHA3_256)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100201#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */
Dave Rodgman0442e1b2023-06-08 16:03:33 +0100202#elif defined(MBEDTLS_MD_CAN_SHA224) || defined(MBEDTLS_MD_CAN_SHA3_224)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100203#define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */
Paul Bakker7db01092013-09-10 11:10:57 +0200204#else
Gilles Peskine83d9e092022-10-22 18:32:43 +0200205#define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160
206 or smaller (MD5 and earlier) */
Paul Bakker7db01092013-09-10 11:10:57 +0200207#endif
Paul Bakker1b57b062011-01-06 15:48:19 +0000208
Dave Rodgmanff45d442023-06-08 10:11:34 +0100209#if defined(MBEDTLS_MD_CAN_SHA3_224)
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200210#define MBEDTLS_MD_MAX_BLOCK_SIZE 144 /* the longest known is SHA3-224 */
Dave Rodgman0442e1b2023-06-08 16:03:33 +0100211#elif defined(MBEDTLS_MD_CAN_SHA3_256)
212#define MBEDTLS_MD_MAX_BLOCK_SIZE 136
Dave Rodgmanf9563122023-06-11 16:04:29 +0100213#elif defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_MD_CAN_SHA384)
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000214#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
Dave Rodgman0442e1b2023-06-08 16:03:33 +0100215#elif defined(MBEDTLS_MD_CAN_SHA3_384)
216#define MBEDTLS_MD_MAX_BLOCK_SIZE 104
217#elif defined(MBEDTLS_MD_CAN_SHA3_512)
218#define MBEDTLS_MD_MAX_BLOCK_SIZE 72
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000219#else
220#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
221#endif
222
Paul Bakker17373852011-01-06 14:20:01 +0000223/**
Chris Jones3848e312021-03-11 16:17:59 +0000224 * Opaque struct.
225 *
226 * Constructed using either #mbedtls_md_info_from_string or
227 * #mbedtls_md_info_from_type.
228 *
229 * Fields can be accessed with #mbedtls_md_get_size,
230 * #mbedtls_md_get_type and #mbedtls_md_get_name.
Paul Bakker17373852011-01-06 14:20:01 +0000231 */
Chris Jones3848e312021-03-11 16:17:59 +0000232/* Defined internally in library/md_wrap.h. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +0000234
235/**
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100236 * Used internally to indicate whether a context uses legacy or PSA.
237 *
238 * Internal use only.
239 */
240typedef enum {
241 MBEDTLS_MD_ENGINE_LEGACY = 0,
242 MBEDTLS_MD_ENGINE_PSA,
243} mbedtls_md_engine_t;
244
245/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000246 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000247 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100248typedef struct mbedtls_md_context_t {
Rose Zadik64feefb2018-01-25 22:01:10 +0000249 /** Information about the associated message digest. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200250 const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000251
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100252#if defined(MBEDTLS_MD_SOME_PSA)
253 /** Are hash operations dispatched to PSA or legacy? */
254 mbedtls_md_engine_t MBEDTLS_PRIVATE(engine);
255#endif
256
257 /** The digest-specific context (legacy) or the PSA operation. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200258 void *MBEDTLS_PRIVATE(md_ctx);
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100259
Manuel Pégourié-Gonnard39a376a2023-03-09 17:21:40 +0100260#if defined(MBEDTLS_MD_C)
Rose Zadik64feefb2018-01-25 22:01:10 +0000261 /** The HMAC part of the context. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200262 void *MBEDTLS_PRIVATE(hmac_ctx);
Manuel Pégourié-Gonnard39a376a2023-03-09 17:21:40 +0100263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000265
Paul Bakker17373852011-01-06 14:20:01 +0000266/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000267 * \brief This function returns the message-digest information
268 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000269 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000270 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000271 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100272 * \return The message-digest information associated with \p md_type.
273 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
Paul Bakker17373852011-01-06 14:20:01 +0000276
277/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000278 * \brief This function initializes a message-digest context without
279 * binding it to a particular message-digest algorithm.
280 *
281 * This function should always be called first. It prepares the
282 * context for mbedtls_md_setup() for binding it to a
283 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200284 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285void mbedtls_md_init(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200286
287/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000288 * \brief This function clears the internal structure of \p ctx and
289 * frees any embedded internal structure, but does not free
290 * \p ctx itself.
291 *
292 * If you have called mbedtls_md_setup() on \p ctx, you must
293 * call mbedtls_md_free() when you are no longer using the
294 * context.
295 * Calling this function if you have previously
296 * called mbedtls_md_init() and nothing else is optional.
297 * You must not call this function if you have not called
298 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200299 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300void mbedtls_md_free(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200301
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100302
Paul Bakker84bbeb52014-07-01 14:53:22 +0200303/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000304 * \brief This function selects the message digest algorithm to use,
305 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000306 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000307 * It should be called after mbedtls_md_init() or
308 * mbedtls_md_free(). Makes it necessary to call
309 * mbedtls_md_free() later.
310 *
311 * \param ctx The context to set up.
312 * \param md_info The information structure of the message-digest algorithm
313 * to use.
Rose Zadik8c9c7942018-03-27 11:52:58 +0100314 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
315 * or non-zero: HMAC is used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000316 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100317 * \return \c 0 on success.
318 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
319 * failure.
320 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000321 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100322MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100323int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
Paul Bakker562535d2011-01-20 16:42:01 +0000324
325/**
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100326 * \brief This function clones the state of a message-digest
Rose Zadik64feefb2018-01-25 22:01:10 +0000327 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200328 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000329 * \note You must call mbedtls_md_setup() on \c dst before calling
330 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200331 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000332 * \note The two contexts must have the same type,
333 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200334 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000335 * \warning This function clones the message-digest state, not the
336 * HMAC state.
337 *
338 * \param dst The destination context.
339 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200340 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100341 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100342 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Manuel Pégourié-Gonnard9b146392023-03-09 15:56:14 +0100343 * \return #MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE if both contexts are
344 * not using the same engine. This can be avoided by moving
345 * the call to psa_crypto_init() before the first call to
346 * mbedtls_md_setup().
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200347 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100348MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100349int mbedtls_md_clone(mbedtls_md_context_t *dst,
350 const mbedtls_md_context_t *src);
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200351
352/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000353 * \brief This function extracts the message-digest size from the
354 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000355 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000356 * \param md_info The information structure of the message-digest algorithm
357 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000358 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000359 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000360 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000362
363/**
Manuel Pégourié-Gonnard1ef26e22023-01-27 11:47:05 +0100364 * \brief This function gives the message-digest size associated to
365 * message-digest type.
366 *
367 * \param md_type The message-digest type.
368 *
369 * \return The size of the message-digest output in Bytes,
370 * or 0 if the message-digest type is not known.
371 */
372static inline unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type)
373{
374 return mbedtls_md_get_size(mbedtls_md_info_from_type(md_type));
375}
376
377/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000378 * \brief This function extracts the message-digest type from the
379 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000380 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000381 * \param md_info The information structure of the message-digest algorithm
382 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000383 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000384 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000385 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000387
388/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000389 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000390 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000391 * You must call this function after setting up the context
392 * with mbedtls_md_setup(), and before passing data with
393 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000394 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000395 * \param ctx The generic message-digest context.
396 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100397 * \return \c 0 on success.
398 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
399 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000400 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100401MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100402int mbedtls_md_starts(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000403
404/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000405 * \brief This function feeds an input buffer into an ongoing
406 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000407 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000408 * You must call mbedtls_md_starts() before calling this
409 * function. You may call this function multiple times.
410 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000411 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000412 * \param ctx The generic message-digest context.
413 * \param input The buffer holding the input data.
414 * \param ilen The length of the input data.
415 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100416 * \return \c 0 on success.
417 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
418 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000419 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100420MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100421int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000422
423/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000424 * \brief This function finishes the digest operation,
425 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000426 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000427 * Call this function after a call to mbedtls_md_starts(),
428 * followed by any number of calls to mbedtls_md_update().
429 * Afterwards, you may either clear the context with
430 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
431 * the context for another digest operation with the same
432 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000433 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000434 * \param ctx The generic message-digest context.
435 * \param output The buffer for the generic message-digest checksum result.
436 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100437 * \return \c 0 on success.
438 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
439 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000440 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100441MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100442int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000443
444/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000445 * \brief This function calculates the message-digest of a buffer,
446 * with respect to a configurable message-digest algorithm
447 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000448 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000449 * The result is calculated as
450 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000451 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000452 * \param md_info The information structure of the message-digest algorithm
453 * to use.
454 * \param input The buffer holding the data.
455 * \param ilen The length of the input data.
456 * \param output The generic message-digest checksum result.
457 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100458 * \return \c 0 on success.
459 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
460 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000461 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100462MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100463int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
464 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000465
Manuel Pégourié-Gonnard82a43942023-02-23 09:36:29 +0100466/**
467 * \brief This function returns the list of digests supported by the
468 * generic digest module.
469 *
470 * \note The list starts with the strongest available hashes.
471 *
472 * \return A statically allocated array of digests. Each element
473 * in the returned list is an integer belonging to the
474 * message-digest enumeration #mbedtls_md_type_t.
475 * The last entry is 0.
476 */
477const int *mbedtls_md_list(void);
478
479/**
480 * \brief This function returns the message-digest information
481 * associated with the given digest name.
482 *
483 * \param md_name The name of the digest to search for.
484 *
485 * \return The message-digest information associated with \p md_name.
486 * \return NULL if the associated message-digest information is not found.
487 */
488const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
489
490/**
Manuel Pégourié-Gonnard0fda0d22023-07-27 12:22:52 +0200491 * \brief This function returns the name of the message digest for
492 * the message-digest information structure given.
Manuel Pégourié-Gonnard82a43942023-02-23 09:36:29 +0100493 *
494 * \param md_info The information structure of the message-digest algorithm
495 * to use.
496 *
497 * \return The name of the message digest.
498 */
499const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
500
501/**
502 * \brief This function returns the message-digest information
503 * from the given context.
504 *
505 * \param ctx The context from which to extract the information.
506 * This must be initialized (or \c NULL).
507 *
508 * \return The message-digest information associated with \p ctx.
509 * \return \c NULL if \p ctx is \c NULL.
510 */
511const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
512 const mbedtls_md_context_t *ctx);
513
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200514#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000515/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000516 * \brief This function calculates the message-digest checksum
517 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000518 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000519 * The result is calculated as
520 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000521 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000522 * \param md_info The information structure of the message-digest algorithm
523 * to use.
524 * \param path The input file name.
525 * \param output The generic message-digest checksum result.
526 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100527 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100528 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
529 * the file pointed by \p path.
530 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000531 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100532MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100533int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
534 unsigned char *output);
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200535#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000536
537/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000538 * \brief This function sets the HMAC key and prepares to
539 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000540 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000541 * Call this function after mbedtls_md_setup(), to use
542 * the MD context for an HMAC calculation, then call
543 * mbedtls_md_hmac_update() to provide the input data, and
544 * mbedtls_md_hmac_finish() to get the HMAC value.
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 * \param key The HMAC secret key.
549 * \param keylen The length of the HMAC key in Bytes.
550 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100551 * \return \c 0 on success.
552 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
553 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000554 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100555MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100556int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
557 size_t keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000558
559/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000560 * \brief This function feeds an input buffer into an ongoing HMAC
561 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000562 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000563 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
564 * before calling this function.
565 * You may call this function multiple times to pass the
566 * input piecewise.
567 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000568 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000569 * \param ctx The message digest context containing an embedded HMAC
570 * context.
571 * \param input The buffer holding the input data.
572 * \param ilen The length of the input data.
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_update(mbedtls_md_context_t *ctx, const unsigned char *input,
580 size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000581
582/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000583 * \brief This function finishes the HMAC operation, and writes
584 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000585 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000586 * Call this function after mbedtls_md_hmac_starts() and
587 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
588 * you may either call mbedtls_md_free() to clear the context,
589 * or call mbedtls_md_hmac_reset() to reuse the context with
590 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000591 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000592 * \param ctx The message digest context containing an embedded HMAC
593 * context.
594 * \param output The generic HMAC checksum result.
595 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100596 * \return \c 0 on success.
597 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
598 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000599 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100600MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100601int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000602
603/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000604 * \brief This function prepares to authenticate a new message with
605 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000606 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000607 * You may call this function after mbedtls_md_hmac_finish().
608 * Afterwards call mbedtls_md_hmac_update() to pass the new
609 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000610 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000611 * \param ctx The message digest context containing an embedded HMAC
612 * context.
613 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100614 * \return \c 0 on success.
615 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
616 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000617 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100618MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100619int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000620
621/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000622 * \brief This function calculates the full generic HMAC
623 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000624 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000625 * The function allocates the context, performs the
626 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000627 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000628 * The HMAC result is calculated as
629 * output = generic HMAC(hmac key, input buffer).
630 *
631 * \param md_info The information structure of the message-digest algorithm
632 * to use.
633 * \param key The HMAC secret key.
634 * \param keylen The length of the HMAC secret key in Bytes.
635 * \param input The buffer holding the input data.
636 * \param ilen The length of the input data.
637 * \param output The generic HMAC result.
638 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100639 * \return \c 0 on success.
640 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
641 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000642 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100643MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100644int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
645 const unsigned char *input, size_t ilen,
646 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000647
Paul Bakker17373852011-01-06 14:20:01 +0000648#ifdef __cplusplus
649}
650#endif
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#endif /* MBEDTLS_MD_H */