blob: 6a21f0590823492e34f0b39f4c1d205cd4fdee85 [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 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/*
Rose Zadik64feefb2018-01-25 22:01:10 +00009 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
Bence Szépkútif744bd72020-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útif744bd72020-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. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030069
70/* MBEDTLS_ERR_MD_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010071#define MBEDTLS_ERR_MD_HW_ACCEL_FAILED -0x5280 /**< MD hardware accelerator failed. */
Paul Bakker335db3f2011-04-25 15:28:35 +000072
Paul Bakker407a0da2013-06-27 14:29:21 +020073#ifdef __cplusplus
74extern "C" {
75#endif
76
Hanno Beckerbbca8c52017-09-25 14:53:51 +010077/**
Rose Zadik8c9c7942018-03-27 11:52:58 +010078 * \brief Supported message digests.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010079 *
80 * \warning MD2, MD4, MD5 and SHA-1 are considered weak message digests and
81 * their use constitutes a security risk. We recommend considering
82 * stronger message digests instead.
83 *
84 */
Paul Bakker17373852011-01-06 14:20:01 +000085typedef enum {
Rose Zadikf3e47362018-04-16 16:31:16 +010086 MBEDTLS_MD_NONE=0, /**< None. */
87 MBEDTLS_MD_MD2, /**< The MD2 message digest. */
88 MBEDTLS_MD_MD4, /**< The MD4 message digest. */
89 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
90 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
91 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
92 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
93 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
94 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
Rose Zadik8c9c7942018-03-27 11:52:58 +010095 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +000097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098#if defined(MBEDTLS_SHA512_C)
99#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +0200100#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
Paul Bakker7db01092013-09-10 11:10:57 +0200102#endif
Paul Bakker1b57b062011-01-06 15:48:19 +0000103
Paul Bakker17373852011-01-06 14:20:01 +0000104/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000105 * Opaque struct defined in md_internal.h.
Paul Bakker17373852011-01-06 14:20:01 +0000106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +0000108
109/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000110 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000111 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200112typedef struct mbedtls_md_context_t
113{
Rose Zadik64feefb2018-01-25 22:01:10 +0000114 /** Information about the associated message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 const mbedtls_md_info_t *md_info;
Paul Bakker17373852011-01-06 14:20:01 +0000116
Rose Zadik64feefb2018-01-25 22:01:10 +0000117 /** The digest-specific context. */
Paul Bakker17373852011-01-06 14:20:01 +0000118 void *md_ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100119
Rose Zadik64feefb2018-01-25 22:01:10 +0000120 /** The HMAC part of the context. */
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100121 void *hmac_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000123
Paul Bakker17373852011-01-06 14:20:01 +0000124/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000125 * \brief This function returns the list of digests supported by the
126 * generic digest module.
Paul Bakker72f62662011-01-16 21:27:44 +0000127 *
Manuel Pégourié-Gonnardfb11d252020-05-22 12:12:36 +0200128 * \note The list starts with the strongest available hashes.
129 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000130 * \return A statically allocated array of digests. Each element
131 * in the returned list is an integer belonging to the
132 * message-digest enumeration #mbedtls_md_type_t.
133 * The last entry is 0.
Paul Bakker72f62662011-01-16 21:27:44 +0000134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135const int *mbedtls_md_list( void );
Paul Bakker72f62662011-01-16 21:27:44 +0000136
137/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000138 * \brief This function returns the message-digest information
139 * associated with the given digest name.
Paul Bakker17373852011-01-06 14:20:01 +0000140 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000141 * \param md_name The name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000142 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100143 * \return The message-digest information associated with \p md_name.
144 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000147
148/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000149 * \brief This function returns the message-digest information
150 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000151 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000152 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000153 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100154 * \return The message-digest information associated with \p md_type.
155 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
Paul Bakker17373852011-01-06 14:20:01 +0000158
159/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000160 * \brief This function initializes a message-digest context without
161 * binding it to a particular message-digest algorithm.
162 *
163 * This function should always be called first. It prepares the
164 * context for mbedtls_md_setup() for binding it to a
165 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167void mbedtls_md_init( mbedtls_md_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200168
169/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000170 * \brief This function clears the internal structure of \p ctx and
171 * frees any embedded internal structure, but does not free
172 * \p ctx itself.
173 *
174 * If you have called mbedtls_md_setup() on \p ctx, you must
175 * call mbedtls_md_free() when you are no longer using the
176 * context.
177 * Calling this function if you have previously
178 * called mbedtls_md_init() and nothing else is optional.
179 * You must not call this function if you have not called
180 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182void mbedtls_md_free( mbedtls_md_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
185#if defined(MBEDTLS_DEPRECATED_WARNING)
186#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100187#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100189#endif
190/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000191 * \brief This function selects the message digest algorithm to use,
192 * and allocates internal structures.
193 *
194 * It should be called after mbedtls_md_init() or mbedtls_md_free().
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 * Makes it necessary to call mbedtls_md_free() later.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100196 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 * \deprecated Superseded by mbedtls_md_setup() in 2.0.0
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100198 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000199 * \param ctx The context to set up.
200 * \param md_info The information structure of the message-digest algorithm
201 * to use.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100202 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100203 * \return \c 0 on success.
204 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
205 * failure.
206 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED;
209#undef MBEDTLS_DEPRECATED
210#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100211
Paul Bakker84bbeb52014-07-01 14:53:22 +0200212/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000213 * \brief This function selects the message digest algorithm to use,
214 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000215 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000216 * It should be called after mbedtls_md_init() or
217 * mbedtls_md_free(). Makes it necessary to call
218 * mbedtls_md_free() later.
219 *
220 * \param ctx The context to set up.
221 * \param md_info The information structure of the message-digest algorithm
222 * to use.
Rose Zadik8c9c7942018-03-27 11:52:58 +0100223 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
224 * or non-zero: HMAC is used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000225 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100226 * \return \c 0 on success.
227 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
228 * failure.
229 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
Paul Bakker562535d2011-01-20 16:42:01 +0000232
233/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000234 * \brief This function clones the state of an message-digest
235 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200236 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000237 * \note You must call mbedtls_md_setup() on \c dst before calling
238 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200239 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000240 * \note The two contexts must have the same type,
241 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200242 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000243 * \warning This function clones the message-digest state, not the
244 * HMAC state.
245 *
246 * \param dst The destination context.
247 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200248 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100249 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100250 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200251 */
252int mbedtls_md_clone( mbedtls_md_context_t *dst,
253 const mbedtls_md_context_t *src );
254
255/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000256 * \brief This function extracts the message-digest size from the
257 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000258 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000259 * \param md_info The information structure of the message-digest algorithm
260 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000261 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000262 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000265
266/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000267 * \brief This function extracts the message-digest type from the
268 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000269 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000270 * \param md_info The information structure of the message-digest algorithm
271 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000272 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000273 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000276
277/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000278 * \brief This function extracts the message-digest name from the
279 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000280 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000281 * \param md_info The information structure of the message-digest algorithm
282 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000283 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000284 * \return The name of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000287
288/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000289 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000290 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000291 * You must call this function after setting up the context
292 * with mbedtls_md_setup(), and before passing data with
293 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000294 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000295 * \param ctx The generic message-digest context.
296 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100297 * \return \c 0 on success.
298 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
299 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301int mbedtls_md_starts( mbedtls_md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000302
303/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000304 * \brief This function feeds an input buffer into an ongoing
305 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000306 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000307 * You must call mbedtls_md_starts() before calling this
308 * function. You may call this function multiple times.
309 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000310 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000311 * \param ctx The generic message-digest context.
312 * \param input The buffer holding the input data.
313 * \param ilen The length of the input data.
314 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100315 * \return \c 0 on success.
316 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
317 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000320
321/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000322 * \brief This function finishes the digest operation,
323 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000324 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000325 * Call this function after a call to mbedtls_md_starts(),
326 * followed by any number of calls to mbedtls_md_update().
327 * Afterwards, you may either clear the context with
328 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
329 * the context for another digest operation with the same
330 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000331 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000332 * \param ctx The generic message-digest context.
333 * \param output The buffer for the generic message-digest checksum result.
334 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100335 * \return \c 0 on success.
336 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
337 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000340
341/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000342 * \brief This function calculates the message-digest of a buffer,
343 * with respect to a configurable message-digest algorithm
344 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000345 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000346 * The result is calculated as
347 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000348 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000349 * \param md_info The information structure of the message-digest algorithm
350 * to use.
351 * \param input The buffer holding the data.
352 * \param ilen The length of the input data.
353 * \param output The generic message-digest checksum result.
354 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100355 * \return \c 0 on success.
356 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
357 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000360 unsigned char *output );
361
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200362#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000363/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000364 * \brief This function calculates the message-digest checksum
365 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000366 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000367 * The result is calculated as
368 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000369 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000370 * \param md_info The information structure of the message-digest algorithm
371 * to use.
372 * \param path The input file name.
373 * \param output The generic message-digest checksum result.
374 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100375 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100376 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
377 * the file pointed by \p path.
378 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200381 unsigned char *output );
382#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000383
384/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000385 * \brief This function sets the HMAC key and prepares to
386 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000387 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000388 * Call this function after mbedtls_md_setup(), to use
389 * the MD context for an HMAC calculation, then call
390 * mbedtls_md_hmac_update() to provide the input data, and
391 * mbedtls_md_hmac_finish() to get the HMAC value.
Paul Bakker17373852011-01-06 14:20:01 +0000392 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000393 * \param ctx The message digest context containing an embedded HMAC
394 * context.
395 * \param key The HMAC secret key.
396 * \param keylen The length of the HMAC key in Bytes.
397 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100398 * \return \c 0 on success.
399 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
400 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200403 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000404
405/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000406 * \brief This function feeds an input buffer into an ongoing HMAC
407 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000408 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000409 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
410 * before calling this function.
411 * You may call this function multiple times to pass the
412 * input piecewise.
413 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000414 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000415 * \param ctx The message digest context containing an embedded HMAC
416 * context.
417 * \param input The buffer holding the input data.
418 * \param ilen The length of the input data.
419 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100420 * \return \c 0 on success.
421 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
422 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200425 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000426
427/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000428 * \brief This function finishes the HMAC operation, and writes
429 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000430 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000431 * Call this function after mbedtls_md_hmac_starts() and
432 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
433 * you may either call mbedtls_md_free() to clear the context,
434 * or call mbedtls_md_hmac_reset() to reuse the context with
435 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000436 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000437 * \param ctx The message digest context containing an embedded HMAC
438 * context.
439 * \param output The generic HMAC checksum result.
440 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100441 * \return \c 0 on success.
442 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
443 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000446
447/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000448 * \brief This function prepares to authenticate a new message with
449 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000450 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000451 * You may call this function after mbedtls_md_hmac_finish().
452 * Afterwards call mbedtls_md_hmac_update() to pass the new
453 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000454 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000455 * \param ctx The message digest context containing an embedded HMAC
456 * context.
457 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100458 * \return \c 0 on success.
459 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
460 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000463
464/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000465 * \brief This function calculates the full generic HMAC
466 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000467 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000468 * The function allocates the context, performs the
469 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000470 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000471 * The HMAC result is calculated as
472 * output = generic HMAC(hmac key, input buffer).
473 *
474 * \param md_info The information structure of the message-digest algorithm
475 * to use.
476 * \param key The HMAC secret key.
477 * \param keylen The length of the HMAC secret key in Bytes.
478 * \param input The buffer holding the input data.
479 * \param ilen The length of the input data.
480 * \param output The generic HMAC result.
481 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100482 * \return \c 0 on success.
483 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
484 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000487 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000488 unsigned char *output );
489
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100490/* Internal use */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100492
Paul Bakker17373852011-01-06 14:20:01 +0000493#ifdef __cplusplus
494}
495#endif
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#endif /* MBEDTLS_MD_H */