blob: 6306af9dbaf24dc46a78328b65ff5023cc390ca5 [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
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)
Jaeden Amero203fdf72019-07-04 20:01:14 +010033#include "mbedtls/config.h"
Ron Eldorf231eaa2017-08-22 14:50:14 +030034#else
35#include MBEDTLS_CONFIG_FILE
36#endif
37
Jaeden Amero59ebddf2019-07-10 11:03:03 +010038#include "mbedtls/platform_util.h"
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
41#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
42#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
43#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030044
45/* MBEDTLS_ERR_MD_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010046#define MBEDTLS_ERR_MD_HW_ACCEL_FAILED -0x5280 /**< MD hardware accelerator failed. */
Paul Bakker335db3f2011-04-25 15:28:35 +000047
Paul Bakker407a0da2013-06-27 14:29:21 +020048#ifdef __cplusplus
49extern "C" {
50#endif
51
Hanno Beckerbbca8c52017-09-25 14:53:51 +010052/**
Rose Zadik8c9c7942018-03-27 11:52:58 +010053 * \brief Supported message digests.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010054 *
55 * \warning MD2, MD4, MD5 and SHA-1 are considered weak message digests and
56 * their use constitutes a security risk. We recommend considering
57 * stronger message digests instead.
58 *
59 */
Paul Bakker17373852011-01-06 14:20:01 +000060typedef enum {
Rose Zadikf3e47362018-04-16 16:31:16 +010061 MBEDTLS_MD_NONE=0, /**< None. */
62 MBEDTLS_MD_MD2, /**< The MD2 message digest. */
63 MBEDTLS_MD_MD4, /**< The MD4 message digest. */
64 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
65 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
66 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
67 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
68 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
69 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
Rose Zadik8c9c7942018-03-27 11:52:58 +010070 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +000072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_SHA512_C)
74#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020075#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
Paul Bakker7db01092013-09-10 11:10:57 +020077#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000078
Hanno Becker2e24c3b2017-12-27 21:28:58 +000079#if defined(MBEDTLS_SHA512_C)
80#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
81#else
82#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
83#endif
84
Paul Bakker17373852011-01-06 14:20:01 +000085/**
Rose Zadik64feefb2018-01-25 22:01:10 +000086 * Opaque struct defined in md_internal.h.
Paul Bakker17373852011-01-06 14:20:01 +000087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +000089
90/**
Rose Zadik64feefb2018-01-25 22:01:10 +000091 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +000092 */
Dawid Drozd428cc522018-07-24 10:02:47 +020093typedef struct mbedtls_md_context_t
94{
Rose Zadik64feefb2018-01-25 22:01:10 +000095 /** Information about the associated message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 const mbedtls_md_info_t *md_info;
Paul Bakker17373852011-01-06 14:20:01 +000097
Rose Zadik64feefb2018-01-25 22:01:10 +000098 /** The digest-specific context. */
Paul Bakker17373852011-01-06 14:20:01 +000099 void *md_ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100100
Rose Zadik64feefb2018-01-25 22:01:10 +0000101 /** The HMAC part of the context. */
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100102 void *hmac_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000104
Paul Bakker17373852011-01-06 14:20:01 +0000105/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000106 * \brief This function returns the list of digests supported by the
107 * generic digest module.
Paul Bakker72f62662011-01-16 21:27:44 +0000108 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000109 * \return A statically allocated array of digests. Each element
110 * in the returned list is an integer belonging to the
111 * message-digest enumeration #mbedtls_md_type_t.
112 * The last entry is 0.
Paul Bakker72f62662011-01-16 21:27:44 +0000113 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100114MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115const int *mbedtls_md_list( void );
Paul Bakker72f62662011-01-16 21:27:44 +0000116
117/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000118 * \brief This function returns the message-digest information
119 * associated with the given digest name.
Paul Bakker17373852011-01-06 14:20:01 +0000120 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000121 * \param md_name The name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000122 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100123 * \return The message-digest information associated with \p md_name.
124 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000125 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100126MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000128
129/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000130 * \brief This function returns the message-digest information
131 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000132 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000133 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000134 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100135 * \return The message-digest information associated with \p md_type.
136 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000137 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100138MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
Paul Bakker17373852011-01-06 14:20:01 +0000140
141/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000142 * \brief This function initializes a message-digest context without
143 * binding it to a particular message-digest algorithm.
144 *
145 * This function should always be called first. It prepares the
146 * context for mbedtls_md_setup() for binding it to a
147 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200148 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100149MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150void mbedtls_md_init( mbedtls_md_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200151
152/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000153 * \brief This function clears the internal structure of \p ctx and
154 * frees any embedded internal structure, but does not free
155 * \p ctx itself.
156 *
157 * If you have called mbedtls_md_setup() on \p ctx, you must
158 * call mbedtls_md_free() when you are no longer using the
159 * context.
160 * Calling this function if you have previously
161 * called mbedtls_md_init() and nothing else is optional.
162 * You must not call this function if you have not called
163 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200164 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100165MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166void mbedtls_md_free( mbedtls_md_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100169/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000170 * \brief This function selects the message digest algorithm to use,
171 * and allocates internal structures.
172 *
173 * It should be called after mbedtls_md_init() or mbedtls_md_free().
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 * Makes it necessary to call mbedtls_md_free() later.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100175 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 * \deprecated Superseded by mbedtls_md_setup() in 2.0.0
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100177 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000178 * \param ctx The context to set up.
179 * \param md_info The information structure of the message-digest algorithm
180 * to use.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100181 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100182 * \return \c 0 on success.
183 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
184 * failure.
185 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100189
Paul Bakker84bbeb52014-07-01 14:53:22 +0200190/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000191 * \brief This function selects the message digest algorithm to use,
192 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000193 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000194 * It should be called after mbedtls_md_init() or
195 * mbedtls_md_free(). Makes it necessary to call
196 * mbedtls_md_free() later.
197 *
198 * \param ctx The context to set up.
199 * \param md_info The information structure of the message-digest algorithm
200 * to use.
Rose Zadik8c9c7942018-03-27 11:52:58 +0100201 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
202 * or non-zero: HMAC is used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000203 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100204 * \return \c 0 on success.
205 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
206 * failure.
207 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000208 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100209MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
Paul Bakker562535d2011-01-20 16:42:01 +0000211
212/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000213 * \brief This function clones the state of an message-digest
214 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200215 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000216 * \note You must call mbedtls_md_setup() on \c dst before calling
217 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200218 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000219 * \note The two contexts must have the same type,
220 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200221 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000222 * \warning This function clones the message-digest state, not the
223 * HMAC state.
224 *
225 * \param dst The destination context.
226 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200227 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100228 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100229 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200230 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100231MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200232int mbedtls_md_clone( mbedtls_md_context_t *dst,
233 const mbedtls_md_context_t *src );
234
235/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000236 * \brief This function extracts the message-digest size 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 size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000243 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100244MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000246
247/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000248 * \brief This function extracts the message-digest type from the
249 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000250 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000251 * \param md_info The information structure of the message-digest algorithm
252 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000253 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000254 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000255 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100256MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000258
259/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000260 * \brief This function extracts the message-digest name from the
261 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000262 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000263 * \param md_info The information structure of the message-digest algorithm
264 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000265 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000266 * \return The name of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000267 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100268MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000270
271/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000272 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000273 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000274 * You must call this function after setting up the context
275 * with mbedtls_md_setup(), and before passing data with
276 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000277 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000278 * \param ctx The generic message-digest context.
279 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100280 * \return \c 0 on success.
281 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
282 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000283 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100284MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285int mbedtls_md_starts( mbedtls_md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000286
287/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000288 * \brief This function feeds an input buffer into an ongoing
289 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000290 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000291 * You must call mbedtls_md_starts() before calling this
292 * function. You may call this function multiple times.
293 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000294 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000295 * \param ctx The generic message-digest context.
296 * \param input The buffer holding the input data.
297 * \param ilen The length of the input data.
298 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100299 * \return \c 0 on success.
300 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
301 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000302 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100303MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000305
306/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000307 * \brief This function finishes the digest operation,
308 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000309 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000310 * Call this function after a call to mbedtls_md_starts(),
311 * followed by any number of calls to mbedtls_md_update().
312 * Afterwards, you may either clear the context with
313 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
314 * the context for another digest operation with the same
315 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000316 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000317 * \param ctx The generic message-digest context.
318 * \param output The buffer for the generic message-digest checksum result.
319 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100320 * \return \c 0 on success.
321 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
322 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000323 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100324MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000326
327/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000328 * \brief This function calculates the message-digest of a buffer,
329 * with respect to a configurable message-digest algorithm
330 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000331 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000332 * The result is calculated as
333 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000334 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000335 * \param md_info The information structure of the message-digest algorithm
336 * to use.
337 * \param input The buffer holding the data.
338 * \param ilen The length of the input data.
339 * \param output The generic message-digest checksum result.
340 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100341 * \return \c 0 on success.
342 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
343 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000344 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100345MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000347 unsigned char *output );
348
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200349#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000350/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000351 * \brief This function calculates the message-digest checksum
352 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000353 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000354 * The result is calculated as
355 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000356 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000357 * \param md_info The information structure of the message-digest algorithm
358 * to use.
359 * \param path The input file name.
360 * \param output The generic message-digest checksum result.
361 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100362 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100363 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
364 * the file pointed by \p path.
365 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000366 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100367MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200369 unsigned char *output );
370#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000371
372/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000373 * \brief This function sets the HMAC key and prepares to
374 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000375 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000376 * Call this function after mbedtls_md_setup(), to use
377 * the MD context for an HMAC calculation, then call
378 * mbedtls_md_hmac_update() to provide the input data, and
379 * mbedtls_md_hmac_finish() to get the HMAC value.
Paul Bakker17373852011-01-06 14:20:01 +0000380 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000381 * \param ctx The message digest context containing an embedded HMAC
382 * context.
383 * \param key The HMAC secret key.
384 * \param keylen The length of the HMAC key in Bytes.
385 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100386 * \return \c 0 on success.
387 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
388 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000389 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100390MBEDTLS_DEPRECATED
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 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100409 * \return \c 0 on success.
410 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
411 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000412 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100413MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200415 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000416
417/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000418 * \brief This function finishes the HMAC operation, and writes
419 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000420 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000421 * Call this function after mbedtls_md_hmac_starts() and
422 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
423 * you may either call mbedtls_md_free() to clear the context,
424 * or call mbedtls_md_hmac_reset() to reuse the context with
425 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000426 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000427 * \param ctx The message digest context containing an embedded HMAC
428 * context.
429 * \param output The generic HMAC checksum result.
430 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100431 * \return \c 0 on success.
432 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
433 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000434 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100435MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000437
438/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000439 * \brief This function prepares to authenticate a new message with
440 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000441 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000442 * You may call this function after mbedtls_md_hmac_finish().
443 * Afterwards call mbedtls_md_hmac_update() to pass the new
444 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000445 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000446 * \param ctx The message digest context containing an embedded HMAC
447 * context.
448 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100449 * \return \c 0 on success.
450 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
451 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000452 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100453MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000455
456/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000457 * \brief This function calculates the full generic HMAC
458 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000459 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000460 * The function allocates the context, performs the
461 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000462 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000463 * The HMAC result is calculated as
464 * output = generic HMAC(hmac key, input buffer).
465 *
466 * \param md_info The information structure of the message-digest algorithm
467 * to use.
468 * \param key The HMAC secret key.
469 * \param keylen The length of the HMAC secret key in Bytes.
470 * \param input The buffer holding the input data.
471 * \param ilen The length of the input data.
472 * \param output The generic HMAC result.
473 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100474 * \return \c 0 on success.
475 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
476 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000477 */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100478MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000480 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000481 unsigned char *output );
482
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100483/* Internal use */
Jaeden Amerodfc7f4a2019-07-10 11:33:37 +0100484MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100486
Paul Bakker17373852011-01-06 14:20:01 +0000487#ifdef __cplusplus
488}
489#endif
490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#endif /* MBEDTLS_MD_H */