blob: 5e0376c05b7272d17a2cf9c3f4c3c45bc34986ad [file] [log] [blame]
Rose Zadik64feefb2018-01-25 22:01:10 +00001 /**
Simon Butcher5b331b92016-01-03 16:14:14 +00002 * \file md.h
Paul Bakker9af723c2014-05-01 13:03:14 +02003 *
Rose Zadik64feefb2018-01-25 22:01:10 +00004 * \brief 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/*
Rose Zadik64feefb2018-01-25 22:01:10 +00009 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
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 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000025 */
Rose Zadik64feefb2018-01-25 22:01:10 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#ifndef MBEDTLS_MD_H
28#define MBEDTLS_MD_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
Ron Eldorf231eaa2017-08-22 14:50:14 +030032#if !defined(MBEDTLS_CONFIG_FILE)
33#include "config.h"
34#else
35#include MBEDTLS_CONFIG_FILE
36#endif
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
39#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
40#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
41#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010042#define MBEDTLS_ERR_MD_HW_ACCEL_FAILED -0x5280 /**< MD hardware accelerator failed. */
Paul Bakker335db3f2011-04-25 15:28:35 +000043
Paul Bakker407a0da2013-06-27 14:29:21 +020044#ifdef __cplusplus
45extern "C" {
46#endif
47
Paul Bakker17373852011-01-06 14:20:01 +000048typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 MBEDTLS_MD_NONE=0,
50 MBEDTLS_MD_MD2,
51 MBEDTLS_MD_MD4,
52 MBEDTLS_MD_MD5,
53 MBEDTLS_MD_SHA1,
54 MBEDTLS_MD_SHA224,
55 MBEDTLS_MD_SHA256,
56 MBEDTLS_MD_SHA384,
57 MBEDTLS_MD_SHA512,
58 MBEDTLS_MD_RIPEMD160,
59} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_SHA512_C)
62#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020063#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
Paul Bakker7db01092013-09-10 11:10:57 +020065#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000066
Paul Bakker17373852011-01-06 14:20:01 +000067/**
Rose Zadik64feefb2018-01-25 22:01:10 +000068 * Opaque struct defined in md_internal.h.
Paul Bakker17373852011-01-06 14:20:01 +000069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +000071
72/**
Rose Zadik64feefb2018-01-25 22:01:10 +000073 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +000074 */
75typedef struct {
Rose Zadik64feefb2018-01-25 22:01:10 +000076 /** Information about the associated message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 const mbedtls_md_info_t *md_info;
Paul Bakker17373852011-01-06 14:20:01 +000078
Rose Zadik64feefb2018-01-25 22:01:10 +000079 /** The digest-specific context. */
Paul Bakker17373852011-01-06 14:20:01 +000080 void *md_ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +010081
Rose Zadik64feefb2018-01-25 22:01:10 +000082 /** The HMAC part of the context. */
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +010083 void *hmac_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +000085
Paul Bakker17373852011-01-06 14:20:01 +000086/**
Rose Zadik64feefb2018-01-25 22:01:10 +000087 * \brief This function returns the list of digests supported by the
88 * generic digest module.
Paul Bakker72f62662011-01-16 21:27:44 +000089 *
Rose Zadik64feefb2018-01-25 22:01:10 +000090 * \return A statically allocated array of digests. Each element
91 * in the returned list is an integer belonging to the
92 * message-digest enumeration #mbedtls_md_type_t.
93 * The last entry is 0.
Paul Bakker72f62662011-01-16 21:27:44 +000094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095const int *mbedtls_md_list( void );
Paul Bakker72f62662011-01-16 21:27:44 +000096
97/**
Rose Zadik64feefb2018-01-25 22:01:10 +000098 * \brief This function returns the message-digest information
99 * associated with the given digest name.
Paul Bakker17373852011-01-06 14:20:01 +0000100 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000101 * \param md_name The name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000102 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000103 * \return The message-digest information associated with \p md_name,
104 * or NULL if not found.
Paul Bakker17373852011-01-06 14:20:01 +0000105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000107
108/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000109 * \brief This function returns the message-digest information
110 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000111 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000112 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000113 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000114 * \return The message-digest information associated with \p md_type,
115 * or NULL if not found.
Paul Bakker17373852011-01-06 14:20:01 +0000116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
Paul Bakker17373852011-01-06 14:20:01 +0000118
119/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000120 * \brief This function initializes a message-digest context without
121 * binding it to a particular message-digest algorithm.
122 *
123 * This function should always be called first. It prepares the
124 * context for mbedtls_md_setup() for binding it to a
125 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127void mbedtls_md_init( mbedtls_md_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200128
129/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000130 * \brief This function clears the internal structure of \p ctx and
131 * frees any embedded internal structure, but does not free
132 * \p ctx itself.
133 *
134 * If you have called mbedtls_md_setup() on \p ctx, you must
135 * call mbedtls_md_free() when you are no longer using the
136 * context.
137 * Calling this function if you have previously
138 * called mbedtls_md_init() and nothing else is optional.
139 * You must not call this function if you have not called
140 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142void mbedtls_md_free( mbedtls_md_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
145#if defined(MBEDTLS_DEPRECATED_WARNING)
146#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100147#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100149#endif
150/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000151 * \brief This function selects the message digest algorithm to use,
152 * and allocates internal structures.
153 *
154 * It should be called after mbedtls_md_init() or mbedtls_md_free().
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 * Makes it necessary to call mbedtls_md_free() later.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100156 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 * \deprecated Superseded by mbedtls_md_setup() in 2.0.0
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100158 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000159 * \param ctx The context to set up.
160 * \param md_info The information structure of the message-digest algorithm
161 * to use.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100162 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100163 * \returns \c 0 on success,
Rose Zadik64feefb2018-01-25 22:01:10 +0000164 * #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure,
165 * #MBEDTLS_ERR_MD_ALLOC_FAILED memory allocation failure.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED;
168#undef MBEDTLS_DEPRECATED
169#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100170
Paul Bakker84bbeb52014-07-01 14:53:22 +0200171/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000172 * \brief This function selects the message digest algorithm to use,
173 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000174 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000175 * It should be called after mbedtls_md_init() or
176 * mbedtls_md_free(). Makes it necessary to call
177 * mbedtls_md_free() later.
178 *
179 * \param ctx The context to set up.
180 * \param md_info The information structure of the message-digest algorithm
181 * to use.
182 * \param hmac <ul><li>0: HMAC is not used. Saves some memory.</li>
183 * <li>non-zero: HMAC is used with this context.</li></ul>
Paul Bakker562535d2011-01-20 16:42:01 +0000184 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100185 * \returns \c 0 on success,
Rose Zadik64feefb2018-01-25 22:01:10 +0000186 * #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure, or
187 * #MBEDTLS_ERR_MD_ALLOC_FAILED on memory allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
Paul Bakker562535d2011-01-20 16:42:01 +0000190
191/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000192 * \brief This function clones the state of an message-digest
193 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200194 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000195 * \note You must call mbedtls_md_setup() on \c dst before calling
196 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200197 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000198 * \note The two contexts must have the same type,
199 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200200 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000201 * \warning This function clones the message-digest state, not the
202 * HMAC state.
203 *
204 * \param dst The destination context.
205 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200206 *
207 * \return \c 0 on success,
Rose Zadik64feefb2018-01-25 22:01:10 +0000208 * #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200209 */
210int mbedtls_md_clone( mbedtls_md_context_t *dst,
211 const mbedtls_md_context_t *src );
212
213/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000214 * \brief This function extracts the message-digest size from the
215 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000216 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000217 * \param md_info The information structure of the message-digest algorithm
218 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000219 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000220 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000223
224/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000225 * \brief This function extracts the message-digest type from the
226 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000227 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000228 * \param md_info The information structure of the message-digest algorithm
229 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000230 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000231 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000234
235/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000236 * \brief This function extracts the message-digest name from the
237 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000238 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000239 * \param md_info The information structure of the message-digest algorithm
240 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000241 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000242 * \return The name of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000245
246/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000247 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000248 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000249 * You must call this function after setting up the context
250 * with mbedtls_md_setup(), and before passing data with
251 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000252 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000253 * \param ctx The generic message-digest context.
254 *
255 * \returns \c 0 on success, #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
256 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258int mbedtls_md_starts( mbedtls_md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000259
260/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000261 * \brief This function feeds an input buffer into an ongoing
262 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000263 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000264 * You must call mbedtls_md_starts() before calling this
265 * function. You may call this function multiple times.
266 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000267 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000268 * \param ctx The generic message-digest context.
269 * \param input The buffer holding the input data.
270 * \param ilen The length of the input data.
271 *
272 * \returns \c 0 on success, #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
273 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000276
277/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000278 * \brief This function finishes the digest operation,
279 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000280 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000281 * Call this function after a call to mbedtls_md_starts(),
282 * followed by any number of calls to mbedtls_md_update().
283 * Afterwards, you may either clear the context with
284 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
285 * the context for another digest operation with the same
286 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000287 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000288 * \param ctx The generic message-digest context.
289 * \param output The buffer for the generic message-digest checksum result.
290 *
291 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
292 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000295
296/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000297 * \brief This function calculates the message-digest of a buffer,
298 * with respect to a configurable message-digest algorithm
299 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000300 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000301 * The result is calculated as
302 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000303 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000304 * \param md_info The information structure of the message-digest algorithm
305 * to use.
306 * \param input The buffer holding the data.
307 * \param ilen The length of the input data.
308 * \param output The generic message-digest checksum result.
309 *
310 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
311 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000314 unsigned char *output );
315
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200316#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000317/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000318 * \brief This function calculates the message-digest checksum
319 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000320 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000321 * The result is calculated as
322 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000323 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000324 * \param md_info The information structure of the message-digest algorithm
325 * to use.
326 * \param path The input file name.
327 * \param output The generic message-digest checksum result.
328 *
329 * \return \c 0 on success,
330 * #MBEDTLS_ERR_MD_FILE_IO_ERROR if file input failed, or
331 * #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000332 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200334 unsigned char *output );
335#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000336
337/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000338 * \brief This function sets the HMAC key and prepares to
339 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000340 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000341 * Call this function after mbedtls_md_setup(), to use
342 * the MD context for an HMAC calculation, then call
343 * mbedtls_md_hmac_update() to provide the input data, and
344 * mbedtls_md_hmac_finish() to get the HMAC value.
Paul Bakker17373852011-01-06 14:20:01 +0000345 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000346 * \param ctx The message digest context containing an embedded HMAC
347 * context.
348 * \param key The HMAC secret key.
349 * \param keylen The length of the HMAC key in Bytes.
350 *
351 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
352 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200355 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000356
357/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000358 * \brief This function feeds an input buffer into an ongoing HMAC
359 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000360 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000361 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
362 * before calling this function.
363 * You may call this function multiple times to pass the
364 * input piecewise.
365 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000366 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000367 * \param ctx The message digest context containing an embedded HMAC
368 * context.
369 * \param input The buffer holding the input data.
370 * \param ilen The length of the input data.
371 *
372 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
373 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200376 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000377
378/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000379 * \brief This function finishes the HMAC operation, and writes
380 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000381 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000382 * Call this function after mbedtls_md_hmac_starts() and
383 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
384 * you may either call mbedtls_md_free() to clear the context,
385 * or call mbedtls_md_hmac_reset() to reuse the context with
386 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000387 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000388 * \param ctx The message digest context containing an embedded HMAC
389 * context.
390 * \param output The generic HMAC checksum result.
391 *
392 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
393 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000396
397/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000398 * \brief This function prepares to authenticate a new message with
399 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000400 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000401 * You may call this function after mbedtls_md_hmac_finish().
402 * Afterwards call mbedtls_md_hmac_update() to pass the new
403 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000404 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000405 * \param ctx The message digest context containing an embedded HMAC
406 * context.
407 *
408 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
409 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000412
413/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000414 * \brief This function calculates the full generic HMAC
415 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000416 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000417 * The function allocates the context, performs the
418 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000419 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000420 * 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 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
432 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000435 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000436 unsigned char *output );
437
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100438/* Internal use */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100440
Paul Bakker17373852011-01-06 14:20:01 +0000441#ifdef __cplusplus
442}
443#endif
444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445#endif /* MBEDTLS_MD_H */