4 * \brief This file contains the generic message-digest wrapper.
\r
6 * \author Adriaan de Jong <dejong@fox-it.com>
\r
9 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
\r
10 * SPDX-License-Identifier: Apache-2.0
\r
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
\r
13 * not use this file except in compliance with the License.
\r
14 * You may obtain a copy of the License at
\r
16 * http://www.apache.org/licenses/LICENSE-2.0
\r
18 * Unless required by applicable law or agreed to in writing, software
\r
19 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
\r
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
21 * See the License for the specific language governing permissions and
\r
22 * limitations under the License.
\r
24 * This file is part of Mbed TLS (https://tls.mbed.org)
\r
27 #ifndef MBEDTLS_MD_H
\r
28 #define MBEDTLS_MD_H
\r
32 #if !defined(MBEDTLS_CONFIG_FILE)
\r
35 #include MBEDTLS_CONFIG_FILE
\r
38 #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */
\r
39 #define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */
\r
40 #define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */
\r
41 #define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */
\r
43 /* MBEDTLS_ERR_MD_HW_ACCEL_FAILED is deprecated and should not be used. */
\r
44 #define MBEDTLS_ERR_MD_HW_ACCEL_FAILED -0x5280 /**< MD hardware accelerator failed. */
\r
51 * \brief Supported message digests.
\r
53 * \warning MD2, MD4, MD5 and SHA-1 are considered weak message digests and
\r
54 * their use constitutes a security risk. We recommend considering
\r
55 * stronger message digests instead.
\r
59 MBEDTLS_MD_NONE=0, /**< None. */
\r
60 MBEDTLS_MD_MD2, /**< The MD2 message digest. */
\r
61 MBEDTLS_MD_MD4, /**< The MD4 message digest. */
\r
62 MBEDTLS_MD_MD5, /**< The MD5 message digest. */
\r
63 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */
\r
64 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */
\r
65 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */
\r
66 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */
\r
67 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */
\r
68 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */
\r
69 } mbedtls_md_type_t;
\r
71 #if defined(MBEDTLS_SHA512_C)
\r
72 #define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */
\r
74 #define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */
\r
77 #if defined(MBEDTLS_SHA512_C)
\r
78 #define MBEDTLS_MD_MAX_BLOCK_SIZE 128
\r
80 #define MBEDTLS_MD_MAX_BLOCK_SIZE 64
\r
84 * Opaque struct defined in md_internal.h.
\r
86 typedef struct mbedtls_md_info_t mbedtls_md_info_t;
\r
89 * The generic message-digest context.
\r
91 typedef struct mbedtls_md_context_t
\r
93 /** Information about the associated message digest. */
\r
94 const mbedtls_md_info_t *md_info;
\r
96 /** The digest-specific context. */
\r
99 /** The HMAC part of the context. */
\r
101 } mbedtls_md_context_t;
\r
104 * \brief This function returns the list of digests supported by the
\r
105 * generic digest module.
\r
107 * \return A statically allocated array of digests. Each element
\r
108 * in the returned list is an integer belonging to the
\r
109 * message-digest enumeration #mbedtls_md_type_t.
\r
110 * The last entry is 0.
\r
112 const int *mbedtls_md_list( void );
\r
115 * \brief This function returns the message-digest information
\r
116 * associated with the given digest name.
\r
118 * \param md_name The name of the digest to search for.
\r
120 * \return The message-digest information associated with \p md_name.
\r
121 * \return NULL if the associated message-digest information is not found.
\r
123 const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name );
\r
126 * \brief This function returns the message-digest information
\r
127 * associated with the given digest type.
\r
129 * \param md_type The type of digest to search for.
\r
131 * \return The message-digest information associated with \p md_type.
\r
132 * \return NULL if the associated message-digest information is not found.
\r
134 const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type );
\r
137 * \brief This function initializes a message-digest context without
\r
138 * binding it to a particular message-digest algorithm.
\r
140 * This function should always be called first. It prepares the
\r
141 * context for mbedtls_md_setup() for binding it to a
\r
142 * message-digest algorithm.
\r
144 void mbedtls_md_init( mbedtls_md_context_t *ctx );
\r
147 * \brief This function clears the internal structure of \p ctx and
\r
148 * frees any embedded internal structure, but does not free
\r
151 * If you have called mbedtls_md_setup() on \p ctx, you must
\r
152 * call mbedtls_md_free() when you are no longer using the
\r
154 * Calling this function if you have previously
\r
155 * called mbedtls_md_init() and nothing else is optional.
\r
156 * You must not call this function if you have not called
\r
157 * mbedtls_md_init().
\r
159 void mbedtls_md_free( mbedtls_md_context_t *ctx );
\r
161 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
\r
162 #if defined(MBEDTLS_DEPRECATED_WARNING)
\r
163 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
\r
165 #define MBEDTLS_DEPRECATED
\r
168 * \brief This function selects the message digest algorithm to use,
\r
169 * and allocates internal structures.
\r
171 * It should be called after mbedtls_md_init() or mbedtls_md_free().
\r
172 * Makes it necessary to call mbedtls_md_free() later.
\r
174 * \deprecated Superseded by mbedtls_md_setup() in 2.0.0
\r
176 * \param ctx The context to set up.
\r
177 * \param md_info The information structure of the message-digest algorithm
\r
180 * \return \c 0 on success.
\r
181 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
\r
183 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
\r
185 int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED;
\r
186 #undef MBEDTLS_DEPRECATED
\r
187 #endif /* MBEDTLS_DEPRECATED_REMOVED */
\r
190 * \brief This function selects the message digest algorithm to use,
\r
191 * and allocates internal structures.
\r
193 * It should be called after mbedtls_md_init() or
\r
194 * mbedtls_md_free(). Makes it necessary to call
\r
195 * mbedtls_md_free() later.
\r
197 * \param ctx The context to set up.
\r
198 * \param md_info The information structure of the message-digest algorithm
\r
200 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory),
\r
201 * or non-zero: HMAC is used with this context.
\r
203 * \return \c 0 on success.
\r
204 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
\r
206 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure.
\r
208 int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac );
\r
211 * \brief This function clones the state of an message-digest
\r
214 * \note You must call mbedtls_md_setup() on \c dst before calling
\r
217 * \note The two contexts must have the same type,
\r
218 * for example, both are SHA-256.
\r
220 * \warning This function clones the message-digest state, not the
\r
223 * \param dst The destination context.
\r
224 * \param src The context to be cloned.
\r
226 * \return \c 0 on success.
\r
227 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure.
\r
229 int mbedtls_md_clone( mbedtls_md_context_t *dst,
\r
230 const mbedtls_md_context_t *src );
\r
233 * \brief This function extracts the message-digest size from the
\r
234 * message-digest information structure.
\r
236 * \param md_info The information structure of the message-digest algorithm
\r
239 * \return The size of the message-digest output in Bytes.
\r
241 unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info );
\r
244 * \brief This function extracts the message-digest type from the
\r
245 * message-digest information structure.
\r
247 * \param md_info The information structure of the message-digest algorithm
\r
250 * \return The type of the message digest.
\r
252 mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info );
\r
255 * \brief This function extracts the message-digest name from the
\r
256 * message-digest information structure.
\r
258 * \param md_info The information structure of the message-digest algorithm
\r
261 * \return The name of the message digest.
\r
263 const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info );
\r
266 * \brief This function starts a message-digest computation.
\r
268 * You must call this function after setting up the context
\r
269 * with mbedtls_md_setup(), and before passing data with
\r
270 * mbedtls_md_update().
\r
272 * \param ctx The generic message-digest context.
\r
274 * \return \c 0 on success.
\r
275 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
\r
278 int mbedtls_md_starts( mbedtls_md_context_t *ctx );
\r
281 * \brief This function feeds an input buffer into an ongoing
\r
282 * message-digest computation.
\r
284 * You must call mbedtls_md_starts() before calling this
\r
285 * function. You may call this function multiple times.
\r
286 * Afterwards, call mbedtls_md_finish().
\r
288 * \param ctx The generic message-digest context.
\r
289 * \param input The buffer holding the input data.
\r
290 * \param ilen The length of the input data.
\r
292 * \return \c 0 on success.
\r
293 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
\r
296 int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen );
\r
299 * \brief This function finishes the digest operation,
\r
300 * and writes the result to the output buffer.
\r
302 * Call this function after a call to mbedtls_md_starts(),
\r
303 * followed by any number of calls to mbedtls_md_update().
\r
304 * Afterwards, you may either clear the context with
\r
305 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse
\r
306 * the context for another digest operation with the same
\r
309 * \param ctx The generic message-digest context.
\r
310 * \param output The buffer for the generic message-digest checksum result.
\r
312 * \return \c 0 on success.
\r
313 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
\r
316 int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output );
\r
319 * \brief This function calculates the message-digest of a buffer,
\r
320 * with respect to a configurable message-digest algorithm
\r
321 * in a single call.
\r
323 * The result is calculated as
\r
324 * Output = message_digest(input buffer).
\r
326 * \param md_info The information structure of the message-digest algorithm
\r
328 * \param input The buffer holding the data.
\r
329 * \param ilen The length of the input data.
\r
330 * \param output The generic message-digest checksum result.
\r
332 * \return \c 0 on success.
\r
333 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
\r
336 int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen,
\r
337 unsigned char *output );
\r
339 #if defined(MBEDTLS_FS_IO)
\r
341 * \brief This function calculates the message-digest checksum
\r
342 * result of the contents of the provided file.
\r
344 * The result is calculated as
\r
345 * Output = message_digest(file contents).
\r
347 * \param md_info The information structure of the message-digest algorithm
\r
349 * \param path The input file name.
\r
350 * \param output The generic message-digest checksum result.
\r
352 * \return \c 0 on success.
\r
353 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing
\r
354 * the file pointed by \p path.
\r
355 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL.
\r
357 int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path,
\r
358 unsigned char *output );
\r
359 #endif /* MBEDTLS_FS_IO */
\r
362 * \brief This function sets the HMAC key and prepares to
\r
363 * authenticate a new message.
\r
365 * Call this function after mbedtls_md_setup(), to use
\r
366 * the MD context for an HMAC calculation, then call
\r
367 * mbedtls_md_hmac_update() to provide the input data, and
\r
368 * mbedtls_md_hmac_finish() to get the HMAC value.
\r
370 * \param ctx The message digest context containing an embedded HMAC
\r
372 * \param key The HMAC secret key.
\r
373 * \param keylen The length of the HMAC key in Bytes.
\r
375 * \return \c 0 on success.
\r
376 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
\r
379 int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key,
\r
383 * \brief This function feeds an input buffer into an ongoing HMAC
\r
386 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset()
\r
387 * before calling this function.
\r
388 * You may call this function multiple times to pass the
\r
390 * Afterwards, call mbedtls_md_hmac_finish().
\r
392 * \param ctx The message digest context containing an embedded HMAC
\r
394 * \param input The buffer holding the input data.
\r
395 * \param ilen The length of the input data.
\r
397 * \return \c 0 on success.
\r
398 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
\r
401 int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input,
\r
405 * \brief This function finishes the HMAC operation, and writes
\r
406 * the result to the output buffer.
\r
408 * Call this function after mbedtls_md_hmac_starts() and
\r
409 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards
\r
410 * you may either call mbedtls_md_free() to clear the context,
\r
411 * or call mbedtls_md_hmac_reset() to reuse the context with
\r
412 * the same HMAC key.
\r
414 * \param ctx The message digest context containing an embedded HMAC
\r
416 * \param output The generic HMAC checksum result.
\r
418 * \return \c 0 on success.
\r
419 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
\r
422 int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output);
\r
425 * \brief This function prepares to authenticate a new message with
\r
426 * the same key as the previous HMAC operation.
\r
428 * You may call this function after mbedtls_md_hmac_finish().
\r
429 * Afterwards call mbedtls_md_hmac_update() to pass the new
\r
432 * \param ctx The message digest context containing an embedded HMAC
\r
435 * \return \c 0 on success.
\r
436 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
\r
439 int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx );
\r
442 * \brief This function calculates the full generic HMAC
\r
443 * on the input buffer with the provided key.
\r
445 * The function allocates the context, performs the
\r
446 * calculation, and frees the context.
\r
448 * The HMAC result is calculated as
\r
449 * output = generic HMAC(hmac key, input buffer).
\r
451 * \param md_info The information structure of the message-digest algorithm
\r
453 * \param key The HMAC secret key.
\r
454 * \param keylen The length of the HMAC secret key in Bytes.
\r
455 * \param input The buffer holding the input data.
\r
456 * \param ilen The length of the input data.
\r
457 * \param output The generic HMAC result.
\r
459 * \return \c 0 on success.
\r
460 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification
\r
463 int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen,
\r
464 const unsigned char *input, size_t ilen,
\r
465 unsigned char *output );
\r
468 int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data );
\r
474 #endif /* MBEDTLS_MD_H */
\r