blob: deff9cf621331882d4c994d8d2517a3326e2ada2 [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)
116#define MBEDTLS_MD_CAN_SHA3
117#endif
Gilles Peskine416d0e22022-10-22 18:27:57 +0200118#if defined(MBEDTLS_RIPEMD160_C)
119#define MBEDTLS_MD_CAN_RIPEMD160
120#define MBEDTLS_MD_SOME_LEGACY
121#endif
122
123#endif /* MBEDTLS_MD_LIGHT */
124
Gilles Peskined2971572021-07-26 18:48:10 +0200125/** The selected feature is not available. */
126#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080
127/** Bad input parameters to function. */
128#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100
129/** Failed to allocate memory. */
130#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180
131/** Opening or reading of file failed. */
132#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200
Ron Eldor9924bdc2018-10-04 10:59:13 +0300133
Paul Bakker407a0da2013-06-27 14:29:21 +0200134#ifdef __cplusplus
135extern "C" {
136#endif
137
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100138/**
Rose Zadik8c9c7942018-03-27 11:52:58 +0100139 * \brief Supported message digests.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100140 *
TRodziewicz10e8cf52021-05-31 17:58:57 +0200141 * \warning MD5 and SHA-1 are considered weak message digests and
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100142 * their use constitutes a security risk. We recommend considering
143 * stronger message digests instead.
144 *
145 */
Manuel Pégourié-Gonnard1f6d2e32023-06-06 12:34:45 +0200146/* Note: these are aligned with the definitions of PSA_ALG_ macros for hashes,
147 * in order to enable an efficient implementation of conversion functions.
148 * This is tested by md_to_from_psa() in test_suite_md. */
Paul Bakker17373852011-01-06 14:20:01 +0000149typedef enum {
Rose Zadikf3e47362018-04-16 16:31:16 +0100150 MBEDTLS_MD_NONE=0, /**< None. */
Manuel Pégourié-Gonnard9b763182023-05-31 10:54:08 +0200151 MBEDTLS_MD_MD5=0x03, /**< The MD5 message digest. */
152 MBEDTLS_MD_RIPEMD160=0x04, /**< The RIPEMD-160 message digest. */
153 MBEDTLS_MD_SHA1=0x05, /**< The SHA-1 message digest. */
154 MBEDTLS_MD_SHA224=0x08, /**< The SHA-224 message digest. */
155 MBEDTLS_MD_SHA256=0x09, /**< The SHA-256 message digest. */
156 MBEDTLS_MD_SHA384=0x0a, /**< The SHA-384 message digest. */
157 MBEDTLS_MD_SHA512=0x0b, /**< The SHA-512 message digest. */
158 MBEDTLS_MD_SHA3_224=0x10, /**< The SHA3-224 message digest. */
159 MBEDTLS_MD_SHA3_256=0x11, /**< The SHA3-256 message digest. */
160 MBEDTLS_MD_SHA3_384=0x12, /**< The SHA3-384 message digest. */
161 MBEDTLS_MD_SHA3_512=0x13, /**< 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
Pol Henarejosd06c6fc2023-05-05 16:01:18 +0200164#if defined(MBEDTLS_MD_CAN_SHA512) || defined(MBEDTLS_SHA3_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200166#elif defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100167#define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200168#elif defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100169#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200170#elif defined(MBEDTLS_MD_CAN_SHA224)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100171#define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */
Paul Bakker7db01092013-09-10 11:10:57 +0200172#else
Gilles Peskine83d9e092022-10-22 18:32:43 +0200173#define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160
174 or smaller (MD5 and earlier) */
Paul Bakker7db01092013-09-10 11:10:57 +0200175#endif
Paul Bakker1b57b062011-01-06 15:48:19 +0000176
Dave Rodgman05d71ff2023-06-07 18:02:04 +0100177#if defined(MBEDTLS_MD_CAN_SHA3)
Pol Henarejos4712d4c2022-05-20 14:17:14 +0200178#define MBEDTLS_MD_MAX_BLOCK_SIZE 144 /* the longest known is SHA3-224 */
Pol Henarejosd06c6fc2023-05-05 16:01:18 +0200179#elif defined(MBEDTLS_MD_CAN_SHA512)
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000180#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
181#else
182#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
183#endif
184
Paul Bakker17373852011-01-06 14:20:01 +0000185/**
Chris Jones3848e312021-03-11 16:17:59 +0000186 * Opaque struct.
187 *
188 * Constructed using either #mbedtls_md_info_from_string or
189 * #mbedtls_md_info_from_type.
190 *
191 * Fields can be accessed with #mbedtls_md_get_size,
192 * #mbedtls_md_get_type and #mbedtls_md_get_name.
Paul Bakker17373852011-01-06 14:20:01 +0000193 */
Chris Jones3848e312021-03-11 16:17:59 +0000194/* Defined internally in library/md_wrap.h. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +0000196
197/**
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100198 * Used internally to indicate whether a context uses legacy or PSA.
199 *
200 * Internal use only.
201 */
202typedef enum {
203 MBEDTLS_MD_ENGINE_LEGACY = 0,
204 MBEDTLS_MD_ENGINE_PSA,
205} mbedtls_md_engine_t;
206
207/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000208 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210typedef struct mbedtls_md_context_t {
Rose Zadik64feefb2018-01-25 22:01:10 +0000211 /** Information about the associated message digest. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200212 const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000213
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100214#if defined(MBEDTLS_MD_SOME_PSA)
215 /** Are hash operations dispatched to PSA or legacy? */
216 mbedtls_md_engine_t MBEDTLS_PRIVATE(engine);
217#endif
218
219 /** The digest-specific context (legacy) or the PSA operation. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200220 void *MBEDTLS_PRIVATE(md_ctx);
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100221
Manuel Pégourié-Gonnard39a376a2023-03-09 17:21:40 +0100222#if defined(MBEDTLS_MD_C)
Rose Zadik64feefb2018-01-25 22:01:10 +0000223 /** The HMAC part of the context. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200224 void *MBEDTLS_PRIVATE(hmac_ctx);
Manuel Pégourié-Gonnard39a376a2023-03-09 17:21:40 +0100225#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000227
Paul Bakker17373852011-01-06 14:20:01 +0000228/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000229 * \brief This function returns the message-digest information
230 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000231 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000232 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000233 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100234 * \return The message-digest information associated with \p md_type.
235 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000236 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
Paul Bakker17373852011-01-06 14:20:01 +0000238
239/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000240 * \brief This function initializes a message-digest context without
241 * binding it to a particular message-digest algorithm.
242 *
243 * This function should always be called first. It prepares the
244 * context for mbedtls_md_setup() for binding it to a
245 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200246 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247void mbedtls_md_init(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200248
249/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000250 * \brief This function clears the internal structure of \p ctx and
251 * frees any embedded internal structure, but does not free
252 * \p ctx itself.
253 *
254 * If you have called mbedtls_md_setup() on \p ctx, you must
255 * call mbedtls_md_free() when you are no longer using the
256 * context.
257 * Calling this function if you have previously
258 * called mbedtls_md_init() and nothing else is optional.
259 * You must not call this function if you have not called
260 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200261 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262void mbedtls_md_free(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200263
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100264
Paul Bakker84bbeb52014-07-01 14:53:22 +0200265/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000266 * \brief This function selects the message digest algorithm to use,
267 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000268 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000269 * It should be called after mbedtls_md_init() or
270 * mbedtls_md_free(). Makes it necessary to call
271 * mbedtls_md_free() later.
272 *
273 * \param ctx The context to set up.
274 * \param md_info The information structure of the message-digest algorithm
275 * to use.
Rose Zadik8c9c7942018-03-27 11:52:58 +0100276 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
277 * or non-zero: HMAC is used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000278 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100279 * \return \c 0 on success.
280 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
281 * failure.
282 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000283 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100284MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100285int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
Paul Bakker562535d2011-01-20 16:42:01 +0000286
287/**
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100288 * \brief This function clones the state of a message-digest
Rose Zadik64feefb2018-01-25 22:01:10 +0000289 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200290 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000291 * \note You must call mbedtls_md_setup() on \c dst before calling
292 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200293 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000294 * \note The two contexts must have the same type,
295 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200296 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000297 * \warning This function clones the message-digest state, not the
298 * HMAC state.
299 *
300 * \param dst The destination context.
301 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200302 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100303 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100304 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Manuel Pégourié-Gonnard9b146392023-03-09 15:56:14 +0100305 * \return #MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE if both contexts are
306 * not using the same engine. This can be avoided by moving
307 * the call to psa_crypto_init() before the first call to
308 * mbedtls_md_setup().
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200309 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100310MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100311int mbedtls_md_clone(mbedtls_md_context_t *dst,
312 const mbedtls_md_context_t *src);
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200313
314/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000315 * \brief This function extracts the message-digest size from the
316 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000317 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000318 * \param md_info The information structure of the message-digest algorithm
319 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000320 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000321 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000322 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100323unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000324
325/**
Manuel Pégourié-Gonnard1ef26e22023-01-27 11:47:05 +0100326 * \brief This function gives the message-digest size associated to
327 * message-digest type.
328 *
329 * \param md_type The message-digest type.
330 *
331 * \return The size of the message-digest output in Bytes,
332 * or 0 if the message-digest type is not known.
333 */
334static inline unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type)
335{
336 return mbedtls_md_get_size(mbedtls_md_info_from_type(md_type));
337}
338
339/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000340 * \brief This function extracts the message-digest type from the
341 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000342 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000343 * \param md_info The information structure of the message-digest algorithm
344 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000345 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000346 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000347 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000349
350/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000351 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000352 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000353 * You must call this function after setting up the context
354 * with mbedtls_md_setup(), and before passing data with
355 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000356 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000357 * \param ctx The generic message-digest context.
358 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100359 * \return \c 0 on success.
360 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
361 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000362 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100363MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100364int mbedtls_md_starts(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000365
366/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000367 * \brief This function feeds an input buffer into an ongoing
368 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000369 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000370 * You must call mbedtls_md_starts() before calling this
371 * function. You may call this function multiple times.
372 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000373 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000374 * \param ctx The generic message-digest context.
375 * \param input The buffer holding the input data.
376 * \param ilen The length of the input data.
377 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100378 * \return \c 0 on success.
379 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
380 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000381 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100382MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100383int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000384
385/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000386 * \brief This function finishes the digest operation,
387 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000388 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000389 * Call this function after a call to mbedtls_md_starts(),
390 * followed by any number of calls to mbedtls_md_update().
391 * Afterwards, you may either clear the context with
392 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
393 * the context for another digest operation with the same
394 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000395 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000396 * \param ctx The generic message-digest context.
397 * \param output The buffer for the generic message-digest checksum result.
398 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100399 * \return \c 0 on success.
400 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
401 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000402 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100403MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100404int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000405
406/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000407 * \brief This function calculates the message-digest of a buffer,
408 * with respect to a configurable message-digest algorithm
409 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000410 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000411 * The result is calculated as
412 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000413 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000414 * \param md_info The information structure of the message-digest algorithm
415 * to use.
416 * \param input The buffer holding the data.
417 * \param ilen The length of the input data.
418 * \param output The generic message-digest checksum result.
419 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100420 * \return \c 0 on success.
421 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
422 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000423 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100424MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100425int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
426 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000427
Manuel Pégourié-Gonnard82a43942023-02-23 09:36:29 +0100428/**
429 * \brief This function returns the list of digests supported by the
430 * generic digest module.
431 *
432 * \note The list starts with the strongest available hashes.
433 *
434 * \return A statically allocated array of digests. Each element
435 * in the returned list is an integer belonging to the
436 * message-digest enumeration #mbedtls_md_type_t.
437 * The last entry is 0.
438 */
439const int *mbedtls_md_list(void);
440
441/**
442 * \brief This function returns the message-digest information
443 * associated with the given digest name.
444 *
445 * \param md_name The name of the digest to search for.
446 *
447 * \return The message-digest information associated with \p md_name.
448 * \return NULL if the associated message-digest information is not found.
449 */
450const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
451
452/**
453 * \brief This function extracts the message-digest name from the
454 * message-digest information structure.
455 *
456 * \param md_info The information structure of the message-digest algorithm
457 * to use.
458 *
459 * \return The name of the message digest.
460 */
461const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
462
463/**
464 * \brief This function returns the message-digest information
465 * from the given context.
466 *
467 * \param ctx The context from which to extract the information.
468 * This must be initialized (or \c NULL).
469 *
470 * \return The message-digest information associated with \p ctx.
471 * \return \c NULL if \p ctx is \c NULL.
472 */
473const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
474 const mbedtls_md_context_t *ctx);
475
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200476#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000477/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000478 * \brief This function calculates the message-digest checksum
479 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000480 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000481 * The result is calculated as
482 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000483 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000484 * \param md_info The information structure of the message-digest algorithm
485 * to use.
486 * \param path The input file name.
487 * \param output The generic message-digest checksum result.
488 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100489 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100490 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
491 * the file pointed by \p path.
492 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000493 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100494MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100495int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
496 unsigned char *output);
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200497#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000498
499/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000500 * \brief This function sets the HMAC key and prepares to
501 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000502 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000503 * Call this function after mbedtls_md_setup(), to use
504 * the MD context for an HMAC calculation, then call
505 * mbedtls_md_hmac_update() to provide the input data, and
506 * mbedtls_md_hmac_finish() to get the HMAC value.
Paul Bakker17373852011-01-06 14:20:01 +0000507 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000508 * \param ctx The message digest context containing an embedded HMAC
509 * context.
510 * \param key The HMAC secret key.
511 * \param keylen The length of the HMAC key in Bytes.
512 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100513 * \return \c 0 on success.
514 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
515 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000516 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100517MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100518int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
519 size_t keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000520
521/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000522 * \brief This function feeds an input buffer into an ongoing HMAC
523 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000524 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000525 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
526 * before calling this function.
527 * You may call this function multiple times to pass the
528 * input piecewise.
529 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000530 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000531 * \param ctx The message digest context containing an embedded HMAC
532 * context.
533 * \param input The buffer holding the input data.
534 * \param ilen The length of the input data.
535 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100536 * \return \c 0 on success.
537 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
538 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000539 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100540MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100541int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
542 size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000543
544/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000545 * \brief This function finishes the HMAC operation, and writes
546 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000547 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000548 * Call this function after mbedtls_md_hmac_starts() and
549 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
550 * you may either call mbedtls_md_free() to clear the context,
551 * or call mbedtls_md_hmac_reset() to reuse the context with
552 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000553 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000554 * \param ctx The message digest context containing an embedded HMAC
555 * context.
556 * \param output The generic HMAC checksum result.
557 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100558 * \return \c 0 on success.
559 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
560 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000561 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100562MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100563int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000564
565/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000566 * \brief This function prepares to authenticate a new message with
567 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000568 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000569 * You may call this function after mbedtls_md_hmac_finish().
570 * Afterwards call mbedtls_md_hmac_update() to pass the new
571 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000572 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000573 * \param ctx The message digest context containing an embedded HMAC
574 * context.
575 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100576 * \return \c 0 on success.
577 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
578 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000579 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100580MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100581int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000582
583/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000584 * \brief This function calculates the full generic HMAC
585 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000586 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000587 * The function allocates the context, performs the
588 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000589 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000590 * The HMAC result is calculated as
591 * output = generic HMAC(hmac key, input buffer).
592 *
593 * \param md_info The information structure of the message-digest algorithm
594 * to use.
595 * \param key The HMAC secret key.
596 * \param keylen The length of the HMAC secret key in Bytes.
597 * \param input The buffer holding the input data.
598 * \param ilen The length of the input data.
599 * \param output The generic HMAC result.
600 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100601 * \return \c 0 on success.
602 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
603 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000604 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100605MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100606int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
607 const unsigned char *input, size_t ilen,
608 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000609
Paul Bakker17373852011-01-06 14:20:01 +0000610#ifdef __cplusplus
611}
612#endif
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614#endif /* MBEDTLS_MD_H */