blob: 2776568849ac09d7b0384f05e60867997d42c43d [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)
33#include "config.h"
34#else
35#include MBEDTLS_CONFIG_FILE
36#endif
37
Hanno Beckerc2908472019-09-04 16:56:11 +010038#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
39 !defined(inline) && !defined(__cplusplus)
40#define inline __inline
41#endif
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
44#define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
45#define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
46#define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030047
48/* MBEDTLS_ERR_MD_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010049#define MBEDTLS_ERR_MD_HW_ACCEL_FAILED -0x5280 /**< MD hardware accelerator failed. */
Paul Bakker335db3f2011-04-25 15:28:35 +000050
Paul Bakker407a0da2013-06-27 14:29:21 +020051#ifdef __cplusplus
52extern "C" {
53#endif
54
Hanno Beckerbbca8c52017-09-25 14:53:51 +010055/**
Rose Zadik8c9c7942018-03-27 11:52:58 +010056 * \brief Supported message digests.
Hanno Beckerbbca8c52017-09-25 14:53:51 +010057 *
58 * \warning MD2, MD4, MD5 and SHA-1 are considered weak message digests and
59 * their use constitutes a security risk. We recommend considering
60 * stronger message digests instead.
61 *
62 */
Paul Bakker17373852011-01-06 14:20:01 +000063typedef enum {
Rose Zadikf3e47362018-04-16 16:31:16 +010064 MBEDTLS_MD_NONE=0, /**< None. */
65 MBEDTLS_MD_MD2, /**< The MD2 message digest. */
66 MBEDTLS_MD_MD4, /**< The MD4 message digest. */
67 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
68 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
69 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
70 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
71 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
72 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
Rose Zadik8c9c7942018-03-27 11:52:58 +010073 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074} mbedtls_md_type_t;
Paul Bakker17373852011-01-06 14:20:01 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_SHA512_C)
77#define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
Paul Bakker7db01092013-09-10 11:10:57 +020078#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
Paul Bakker7db01092013-09-10 11:10:57 +020080#endif
Paul Bakker1b57b062011-01-06 15:48:19 +000081
Hanno Becker4c6876b2017-12-27 21:28:58 +000082#if defined(MBEDTLS_SHA512_C)
83#define MBEDTLS_MD_MAX_BLOCK_SIZE 128
84#else
85#define MBEDTLS_MD_MAX_BLOCK_SIZE 64
86#endif
87
Hanno Becker1292c352019-08-13 15:43:26 +010088#if !defined(MBEDTLS_MD_SINGLE_HASH)
89
Hanno Becker527f7c92019-09-04 12:46:07 +010090#define MBEDTLS_MD_INLINABLE_API
91
Paul Bakker17373852011-01-06 14:20:01 +000092/**
Hanno Beckerd03949e2019-07-26 14:38:44 +010093 * Opaque struct defined in md.c.
Paul Bakker17373852011-01-06 14:20:01 +000094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095typedef struct mbedtls_md_info_t mbedtls_md_info_t;
Paul Bakker17373852011-01-06 14:20:01 +000096
Hanno Beckera5cedbc2019-07-17 11:21:02 +010097
98typedef struct mbedtls_md_info_t const * mbedtls_md_handle_t;
99#define MBEDTLS_MD_INVALID_HANDLE ( (mbedtls_md_handle_t) NULL )
100
Hanno Becker1292c352019-08-13 15:43:26 +0100101#else /* !MBEDTLS_MD_SINGLE_HASH */
102
Hanno Becker527f7c92019-09-04 12:46:07 +0100103#define MBEDTLS_MD_INLINABLE_API MBEDTLS_ALWAYS_INLINE static inline
104
Hanno Becker1292c352019-08-13 15:43:26 +0100105typedef int mbedtls_md_handle_t;
106#define MBEDTLS_MD_INVALID_HANDLE ( (mbedtls_md_handle_t) 0 )
107#define MBEDTLS_MD_UNIQUE_VALID_HANDLE ( (mbedtls_md_handle_t) 1 )
108
109#endif /* !MBEDTLS_MD_SINGLE_HASH */
110
Paul Bakker17373852011-01-06 14:20:01 +0000111/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000112 * The generic message-digest context.
Paul Bakker17373852011-01-06 14:20:01 +0000113 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200114typedef struct mbedtls_md_context_t
115{
Hanno Beckerccb2b622019-09-03 13:19:14 +0100116#if !defined(MBEDTLS_MD_SINGLE_HASH)
Rose Zadik64feefb2018-01-25 22:01:10 +0000117 /** Information about the associated message digest. */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100118 mbedtls_md_handle_t md_info;
Hanno Beckerccb2b622019-09-03 13:19:14 +0100119#endif
Paul Bakker17373852011-01-06 14:20:01 +0000120
Rose Zadik64feefb2018-01-25 22:01:10 +0000121 /** The digest-specific context. */
Paul Bakker17373852011-01-06 14:20:01 +0000122 void *md_ctx;
Manuel Pégourié-Gonnard8379a822015-03-24 16:48:22 +0100123
Rose Zadik64feefb2018-01-25 22:01:10 +0000124 /** The HMAC part of the context. */
Manuel Pégourié-Gonnarddfb3dc82015-03-25 11:49:07 +0100125 void *hmac_ctx;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126} mbedtls_md_context_t;
Paul Bakker17373852011-01-06 14:20:01 +0000127
Hanno Beckerccb2b622019-09-03 13:19:14 +0100128#if !defined(MBEDTLS_MD_SINGLE_HASH)
Hanno Beckerd3827c72019-09-03 12:56:37 +0100129static inline mbedtls_md_handle_t mbedtls_md_get_handle(
130 struct mbedtls_md_context_t const *ctx )
131{
132 return( ctx->md_info );
133}
Hanno Beckerccb2b622019-09-03 13:19:14 +0100134#else /* !MBEDTLS_MD_SINGLE_HASH */
135static inline mbedtls_md_handle_t mbedtls_md_get_handle(
136 struct mbedtls_md_context_t const *ctx )
137{
138 ((void) ctx);
139 return( MBEDTLS_MD_UNIQUE_VALID_HANDLE );
140}
141#endif /* !MBEDTLS_MD_SINGLE_HASH */
Hanno Beckerd3827c72019-09-03 12:56:37 +0100142
Hanno Beckerc4e42102019-09-04 12:43:22 +0100143#include "md_internal.h"
144
Paul Bakker17373852011-01-06 14:20:01 +0000145/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000146 * \brief This function returns the list of digests supported by the
147 * generic digest module.
Paul Bakker72f62662011-01-16 21:27:44 +0000148 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000149 * \return A statically allocated array of digests. Each element
150 * in the returned list is an integer belonging to the
151 * message-digest enumeration #mbedtls_md_type_t.
152 * The last entry is 0.
Paul Bakker72f62662011-01-16 21:27:44 +0000153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154const int *mbedtls_md_list( void );
Paul Bakker72f62662011-01-16 21:27:44 +0000155
156/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000157 * \brief This function returns the message-digest information
158 * associated with the given digest name.
Paul Bakker17373852011-01-06 14:20:01 +0000159 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000160 * \param md_name The name of the digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000161 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100162 * \return The message-digest information associated with \p md_name.
163 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000164 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100165mbedtls_md_handle_t mbedtls_md_info_from_string( const char *md_name );
Paul Bakker17373852011-01-06 14:20:01 +0000166
167/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000168 * \brief This function returns the message-digest information
169 * associated with the given digest type.
Paul Bakker17373852011-01-06 14:20:01 +0000170 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000171 * \param md_type The type of digest to search for.
Paul Bakker17373852011-01-06 14:20:01 +0000172 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100173 * \return The message-digest information associated with \p md_type.
174 * \return NULL if the associated message-digest information is not found.
Paul Bakker17373852011-01-06 14:20:01 +0000175 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100176mbedtls_md_handle_t mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
Paul Bakker17373852011-01-06 14:20:01 +0000177
178/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000179 * \brief This function initializes a message-digest context without
180 * binding it to a particular message-digest algorithm.
181 *
182 * This function should always be called first. It prepares the
183 * context for mbedtls_md_setup() for binding it to a
184 * message-digest algorithm.
Paul Bakker84bbeb52014-07-01 14:53:22 +0200185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186void mbedtls_md_init( mbedtls_md_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200187
188/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000189 * \brief This function clears the internal structure of \p ctx and
190 * frees any embedded internal structure, but does not free
191 * \p ctx itself.
192 *
193 * If you have called mbedtls_md_setup() on \p ctx, you must
194 * call mbedtls_md_free() when you are no longer using the
195 * context.
196 * Calling this function if you have previously
197 * called mbedtls_md_init() and nothing else is optional.
198 * You must not call this function if you have not called
199 * mbedtls_md_init().
Paul Bakker84bbeb52014-07-01 14:53:22 +0200200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201void mbedtls_md_free( mbedtls_md_context_t *ctx );
Paul Bakker84bbeb52014-07-01 14:53:22 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
204#if defined(MBEDTLS_DEPRECATED_WARNING)
205#define MBEDTLS_DEPRECATED __attribute__((deprecated))
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100206#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#define MBEDTLS_DEPRECATED
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100208#endif
209/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000210 * \brief This function selects the message digest algorithm to use,
211 * and allocates internal structures.
212 *
213 * It should be called after mbedtls_md_init() or mbedtls_md_free().
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 * Makes it necessary to call mbedtls_md_free() later.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100215 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 * \deprecated Superseded by mbedtls_md_setup() in 2.0.0
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100217 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000218 * \param ctx The context to set up.
219 * \param md_info The information structure of the message-digest algorithm
220 * to use.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100221 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100222 * \return \c 0 on success.
223 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
224 * failure.
225 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100226 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100227int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, mbedtls_md_handle_t md_info ) MBEDTLS_DEPRECATED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#undef MBEDTLS_DEPRECATED
229#endif /* MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard147fa092015-03-25 16:43:14 +0100230
Paul Bakker84bbeb52014-07-01 14:53:22 +0200231/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000232 * \brief This function selects the message digest algorithm to use,
233 * and allocates internal structures.
Paul Bakker562535d2011-01-20 16:42:01 +0000234 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000235 * It should be called after mbedtls_md_init() or
236 * mbedtls_md_free(). Makes it necessary to call
237 * mbedtls_md_free() later.
238 *
239 * \param ctx The context to set up.
240 * \param md_info The information structure of the message-digest algorithm
241 * to use.
Rose Zadik8c9c7942018-03-27 11:52:58 +0100242 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
243 * or non-zero: HMAC is used with this context.
Paul Bakker562535d2011-01-20 16:42:01 +0000244 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100245 * \return \c 0 on success.
246 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
247 * failure.
248 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
Paul Bakker562535d2011-01-20 16:42:01 +0000249 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100250int mbedtls_md_setup( mbedtls_md_context_t *ctx, mbedtls_md_handle_t md_info, int hmac );
Paul Bakker562535d2011-01-20 16:42:01 +0000251
252/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000253 * \brief This function clones the state of an message-digest
254 * context.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200255 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000256 * \note You must call mbedtls_md_setup() on \c dst before calling
257 * this function.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200258 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000259 * \note The two contexts must have the same type,
260 * for example, both are SHA-256.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200261 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000262 * \warning This function clones the message-digest state, not the
263 * HMAC state.
264 *
265 * \param dst The destination context.
266 * \param src The context to be cloned.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200267 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100268 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100269 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
Manuel Pégourié-Gonnard052a6c92015-07-06 16:06:02 +0200270 */
271int mbedtls_md_clone( mbedtls_md_context_t *dst,
272 const mbedtls_md_context_t *src );
273
274/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000275 * \brief This function extracts the message-digest size from the
276 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000277 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000278 * \param md_info The information structure of the message-digest algorithm
279 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000280 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000281 * \return The size of the message-digest output in Bytes.
Paul Bakker17373852011-01-06 14:20:01 +0000282 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100283unsigned char mbedtls_md_get_size( mbedtls_md_handle_t md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000284
285/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000286 * \brief This function extracts the message-digest type from the
287 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000288 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000289 * \param md_info The information structure of the message-digest algorithm
290 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000291 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000292 * \return The type of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000293 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100294mbedtls_md_type_t mbedtls_md_get_type( mbedtls_md_handle_t md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000295
296/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000297 * \brief This function extracts the message-digest name from the
298 * message-digest information structure.
Paul Bakker17373852011-01-06 14:20:01 +0000299 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000300 * \param md_info The information structure of the message-digest algorithm
301 * to use.
Paul Bakker17373852011-01-06 14:20:01 +0000302 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000303 * \return The name of the message digest.
Paul Bakker17373852011-01-06 14:20:01 +0000304 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100305const char *mbedtls_md_get_name( mbedtls_md_handle_t md_info );
Paul Bakker17373852011-01-06 14:20:01 +0000306
307/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000308 * \brief This function starts a message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000309 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000310 * You must call this function after setting up the context
311 * with mbedtls_md_setup(), and before passing data with
312 * mbedtls_md_update().
Paul Bakker17373852011-01-06 14:20:01 +0000313 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000314 * \param ctx The generic message-digest context.
315 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100316 * \return \c 0 on success.
317 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
318 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000319 */
Hanno Becker527f7c92019-09-04 12:46:07 +0100320MBEDTLS_MD_INLINABLE_API int mbedtls_md_starts( mbedtls_md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000321
322/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000323 * \brief This function feeds an input buffer into an ongoing
324 * message-digest computation.
Paul Bakker17373852011-01-06 14:20:01 +0000325 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000326 * You must call mbedtls_md_starts() before calling this
327 * function. You may call this function multiple times.
328 * Afterwards, call mbedtls_md_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000329 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000330 * \param ctx The generic message-digest context.
331 * \param input The buffer holding the input data.
332 * \param ilen The length of the input data.
333 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100334 * \return \c 0 on success.
335 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
336 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000337 */
Hanno Beckerfdef5ac2019-09-04 13:20:05 +0100338MBEDTLS_MD_INLINABLE_API int mbedtls_md_update( mbedtls_md_context_t *ctx,
339 const unsigned char *input,
340 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000341
342/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000343 * \brief This function finishes the digest operation,
344 * and writes the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000345 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000346 * Call this function after a call to mbedtls_md_starts(),
347 * followed by any number of calls to mbedtls_md_update().
348 * Afterwards, you may either clear the context with
349 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
350 * the context for another digest operation with the same
351 * algorithm.
Paul Bakker17373852011-01-06 14:20:01 +0000352 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000353 * \param ctx The generic message-digest context.
354 * \param output The buffer for the generic message-digest checksum result.
355 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100356 * \return \c 0 on success.
357 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
358 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000359 */
Hanno Becker993691d2019-09-04 13:24:44 +0100360MBEDTLS_MD_INLINABLE_API int mbedtls_md_finish( mbedtls_md_context_t *ctx,
361 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000362
363/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000364 * \brief This function calculates the message-digest of a buffer,
365 * with respect to a configurable message-digest algorithm
366 * in a single call.
Paul Bakker17373852011-01-06 14:20:01 +0000367 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000368 * The result is calculated as
369 * Output = message_digest(input buffer).
Paul Bakker17373852011-01-06 14:20:01 +0000370 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000371 * \param md_info The information structure of the message-digest algorithm
372 * to use.
373 * \param input The buffer holding the data.
374 * \param ilen The length of the input data.
375 * \param output The generic message-digest checksum result.
376 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100377 * \return \c 0 on success.
378 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
379 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000380 */
Hanno Becker993691d2019-09-04 13:24:44 +0100381MBEDTLS_MD_INLINABLE_API int mbedtls_md(
382 mbedtls_md_handle_t md_info,
383 const unsigned char *input,
384 size_t ilen,
385 unsigned char *output );
Paul Bakker17373852011-01-06 14:20:01 +0000386
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200387#if defined(MBEDTLS_FS_IO)
Paul Bakker17373852011-01-06 14:20:01 +0000388/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000389 * \brief This function calculates the message-digest checksum
390 * result of the contents of the provided file.
Paul Bakker17373852011-01-06 14:20:01 +0000391 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000392 * The result is calculated as
393 * Output = message_digest(file contents).
Paul Bakker17373852011-01-06 14:20:01 +0000394 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000395 * \param md_info The information structure of the message-digest algorithm
396 * to use.
397 * \param path The input file name.
398 * \param output The generic message-digest checksum result.
399 *
Rose Zadik8c9c7942018-03-27 11:52:58 +0100400 * \return \c 0 on success.
Rose Zadikf3e47362018-04-16 16:31:16 +0100401 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
402 * the file pointed by \p path.
403 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
Paul Bakker17373852011-01-06 14:20:01 +0000404 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100405int mbedtls_md_file( mbedtls_md_handle_t md_info, const char *path,
Manuel Pégourié-Gonnardbfffa902015-05-28 14:44:00 +0200406 unsigned char *output );
407#endif /* MBEDTLS_FS_IO */
Paul Bakker17373852011-01-06 14:20:01 +0000408
409/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000410 * \brief This function sets the HMAC key and prepares to
411 * authenticate a new message.
Paul Bakker17373852011-01-06 14:20:01 +0000412 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000413 * Call this function after mbedtls_md_setup(), to use
414 * the MD context for an HMAC calculation, then call
415 * mbedtls_md_hmac_update() to provide the input data, and
416 * mbedtls_md_hmac_finish() to get the HMAC value.
Paul Bakker17373852011-01-06 14:20:01 +0000417 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000418 * \param ctx The message digest context containing an embedded HMAC
419 * context.
420 * \param key The HMAC secret key.
421 * \param keylen The length of the HMAC key in Bytes.
422 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100423 * \return \c 0 on success.
424 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
425 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200428 size_t keylen );
Paul Bakker17373852011-01-06 14:20:01 +0000429
430/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000431 * \brief This function feeds an input buffer into an ongoing HMAC
432 * computation.
Paul Bakker17373852011-01-06 14:20:01 +0000433 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000434 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
435 * before calling this function.
436 * You may call this function multiple times to pass the
437 * input piecewise.
438 * Afterwards, call mbedtls_md_hmac_finish().
Paul Bakker17373852011-01-06 14:20:01 +0000439 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000440 * \param ctx The message digest context containing an embedded HMAC
441 * context.
442 * \param input The buffer holding the input data.
443 * \param ilen The length of the input data.
444 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100445 * \return \c 0 on success.
446 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
447 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200450 size_t ilen );
Paul Bakker17373852011-01-06 14:20:01 +0000451
452/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000453 * \brief This function finishes the HMAC operation, and writes
454 * the result to the output buffer.
Paul Bakker17373852011-01-06 14:20:01 +0000455 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000456 * Call this function after mbedtls_md_hmac_starts() and
457 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
458 * you may either call mbedtls_md_free() to clear the context,
459 * or call mbedtls_md_hmac_reset() to reuse the context with
460 * the same HMAC key.
Paul Bakker17373852011-01-06 14:20:01 +0000461 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000462 * \param ctx The message digest context containing an embedded HMAC
463 * context.
464 * \param output The generic HMAC checksum result.
465 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100466 * \return \c 0 on success.
467 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
468 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
Paul Bakker17373852011-01-06 14:20:01 +0000471
472/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000473 * \brief This function prepares to authenticate a new message with
474 * the same key as the previous HMAC operation.
Paul Bakker17373852011-01-06 14:20:01 +0000475 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000476 * You may call this function after mbedtls_md_hmac_finish().
477 * Afterwards call mbedtls_md_hmac_update() to pass the new
478 * input.
Paul Bakker17373852011-01-06 14:20:01 +0000479 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000480 * \param ctx The message digest context containing an embedded HMAC
481 * context.
482 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100483 * \return \c 0 on success.
484 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
485 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
Paul Bakker17373852011-01-06 14:20:01 +0000488
489/**
Rose Zadik64feefb2018-01-25 22:01:10 +0000490 * \brief This function calculates the full generic HMAC
491 * on the input buffer with the provided key.
Paul Bakker17373852011-01-06 14:20:01 +0000492 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000493 * The function allocates the context, performs the
494 * calculation, and frees the context.
Paul Bakker17373852011-01-06 14:20:01 +0000495 *
Rose Zadik64feefb2018-01-25 22:01:10 +0000496 * The HMAC result is calculated as
497 * output = generic HMAC(hmac key, input buffer).
498 *
499 * \param md_info The information structure of the message-digest algorithm
500 * to use.
501 * \param key The HMAC secret key.
502 * \param keylen The length of the HMAC secret key in Bytes.
503 * \param input The buffer holding the input data.
504 * \param ilen The length of the input data.
505 * \param output The generic HMAC result.
506 *
Rose Zadikf3e47362018-04-16 16:31:16 +0100507 * \return \c 0 on success.
508 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
509 * failure.
Paul Bakker17373852011-01-06 14:20:01 +0000510 */
Hanno Beckera5cedbc2019-07-17 11:21:02 +0100511int mbedtls_md_hmac( mbedtls_md_handle_t md_info, const unsigned char *key, size_t keylen,
Paul Bakker23986e52011-04-24 08:57:21 +0000512 const unsigned char *input, size_t ilen,
Paul Bakker17373852011-01-06 14:20:01 +0000513 unsigned char *output );
514
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100515/* Internal use */
Hanno Becker53ade9f2019-09-04 13:44:51 +0100516MBEDTLS_MD_INLINABLE_API int mbedtls_md_process( mbedtls_md_context_t *ctx,
517 const unsigned char *data );
Paul Bakker1bd3ae82013-03-13 10:26:44 +0100518
Hanno Becker527f7c92019-09-04 12:46:07 +0100519/*
520 * Internal wrapper functions for those MD API functions which should be
521 * inlined in some but not all configurations. The actual MD API will be
522 * implemented either here or in md.c, and forward to the wrappers.
523 */
524
525MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_starts_internal(
526 mbedtls_md_context_t *ctx )
527{
528 mbedtls_md_handle_t md_info;
529 if( ctx == NULL )
530 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
531
532 md_info = mbedtls_md_get_handle( ctx );
533 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
534 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
535
536 return( mbedtls_md_info_starts( md_info, ctx->md_ctx ) );
537}
538
Hanno Beckerfdef5ac2019-09-04 13:20:05 +0100539MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_update_internal(
540 mbedtls_md_context_t *ctx,
541 const unsigned char *input,
542 size_t ilen )
543{
544 mbedtls_md_handle_t md_info;
545 if( ctx == NULL )
546 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
547
548 md_info = mbedtls_md_get_handle( ctx );
549 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
550 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
551
552 return( mbedtls_md_info_update( md_info, ctx->md_ctx,
553 input, ilen ) );
554}
555
Hanno Becker993691d2019-09-04 13:24:44 +0100556MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_finish_internal(
557 mbedtls_md_context_t *ctx, unsigned char *output )
558{
559 mbedtls_md_handle_t md_info;
560 if( ctx == NULL )
561 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
562
563 md_info = mbedtls_md_get_handle( ctx );
564 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
565 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
566
567 return( mbedtls_md_info_finish( md_info, ctx->md_ctx,
568 output ) );
569}
570
571MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_internal(
572 mbedtls_md_handle_t md_info,
573 const unsigned char *input,
574 size_t ilen,
575 unsigned char *output )
576{
577 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
578 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
579
580 return( mbedtls_md_info_digest( md_info, input,
581 ilen, output) );
582}
583
Hanno Becker53ade9f2019-09-04 13:44:51 +0100584MBEDTLS_ALWAYS_INLINE static inline int mbedtls_md_process_internal(
585 mbedtls_md_context_t *ctx, const unsigned char *data )
586{
587 mbedtls_md_handle_t md_info;
588 if( ctx == NULL )
589 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
590
591 md_info = mbedtls_md_get_handle( ctx );
592 if( md_info == MBEDTLS_MD_INVALID_HANDLE )
593 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
594
595 return( mbedtls_md_info_process( md_info, ctx->md_ctx, data ) );
596}
597
Hanno Becker527f7c92019-09-04 12:46:07 +0100598#if defined(MBEDTLS_MD_SINGLE_HASH)
599MBEDTLS_MD_INLINABLE_API int mbedtls_md_starts(
600 mbedtls_md_context_t *ctx )
601{
602 return( mbedtls_md_starts_internal( ctx ) );
603}
Hanno Beckerfdef5ac2019-09-04 13:20:05 +0100604
605MBEDTLS_MD_INLINABLE_API int mbedtls_md_update(
606 mbedtls_md_context_t *ctx,
607 const unsigned char *input,
608 size_t ilen )
609{
610 return( mbedtls_md_update_internal( ctx, input, ilen ) );
611}
Hanno Becker993691d2019-09-04 13:24:44 +0100612
613MBEDTLS_MD_INLINABLE_API int mbedtls_md_finish(
614 mbedtls_md_context_t *ctx, unsigned char *output )
615{
616 return( mbedtls_md_finish_internal( ctx, output ) );
617}
618
619MBEDTLS_MD_INLINABLE_API int mbedtls_md(
620 mbedtls_md_handle_t md_info,
621 const unsigned char *input,
622 size_t ilen,
623 unsigned char *output )
624{
625 return( mbedtls_md_internal( md_info, input, ilen, output ) );
626}
627
Hanno Becker53ade9f2019-09-04 13:44:51 +0100628MBEDTLS_MD_INLINABLE_API int mbedtls_md_process(
629 mbedtls_md_context_t *ctx, const unsigned char *data )
630{
631 return( mbedtls_md_process_internal( ctx, data ) );
632}
633
Hanno Becker527f7c92019-09-04 12:46:07 +0100634#endif /* MBEDTLS_MD_SINGLE_HASH */
635
Paul Bakker17373852011-01-06 14:20:01 +0000636#ifdef __cplusplus
637}
638#endif
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640#endif /* MBEDTLS_MD_H */