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 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 Enumeration of 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, 00058 MBEDTLS_MD_MD2, 00059 MBEDTLS_MD_MD4, 00060 MBEDTLS_MD_MD5, 00061 MBEDTLS_MD_SHA1, 00062 MBEDTLS_MD_SHA224, 00063 MBEDTLS_MD_SHA256, 00064 MBEDTLS_MD_SHA384, 00065 MBEDTLS_MD_SHA512, 00066 MBEDTLS_MD_RIPEMD160, 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 * or NULL if 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 * or NULL if 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 * \returns \c 0 on success, 00172 * #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure, 00173 * #MBEDTLS_ERR_MD_ALLOC_FAILED memory allocation failure. 00174 */ 00175 int mbedtls_md_init_ctx( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info ) MBEDTLS_DEPRECATED; 00176 #undef MBEDTLS_DEPRECATED 00177 #endif /* MBEDTLS_DEPRECATED_REMOVED */ 00178 00179 /** 00180 * \brief This function selects the message digest algorithm to use, 00181 * and allocates internal structures. 00182 * 00183 * It should be called after mbedtls_md_init() or 00184 * mbedtls_md_free(). Makes it necessary to call 00185 * mbedtls_md_free() later. 00186 * 00187 * \param ctx The context to set up. 00188 * \param md_info The information structure of the message-digest algorithm 00189 * to use. 00190 * \param hmac <ul><li>0: HMAC is not used. Saves some memory.</li> 00191 * <li>non-zero: HMAC is used with this context.</li></ul> 00192 * 00193 * \returns \c 0 on success, 00194 * #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure, or 00195 * #MBEDTLS_ERR_MD_ALLOC_FAILED on memory allocation failure. 00196 */ 00197 int mbedtls_md_setup( mbedtls_md_context_t *ctx, const mbedtls_md_info_t *md_info, int hmac ); 00198 00199 /** 00200 * \brief This function clones the state of an message-digest 00201 * context. 00202 * 00203 * \note You must call mbedtls_md_setup() on \c dst before calling 00204 * this function. 00205 * 00206 * \note The two contexts must have the same type, 00207 * for example, both are SHA-256. 00208 * 00209 * \warning This function clones the message-digest state, not the 00210 * HMAC state. 00211 * 00212 * \param dst The destination context. 00213 * \param src The context to be cloned. 00214 * 00215 * \return \c 0 on success, 00216 * #MBEDTLS_ERR_MD_BAD_INPUT_DATA on parameter failure. 00217 */ 00218 int mbedtls_md_clone( mbedtls_md_context_t *dst, 00219 const mbedtls_md_context_t *src ); 00220 00221 /** 00222 * \brief This function extracts the message-digest size from the 00223 * message-digest information structure. 00224 * 00225 * \param md_info The information structure of the message-digest algorithm 00226 * to use. 00227 * 00228 * \return The size of the message-digest output in Bytes. 00229 */ 00230 unsigned char mbedtls_md_get_size( const mbedtls_md_info_t *md_info ); 00231 00232 /** 00233 * \brief This function extracts the message-digest type 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 type of the message digest. 00240 */ 00241 mbedtls_md_type_t mbedtls_md_get_type( const mbedtls_md_info_t *md_info ); 00242 00243 /** 00244 * \brief This function extracts the message-digest name 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 name of the message digest. 00251 */ 00252 const char *mbedtls_md_get_name( const mbedtls_md_info_t *md_info ); 00253 00254 /** 00255 * \brief This function starts a message-digest computation. 00256 * 00257 * You must call this function after setting up the context 00258 * with mbedtls_md_setup(), and before passing data with 00259 * mbedtls_md_update(). 00260 * 00261 * \param ctx The generic message-digest context. 00262 * 00263 * \returns \c 0 on success, #MBEDTLS_ERR_MD_BAD_INPUT_DATA if 00264 * parameter verification fails. 00265 */ 00266 int mbedtls_md_starts( mbedtls_md_context_t *ctx ); 00267 00268 /** 00269 * \brief This function feeds an input buffer into an ongoing 00270 * message-digest computation. 00271 * 00272 * You must call mbedtls_md_starts() before calling this 00273 * function. You may call this function multiple times. 00274 * Afterwards, call mbedtls_md_finish(). 00275 * 00276 * \param ctx The generic message-digest context. 00277 * \param input The buffer holding the input data. 00278 * \param ilen The length of the input data. 00279 * 00280 * \returns \c 0 on success, #MBEDTLS_ERR_MD_BAD_INPUT_DATA if 00281 * parameter verification fails. 00282 */ 00283 int mbedtls_md_update( mbedtls_md_context_t *ctx, const unsigned char *input, size_t ilen ); 00284 00285 /** 00286 * \brief This function finishes the digest operation, 00287 * and writes the result to the output buffer. 00288 * 00289 * Call this function after a call to mbedtls_md_starts(), 00290 * followed by any number of calls to mbedtls_md_update(). 00291 * Afterwards, you may either clear the context with 00292 * mbedtls_md_free(), or call mbedtls_md_starts() to reuse 00293 * the context for another digest operation with the same 00294 * algorithm. 00295 * 00296 * \param ctx The generic message-digest context. 00297 * \param output The buffer for the generic message-digest checksum result. 00298 * 00299 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if 00300 * parameter verification fails. 00301 */ 00302 int mbedtls_md_finish( mbedtls_md_context_t *ctx, unsigned char *output ); 00303 00304 /** 00305 * \brief This function calculates the message-digest of a buffer, 00306 * with respect to a configurable message-digest algorithm 00307 * in a single call. 00308 * 00309 * The result is calculated as 00310 * Output = message_digest(input buffer). 00311 * 00312 * \param md_info The information structure of the message-digest algorithm 00313 * to use. 00314 * \param input The buffer holding the data. 00315 * \param ilen The length of the input data. 00316 * \param output The generic message-digest checksum result. 00317 * 00318 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if 00319 * parameter verification fails. 00320 */ 00321 int mbedtls_md( const mbedtls_md_info_t *md_info, const unsigned char *input, size_t ilen, 00322 unsigned char *output ); 00323 00324 #if defined(MBEDTLS_FS_IO) 00325 /** 00326 * \brief This function calculates the message-digest checksum 00327 * result of the contents of the provided file. 00328 * 00329 * The result is calculated as 00330 * Output = message_digest(file contents). 00331 * 00332 * \param md_info The information structure of the message-digest algorithm 00333 * to use. 00334 * \param path The input file name. 00335 * \param output The generic message-digest checksum result. 00336 * 00337 * \return \c 0 on success, 00338 * #MBEDTLS_ERR_MD_FILE_IO_ERROR if file input failed, or 00339 * #MBEDTLS_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL. 00340 */ 00341 int mbedtls_md_file( const mbedtls_md_info_t *md_info, const char *path, 00342 unsigned char *output ); 00343 #endif /* MBEDTLS_FS_IO */ 00344 00345 /** 00346 * \brief This function sets the HMAC key and prepares to 00347 * authenticate a new message. 00348 * 00349 * Call this function after mbedtls_md_setup(), to use 00350 * the MD context for an HMAC calculation, then call 00351 * mbedtls_md_hmac_update() to provide the input data, and 00352 * mbedtls_md_hmac_finish() to get the HMAC value. 00353 * 00354 * \param ctx The message digest context containing an embedded HMAC 00355 * context. 00356 * \param key The HMAC secret key. 00357 * \param keylen The length of the HMAC key in Bytes. 00358 * 00359 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if 00360 * parameter verification fails. 00361 */ 00362 int mbedtls_md_hmac_starts( mbedtls_md_context_t *ctx, const unsigned char *key, 00363 size_t keylen ); 00364 00365 /** 00366 * \brief This function feeds an input buffer into an ongoing HMAC 00367 * computation. 00368 * 00369 * Call mbedtls_md_hmac_starts() or mbedtls_md_hmac_reset() 00370 * before calling this function. 00371 * You may call this function multiple times to pass the 00372 * input piecewise. 00373 * Afterwards, call mbedtls_md_hmac_finish(). 00374 * 00375 * \param ctx The message digest context containing an embedded HMAC 00376 * context. 00377 * \param input The buffer holding the input data. 00378 * \param ilen The length of the input data. 00379 * 00380 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if 00381 * parameter verification fails. 00382 */ 00383 int mbedtls_md_hmac_update( mbedtls_md_context_t *ctx, const unsigned char *input, 00384 size_t ilen ); 00385 00386 /** 00387 * \brief This function finishes the HMAC operation, and writes 00388 * the result to the output buffer. 00389 * 00390 * Call this function after mbedtls_md_hmac_starts() and 00391 * mbedtls_md_hmac_update() to get the HMAC value. Afterwards 00392 * you may either call mbedtls_md_free() to clear the context, 00393 * or call mbedtls_md_hmac_reset() to reuse the context with 00394 * the same HMAC key. 00395 * 00396 * \param ctx The message digest context containing an embedded HMAC 00397 * context. 00398 * \param output The generic HMAC checksum result. 00399 * 00400 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if 00401 * parameter verification fails. 00402 */ 00403 int mbedtls_md_hmac_finish( mbedtls_md_context_t *ctx, unsigned char *output); 00404 00405 /** 00406 * \brief This function prepares to authenticate a new message with 00407 * the same key as the previous HMAC operation. 00408 * 00409 * You may call this function after mbedtls_md_hmac_finish(). 00410 * Afterwards call mbedtls_md_hmac_update() to pass the new 00411 * input. 00412 * 00413 * \param ctx The message digest context containing an embedded HMAC 00414 * context. 00415 * 00416 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if 00417 * parameter verification fails. 00418 */ 00419 int mbedtls_md_hmac_reset( mbedtls_md_context_t *ctx ); 00420 00421 /** 00422 * \brief This function calculates the full generic HMAC 00423 * on the input buffer with the provided key. 00424 * 00425 * The function allocates the context, performs the 00426 * calculation, and frees the context. 00427 * 00428 * The HMAC result is calculated as 00429 * output = generic HMAC(hmac key, input buffer). 00430 * 00431 * \param md_info The information structure of the message-digest algorithm 00432 * to use. 00433 * \param key The HMAC secret key. 00434 * \param keylen The length of the HMAC secret key in Bytes. 00435 * \param input The buffer holding the input data. 00436 * \param ilen The length of the input data. 00437 * \param output The generic HMAC result. 00438 * 00439 * \returns \c 0 on success, or #MBEDTLS_ERR_MD_BAD_INPUT_DATA if 00440 * parameter verification fails. 00441 */ 00442 int mbedtls_md_hmac( const mbedtls_md_info_t *md_info, const unsigned char *key, size_t keylen, 00443 const unsigned char *input, size_t ilen, 00444 unsigned char *output ); 00445 00446 /* Internal use */ 00447 int mbedtls_md_process( mbedtls_md_context_t *ctx, const unsigned char *data ); 00448 00449 #ifdef __cplusplus 00450 } 00451 #endif 00452 00453 #endif /* MBEDTLS_MD_H */
Generated on Tue Jul 12 2022 14:24:27 by
