blob: f424903cb1fc69cc7631cc8c79266e072dc351db [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
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020010 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
11 *
12 * This file is provided under the Apache License 2.0, or the
13 * GNU General Public License v2.0 or later.
14 *
15 * **********
16 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020017 *
18 * Licensed under the Apache License, Version 2.0 (the "License"); you may
19 * not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Unless required by applicable law or agreed to in writing, software
25 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
26 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27 * See the License for the specific language governing permissions and
28 * limitations under the License.
Paul Bakker17373852011-01-06 14:20:01 +000029 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020030 * **********
31 *
32 * **********
33 * GNU General Public License v2.0 or later:
34 *
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License as published by
37 * the Free Software Foundation; either version 2 of the License, or
38 * (at your option) any later version.
39 *
40 * This program is distributed in the hope that it will be useful,
41 * but WITHOUT ANY WARRANTY; without even the implied warranty of
42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43 * GNU General Public License for more details.
44 *
45 * You should have received a copy of the GNU General Public License along
46 * with this program; if not, write to the Free Software Foundation, Inc.,
47 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
48 *
49 * **********
50 *
Rose Zadik64feefb2018-01-25 22:01:10 +000051 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker17373852011-01-06 14:20:01 +000052 */
Rose Zadik64feefb2018-01-25 22:01:10 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#ifndef MBEDTLS_MD_H
55#define MBEDTLS_MD_H
Paul Bakker17373852011-01-06 14:20:01 +000056
Rich Evans00ab4702015-02-06 13:43:58 +000057#include <stddef.h>
Paul Bakker23986e52011-04-24 08:57:21 +000058
Ron Eldorf231eaa2017-08-22 14:50:14 +030059#if !defined(MBEDTLS_CONFIG_FILE)
60#include "config.h"
61#else
62#include MBEDTLS_CONFIG_FILE
63#endif
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
66#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
67#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
68#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010069#define MBEDTLS_ERR_MD_HW_ACCEL_FAILED -0x5280 /**< MD hardware accelerator failed. */
Paul Bakker335db3f2011-04-25 15:28:35 +000070
Paul Bakker407a0da2013-06-27 14:29:21 +020071#ifdef __cplusplus
72extern "C" {
73#endif
74
Hanno Beckerbbca8c52017-09-25 14:53:51 +010075/**
76 * \brief Enumeration of supported message digests
77 *
78 * \warning MD2, MD4, MD5 and SHA-1 are considered weak message digests and
79 * their use constitutes a security risk. We recommend considering
80 * stronger message digests instead.
81 *
82 */
Paul Bakker17373852011-01-06 14:20:01 +000083typedef enum {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 MBEDTLS_MD_NONE=0,
85 MBEDTLS_MD_MD2,
86 MBEDTLS_MD_MD4,
87 MBEDTLS_MD_MD5,
88 MBEDTLS_MD_SHA1,
89 MBEDTLS_MD_SHA224,
90 MBEDTLS_MD_SHA256,
91 MBEDTLS_MD_SHA384,
92 MBEDTLS_MD_SHA512,
93 MBEDTLS_MD_RIPEMD160,
94} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +000095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if defined(MBEDTLS_SHA512_C)
97#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020098#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
Paul Bakker7db01092013-09-10 11:10:57 +0200100#endif
Paul Bakker1b57b062011-01-06 15:48:19 +0000101
Paul Bakker17373852011-01-06 14:20:01 +0000102/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000103 * Opaque struct defined in md_internal.h.
Paul Bakker17373852011-01-06 14:20:01 +0000104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +0000106
107/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000108 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000109 */
110typedef struct {
Rose Zadik64feefb2018-01-25 22:01:10 +0000111 /** Information about the associated message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 const mbedtls_md_info_t *md_info;
Paul Bakker17373852011-01-06 14:20:01 +0000113
Rose Zadik64feefb2018-01-25 22:01:10 +0000114 /** The digest-specific context. */
Paul Bakker17373852011-01-06 14:20:01 +0000115 void *md_ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100116
Rose Zadik64feefb2018-01-25 22:01:10 +0000117 /** The HMAC part of the context. */
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100118 void *hmac_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000120
Paul Bakker17373852011-01-06 14:20:01 +0000121/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000122 * \brief This function returns the list of digests supported by the
123 * generic digest module.
Paul Bakker72f62662011-01-16 21:27:44 +0000124 *
Manuel Pégourié-Gonnard75036a02020-05-22 12:12:36 +0200125 * \note The list starts with the strongest available hashes.
126 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000127 * \return A statically allocated array of digests. Each element
128 * in the returned list is an integer belonging to the
129 * message-digest enumeration #mbedtls_md_type_t.
130 * The last entry is 0.
Paul Bakker72f62662011-01-16 21:27:44 +0000131 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132const int *mbedtls_md_list( void );
Paul Bakker72f62662011-01-16 21:27:44 +0000133
134/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000135 * \brief This function returns the message-digest information
136 * associated with the given digest name.
Paul Bakker17373852011-01-06 14:20:01 +0000137 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000138 * \param md_name The name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000139 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000140 * \return The message-digest information associated with \p md_name,
141 * or NULL if not found.
Paul Bakker17373852011-01-06 14:20:01 +0000142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000144
145/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000146 * \brief This function returns the message-digest information
147 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000148 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000149 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000150 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000151 * \return The message-digest information associated with \p md_type,
152 * or NULL if not found.
Paul Bakker17373852011-01-06 14:20:01 +0000153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
Paul Bakker17373852011-01-06 14:20:01 +0000155
156/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000157 * \brief This function initializes a message-digest context without
158 * binding it to a particular message-digest algorithm.
159 *
160 * This function should always be called first. It prepares the
161 * context for mbedtls_md_setup() for binding it to a
162 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164void mbedtls_md_init( mbedtls_md_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200165
166/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000167 * \brief This function clears the internal structure of \p ctx and
168 * frees any embedded internal structure, but does not free
169 * \p ctx itself.
170 *
171 * If you have called mbedtls_md_setup() on \p ctx, you must
172 * call mbedtls_md_free() when you are no longer using the
173 * context.
174 * Calling this function if you have previously
175 * called mbedtls_md_init() and nothing else is optional.
176 * You must not call this function if you have not called
177 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179void mbedtls_md_free( mbedtls_md_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
182#if defined(MBEDTLS_DEPRECATED_WARNING)
183#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100184#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100186#endif
187/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000188 * \brief This function selects the message digest algorithm to use,
189 * and allocates internal structures.
190 *
191 * It should be called after mbedtls_md_init() or mbedtls_md_free().
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 * Makes it necessary to call mbedtls_md_free() later.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100193 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 * \deprecated Superseded by mbedtls_md_setup() in 2.0.0
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100195 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000196 * \param ctx The context to set up.
197 * \param md_info The information structure of the message-digest algorithm
198 * to use.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100199 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100200 * \returns \c 0 on success,
Rose Zadik64feefb2018-01-25 22:01:10 +0000201 * #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure,
202 * #MBEDTLS_ERR_MD_ALLOC_FAILED memory allocation failure.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED;
205#undef MBEDTLS_DEPRECATED
206#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100207
Paul Bakker84bbeb52014-07-01 14:53:22 +0200208/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000209 * \brief This function selects the message digest algorithm to use,
210 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000211 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000212 * It should be called after mbedtls_md_init() or
213 * mbedtls_md_free(). Makes it necessary to call
214 * mbedtls_md_free() later.
215 *
216 * \param ctx The context to set up.
217 * \param md_info The information structure of the message-digest algorithm
218 * to use.
219 * \param hmac <ul><li>0: HMAC is not used. Saves some memory.</li>
220 * <li>non-zero: HMAC is used with this context.</li></ul>
Paul Bakker562535d2011-01-20 16:42:01 +0000221 *
Manuel Pégourié-Gonnardeca510f2015-03-26 12:26:34 +0100222 * \returns \c 0 on success,
Rose Zadik64feefb2018-01-25 22:01:10 +0000223 * #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure, or
224 * #MBEDTLS_ERR_MD_ALLOC_FAILED on memory allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
Paul Bakker562535d2011-01-20 16:42:01 +0000227
228/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000229 * \brief This function clones the state of an message-digest
230 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200231 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000232 * \note You must call mbedtls_md_setup() on \c dst before calling
233 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200234 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000235 * \note The two contexts must have the same type,
236 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200237 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000238 * \warning This function clones the message-digest state, not the
239 * HMAC state.
240 *
241 * \param dst The destination context.
242 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200243 *
244 * \return \c 0 on success,
Rose Zadik64feefb2018-01-25 22:01:10 +0000245 * #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200246 */
247int mbedtls_md_clone( mbedtls_md_context_t *dst,
248 const mbedtls_md_context_t *src );
249
250/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000251 * \brief This function extracts the message-digest size from the
252 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000253 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000254 * \param md_info The information structure of the message-digest algorithm
255 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000256 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000257 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000260
261/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000262 * \brief This function extracts the message-digest type from the
263 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000264 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000265 * \param md_info The information structure of the message-digest algorithm
266 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000267 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000268 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000271
272/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000273 * \brief This function extracts the message-digest name from the
274 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000275 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000276 * \param md_info The information structure of the message-digest algorithm
277 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000278 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000279 * \return The name of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000282
283/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000284 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000285 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000286 * You must call this function after setting up the context
287 * with mbedtls_md_setup(), and before passing data with
288 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000289 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000290 * \param ctx The generic message-digest context.
291 *
292 * \returns \c 0 on success, #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
293 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000294 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295int mbedtls_md_starts( mbedtls_md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000296
297/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000298 * \brief This function feeds an input buffer into an ongoing
299 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000300 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000301 * You must call mbedtls_md_starts() before calling this
302 * function. You may call this function multiple times.
303 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000304 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000305 * \param ctx The generic message-digest context.
306 * \param input The buffer holding the input data.
307 * \param ilen The length of the input data.
308 *
309 * \returns \c 0 on success, #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
310 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000313
314/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000315 * \brief This function finishes the digest operation,
316 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000317 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000318 * Call this function after a call to mbedtls_md_starts(),
319 * followed by any number of calls to mbedtls_md_update().
320 * Afterwards, you may either clear the context with
321 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
322 * the context for another digest operation with the same
323 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000324 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000325 * \param ctx The generic message-digest context.
326 * \param output The buffer for the generic message-digest checksum result.
327 *
328 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
329 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000332
333/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000334 * \brief This function calculates the message-digest of a buffer,
335 * with respect to a configurable message-digest algorithm
336 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000337 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000338 * The result is calculated as
339 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000340 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000341 * \param md_info The information structure of the message-digest algorithm
342 * to use.
343 * \param input The buffer holding the data.
344 * \param ilen The length of the input data.
345 * \param output The generic message-digest checksum result.
346 *
347 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
348 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000351 unsigned char *output );
352
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200353#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000354/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000355 * \brief This function calculates the message-digest checksum
356 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000357 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000358 * The result is calculated as
359 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000360 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000361 * \param md_info The information structure of the message-digest algorithm
362 * to use.
363 * \param path The input file name.
364 * \param output The generic message-digest checksum result.
365 *
366 * \return \c 0 on success,
367 * #MBEDTLS_ERR_MD_FILE_IO_ERROR if file input failed, or
368 * #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200371 unsigned char *output );
372#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 *
388 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
389 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200392 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000393
394/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000395 * \brief This function feeds an input buffer into an ongoing HMAC
396 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000397 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000398 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
399 * before calling this function.
400 * You may call this function multiple times to pass the
401 * input piecewise.
402 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000403 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000404 * \param ctx The message digest context containing an embedded HMAC
405 * context.
406 * \param input The buffer holding the input data.
407 * \param ilen The length of the input data.
408 *
409 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
410 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200413 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000414
415/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000416 * \brief This function finishes the HMAC operation, and writes
417 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000418 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000419 * Call this function after mbedtls_md_hmac_starts() and
420 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
421 * you may either call mbedtls_md_free() to clear the context,
422 * or call mbedtls_md_hmac_reset() to reuse the context with
423 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000424 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000425 * \param ctx The message digest context containing an embedded HMAC
426 * context.
427 * \param output The generic HMAC checksum result.
428 *
429 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
430 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000433
434/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000435 * \brief This function prepares to authenticate a new message with
436 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000437 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000438 * You may call this function after mbedtls_md_hmac_finish().
439 * Afterwards call mbedtls_md_hmac_update() to pass the new
440 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000441 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000442 * \param ctx The message digest context containing an embedded HMAC
443 * context.
444 *
445 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
446 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000449
450/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000451 * \brief This function calculates the full generic HMAC
452 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000453 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000454 * The function allocates the context, performs the
455 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000456 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000457 * The HMAC result is calculated as
458 * output = generic HMAC(hmac key, input buffer).
459 *
460 * \param md_info The information structure of the message-digest algorithm
461 * to use.
462 * \param key The HMAC secret key.
463 * \param keylen The length of the HMAC secret key in Bytes.
464 * \param input The buffer holding the input data.
465 * \param ilen The length of the input data.
466 * \param output The generic HMAC result.
467 *
468 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if
469 * parameter verification fails.
Paul Bakker17373852011-01-06 14:20:01 +0000470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000472 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000473 unsigned char *output );
474
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100475/* Internal use */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100477
Paul Bakker17373852011-01-06 14:20:01 +0000478#ifdef __cplusplus
479}
480#endif
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#endif /* MBEDTLS_MD_H */