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