blob: 1170bc1ad5f558ee891f820f379f20df9111fe54 [file] [log] [blame]
Fabio Utzig3ac36ea2018-12-27 12:35:39 -02001 /**
Fabio Utzigba05f2a2017-12-05 11:00:41 -02002 * \file md.h
3 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -02004 * \brief This file contains the generic message-digest wrapper.
Fabio Utzigba05f2a2017-12-05 11:00:41 -02005 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
Fabio Utzig3ac36ea2018-12-27 12:35:39 -02007 */
8/*
Roman Okhrimenko977b3752022-03-31 14:40:48 +03009 * Copyright The Mbed TLS Contributors
Fabio Utzigba05f2a2017-12-05 11:00:41 -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.
Fabio Utzigba05f2a2017-12-05 11:00:41 -020023 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020024
Fabio Utzigba05f2a2017-12-05 11:00:41 -020025#ifndef MBEDTLS_MD_H
26#define MBEDTLS_MD_H
Roman Okhrimenko977b3752022-03-31 14:40:48 +030027#include "mbedtls/private_access.h"
Fabio Utzigba05f2a2017-12-05 11:00:41 -020028
29#include <stddef.h>
30
Roman Okhrimenko977b3752022-03-31 14:40:48 +030031#include "mbedtls/build_info.h"
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020032
Fabio Utzigba05f2a2017-12-05 11:00:41 -020033#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
34#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
35#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
36#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020042/**
43 * \brief Supported message digests.
44 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +030045 * \warning MD5 and SHA-1 are considered weak message digests and
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020046 * their use constitutes a security risk. We recommend considering
47 * stronger message digests instead.
48 *
49 */
Fabio Utzigba05f2a2017-12-05 11:00:41 -020050typedef enum {
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020051 MBEDTLS_MD_NONE=0, /**< None. */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020052 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
53 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
54 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
55 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
56 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
57 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
58 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
Fabio Utzigba05f2a2017-12-05 11:00:41 -020059} mbedtls_md_type_t;
60
61#if defined(MBEDTLS_SHA512_C)
62#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
63#else
64#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
65#endif
66
Roman Okhrimenko977b3752022-03-31 14:40:48 +030067#if defined(MBEDTLS_SHA512_C)
68#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
69#else
70#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
71#endif
72
Fabio Utzigba05f2a2017-12-05 11:00:41 -020073/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +030074 * Opaque struct.
75 *
76 * Constructed using either #mbedtls_md_info_from_string or
77 * #mbedtls_md_info_from_type.
78 *
79 * Fields can be accessed with #mbedtls_md_get_size,
80 * #mbedtls_md_get_type and #mbedtls_md_get_name.
Fabio Utzigba05f2a2017-12-05 11:00:41 -020081 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +030082/* Defined internally in library/md_wrap.h. */
Fabio Utzigba05f2a2017-12-05 11:00:41 -020083typedef struct mbedtls_md_info_t mbedtls_md_info_t;
84
85/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020086 * The generic message-digest context.
Fabio Utzigba05f2a2017-12-05 11:00:41 -020087 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020088typedef struct mbedtls_md_context_t
89{
90 /** Information about the associated message digest. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +030091 const mbedtls_md_info_t *MBEDTLS_PRIVATE(md_info);
Fabio Utzigba05f2a2017-12-05 11:00:41 -020092
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020093 /** The digest-specific context. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +030094 void *MBEDTLS_PRIVATE(md_ctx);
Fabio Utzigba05f2a2017-12-05 11:00:41 -020095
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020096 /** The HMAC part of the context. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +030097 void *MBEDTLS_PRIVATE(hmac_ctx);
Fabio Utzigba05f2a2017-12-05 11:00:41 -020098} mbedtls_md_context_t;
99
100/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200101 * \brief This function returns the list of digests supported by the
102 * generic digest module.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200103 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300104 * \note The list starts with the strongest available hashes.
105 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200106 * \return A statically allocated array of digests. Each element
107 * in the returned list is an integer belonging to the
108 * message-digest enumeration #mbedtls_md_type_t.
109 * The last entry is 0.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200110 */
111const int *mbedtls_md_list( void );
112
113/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200114 * \brief This function returns the message-digest information
115 * associated with the given digest name.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200116 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200117 * \param md_name The name of the digest to search for.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200118 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200119 * \return The message-digest information associated with \p md_name.
120 * \return NULL if the associated message-digest information is not found.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200121 */
122const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
123
124/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200125 * \brief This function returns the message-digest information
126 * associated with the given digest type.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200127 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200128 * \param md_type The type of digest to search for.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200129 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200130 * \return The message-digest information associated with \p md_type.
131 * \return NULL if the associated message-digest information is not found.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200132 */
133const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
134
135/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200136 * \brief This function initializes a message-digest context without
137 * binding it to a particular message-digest algorithm.
138 *
139 * This function should always be called first. It prepares the
140 * context for mbedtls_md_setup() for binding it to a
141 * message-digest algorithm.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200142 */
143void mbedtls_md_init( mbedtls_md_context_t *ctx );
144
145/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200146 * \brief This function clears the internal structure of \p ctx and
147 * frees any embedded internal structure, but does not free
148 * \p ctx itself.
149 *
150 * If you have called mbedtls_md_setup() on \p ctx, you must
151 * call mbedtls_md_free() when you are no longer using the
152 * context.
153 * Calling this function if you have previously
154 * called mbedtls_md_init() and nothing else is optional.
155 * You must not call this function if you have not called
156 * mbedtls_md_init().
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200157 */
158void mbedtls_md_free( mbedtls_md_context_t *ctx );
159
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200160
161/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200162 * \brief This function selects the message digest algorithm to use,
163 * and allocates internal structures.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200164 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200165 * It should be called after mbedtls_md_init() or
166 * mbedtls_md_free(). Makes it necessary to call
167 * mbedtls_md_free() later.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200168 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200169 * \param ctx The context to set up.
170 * \param md_info The information structure of the message-digest algorithm
171 * to use.
172 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
173 * or non-zero: HMAC is used with this context.
174 *
175 * \return \c 0 on success.
176 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
177 * failure.
178 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200179 */
180int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
181
182/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200183 * \brief This function clones the state of an message-digest
184 * context.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200185 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200186 * \note You must call mbedtls_md_setup() on \c dst before calling
187 * this function.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200188 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200189 * \note The two contexts must have the same type,
190 * for example, both are SHA-256.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200191 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200192 * \warning This function clones the message-digest state, not the
193 * HMAC state.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200194 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200195 * \param dst The destination context.
196 * \param src The context to be cloned.
197 *
198 * \return \c 0 on success.
199 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200200 */
201int mbedtls_md_clone( mbedtls_md_context_t *dst,
202 const mbedtls_md_context_t *src );
203
204/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200205 * \brief This function extracts the message-digest size from the
206 * message-digest information structure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200207 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200208 * \param md_info The information structure of the message-digest algorithm
209 * to use.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200210 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200211 * \return The size of the message-digest output in Bytes.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200212 */
213unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
214
215/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200216 * \brief This function extracts the message-digest type from the
217 * message-digest information structure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200218 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200219 * \param md_info The information structure of the message-digest algorithm
220 * to use.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200221 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200222 * \return The type of the message digest.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200223 */
224mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
225
226/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200227 * \brief This function extracts the message-digest name from the
228 * message-digest information structure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200229 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200230 * \param md_info The information structure of the message-digest algorithm
231 * to use.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200232 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200233 * \return The name of the message digest.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200234 */
235const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
236
237/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200238 * \brief This function starts a message-digest computation.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200239 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200240 * You must call this function after setting up the context
241 * with mbedtls_md_setup(), and before passing data with
242 * mbedtls_md_update().
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200243 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200244 * \param ctx The generic message-digest context.
245 *
246 * \return \c 0 on success.
247 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
248 * failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200249 */
250int mbedtls_md_starts( mbedtls_md_context_t *ctx );
251
252/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200253 * \brief This function feeds an input buffer into an ongoing
254 * message-digest computation.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200255 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200256 * You must call mbedtls_md_starts() before calling this
257 * function. You may call this function multiple times.
258 * Afterwards, call mbedtls_md_finish().
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200259 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200260 * \param ctx The generic message-digest context.
261 * \param input The buffer holding the input data.
262 * \param ilen The length of the input data.
263 *
264 * \return \c 0 on success.
265 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
266 * failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200267 */
268int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
269
270/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200271 * \brief This function finishes the digest operation,
272 * and writes the result to the output buffer.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200273 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200274 * Call this function after a call to mbedtls_md_starts(),
275 * followed by any number of calls to mbedtls_md_update().
276 * Afterwards, you may either clear the context with
277 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
278 * the context for another digest operation with the same
279 * algorithm.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200280 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200281 * \param ctx The generic message-digest context.
282 * \param output The buffer for the generic message-digest checksum result.
283 *
284 * \return \c 0 on success.
285 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
286 * failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200287 */
288int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
289
290/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200291 * \brief This function calculates the message-digest of a buffer,
292 * with respect to a configurable message-digest algorithm
293 * in a single call.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200294 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200295 * The result is calculated as
296 * Output = message_digest(input buffer).
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200297 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200298 * \param md_info The information structure of the message-digest algorithm
299 * to use.
300 * \param input The buffer holding the data.
301 * \param ilen The length of the input data.
302 * \param output The generic message-digest checksum result.
303 *
304 * \return \c 0 on success.
305 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
306 * failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200307 */
308int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
309 unsigned char *output );
310
311#if defined(MBEDTLS_FS_IO)
312/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200313 * \brief This function calculates the message-digest checksum
314 * result of the contents of the provided file.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200315 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200316 * The result is calculated as
317 * Output = message_digest(file contents).
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200318 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200319 * \param md_info The information structure of the message-digest algorithm
320 * to use.
321 * \param path The input file name.
322 * \param output The generic message-digest checksum result.
323 *
324 * \return \c 0 on success.
325 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
326 * the file pointed by \p path.
327 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200328 */
329int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
330 unsigned char *output );
331#endif /* MBEDTLS_FS_IO */
332
333/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200334 * \brief This function sets the HMAC key and prepares to
335 * authenticate a new message.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200336 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200337 * Call this function after mbedtls_md_setup(), to use
338 * the MD context for an HMAC calculation, then call
339 * mbedtls_md_hmac_update() to provide the input data, and
340 * mbedtls_md_hmac_finish() to get the HMAC value.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200341 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200342 * \param ctx The message digest context containing an embedded HMAC
343 * context.
344 * \param key The HMAC secret key.
345 * \param keylen The length of the HMAC key in Bytes.
346 *
347 * \return \c 0 on success.
348 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
349 * failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200350 */
351int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
352 size_t keylen );
353
354/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200355 * \brief This function feeds an input buffer into an ongoing HMAC
356 * computation.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200357 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200358 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
359 * before calling this function.
360 * You may call this function multiple times to pass the
361 * input piecewise.
362 * Afterwards, call mbedtls_md_hmac_finish().
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200363 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200364 * \param ctx The message digest context containing an embedded HMAC
365 * context.
366 * \param input The buffer holding the input data.
367 * \param ilen The length of the input data.
368 *
369 * \return \c 0 on success.
370 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
371 * failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200372 */
373int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
374 size_t ilen );
375
376/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200377 * \brief This function finishes the HMAC operation, and writes
378 * the result to the output buffer.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200379 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200380 * Call this function after mbedtls_md_hmac_starts() and
381 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
382 * you may either call mbedtls_md_free() to clear the context,
383 * or call mbedtls_md_hmac_reset() to reuse the context with
384 * the same HMAC key.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200385 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200386 * \param ctx The message digest context containing an embedded HMAC
387 * context.
388 * \param output The generic HMAC checksum result.
389 *
390 * \return \c 0 on success.
391 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
392 * failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200393 */
394int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
395
396/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200397 * \brief This function prepares to authenticate a new message with
398 * the same key as the previous HMAC operation.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200399 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200400 * You may call this function after mbedtls_md_hmac_finish().
401 * Afterwards call mbedtls_md_hmac_update() to pass the new
402 * input.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200403 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200404 * \param ctx The message digest context containing an embedded HMAC
405 * context.
406 *
407 * \return \c 0 on success.
408 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
409 * failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200410 */
411int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
412
413/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200414 * \brief This function calculates the full generic HMAC
415 * on the input buffer with the provided key.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200416 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200417 * The function allocates the context, performs the
418 * calculation, and frees the context.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200419 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200420 * The HMAC result is calculated as
421 * output = generic HMAC(hmac key, input buffer).
422 *
423 * \param md_info The information structure of the message-digest algorithm
424 * to use.
425 * \param key The HMAC secret key.
426 * \param keylen The length of the HMAC secret key in Bytes.
427 * \param input The buffer holding the input data.
428 * \param ilen The length of the input data.
429 * \param output The generic HMAC result.
430 *
431 * \return \c 0 on success.
432 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
433 * failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200434 */
435int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
436 const unsigned char *input, size_t ilen,
437 unsigned char *output );
438
439/* Internal use */
440int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
441
442#ifdef __cplusplus
443}
444#endif
445
446#endif /* MBEDTLS_MD_H */