blob: 657b5cc01ceb9875c2af62000502885ea8d0d957 [file] [log] [blame]
Gilles Peskine449bd832023-01-11 14:50:10 +01001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file md.h
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Manuel Pégourié-Gonnardb9b630d2023-02-16 19:07:31 +01004 * \brief This file contains the generic functions for message-digest
5 * (hashing) and HMAC.
6 *
Paul Bakker17373852011-01-06 14:20:01 +00007 * \author Adriaan de Jong <dejong@fox-it.com>
Darryl Greena40a1012018-01-05 15:33:17 +00008 */
9/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020010 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020011 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker17373852011-01-06 14:20:01 +000024 */
Rose Zadik64feefb2018-01-25 22:01:10 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#ifndef MBEDTLS_MD_H
27#define MBEDTLS_MD_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020028#include "mbedtls/private_access.h"
Paul Bakker17373852011-01-06 14:20:01 +000029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000031
Bence Szépkútic662b362021-05-27 11:25:03 +020032#include "mbedtls/build_info.h"
Gilles Peskineecf6beb2021-12-10 21:35:10 +010033#include "mbedtls/platform_util.h"
Ron Eldorf231eaa2017-08-22 14:50:14 +030034
Gilles Peskine416d0e22022-10-22 18:27:57 +020035#if defined(MBEDTLS_MD_LIGHT)
36
37/*
38 * - MBEDTLS_MD_CAN_xxx is defined if the md module can perform xxx.
Manuel Pégourié-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
115#if defined(MBEDTLS_RIPEMD160_C)
116#define MBEDTLS_MD_CAN_RIPEMD160
117#define MBEDTLS_MD_SOME_LEGACY
118#endif
119
120#endif /* MBEDTLS_MD_LIGHT */
121
Gilles Peskined2971572021-07-26 18:48:10 +0200122/** The selected feature is not available. */
123#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080
124/** Bad input parameters to function. */
125#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100
126/** Failed to allocate memory. */
127#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180
128/** Opening or reading of file failed. */
129#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200
Ron Eldor9924bdc2018-10-04 10:59:13 +0300130
Paul Bakker407a0da2013-06-27 14:29:21 +0200131#ifdef __cplusplus
132extern "C" {
133#endif
134
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100135/**
Rose Zadik8c9c7942018-03-27 11:52:58 +0100136 * \brief Supported message digests.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100137 *
TRodziewicz10e8cf52021-05-31 17:58:57 +0200138 * \warning MD5 and SHA-1 are considered weak message digests and
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100139 * their use constitutes a security risk. We recommend considering
140 * stronger message digests instead.
141 *
142 */
Paul Bakker17373852011-01-06 14:20:01 +0000143typedef enum {
Rose Zadikf3e47362018-04-16 16:31:16 +0100144 MBEDTLS_MD_NONE=0, /**< None. */
Rose Zadikf3e47362018-04-16 16:31:16 +0100145 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
146 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
147 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
148 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
149 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
150 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
Rose Zadik8c9c7942018-03-27 11:52:58 +0100151 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +0000153
Manuel Pégourié-Gonnard45b34512023-03-30 12:19:35 +0200154/* Note: this should be kept in sync with PSA_HASH_MAX_SIZE */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200155#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200157#elif defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100158#define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200159#elif defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100160#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200161#elif defined(MBEDTLS_MD_CAN_SHA224)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100162#define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */
Paul Bakker7db01092013-09-10 11:10:57 +0200163#else
Gilles Peskine83d9e092022-10-22 18:32:43 +0200164#define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160
165 or smaller (MD5 and earlier) */
Paul Bakker7db01092013-09-10 11:10:57 +0200166#endif
Paul Bakker1b57b062011-01-06 15:48:19 +0000167
Gilles Peskine83d9e092022-10-22 18:32:43 +0200168#if defined(MBEDTLS_MD_CAN_SHA512)
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000169#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
170#else
171#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
172#endif
173
Paul Bakker17373852011-01-06 14:20:01 +0000174/**
Chris Jones3848e312021-03-11 16:17:59 +0000175 * Opaque struct.
176 *
177 * Constructed using either #mbedtls_md_info_from_string or
178 * #mbedtls_md_info_from_type.
179 *
180 * Fields can be accessed with #mbedtls_md_get_size,
181 * #mbedtls_md_get_type and #mbedtls_md_get_name.
Paul Bakker17373852011-01-06 14:20:01 +0000182 */
Chris Jones3848e312021-03-11 16:17:59 +0000183/* Defined internally in library/md_wrap.h. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +0000185
186/**
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100187 * Used internally to indicate whether a context uses legacy or PSA.
188 *
189 * Internal use only.
190 */
191typedef enum {
192 MBEDTLS_MD_ENGINE_LEGACY = 0,
193 MBEDTLS_MD_ENGINE_PSA,
194} mbedtls_md_engine_t;
195
196/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000197 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000198 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100199typedef struct mbedtls_md_context_t {
Rose Zadik64feefb2018-01-25 22:01:10 +0000200 /** Information about the associated message digest. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200201 const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000202
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100203#if defined(MBEDTLS_MD_SOME_PSA)
204 /** Are hash operations dispatched to PSA or legacy? */
205 mbedtls_md_engine_t MBEDTLS_PRIVATE(engine);
206#endif
207
208 /** The digest-specific context (legacy) or the PSA operation. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200209 void *MBEDTLS_PRIVATE(md_ctx);
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100210
Manuel Pégourié-Gonnard39a376a2023-03-09 17:21:40 +0100211#if defined(MBEDTLS_MD_C)
Rose Zadik64feefb2018-01-25 22:01:10 +0000212 /** The HMAC part of the context. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200213 void *MBEDTLS_PRIVATE(hmac_ctx);
Manuel Pégourié-Gonnard39a376a2023-03-09 17:21:40 +0100214#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000216
Paul Bakker17373852011-01-06 14:20:01 +0000217/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000218 * \brief This function returns the message-digest information
219 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000220 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000221 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000222 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100223 * \return The message-digest information associated with \p md_type.
224 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000225 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
Paul Bakker17373852011-01-06 14:20:01 +0000227
Max Fillinger0bb38332021-12-28 16:32:00 +0100228/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000229 * \brief This function initializes a message-digest context without
230 * binding it to a particular message-digest algorithm.
231 *
232 * This function should always be called first. It prepares the
233 * context for mbedtls_md_setup() for binding it to a
234 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200235 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236void mbedtls_md_init(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200237
238/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000239 * \brief This function clears the internal structure of \p ctx and
240 * frees any embedded internal structure, but does not free
241 * \p ctx itself.
242 *
243 * If you have called mbedtls_md_setup() on \p ctx, you must
244 * call mbedtls_md_free() when you are no longer using the
245 * context.
246 * Calling this function if you have previously
247 * called mbedtls_md_init() and nothing else is optional.
248 * You must not call this function if you have not called
249 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200250 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251void mbedtls_md_free(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200252
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100253
Paul Bakker84bbeb52014-07-01 14:53:22 +0200254/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000255 * \brief This function selects the message digest algorithm to use,
256 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000257 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000258 * It should be called after mbedtls_md_init() or
259 * mbedtls_md_free(). Makes it necessary to call
260 * mbedtls_md_free() later.
261 *
262 * \param ctx The context to set up.
263 * \param md_info The information structure of the message-digest algorithm
264 * to use.
Rose Zadik8c9c7942018-03-27 11:52:58 +0100265 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
266 * or non-zero: HMAC is used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000267 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100268 * \return \c 0 on success.
269 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
270 * failure.
271 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000272 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100273MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100274int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
Paul Bakker562535d2011-01-20 16:42:01 +0000275
276/**
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100277 * \brief This function clones the state of a message-digest
Rose Zadik64feefb2018-01-25 22:01:10 +0000278 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200279 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000280 * \note You must call mbedtls_md_setup() on \c dst before calling
281 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200282 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000283 * \note The two contexts must have the same type,
284 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200285 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000286 * \warning This function clones the message-digest state, not the
287 * HMAC state.
288 *
289 * \param dst The destination context.
290 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200291 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100292 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100293 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Manuel Pégourié-Gonnard9b146392023-03-09 15:56:14 +0100294 * \return #MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE if both contexts are
295 * not using the same engine. This can be avoided by moving
296 * the call to psa_crypto_init() before the first call to
297 * mbedtls_md_setup().
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200298 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100299MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100300int mbedtls_md_clone(mbedtls_md_context_t *dst,
301 const mbedtls_md_context_t *src);
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200302
303/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000304 * \brief This function extracts the message-digest size from the
305 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000306 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000307 * \param md_info The information structure of the message-digest algorithm
308 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000309 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000310 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000311 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000313
314/**
Manuel Pégourié-Gonnard1ef26e22023-01-27 11:47:05 +0100315 * \brief This function gives the message-digest size associated to
316 * message-digest type.
317 *
318 * \param md_type The message-digest type.
319 *
320 * \return The size of the message-digest output in Bytes,
321 * or 0 if the message-digest type is not known.
322 */
323static inline unsigned char mbedtls_md_get_size_from_type(mbedtls_md_type_t md_type)
324{
325 return mbedtls_md_get_size(mbedtls_md_info_from_type(md_type));
326}
327
328/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000329 * \brief This function extracts the message-digest type from the
330 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000331 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000332 * \param md_info The information structure of the message-digest algorithm
333 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000334 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000335 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000336 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000338
Paul Bakker17373852011-01-06 14:20:01 +0000339/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000340 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000341 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000342 * You must call this function after setting up the context
343 * with mbedtls_md_setup(), and before passing data with
344 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000345 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000346 * \param ctx The generic message-digest context.
347 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100348 * \return \c 0 on success.
349 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
350 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000351 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100352MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100353int mbedtls_md_starts(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000354
355/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000356 * \brief This function feeds an input buffer into an ongoing
357 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000358 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000359 * You must call mbedtls_md_starts() before calling this
360 * function. You may call this function multiple times.
361 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000362 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000363 * \param ctx The generic message-digest context.
364 * \param input The buffer holding the input data.
365 * \param ilen The length of the input data.
366 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100367 * \return \c 0 on success.
368 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
369 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000370 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100371MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100372int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000373
374/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000375 * \brief This function finishes the digest operation,
376 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000377 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000378 * Call this function after a call to mbedtls_md_starts(),
379 * followed by any number of calls to mbedtls_md_update().
380 * Afterwards, you may either clear the context with
381 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
382 * the context for another digest operation with the same
383 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000384 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000385 * \param ctx The generic message-digest context.
386 * \param output The buffer for the generic message-digest checksum result.
387 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100388 * \return \c 0 on success.
389 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
390 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000391 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100392MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100393int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000394
395/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000396 * \brief This function calculates the message-digest of a buffer,
397 * with respect to a configurable message-digest algorithm
398 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000399 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000400 * The result is calculated as
401 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000402 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000403 * \param md_info The information structure of the message-digest algorithm
404 * to use.
405 * \param input The buffer holding the data.
406 * \param ilen The length of the input data.
407 * \param output The generic message-digest checksum result.
408 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100409 * \return \c 0 on success.
410 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
411 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000412 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100413MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100414int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
415 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000416
Manuel Pégourié-Gonnard82a43942023-02-23 09:36:29 +0100417/**
418 * \brief This function returns the list of digests supported by the
419 * generic digest module.
420 *
421 * \note The list starts with the strongest available hashes.
422 *
423 * \return A statically allocated array of digests. Each element
424 * in the returned list is an integer belonging to the
425 * message-digest enumeration #mbedtls_md_type_t.
426 * The last entry is 0.
427 */
428const int *mbedtls_md_list(void);
429
430/**
431 * \brief This function returns the message-digest information
432 * associated with the given digest name.
433 *
434 * \param md_name The name of the digest to search for.
435 *
436 * \return The message-digest information associated with \p md_name.
437 * \return NULL if the associated message-digest information is not found.
438 */
439const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
440
441/**
442 * \brief This function extracts the message-digest name from the
443 * message-digest information structure.
444 *
445 * \param md_info The information structure of the message-digest algorithm
446 * to use.
447 *
448 * \return The name of the message digest.
449 */
450const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
451
452/**
453 * \brief This function returns the message-digest information
454 * from the given context.
455 *
456 * \param ctx The context from which to extract the information.
457 * This must be initialized (or \c NULL).
458 *
459 * \return The message-digest information associated with \p ctx.
460 * \return \c NULL if \p ctx is \c NULL.
461 */
462const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
463 const mbedtls_md_context_t *ctx);
464
465#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000466/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000467 * \brief This function calculates the message-digest checksum
468 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000469 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000470 * The result is calculated as
471 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000472 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000473 * \param md_info The information structure of the message-digest algorithm
474 * to use.
475 * \param path The input file name.
476 * \param output The generic message-digest checksum result.
477 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100478 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100479 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
480 * the file pointed by \p path.
481 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000482 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100483MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100484int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
485 unsigned char *output);
Manuel Pégourié-Gonnard82a43942023-02-23 09:36:29 +0100486#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000487
488/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000489 * \brief This function sets the HMAC key and prepares to
490 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000491 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000492 * Call this function after mbedtls_md_setup(), to use
493 * the MD context for an HMAC calculation, then call
494 * mbedtls_md_hmac_update() to provide the input data, and
495 * mbedtls_md_hmac_finish() to get the HMAC value.
Paul Bakker17373852011-01-06 14:20:01 +0000496 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000497 * \param ctx The message digest context containing an embedded HMAC
498 * context.
499 * \param key The HMAC secret key.
500 * \param keylen The length of the HMAC key in Bytes.
501 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100502 * \return \c 0 on success.
503 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
504 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000505 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100506MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100507int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
508 size_t keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000509
510/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000511 * \brief This function feeds an input buffer into an ongoing HMAC
512 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000513 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000514 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
515 * before calling this function.
516 * You may call this function multiple times to pass the
517 * input piecewise.
518 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000519 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000520 * \param ctx The message digest context containing an embedded HMAC
521 * context.
522 * \param input The buffer holding the input data.
523 * \param ilen The length of the input data.
524 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100525 * \return \c 0 on success.
526 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
527 * failure.
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_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
531 size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000532
533/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000534 * \brief This function finishes the HMAC operation, and writes
535 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000536 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000537 * Call this function after mbedtls_md_hmac_starts() and
538 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
539 * you may either call mbedtls_md_free() to clear the context,
540 * or call mbedtls_md_hmac_reset() to reuse the context with
541 * the same HMAC key.
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 output The generic HMAC checksum result.
546 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100547 * \return \c 0 on success.
548 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
549 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000550 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100551MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100552int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000553
554/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000555 * \brief This function prepares to authenticate a new message with
556 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000557 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000558 * You may call this function after mbedtls_md_hmac_finish().
559 * Afterwards call mbedtls_md_hmac_update() to pass the new
560 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000561 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000562 * \param ctx The message digest context containing an embedded HMAC
563 * context.
564 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100565 * \return \c 0 on success.
566 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
567 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000568 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100569MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100570int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000571
572/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000573 * \brief This function calculates the full generic HMAC
574 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000575 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000576 * The function allocates the context, performs the
577 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000578 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000579 * The HMAC result is calculated as
580 * output = generic HMAC(hmac key, input buffer).
581 *
582 * \param md_info The information structure of the message-digest algorithm
583 * to use.
584 * \param key The HMAC secret key.
585 * \param keylen The length of the HMAC secret key in Bytes.
586 * \param input The buffer holding the input data.
587 * \param ilen The length of the input data.
588 * \param output The generic HMAC result.
589 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100590 * \return \c 0 on success.
591 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
592 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000593 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100594MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100595int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
596 const unsigned char *input, size_t ilen,
597 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000598
Paul Bakker17373852011-01-06 14:20:01 +0000599#ifdef __cplusplus
600}
601#endif
602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603#endif /* MBEDTLS_MD_H */