Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
md.h
00001 /** 00002 * \file md.h 00003 * 00004 * \brief This file contains the generic message-digest wrapper. 00005 * 00006 * \author Adriaan de Jong <dejong@fox-it.com> 00007 */ 00008 /* 00009 * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved 00010 * SPDX-License-Identifier: Apache-2.0 00011 * 00012 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00013 * not use this file except in compliance with the License. 00014 * You may obtain a copy of the License at 00015 * 00016 * http://www.apache.org/licenses/LICENSE-2.0 00017 * 00018 * Unless required by applicable law or agreed to in writing, software 00019 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00020 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00021 * See the License for the specific language governing permissions and 00022 * limitations under the License. 00023 * 00024 * This file is part of Mbed TLS (https://tls.mbed.org) 00025 */ 00026 00027 #ifndef MBEDTLS_MD_H 00028 #define MBEDTLS_MD_H 00029 00030 #include <stddef.h> 00031 00032 #if !defined(MBEDTLS_CONFIG_FILE) 00033 #include "config.h" 00034 #else 00035 #include MBEDTLS_CONFIG_FILE 00036 #endif 00037 00038 #define MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */ 00039 #define MBEDTLS_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */ 00040 #define MBEDTLS_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */ 00041 #define MBEDTLS_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */ 00042 #define MBEDTLS_ERR_MD_HW_ACCEL_FAILED -0x5280 /**< MD hardware accelerator failed. */ 00043 00044 #ifdef __cplusplus 00045 extern "C" { 00046 #endif 00047 00048 /** 00049 * \brief Supported message digests. 00050 * 00051 * \warning MD2, MD4, MD5 and SHA-1 are considered weak message digests and 00052 * their use constitutes a security risk. We recommend considering 00053 * stronger message digests instead. 00054 * 00055 */ 00056 typedef enum { 00057 MBEDTLS_MD_NONE=0, /**< None. */ 00058 MBEDTLS_MD_MD2, /**< The MD2 message digest. */ 00059 MBEDTLS_MD_MD4, /**< The MD4 message digest. */ 00060 MBEDTLS_MD_MD5, /**< The MD5 message digest. */ 00061 MBEDTLS_MD_SHA1, /**< The SHA-1 message digest. */ 00062 MBEDTLS_MD_SHA224, /**< The SHA-224 message digest. */ 00063 MBEDTLS_MD_SHA256, /**< The SHA-256 message digest. */ 00064 MBEDTLS_MD_SHA384, /**< The SHA-384 message digest. */ 00065 MBEDTLS_MD_SHA512, /**< The SHA-512 message digest. */ 00066 MBEDTLS_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */ 00067 } mbedtls_md_type_t; 00068 00069 #if defined(MBEDTLS_SHA512_C) 00070 #define MBEDTLS_MD_MAX_SIZE 64 /* longest known is SHA512 */ 00071 #else 00072 #define MBEDTLS_MD_MAX_SIZE 32 /* longest known is SHA256 or less */ 00073 #endif 00074 00075 /** 00076 * Opaque struct defined in md_internal.h. 00077 */ 00078 typedef struct mbedtls_md_info_t mbedtls_md_info_t; 00079 00080 /** 00081 * The generic message-digest context. 00082 */ 00083 typedef struct { 00084 /** Information about the associated message digest. */ 00085 const mbedtls_md_info_t *md_info; 00086 00087 /** The digest-specific context. */ 00088 void *md_ctx; 00089 00090 /** The HMAC part of the context. */ 00091 void *hmac_ctx; 00092 } mbedtls_md_context_t; 00093 00094 /** 00095 * \brief This function returns the list of digests supported by the 00096 * generic digest module. 00097 * 00098 * \return A statically allocated array of digests. Each element 00099 * in the returned list is an integer belonging to the 00100 * message-digest enumeration #mbedtls_md_type_t. 00101 * The last entry is 0. 00102 */ 00103 const int *mbedtls_md_list( void ); 00104 00105 /** 00106 * \brief This function returns the message-digest information 00107 * associated with the given digest name. 00108 * 00109 * \param md_name The name of the digest to search for. 00110 * 00111 * \return The message-digest information associated with \p md_name. 00112 * \return NULL if the associated message-digest information is not found. 00113 */ 00114 const mbedtls_md_info_t *mbedtls_md_info_from_string( const char *md_name ); 00115 00116 /** 00117 * \brief This function returns the message-digest information 00118 * associated with the given digest type. 00119 * 00120 * \param md_type The type of digest to search for. 00121 * 00122 * \return The message-digest information associated with \p md_type. 00123 * \return NULL if the associated message-digest information is not found. 00124 */ 00125 const mbedtls_md_info_t *mbedtls_md_info_from_type( mbedtls_md_type_t md_type ); 00126 00127 /** 00128 * \brief This function initializes a message-digest context without 00129 * binding it to a particular message-digest algorithm. 00130 * 00131 * This function should always be called first. It prepares the 00132 * context for mbedtls_md_setup() for binding it to a 00133 * message-digest algorithm. 00134 */ 00135 void mbedtls_md_init( mbedtls_md_context_t *ctx ); 00136 00137 /** 00138 * \brief This function clears the internal structure of \p ctx and 00139 * frees any embedded internal structure, but does not free 00140 * \p ctx itself. 00141 * 00142 * If you have called mbedtls_md_setup() on \p ctx, you must 00143 * call mbedtls_md_free() when you are no longer using the 00144 * context. 00145 * Calling this function if you have previously 00146 * called mbedtls_md_init() and nothing else is optional. 00147 * You must not call this function if you have not called 00148 * mbedtls_md_init(). 00149 */ 00150 void mbedtls_md_free( mbedtls_md_context_t *ctx ); 00151 00152 #if ! defined(MBEDTLS_DEPRECATED_REMOVED) 00153 #if defined(MBEDTLS_DEPRECATED_WARNING) 00154 #define MBEDTLS_DEPRECATED __attribute__((deprecated)) 00155 #else 00156 #define MBEDTLS_DEPRECATED 00157 #endif 00158 /** 00159 * \brief This function selects the message digest algorithm to use, 00160 * and allocates internal structures. 00161 * 00162 * It should be called after mbedtls_md_init() or mbedtls_md_free(). 00163 * Makes it necessary to call mbedtls_md_free() later. 00164 * 00165 * \deprecated Superseded by mbedtls_md_setup() in 2.0.0 00166 * 00167 * \param ctx The context to set up. 00168 * \param md_info The information structure of the message-digest algorithm 00169 * to use. 00170 * 00171 * \return \c 0 on success. 00172 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification 00173 * failure. 00174 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure. 00175 */ 00176 int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED; 00177 #undef MBEDTLS_DEPRECATED 00178 #endif /* MBEDTLS_DEPRECATED_REMOVED */ 00179 00180 /** 00181 * \brief This function selects the message digest algorithm to use, 00182 * and allocates internal structures. 00183 * 00184 * It should be called after mbedtls_md_init() or 00185 * mbedtls_md_free(). Makes it necessary to call 00186 * mbedtls_md_free() later. 00187 * 00188 * \param ctx The context to set up. 00189 * \param md_info The information structure of the message-digest algorithm 00190 * to use. 00191 * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory), 00192 * or non-zero: HMAC is used with this context. 00193 * 00194 * \return \c 0 on success. 00195 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification 00196 * failure. 00197 * \return #MBEDTLS_ERR_MD_ALLOC_FAILED on memory-allocation failure. 00198 */ 00199 int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac ); 00200 00201 /** 00202 * \brief This function clones the state of an message-digest 00203 * context. 00204 * 00205 * \note You must call mbedtls_md_setup() on \c dst before calling 00206 * this function. 00207 * 00208 * \note The two contexts must have the same type, 00209 * for example, both are SHA-256. 00210 * 00211 * \warning This function clones the message-digest state, not the 00212 * HMAC state. 00213 * 00214 * \param dst The destination context. 00215 * \param src The context to be cloned. 00216 * 00217 * \return \c 0 on success. 00218 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification failure. 00219 */ 00220 int mbedtls_md_clone( mbedtls_md_context_t *dst, 00221 const mbedtls_md_context_t *src ); 00222 00223 /** 00224 * \brief This function extracts the message-digest size from the 00225 * message-digest information structure. 00226 * 00227 * \param md_info The information structure of the message-digest algorithm 00228 * to use. 00229 * 00230 * \return The size of the message-digest output in Bytes. 00231 */ 00232 unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info ); 00233 00234 /** 00235 * \brief This function extracts the message-digest type from the 00236 * message-digest information structure. 00237 * 00238 * \param md_info The information structure of the message-digest algorithm 00239 * to use. 00240 * 00241 * \return The type of the message digest. 00242 */ 00243 mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info ); 00244 00245 /** 00246 * \brief This function extracts the message-digest name from the 00247 * message-digest information structure. 00248 * 00249 * \param md_info The information structure of the message-digest algorithm 00250 * to use. 00251 * 00252 * \return The name of the message digest. 00253 */ 00254 const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info ); 00255 00256 /** 00257 * \brief This function starts a message-digest computation. 00258 * 00259 * You must call this function after setting up the context 00260 * with mbedtls_md_setup(), and before passing data with 00261 * mbedtls_md_update(). 00262 * 00263 * \param ctx The generic message-digest context. 00264 * 00265 * \return \c 0 on success. 00266 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification 00267 * failure. 00268 */ 00269 int mbedtls_md_starts( mbedtls_md_context_t *ctx ); 00270 00271 /** 00272 * \brief This function feeds an input buffer into an ongoing 00273 * message-digest computation. 00274 * 00275 * You must call mbedtls_md_starts() before calling this 00276 * function. You may call this function multiple times. 00277 * Afterwards, call mbedtls_md_finish(). 00278 * 00279 * \param ctx The generic message-digest context. 00280 * \param input The buffer holding the input data. 00281 * \param ilen The length of the input data. 00282 * 00283 * \return \c 0 on success. 00284 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification 00285 * failure. 00286 */ 00287 int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen ); 00288 00289 /** 00290 * \brief This function finishes the digest operation, 00291 * and writes the result to the output buffer. 00292 * 00293 * Call this function after a call to mbedtls_md_starts(), 00294 * followed by any number of calls to mbedtls_md_update(). 00295 * Afterwards, you may either clear the context with 00296 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse 00297 * the context for another digest operation with the same 00298 * algorithm. 00299 * 00300 * \param ctx The generic message-digest context. 00301 * \param output The buffer for the generic message-digest checksum result. 00302 * 00303 * \return \c 0 on success. 00304 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification 00305 * failure. 00306 */ 00307 int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output ); 00308 00309 /** 00310 * \brief This function calculates the message-digest of a buffer, 00311 * with respect to a configurable message-digest algorithm 00312 * in a single call. 00313 * 00314 * The result is calculated as 00315 * Output = message_digest(input buffer). 00316 * 00317 * \param md_info The information structure of the message-digest algorithm 00318 * to use. 00319 * \param input The buffer holding the data. 00320 * \param ilen The length of the input data. 00321 * \param output The generic message-digest checksum result. 00322 * 00323 * \return \c 0 on success. 00324 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification 00325 * failure. 00326 */ 00327 int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, 00328 unsigned char *output ); 00329 00330 #if defined(MBEDTLS_FS_IO) 00331 /** 00332 * \brief This function calculates the message-digest checksum 00333 * result of the contents of the provided file. 00334 * 00335 * The result is calculated as 00336 * Output = message_digest(file contents). 00337 * 00338 * \param md_info The information structure of the message-digest algorithm 00339 * to use. 00340 * \param path The input file name. 00341 * \param output The generic message-digest checksum result. 00342 * 00343 * \return \c 0 on success. 00344 * \return #MBEDTLS_ERR_MD_FILE_IO_ERROR on an I/O error accessing 00345 * the file pointed by \p path. 00346 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL. 00347 */ 00348 int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, 00349 unsigned char *output ); 00350 #endif /* MBEDTLS_FS_IO */ 00351 00352 /** 00353 * \brief This function sets the HMAC key and prepares to 00354 * authenticate a new message. 00355 * 00356 * Call this function after mbedtls_md_setup(), to use 00357 * the MD context for an HMAC calculation, then call 00358 * mbedtls_md_hmac_update() to provide the input data, and 00359 * mbedtls_md_hmac_finish() to get the HMAC value. 00360 * 00361 * \param ctx The message digest context containing an embedded HMAC 00362 * context. 00363 * \param key The HMAC secret key. 00364 * \param keylen The length of the HMAC key in Bytes. 00365 * 00366 * \return \c 0 on success. 00367 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification 00368 * failure. 00369 */ 00370 int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, 00371 size_t keylen ); 00372 00373 /** 00374 * \brief This function feeds an input buffer into an ongoing HMAC 00375 * computation. 00376 * 00377 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset() 00378 * before calling this function. 00379 * You may call this function multiple times to pass the 00380 * input piecewise. 00381 * Afterwards, call mbedtls_md_hmac_finish(). 00382 * 00383 * \param ctx The message digest context containing an embedded HMAC 00384 * context. 00385 * \param input The buffer holding the input data. 00386 * \param ilen The length of the input data. 00387 * 00388 * \return \c 0 on success. 00389 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification 00390 * failure. 00391 */ 00392 int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, 00393 size_t ilen ); 00394 00395 /** 00396 * \brief This function finishes the HMAC operation, and writes 00397 * the result to the output buffer. 00398 * 00399 * Call this function after mbedtls_md_hmac_starts() and 00400 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards 00401 * you may either call mbedtls_md_free() to clear the context, 00402 * or call mbedtls_md_hmac_reset() to reuse the context with 00403 * the same HMAC key. 00404 * 00405 * \param ctx The message digest context containing an embedded HMAC 00406 * context. 00407 * \param output The generic HMAC checksum result. 00408 * 00409 * \return \c 0 on success. 00410 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification 00411 * failure. 00412 */ 00413 int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output); 00414 00415 /** 00416 * \brief This function prepares to authenticate a new message with 00417 * the same key as the previous HMAC operation. 00418 * 00419 * You may call this function after mbedtls_md_hmac_finish(). 00420 * Afterwards call mbedtls_md_hmac_update() to pass the new 00421 * input. 00422 * 00423 * \param ctx The message digest context containing an embedded HMAC 00424 * context. 00425 * 00426 * \return \c 0 on success. 00427 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification 00428 * failure. 00429 */ 00430 int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx ); 00431 00432 /** 00433 * \brief This function calculates the full generic HMAC 00434 * on the input buffer with the provided key. 00435 * 00436 * The function allocates the context, performs the 00437 * calculation, and frees the context. 00438 * 00439 * The HMAC result is calculated as 00440 * output = generic HMAC(hmac key, input buffer). 00441 * 00442 * \param md_info The information structure of the message-digest algorithm 00443 * to use. 00444 * \param key The HMAC secret key. 00445 * \param keylen The length of the HMAC secret key in Bytes. 00446 * \param input The buffer holding the input data. 00447 * \param ilen The length of the input data. 00448 * \param output The generic HMAC result. 00449 * 00450 * \return \c 0 on success. 00451 * \return #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter-verification 00452 * failure. 00453 */ 00454 int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen, 00455 const unsigned char *input, size_t ilen, 00456 unsigned char *output ); 00457 00458 /* Internal use */ 00459 int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data ); 00460 00461 #ifdef __cplusplus 00462 } 00463 #endif 00464 00465 #endif /* MBEDTLS_MD_H */
Generated on Tue Jul 12 2022 12:45:31 by
1.7.2