blob: 5ab09672720059fdc3c44c20976ac08147adceb9 [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.
39 * - MBEDTLS_MD_xxx_VIA_PSA is defined if the md module performs xxx via PSA.
40 * - MBEDTLS_MD_SOME_PSA is defined if at least one algorithm is performed
41 * via PSA.
42 * - MBEDTLS_MD_SOME_LEGACY is defined if at least one algorithm is performed
43 * via a direct legacy call.
44 *
45 * The md module performs an algorithm via PSA if there is a PSA hash
46 * accelerator, and makes a direct legacy call otherwise.
47 */
48
49/* PSA accelerated implementations */
50#if defined(MBEDTLS_PSA_CRYPTO_C)
51#if defined(MBEDTLS_PSA_ACCEL_ALG_MD5)
52#define MBEDTLS_MD_CAN_MD5
53#define MBEDTLS_MD_MD5_VIA_PSA
54#define MBEDTLS_MD_SOME_PSA
55#endif
56#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_1)
57#define MBEDTLS_MD_CAN_SHA1
58#define MBEDTLS_MD_SHA1_VIA_PSA
59#define MBEDTLS_MD_SOME_PSA
60#endif
61#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_224)
62#define MBEDTLS_MD_CAN_SHA224
63#define MBEDTLS_MD_SHA224_VIA_PSA
64#define MBEDTLS_MD_SOME_PSA
65#endif
66#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_256)
67#define MBEDTLS_MD_CAN_SHA256
68#define MBEDTLS_MD_SHA256_VIA_PSA
69#define MBEDTLS_MD_SOME_PSA
70#endif
71#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_384)
72#define MBEDTLS_MD_CAN_SHA384
73#define MBEDTLS_MD_SHA384_VIA_PSA
74#define MBEDTLS_MD_SOME_PSA
75#endif
76#if defined(MBEDTLS_PSA_ACCEL_ALG_SHA_512)
77#define MBEDTLS_MD_CAN_SHA512
78#define MBEDTLS_MD_SHA512_VIA_PSA
79#define MBEDTLS_MD_SOME_PSA
80#endif
81#if defined(MBEDTLS_PSA_ACCEL_ALG_RIPEMD160)
82#define MBEDTLS_MD_CAN_RIPEMD160
83#define MBEDTLS_MD_RIPEMD160_VIA_PSA
84#define MBEDTLS_MD_SOME_PSA
85#endif
86#endif /* MBEDTLS_PSA_CRYPTO_C */
87
88/* Built-in implementations */
89#if defined(MBEDTLS_MD5_C)
90#define MBEDTLS_MD_CAN_MD5
91#define MBEDTLS_MD_SOME_LEGACY
92#endif
93#if defined(MBEDTLS_SHA1_C)
94#define MBEDTLS_MD_CAN_SHA1
95#define MBEDTLS_MD_SOME_LEGACY
96#endif
97#if defined(MBEDTLS_SHA224_C)
98#define MBEDTLS_MD_CAN_SHA224
99#define MBEDTLS_MD_SOME_LEGACY
100#endif
101#if defined(MBEDTLS_SHA256_C)
102#define MBEDTLS_MD_CAN_SHA256
103#define MBEDTLS_MD_SOME_LEGACY
104#endif
105#if defined(MBEDTLS_SHA384_C)
106#define MBEDTLS_MD_CAN_SHA384
107#define MBEDTLS_MD_SOME_LEGACY
108#endif
109#if defined(MBEDTLS_SHA512_C)
110#define MBEDTLS_MD_CAN_SHA512
111#define MBEDTLS_MD_SOME_LEGACY
112#endif
113#if defined(MBEDTLS_RIPEMD160_C)
114#define MBEDTLS_MD_CAN_RIPEMD160
115#define MBEDTLS_MD_SOME_LEGACY
116#endif
117
118#endif /* MBEDTLS_MD_LIGHT */
119
Gilles Peskined2971572021-07-26 18:48:10 +0200120/** The selected feature is not available. */
121#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080
122/** Bad input parameters to function. */
123#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100
124/** Failed to allocate memory. */
125#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180
126/** Opening or reading of file failed. */
127#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200
Ron Eldor9924bdc2018-10-04 10:59:13 +0300128
Paul Bakker407a0da2013-06-27 14:29:21 +0200129#ifdef __cplusplus
130extern "C" {
131#endif
132
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100133/**
Rose Zadik8c9c7942018-03-27 11:52:58 +0100134 * \brief Supported message digests.
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100135 *
TRodziewicz10e8cf52021-05-31 17:58:57 +0200136 * \warning MD5 and SHA-1 are considered weak message digests and
Hanno Beckerbbca8c52017-09-25 14:53:51 +0100137 * their use constitutes a security risk. We recommend considering
138 * stronger message digests instead.
139 *
140 */
Paul Bakker17373852011-01-06 14:20:01 +0000141typedef enum {
Rose Zadikf3e47362018-04-16 16:31:16 +0100142 MBEDTLS_MD_NONE=0, /**< None. */
Rose Zadikf3e47362018-04-16 16:31:16 +0100143 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
144 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
145 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
146 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
147 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
148 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
Rose Zadik8c9c7942018-03-27 11:52:58 +0100149 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +0000151
Gilles Peskine83d9e092022-10-22 18:32:43 +0200152#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200154#elif defined(MBEDTLS_MD_CAN_SHA384)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100155#define MBEDTLS_MD_MAX_SIZE 48 /* longest known is SHA384 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200156#elif defined(MBEDTLS_MD_CAN_SHA256)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100157#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 */
Gilles Peskine83d9e092022-10-22 18:32:43 +0200158#elif defined(MBEDTLS_MD_CAN_SHA224)
Valerio Settid55cb5b2022-12-22 14:26:55 +0100159#define MBEDTLS_MD_MAX_SIZE 28 /* longest known is SHA224 */
Paul Bakker7db01092013-09-10 11:10:57 +0200160#else
Gilles Peskine83d9e092022-10-22 18:32:43 +0200161#define MBEDTLS_MD_MAX_SIZE 20 /* longest known is SHA1 or RIPE MD-160
162 or smaller (MD5 and earlier) */
Paul Bakker7db01092013-09-10 11:10:57 +0200163#endif
Paul Bakker1b57b062011-01-06 15:48:19 +0000164
Gilles Peskine83d9e092022-10-22 18:32:43 +0200165#if defined(MBEDTLS_MD_CAN_SHA512)
Hanno Becker2e24c3b2017-12-27 21:28:58 +0000166#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
167#else
168#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
169#endif
170
Paul Bakker17373852011-01-06 14:20:01 +0000171/**
Chris Jones3848e312021-03-11 16:17:59 +0000172 * Opaque struct.
173 *
174 * Constructed using either #mbedtls_md_info_from_string or
175 * #mbedtls_md_info_from_type.
176 *
177 * Fields can be accessed with #mbedtls_md_get_size,
178 * #mbedtls_md_get_type and #mbedtls_md_get_name.
Paul Bakker17373852011-01-06 14:20:01 +0000179 */
Chris Jones3848e312021-03-11 16:17:59 +0000180/* Defined internally in library/md_wrap.h. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +0000182
183/**
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100184 * Used internally to indicate whether a context uses legacy or PSA.
185 *
186 * Internal use only.
187 */
188typedef enum {
189 MBEDTLS_MD_ENGINE_LEGACY = 0,
190 MBEDTLS_MD_ENGINE_PSA,
191} mbedtls_md_engine_t;
192
193/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000194 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000195 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196typedef struct mbedtls_md_context_t {
Rose Zadik64feefb2018-01-25 22:01:10 +0000197 /** Information about the associated message digest. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200198 const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000199
Manuel Pégourié-Gonnardd8ea37f2023-03-09 10:46:22 +0100200#if defined(MBEDTLS_MD_SOME_PSA)
201 /** Are hash operations dispatched to PSA or legacy? */
202 mbedtls_md_engine_t MBEDTLS_PRIVATE(engine);
203#endif
204
205 /** The digest-specific context (legacy) or the PSA operation. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200206 void *MBEDTLS_PRIVATE(md_ctx);
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100207
Rose Zadik64feefb2018-01-25 22:01:10 +0000208 /** The HMAC part of the context. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200209 void *MBEDTLS_PRIVATE(hmac_ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000211
Paul Bakker17373852011-01-06 14:20:01 +0000212/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000213 * \brief This function returns the message-digest information
214 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000215 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000216 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000217 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100218 * \return The message-digest information associated with \p md_type.
219 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000220 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
Paul Bakker17373852011-01-06 14:20:01 +0000222
Max Fillinger0bb38332021-12-28 16:32:00 +0100223/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000224 * \brief This function initializes a message-digest context without
225 * binding it to a particular message-digest algorithm.
226 *
227 * This function should always be called first. It prepares the
228 * context for mbedtls_md_setup() for binding it to a
229 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200230 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231void mbedtls_md_init(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200232
233/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000234 * \brief This function clears the internal structure of \p ctx and
235 * frees any embedded internal structure, but does not free
236 * \p ctx itself.
237 *
238 * If you have called mbedtls_md_setup() on \p ctx, you must
239 * call mbedtls_md_free() when you are no longer using the
240 * context.
241 * Calling this function if you have previously
242 * called mbedtls_md_init() and nothing else is optional.
243 * You must not call this function if you have not called
244 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200245 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246void mbedtls_md_free(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200247
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100248
Paul Bakker84bbeb52014-07-01 14:53:22 +0200249/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000250 * \brief This function selects the message digest algorithm to use,
251 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000252 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000253 * It should be called after mbedtls_md_init() or
254 * mbedtls_md_free(). Makes it necessary to call
255 * mbedtls_md_free() later.
256 *
257 * \param ctx The context to set up.
258 * \param md_info The information structure of the message-digest algorithm
259 * to use.
Rose Zadik8c9c7942018-03-27 11:52:58 +0100260 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
261 * or non-zero: HMAC is used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000262 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100263 * \return \c 0 on success.
264 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
265 * failure.
266 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000267 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100268MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100269int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
Paul Bakker562535d2011-01-20 16:42:01 +0000270
271/**
Tom Cosgrovece7f18c2022-07-28 05:50:56 +0100272 * \brief This function clones the state of a message-digest
Rose Zadik64feefb2018-01-25 22:01:10 +0000273 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200274 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000275 * \note You must call mbedtls_md_setup() on \c dst before calling
276 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200277 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000278 * \note The two contexts must have the same type,
279 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200280 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000281 * \warning This function clones the message-digest state, not the
282 * HMAC state.
283 *
284 * \param dst The destination context.
285 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200286 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100287 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100288 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Manuel Pégourié-Gonnard9b146392023-03-09 15:56:14 +0100289 * \return #MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE if both contexts are
290 * not using the same engine. This can be avoided by moving
291 * the call to psa_crypto_init() before the first call to
292 * mbedtls_md_setup().
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200293 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100294MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100295int mbedtls_md_clone(mbedtls_md_context_t *dst,
296 const mbedtls_md_context_t *src);
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200297
298/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000299 * \brief This function extracts the message-digest size from the
300 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000301 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000302 * \param md_info The information structure of the message-digest algorithm
303 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000304 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000305 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000306 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000308
309/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000310 * \brief This function extracts the message-digest type from the
311 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000312 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000313 * \param md_info The information structure of the message-digest algorithm
314 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000315 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000316 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000317 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000319
Paul Bakker17373852011-01-06 14:20:01 +0000320/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000321 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000322 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000323 * You must call this function after setting up the context
324 * with mbedtls_md_setup(), and before passing data with
325 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000326 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000327 * \param ctx The generic message-digest context.
328 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100329 * \return \c 0 on success.
330 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
331 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000332 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100333MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100334int mbedtls_md_starts(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000335
336/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000337 * \brief This function feeds an input buffer into an ongoing
338 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000339 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000340 * You must call mbedtls_md_starts() before calling this
341 * function. You may call this function multiple times.
342 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000343 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000344 * \param ctx The generic message-digest context.
345 * \param input The buffer holding the input data.
346 * \param ilen The length of the input data.
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_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000354
355/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000356 * \brief This function finishes the digest operation,
357 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000358 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000359 * Call this function after a call to mbedtls_md_starts(),
360 * followed by any number of calls to mbedtls_md_update().
361 * Afterwards, you may either clear the context with
362 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
363 * the context for another digest operation with the same
364 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000365 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000366 * \param ctx The generic message-digest context.
367 * \param output The buffer for the generic message-digest checksum result.
368 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100369 * \return \c 0 on success.
370 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
371 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000372 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100373MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100374int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000375
376/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000377 * \brief This function calculates the message-digest of a buffer,
378 * with respect to a configurable message-digest algorithm
379 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000380 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000381 * The result is calculated as
382 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000383 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000384 * \param md_info The information structure of the message-digest algorithm
385 * to use.
386 * \param input The buffer holding the data.
387 * \param ilen The length of the input data.
388 * \param output The generic message-digest checksum result.
389 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100390 * \return \c 0 on success.
391 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
392 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000393 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100394MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100395int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
396 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000397
Manuel Pégourié-Gonnard82a43942023-02-23 09:36:29 +0100398/**
399 * \brief This function returns the list of digests supported by the
400 * generic digest module.
401 *
402 * \note The list starts with the strongest available hashes.
403 *
404 * \return A statically allocated array of digests. Each element
405 * in the returned list is an integer belonging to the
406 * message-digest enumeration #mbedtls_md_type_t.
407 * The last entry is 0.
408 */
409const int *mbedtls_md_list(void);
410
411/**
412 * \brief This function returns the message-digest information
413 * associated with the given digest name.
414 *
415 * \param md_name The name of the digest to search for.
416 *
417 * \return The message-digest information associated with \p md_name.
418 * \return NULL if the associated message-digest information is not found.
419 */
420const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
421
422/**
423 * \brief This function extracts the message-digest name from the
424 * message-digest information structure.
425 *
426 * \param md_info The information structure of the message-digest algorithm
427 * to use.
428 *
429 * \return The name of the message digest.
430 */
431const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
432
433/**
434 * \brief This function returns the message-digest information
435 * from the given context.
436 *
437 * \param ctx The context from which to extract the information.
438 * This must be initialized (or \c NULL).
439 *
440 * \return The message-digest information associated with \p ctx.
441 * \return \c NULL if \p ctx is \c NULL.
442 */
443const mbedtls_md_info_t *mbedtls_md_info_from_ctx(
444 const mbedtls_md_context_t *ctx);
445
446#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000447/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000448 * \brief This function calculates the message-digest checksum
449 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000450 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000451 * The result is calculated as
452 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000453 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000454 * \param md_info The information structure of the message-digest algorithm
455 * to use.
456 * \param path The input file name.
457 * \param output The generic message-digest checksum result.
458 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100459 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100460 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
461 * the file pointed by \p path.
462 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000463 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100464MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100465int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
466 unsigned char *output);
Manuel Pégourié-Gonnard82a43942023-02-23 09:36:29 +0100467#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000468
469/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000470 * \brief This function sets the HMAC key and prepares to
471 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000472 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000473 * Call this function after mbedtls_md_setup(), to use
474 * the MD context for an HMAC calculation, then call
475 * mbedtls_md_hmac_update() to provide the input data, and
476 * mbedtls_md_hmac_finish() to get the HMAC value.
Paul Bakker17373852011-01-06 14:20:01 +0000477 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000478 * \param ctx The message digest context containing an embedded HMAC
479 * context.
480 * \param key The HMAC secret key.
481 * \param keylen The length of the HMAC key in Bytes.
482 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100483 * \return \c 0 on success.
484 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
485 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000486 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100487MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100488int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
489 size_t keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000490
491/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000492 * \brief This function feeds an input buffer into an ongoing HMAC
493 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000494 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000495 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
496 * before calling this function.
497 * You may call this function multiple times to pass the
498 * input piecewise.
499 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000500 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000501 * \param ctx The message digest context containing an embedded HMAC
502 * context.
503 * \param input The buffer holding the input data.
504 * \param ilen The length of the input data.
505 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100506 * \return \c 0 on success.
507 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
508 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000509 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100510MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100511int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
512 size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000513
514/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000515 * \brief This function finishes the HMAC operation, and writes
516 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000517 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000518 * Call this function after mbedtls_md_hmac_starts() and
519 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
520 * you may either call mbedtls_md_free() to clear the context,
521 * or call mbedtls_md_hmac_reset() to reuse the context with
522 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000523 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000524 * \param ctx The message digest context containing an embedded HMAC
525 * context.
526 * \param output The generic HMAC checksum result.
527 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100528 * \return \c 0 on success.
529 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
530 * failure.
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_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000534
535/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000536 * \brief This function prepares to authenticate a new message with
537 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000538 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000539 * You may call this function after mbedtls_md_hmac_finish().
540 * Afterwards call mbedtls_md_hmac_update() to pass the new
541 * input.
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 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100546 * \return \c 0 on success.
547 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
548 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000549 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100550MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100551int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000552
553/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000554 * \brief This function calculates the full generic HMAC
555 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000556 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000557 * The function allocates the context, performs the
558 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000559 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000560 * The HMAC result is calculated as
561 * output = generic HMAC(hmac key, input buffer).
562 *
563 * \param md_info The information structure of the message-digest algorithm
564 * to use.
565 * \param key The HMAC secret key.
566 * \param keylen The length of the HMAC secret key in Bytes.
567 * \param input The buffer holding the input data.
568 * \param ilen The length of the input data.
569 * \param output The generic HMAC result.
570 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100571 * \return \c 0 on success.
572 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
573 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000574 */
Gilles Peskineecf6beb2021-12-10 21:35:10 +0100575MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100576int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
577 const unsigned char *input, size_t ilen,
578 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000579
Paul Bakker17373852011-01-06 14:20:01 +0000580#ifdef __cplusplus
581}
582#endif
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#endif /* MBEDTLS_MD_H */