blob: db4d14c044e8fab335ab49d962c85fa055356385 [file] [log] [blame]
Gilles Peskine1b6c09a2023-01-11 14:52:35 +01001/**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file md.h
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Rose Zadik8c9c7942018-03-27 11:52:58 +01004 * \brief This file contains the generic message-digest wrapper.
Paul Bakker17373852011-01-06 14:20:01 +00005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
Darryl Greena40a1012018-01-05 15:33:17 +00007 */
8/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02009 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020010 * SPDX-License-Identifier: Apache-2.0
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
Paul Bakker17373852011-01-06 14:20:01 +000023 */
Rose Zadik64feefb2018-01-25 22:01:10 +000024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#ifndef MBEDTLS_MD_H
26#define MBEDTLS_MD_H
Paul Bakker17373852011-01-06 14:20:01 +000027
Rich Evans00ab4702015-02-06 13:43:58 +000028#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000029
Ron Eldorf231eaa2017-08-22 14:50:14 +030030#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010031#include "mbedtls/config.h"
Ron Eldorf231eaa2017-08-22 14:50:14 +030032#else
33#include MBEDTLS_CONFIG_FILE
34#endif
Gilles Peskine2b3f21d2021-12-10 21:35:10 +010035#include "mbedtls/platform_util.h"
Ron Eldorf231eaa2017-08-22 14:50:14 +030036
Gilles Peskinea3974432021-07-26 18:48:10 +020037/** The selected feature is not available. */
38#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080
39/** Bad input parameters to function. */
40#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100
41/** Failed to allocate memory. */
42#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180
43/** Opening or reading of file failed. */
44#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200
Ron Eldor9924bdc2018-10-04 10:59:13 +030045
46/* MBEDTLS_ERR_MD_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020047/** MD hardware accelerator failed. */
48#define MBEDTLS_ERR_MD_HW_ACCEL_FAILED -0x5280
Paul Bakker335db3f2011-04-25 15:28:35 +000049
Paul Bakker407a0da2013-06-27 14:29:21 +020050#ifdef __cplusplus
51extern "C" {
52#endif
53
Hanno Beckerbbca8c52017-09-25 14:53:51 +010054/**
Rose Zadik8c9c7942018-03-27 11:52:58 +010055 * \brief Supported message digests.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010056 *
57 * \warning MD2, MD4, MD5 and SHA-1 are considered weak message digests and
58 * their use constitutes a security risk. We recommend considering
59 * stronger message digests instead.
60 *
61 */
Paul Bakker17373852011-01-06 14:20:01 +000062typedef enum {
Rose Zadikf3e47362018-04-16 16:31:16 +010063 MBEDTLS_MD_NONE=0, /**< None. */
64 MBEDTLS_MD_MD2, /**< The MD2 message digest. */
65 MBEDTLS_MD_MD4, /**< The MD4 message digest. */
66 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
67 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
68 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
69 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
70 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
71 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
Rose Zadik8c9c7942018-03-27 11:52:58 +010072 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +000074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_SHA512_C)
76#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020077#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
Paul Bakker7db01092013-09-10 11:10:57 +020079#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000080
Hanno Becker2e24c3b2017-12-27 21:28:58 +000081#if defined(MBEDTLS_SHA512_C)
82#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
83#else
84#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
85#endif
86
Paul Bakker17373852011-01-06 14:20:01 +000087/**
Rose Zadik64feefb2018-01-25 22:01:10 +000088 * Opaque struct defined in md_internal.h.
Paul Bakker17373852011-01-06 14:20:01 +000089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +000091
92/**
Rose Zadik64feefb2018-01-25 22:01:10 +000093 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +000094 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095typedef struct mbedtls_md_context_t {
Rose Zadik64feefb2018-01-25 22:01:10 +000096 /** Information about the associated message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 const mbedtls_md_info_t *md_info;
Paul Bakker17373852011-01-06 14:20:01 +000098
Rose Zadik64feefb2018-01-25 22:01:10 +000099 /** The digest-specific context. */
Paul Bakker17373852011-01-06 14:20:01 +0000100 void *md_ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100101
Rose Zadik64feefb2018-01-25 22:01:10 +0000102 /** The HMAC part of the context. */
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100103 void *hmac_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000105
Paul Bakker17373852011-01-06 14:20:01 +0000106/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000107 * \brief This function returns the list of digests supported by the
108 * generic digest module.
Paul Bakker72f62662011-01-16 21:27:44 +0000109 *
Manuel Pégourié-Gonnardc52a43c2020-05-22 12:12:36 +0200110 * \note The list starts with the strongest available hashes.
111 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000112 * \return A statically allocated array of digests. Each element
113 * in the returned list is an integer belonging to the
114 * message-digest enumeration #mbedtls_md_type_t.
115 * The last entry is 0.
Paul Bakker72f62662011-01-16 21:27:44 +0000116 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117const int *mbedtls_md_list(void);
Paul Bakker72f62662011-01-16 21:27:44 +0000118
119/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000120 * \brief This function returns the message-digest information
121 * associated with the given digest name.
Paul Bakker17373852011-01-06 14:20:01 +0000122 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000123 * \param md_name The name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000124 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100125 * \return The message-digest information associated with \p md_name.
126 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000127 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128const mbedtls_md_info_t *mbedtls_md_info_from_string(const char *md_name);
Paul Bakker17373852011-01-06 14:20:01 +0000129
130/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000131 * \brief This function returns the message-digest information
132 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000133 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000134 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000135 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100136 * \return The message-digest information associated with \p md_type.
137 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000138 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139const mbedtls_md_info_t *mbedtls_md_info_from_type(mbedtls_md_type_t md_type);
Paul Bakker17373852011-01-06 14:20:01 +0000140
141/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000142 * \brief This function initializes a message-digest context without
143 * binding it to a particular message-digest algorithm.
144 *
145 * This function should always be called first. It prepares the
146 * context for mbedtls_md_setup() for binding it to a
147 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200148 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149void mbedtls_md_init(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200150
151/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000152 * \brief This function clears the internal structure of \p ctx and
153 * frees any embedded internal structure, but does not free
154 * \p ctx itself.
155 *
156 * If you have called mbedtls_md_setup() on \p ctx, you must
157 * call mbedtls_md_free() when you are no longer using the
158 * context.
159 * Calling this function if you have previously
160 * called mbedtls_md_init() and nothing else is optional.
161 * You must not call this function if you have not called
162 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200163 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164void mbedtls_md_free(mbedtls_md_context_t *ctx);
Paul Bakker84bbeb52014-07-01 14:53:22 +0200165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200167#if defined(MBEDTLS_DEPRECATED_WARNING)
168#define MBEDTLS_DEPRECATED __attribute__((deprecated))
169#else
170#define MBEDTLS_DEPRECATED
171#endif
172/**
173 * \brief This function selects the message digest algorithm to use,
174 * and allocates internal structures.
175 *
176 * It should be called after mbedtls_md_init() or mbedtls_md_free().
177 * Makes it necessary to call mbedtls_md_free() later.
178 *
179 * \deprecated Superseded by mbedtls_md_setup() in 2.0.0
180 *
181 * \param ctx The context to set up.
182 * \param md_info The information structure of the message-digest algorithm
183 * to use.
184 *
185 * \return \c 0 on success.
186 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
187 * failure.
188 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
189 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190int mbedtls_md_init_ctx(mbedtls_md_context_t *ctx,
191 const mbedtls_md_info_t *md_info) MBEDTLS_DEPRECATED;
Manuel Pégourié-Gonnard93c08472021-04-15 12:23:55 +0200192#undef MBEDTLS_DEPRECATED
193#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100194
Paul Bakker84bbeb52014-07-01 14:53:22 +0200195/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000196 * \brief This function selects the message digest algorithm to use,
197 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000198 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000199 * It should be called after mbedtls_md_init() or
200 * mbedtls_md_free(). Makes it necessary to call
201 * mbedtls_md_free() later.
202 *
203 * \param ctx The context to set up.
204 * \param md_info The information structure of the message-digest algorithm
205 * to use.
Rose Zadik8c9c7942018-03-27 11:52:58 +0100206 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
207 * or non-zero: HMAC is used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000208 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100209 * \return \c 0 on success.
210 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
211 * failure.
212 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000213 */
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100214MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215int mbedtls_md_setup(mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac);
Paul Bakker562535d2011-01-20 16:42:01 +0000216
217/**
Tom Cosgrove5205c972022-07-28 06:12:08 +0100218 * \brief This function clones the state of a message-digest
Rose Zadik64feefb2018-01-25 22:01:10 +0000219 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200220 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000221 * \note You must call mbedtls_md_setup() on \c dst before calling
222 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200223 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000224 * \note The two contexts must have the same type,
225 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200226 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000227 * \warning This function clones the message-digest state, not the
228 * HMAC state.
229 *
230 * \param dst The destination context.
231 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200232 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100233 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100234 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200235 */
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100236MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237int mbedtls_md_clone(mbedtls_md_context_t *dst,
238 const mbedtls_md_context_t *src);
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200239
240/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000241 * \brief This function extracts the message-digest size from the
242 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000243 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000244 * \param md_info The information structure of the message-digest algorithm
245 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000246 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000247 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000248 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249unsigned char mbedtls_md_get_size(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000250
251/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000252 * \brief This function extracts the message-digest type from the
253 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000254 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000255 * \param md_info The information structure of the message-digest algorithm
256 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000257 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000258 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000259 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260mbedtls_md_type_t mbedtls_md_get_type(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000261
262/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000263 * \brief This function extracts the message-digest name from the
264 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000265 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000266 * \param md_info The information structure of the message-digest algorithm
267 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000268 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000269 * \return The name of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000270 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271const char *mbedtls_md_get_name(const mbedtls_md_info_t *md_info);
Paul Bakker17373852011-01-06 14:20:01 +0000272
273/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000274 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000275 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000276 * You must call this function after setting up the context
277 * with mbedtls_md_setup(), and before passing data with
278 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000279 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000280 * \param ctx The generic message-digest context.
281 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100282 * \return \c 0 on success.
283 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
284 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000285 */
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100286MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100287int mbedtls_md_starts(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000288
289/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000290 * \brief This function feeds an input buffer into an ongoing
291 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000292 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000293 * You must call mbedtls_md_starts() before calling this
294 * function. You may call this function multiple times.
295 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000296 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000297 * \param ctx The generic message-digest context.
298 * \param input The buffer holding the input data.
299 * \param ilen The length of the input data.
300 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100301 * \return \c 0 on success.
302 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
303 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000304 */
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100305MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306int mbedtls_md_update(mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000307
308/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000309 * \brief This function finishes the digest operation,
310 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000311 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000312 * Call this function after a call to mbedtls_md_starts(),
313 * followed by any number of calls to mbedtls_md_update().
314 * Afterwards, you may either clear the context with
315 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
316 * the context for another digest operation with the same
317 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000318 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000319 * \param ctx The generic message-digest context.
320 * \param output The buffer for the generic message-digest checksum result.
321 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100322 * \return \c 0 on success.
323 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
324 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000325 */
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100326MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327int mbedtls_md_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000328
329/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000330 * \brief This function calculates the message-digest of a buffer,
331 * with respect to a configurable message-digest algorithm
332 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000333 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000334 * The result is calculated as
335 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000336 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000337 * \param md_info The information structure of the message-digest algorithm
338 * to use.
339 * \param input The buffer holding the data.
340 * \param ilen The length of the input data.
341 * \param output The generic message-digest checksum result.
342 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100343 * \return \c 0 on success.
344 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
345 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000346 */
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100347MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348int mbedtls_md(const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
349 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000350
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200351#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000352/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000353 * \brief This function calculates the message-digest checksum
354 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000355 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000356 * The result is calculated as
357 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000358 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000359 * \param md_info The information structure of the message-digest algorithm
360 * to use.
361 * \param path The input file name.
362 * \param output The generic message-digest checksum result.
363 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100364 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100365 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
366 * the file pointed by \p path.
367 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000368 */
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100369MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370int mbedtls_md_file(const mbedtls_md_info_t *md_info, const char *path,
371 unsigned char *output);
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200372#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000373
374/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000375 * \brief This function sets the HMAC key and prepares to
376 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000377 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000378 * Call this function after mbedtls_md_setup(), to use
379 * the MD context for an HMAC calculation, then call
380 * mbedtls_md_hmac_update() to provide the input data, and
381 * mbedtls_md_hmac_finish() to get the HMAC value.
Paul Bakker17373852011-01-06 14:20:01 +0000382 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000383 * \param ctx The message digest context containing an embedded HMAC
384 * context.
385 * \param key The HMAC secret key.
386 * \param keylen The length of the HMAC key in Bytes.
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 Peskine2b3f21d2021-12-10 21:35:10 +0100392MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393int mbedtls_md_hmac_starts(mbedtls_md_context_t *ctx, const unsigned char *key,
394 size_t keylen);
Paul Bakker17373852011-01-06 14:20:01 +0000395
396/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000397 * \brief This function feeds an input buffer into an ongoing HMAC
398 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000399 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000400 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
401 * before calling this function.
402 * You may call this function multiple times to pass the
403 * input piecewise.
404 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000405 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000406 * \param ctx The message digest context containing an embedded HMAC
407 * context.
408 * \param input The buffer holding the input data.
409 * \param ilen The length of the input data.
410 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100411 * \return \c 0 on success.
412 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
413 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000414 */
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100415MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416int mbedtls_md_hmac_update(mbedtls_md_context_t *ctx, const unsigned char *input,
417 size_t ilen);
Paul Bakker17373852011-01-06 14:20:01 +0000418
419/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000420 * \brief This function finishes the HMAC operation, and writes
421 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000422 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000423 * Call this function after mbedtls_md_hmac_starts() and
424 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
425 * you may either call mbedtls_md_free() to clear the context,
426 * or call mbedtls_md_hmac_reset() to reuse the context with
427 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000428 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000429 * \param ctx The message digest context containing an embedded HMAC
430 * context.
431 * \param output The generic HMAC checksum result.
432 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100433 * \return \c 0 on success.
434 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
435 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000436 */
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100437MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438int mbedtls_md_hmac_finish(mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000439
440/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000441 * \brief This function prepares to authenticate a new message with
442 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000443 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000444 * You may call this function after mbedtls_md_hmac_finish().
445 * Afterwards call mbedtls_md_hmac_update() to pass the new
446 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000447 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000448 * \param ctx The message digest context containing an embedded HMAC
449 * context.
450 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100451 * \return \c 0 on success.
452 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
453 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000454 */
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100455MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100456int mbedtls_md_hmac_reset(mbedtls_md_context_t *ctx);
Paul Bakker17373852011-01-06 14:20:01 +0000457
458/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000459 * \brief This function calculates the full generic HMAC
460 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000461 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000462 * The function allocates the context, performs the
463 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000464 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000465 * The HMAC result is calculated as
466 * output = generic HMAC(hmac key, input buffer).
467 *
468 * \param md_info The information structure of the message-digest algorithm
469 * to use.
470 * \param key The HMAC secret key.
471 * \param keylen The length of the HMAC secret key in Bytes.
472 * \param input The buffer holding the input data.
473 * \param ilen The length of the input data.
474 * \param output The generic HMAC result.
475 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100476 * \return \c 0 on success.
477 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
478 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000479 */
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100480MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100481int mbedtls_md_hmac(const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
482 const unsigned char *input, size_t ilen,
483 unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000484
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100485/* Internal use */
Gilles Peskine2b3f21d2021-12-10 21:35:10 +0100486MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487int mbedtls_md_process(mbedtls_md_context_t *ctx, const unsigned char *data);
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100488
Paul Bakker17373852011-01-06 14:20:01 +0000489#ifdef __cplusplus
490}
491#endif
492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#endif /* MBEDTLS_MD_H */