Jaeden Amero | e54e693 | 2018-08-06 16:19:58 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file md.h |
| 3 | * |
| 4 | * \brief This file contains the generic message-digest wrapper. |
| 5 | * |
| 6 | * \author Adriaan de Jong <dejong@fox-it.com> |
| 7 | */ |
| 8 | /* |
| 9 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved |
| 10 | * 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. |
| 23 | * |
| 24 | * This file is part of Mbed Crypto (https://tls.mbed.org) |
| 25 | */ |
| 26 | |
| 27 | #ifndef MBEDCRYPTO_MD_H |
| 28 | #define MBEDCRYPTO_MD_H |
| 29 | |
| 30 | #include <stddef.h> |
| 31 | |
| 32 | #if !defined(MBEDCRYPTO_CONFIG_FILE) |
| 33 | #include "config.h" |
| 34 | #else |
| 35 | #include MBEDCRYPTO_CONFIG_FILE |
| 36 | #endif |
| 37 | |
| 38 | #define MBEDCRYPTO_ERR_MD_FEATURE_UNAVAILABLE -0x5080 /**< The selected feature is not available. */ |
| 39 | #define MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA -0x5100 /**< Bad input parameters to function. */ |
| 40 | #define MBEDCRYPTO_ERR_MD_ALLOC_FAILED -0x5180 /**< Failed to allocate memory. */ |
| 41 | #define MBEDCRYPTO_ERR_MD_FILE_IO_ERROR -0x5200 /**< Opening or reading of file failed. */ |
| 42 | #define MBEDCRYPTO_ERR_MD_HW_ACCEL_FAILED -0x5280 /**< MD hardware accelerator failed. */ |
| 43 | |
| 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
| 48 | /** |
| 49 | * \brief Supported message digests. |
| 50 | * |
| 51 | * \warning MD2, MD4, MD5 and SHA-1 are considered weak message digests and |
| 52 | * their use constitutes a security risk. We recommend considering |
| 53 | * stronger message digests instead. |
| 54 | * |
| 55 | */ |
| 56 | typedef enum { |
| 57 | MBEDCRYPTO_MD_NONE=0, /**< None. */ |
| 58 | MBEDCRYPTO_MD_MD2, /**< The MD2 message digest. */ |
| 59 | MBEDCRYPTO_MD_MD4, /**< The MD4 message digest. */ |
| 60 | MBEDCRYPTO_MD_MD5, /**< The MD5 message digest. */ |
| 61 | MBEDCRYPTO_MD_SHA1, /**< The SHA-1 message digest. */ |
| 62 | MBEDCRYPTO_MD_SHA224, /**< The SHA-224 message digest. */ |
| 63 | MBEDCRYPTO_MD_SHA256, /**< The SHA-256 message digest. */ |
| 64 | MBEDCRYPTO_MD_SHA384, /**< The SHA-384 message digest. */ |
| 65 | MBEDCRYPTO_MD_SHA512, /**< The SHA-512 message digest. */ |
| 66 | MBEDCRYPTO_MD_RIPEMD160, /**< The RIPEMD-160 message digest. */ |
| 67 | } mbedcrypto_md_type_t; |
| 68 | |
| 69 | #if defined(MBEDCRYPTO_SHA512_C) |
| 70 | #define MBEDCRYPTO_MD_MAX_SIZE 64 /* longest known is SHA512 */ |
| 71 | #else |
| 72 | #define MBEDCRYPTO_MD_MAX_SIZE 32 /* longest known is SHA256 or less */ |
| 73 | #endif |
| 74 | |
| 75 | /** |
| 76 | * Opaque struct defined in md_internal.h. |
| 77 | */ |
| 78 | typedef struct mbedcrypto_md_info_t mbedcrypto_md_info_t; |
| 79 | |
| 80 | /** |
| 81 | * The generic message-digest context. |
| 82 | */ |
| 83 | typedef struct { |
| 84 | /** Information about the associated message digest. */ |
| 85 | const mbedcrypto_md_info_t *md_info; |
| 86 | |
| 87 | /** The digest-specific context. */ |
| 88 | void *md_ctx; |
| 89 | |
| 90 | /** The HMAC part of the context. */ |
| 91 | void *hmac_ctx; |
| 92 | } mbedcrypto_md_context_t; |
| 93 | |
| 94 | /** |
| 95 | * \brief This function returns the list of digests supported by the |
| 96 | * generic digest module. |
| 97 | * |
| 98 | * \return A statically allocated array of digests. Each element |
| 99 | * in the returned list is an integer belonging to the |
| 100 | * message-digest enumeration #mbedcrypto_md_type_t. |
| 101 | * The last entry is 0. |
| 102 | */ |
| 103 | const int *mbedcrypto_md_list( void ); |
| 104 | |
| 105 | /** |
| 106 | * \brief This function returns the message-digest information |
| 107 | * associated with the given digest name. |
| 108 | * |
| 109 | * \param md_name The name of the digest to search for. |
| 110 | * |
| 111 | * \return The message-digest information associated with \p md_name. |
| 112 | * \return NULL if the associated message-digest information is not found. |
| 113 | */ |
| 114 | const mbedcrypto_md_info_t *mbedcrypto_md_info_from_string( const char *md_name ); |
| 115 | |
| 116 | /** |
| 117 | * \brief This function returns the message-digest information |
| 118 | * associated with the given digest type. |
| 119 | * |
| 120 | * \param md_type The type of digest to search for. |
| 121 | * |
| 122 | * \return The message-digest information associated with \p md_type. |
| 123 | * \return NULL if the associated message-digest information is not found. |
| 124 | */ |
| 125 | const mbedcrypto_md_info_t *mbedcrypto_md_info_from_type( mbedcrypto_md_type_t md_type ); |
| 126 | |
| 127 | /** |
| 128 | * \brief This function initializes a message-digest context without |
| 129 | * binding it to a particular message-digest algorithm. |
| 130 | * |
| 131 | * This function should always be called first. It prepares the |
| 132 | * context for mbedcrypto_md_setup() for binding it to a |
| 133 | * message-digest algorithm. |
| 134 | */ |
| 135 | void mbedcrypto_md_init( mbedcrypto_md_context_t *ctx ); |
| 136 | |
| 137 | /** |
| 138 | * \brief This function clears the internal structure of \p ctx and |
| 139 | * frees any embedded internal structure, but does not free |
| 140 | * \p ctx itself. |
| 141 | * |
| 142 | * If you have called mbedcrypto_md_setup() on \p ctx, you must |
| 143 | * call mbedcrypto_md_free() when you are no longer using the |
| 144 | * context. |
| 145 | * Calling this function if you have previously |
| 146 | * called mbedcrypto_md_init() and nothing else is optional. |
| 147 | * You must not call this function if you have not called |
| 148 | * mbedcrypto_md_init(). |
| 149 | */ |
| 150 | void mbedcrypto_md_free( mbedcrypto_md_context_t *ctx ); |
| 151 | |
| 152 | #if ! defined(MBEDCRYPTO_DEPRECATED_REMOVED) |
| 153 | #if defined(MBEDCRYPTO_DEPRECATED_WARNING) |
| 154 | #define MBEDCRYPTO_DEPRECATED __attribute__((deprecated)) |
| 155 | #else |
| 156 | #define MBEDCRYPTO_DEPRECATED |
| 157 | #endif |
| 158 | /** |
| 159 | * \brief This function selects the message digest algorithm to use, |
| 160 | * and allocates internal structures. |
| 161 | * |
| 162 | * It should be called after mbedcrypto_md_init() or mbedcrypto_md_free(). |
| 163 | * Makes it necessary to call mbedcrypto_md_free() later. |
| 164 | * |
| 165 | * \deprecated Superseded by mbedcrypto_md_setup() in 2.0.0 |
| 166 | * |
| 167 | * \param ctx The context to set up. |
| 168 | * \param md_info The information structure of the message-digest algorithm |
| 169 | * to use. |
| 170 | * |
| 171 | * \return \c 0 on success. |
| 172 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 173 | * failure. |
| 174 | * \return #MBEDCRYPTO_ERR_MD_ALLOC_FAILED on memory-allocation failure. |
| 175 | */ |
| 176 | int mbedcrypto_md_init_ctx( mbedcrypto_md_context_t *ctx, const mbedcrypto_md_info_t *md_info ) MBEDCRYPTO_DEPRECATED; |
| 177 | #undef MBEDCRYPTO_DEPRECATED |
| 178 | #endif /* MBEDCRYPTO_DEPRECATED_REMOVED */ |
| 179 | |
| 180 | /** |
| 181 | * \brief This function selects the message digest algorithm to use, |
| 182 | * and allocates internal structures. |
| 183 | * |
| 184 | * It should be called after mbedcrypto_md_init() or |
| 185 | * mbedcrypto_md_free(). Makes it necessary to call |
| 186 | * mbedcrypto_md_free() later. |
| 187 | * |
| 188 | * \param ctx The context to set up. |
| 189 | * \param md_info The information structure of the message-digest algorithm |
| 190 | * to use. |
| 191 | * \param hmac Defines if HMAC is used. 0: HMAC is not used (saves some memory), |
| 192 | * or non-zero: HMAC is used with this context. |
| 193 | * |
| 194 | * \return \c 0 on success. |
| 195 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 196 | * failure. |
| 197 | * \return #MBEDCRYPTO_ERR_MD_ALLOC_FAILED on memory-allocation failure. |
| 198 | */ |
| 199 | int mbedcrypto_md_setup( mbedcrypto_md_context_t *ctx, const mbedcrypto_md_info_t *md_info, int hmac ); |
| 200 | |
| 201 | /** |
| 202 | * \brief This function clones the state of an message-digest |
| 203 | * context. |
| 204 | * |
| 205 | * \note You must call mbedcrypto_md_setup() on \c dst before calling |
| 206 | * this function. |
| 207 | * |
| 208 | * \note The two contexts must have the same type, |
| 209 | * for example, both are SHA-256. |
| 210 | * |
| 211 | * \warning This function clones the message-digest state, not the |
| 212 | * HMAC state. |
| 213 | * |
| 214 | * \param dst The destination context. |
| 215 | * \param src The context to be cloned. |
| 216 | * |
| 217 | * \return \c 0 on success. |
| 218 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA on parameter-verification failure. |
| 219 | */ |
| 220 | int mbedcrypto_md_clone( mbedcrypto_md_context_t *dst, |
| 221 | const mbedcrypto_md_context_t *src ); |
| 222 | |
| 223 | /** |
| 224 | * \brief This function extracts the message-digest size from the |
| 225 | * message-digest information structure. |
| 226 | * |
| 227 | * \param md_info The information structure of the message-digest algorithm |
| 228 | * to use. |
| 229 | * |
| 230 | * \return The size of the message-digest output in Bytes. |
| 231 | */ |
| 232 | unsigned char mbedcrypto_md_get_size( const mbedcrypto_md_info_t *md_info ); |
| 233 | |
| 234 | /** |
| 235 | * \brief This function extracts the message-digest type from the |
| 236 | * message-digest information structure. |
| 237 | * |
| 238 | * \param md_info The information structure of the message-digest algorithm |
| 239 | * to use. |
| 240 | * |
| 241 | * \return The type of the message digest. |
| 242 | */ |
| 243 | mbedcrypto_md_type_t mbedcrypto_md_get_type( const mbedcrypto_md_info_t *md_info ); |
| 244 | |
| 245 | /** |
| 246 | * \brief This function extracts the message-digest name from the |
| 247 | * message-digest information structure. |
| 248 | * |
| 249 | * \param md_info The information structure of the message-digest algorithm |
| 250 | * to use. |
| 251 | * |
| 252 | * \return The name of the message digest. |
| 253 | */ |
| 254 | const char *mbedcrypto_md_get_name( const mbedcrypto_md_info_t *md_info ); |
| 255 | |
| 256 | /** |
| 257 | * \brief This function starts a message-digest computation. |
| 258 | * |
| 259 | * You must call this function after setting up the context |
| 260 | * with mbedcrypto_md_setup(), and before passing data with |
| 261 | * mbedcrypto_md_update(). |
| 262 | * |
| 263 | * \param ctx The generic message-digest context. |
| 264 | * |
| 265 | * \return \c 0 on success. |
| 266 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 267 | * failure. |
| 268 | */ |
| 269 | int mbedcrypto_md_starts( mbedcrypto_md_context_t *ctx ); |
| 270 | |
| 271 | /** |
| 272 | * \brief This function feeds an input buffer into an ongoing |
| 273 | * message-digest computation. |
| 274 | * |
| 275 | * You must call mbedcrypto_md_starts() before calling this |
| 276 | * function. You may call this function multiple times. |
| 277 | * Afterwards, call mbedcrypto_md_finish(). |
| 278 | * |
| 279 | * \param ctx The generic message-digest context. |
| 280 | * \param input The buffer holding the input data. |
| 281 | * \param ilen The length of the input data. |
| 282 | * |
| 283 | * \return \c 0 on success. |
| 284 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 285 | * failure. |
| 286 | */ |
| 287 | int mbedcrypto_md_update( mbedcrypto_md_context_t *ctx, const unsigned char *input, size_t ilen ); |
| 288 | |
| 289 | /** |
| 290 | * \brief This function finishes the digest operation, |
| 291 | * and writes the result to the output buffer. |
| 292 | * |
| 293 | * Call this function after a call to mbedcrypto_md_starts(), |
| 294 | * followed by any number of calls to mbedcrypto_md_update(). |
| 295 | * Afterwards, you may either clear the context with |
| 296 | * mbedcrypto_md_free(), or call mbedcrypto_md_starts() to reuse |
| 297 | * the context for another digest operation with the same |
| 298 | * algorithm. |
| 299 | * |
| 300 | * \param ctx The generic message-digest context. |
| 301 | * \param output The buffer for the generic message-digest checksum result. |
| 302 | * |
| 303 | * \return \c 0 on success. |
| 304 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 305 | * failure. |
| 306 | */ |
| 307 | int mbedcrypto_md_finish( mbedcrypto_md_context_t *ctx, unsigned char *output ); |
| 308 | |
| 309 | /** |
| 310 | * \brief This function calculates the message-digest of a buffer, |
| 311 | * with respect to a configurable message-digest algorithm |
| 312 | * in a single call. |
| 313 | * |
| 314 | * The result is calculated as |
| 315 | * Output = message_digest(input buffer). |
| 316 | * |
| 317 | * \param md_info The information structure of the message-digest algorithm |
| 318 | * to use. |
| 319 | * \param input The buffer holding the data. |
| 320 | * \param ilen The length of the input data. |
| 321 | * \param output The generic message-digest checksum result. |
| 322 | * |
| 323 | * \return \c 0 on success. |
| 324 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 325 | * failure. |
| 326 | */ |
| 327 | int mbedcrypto_md( const mbedcrypto_md_info_t *md_info, const unsigned char *input, size_t ilen, |
| 328 | unsigned char *output ); |
| 329 | |
| 330 | #if defined(MBEDCRYPTO_FS_IO) |
| 331 | /** |
| 332 | * \brief This function calculates the message-digest checksum |
| 333 | * result of the contents of the provided file. |
| 334 | * |
| 335 | * The result is calculated as |
| 336 | * Output = message_digest(file contents). |
| 337 | * |
| 338 | * \param md_info The information structure of the message-digest algorithm |
| 339 | * to use. |
| 340 | * \param path The input file name. |
| 341 | * \param output The generic message-digest checksum result. |
| 342 | * |
| 343 | * \return \c 0 on success. |
| 344 | * \return #MBEDCRYPTO_ERR_MD_FILE_IO_ERROR on an I/O error accessing |
| 345 | * the file pointed by \p path. |
| 346 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA if \p md_info was NULL. |
| 347 | */ |
| 348 | int mbedcrypto_md_file( const mbedcrypto_md_info_t *md_info, const char *path, |
| 349 | unsigned char *output ); |
| 350 | #endif /* MBEDCRYPTO_FS_IO */ |
| 351 | |
| 352 | /** |
| 353 | * \brief This function sets the HMAC key and prepares to |
| 354 | * authenticate a new message. |
| 355 | * |
| 356 | * Call this function after mbedcrypto_md_setup(), to use |
| 357 | * the MD context for an HMAC calculation, then call |
| 358 | * mbedcrypto_md_hmac_update() to provide the input data, and |
| 359 | * mbedcrypto_md_hmac_finish() to get the HMAC value. |
| 360 | * |
| 361 | * \param ctx The message digest context containing an embedded HMAC |
| 362 | * context. |
| 363 | * \param key The HMAC secret key. |
| 364 | * \param keylen The length of the HMAC key in Bytes. |
| 365 | * |
| 366 | * \return \c 0 on success. |
| 367 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 368 | * failure. |
| 369 | */ |
| 370 | int mbedcrypto_md_hmac_starts( mbedcrypto_md_context_t *ctx, const unsigned char *key, |
| 371 | size_t keylen ); |
| 372 | |
| 373 | /** |
| 374 | * \brief This function feeds an input buffer into an ongoing HMAC |
| 375 | * computation. |
| 376 | * |
| 377 | * Call mbedcrypto_md_hmac_starts() or mbedcrypto_md_hmac_reset() |
| 378 | * before calling this function. |
| 379 | * You may call this function multiple times to pass the |
| 380 | * input piecewise. |
| 381 | * Afterwards, call mbedcrypto_md_hmac_finish(). |
| 382 | * |
| 383 | * \param ctx The message digest context containing an embedded HMAC |
| 384 | * context. |
| 385 | * \param input The buffer holding the input data. |
| 386 | * \param ilen The length of the input data. |
| 387 | * |
| 388 | * \return \c 0 on success. |
| 389 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 390 | * failure. |
| 391 | */ |
| 392 | int mbedcrypto_md_hmac_update( mbedcrypto_md_context_t *ctx, const unsigned char *input, |
| 393 | size_t ilen ); |
| 394 | |
| 395 | /** |
| 396 | * \brief This function finishes the HMAC operation, and writes |
| 397 | * the result to the output buffer. |
| 398 | * |
| 399 | * Call this function after mbedcrypto_md_hmac_starts() and |
| 400 | * mbedcrypto_md_hmac_update() to get the HMAC value. Afterwards |
| 401 | * you may either call mbedcrypto_md_free() to clear the context, |
| 402 | * or call mbedcrypto_md_hmac_reset() to reuse the context with |
| 403 | * the same HMAC key. |
| 404 | * |
| 405 | * \param ctx The message digest context containing an embedded HMAC |
| 406 | * context. |
| 407 | * \param output The generic HMAC checksum result. |
| 408 | * |
| 409 | * \return \c 0 on success. |
| 410 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 411 | * failure. |
| 412 | */ |
| 413 | int mbedcrypto_md_hmac_finish( mbedcrypto_md_context_t *ctx, unsigned char *output); |
| 414 | |
| 415 | /** |
| 416 | * \brief This function prepares to authenticate a new message with |
| 417 | * the same key as the previous HMAC operation. |
| 418 | * |
| 419 | * You may call this function after mbedcrypto_md_hmac_finish(). |
| 420 | * Afterwards call mbedcrypto_md_hmac_update() to pass the new |
| 421 | * input. |
| 422 | * |
| 423 | * \param ctx The message digest context containing an embedded HMAC |
| 424 | * context. |
| 425 | * |
| 426 | * \return \c 0 on success. |
| 427 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 428 | * failure. |
| 429 | */ |
| 430 | int mbedcrypto_md_hmac_reset( mbedcrypto_md_context_t *ctx ); |
| 431 | |
| 432 | /** |
| 433 | * \brief This function calculates the full generic HMAC |
| 434 | * on the input buffer with the provided key. |
| 435 | * |
| 436 | * The function allocates the context, performs the |
| 437 | * calculation, and frees the context. |
| 438 | * |
| 439 | * The HMAC result is calculated as |
| 440 | * output = generic HMAC(hmac key, input buffer). |
| 441 | * |
| 442 | * \param md_info The information structure of the message-digest algorithm |
| 443 | * to use. |
| 444 | * \param key The HMAC secret key. |
| 445 | * \param keylen The length of the HMAC secret key in Bytes. |
| 446 | * \param input The buffer holding the input data. |
| 447 | * \param ilen The length of the input data. |
| 448 | * \param output The generic HMAC result. |
| 449 | * |
| 450 | * \return \c 0 on success. |
| 451 | * \return #MBEDCRYPTO_ERR_MD_BAD_INPUT_DATA on parameter-verification |
| 452 | * failure. |
| 453 | */ |
| 454 | int mbedcrypto_md_hmac( const mbedcrypto_md_info_t *md_info, const unsigned char *key, size_t keylen, |
| 455 | const unsigned char *input, size_t ilen, |
| 456 | unsigned char *output ); |
| 457 | |
| 458 | /* Internal use */ |
| 459 | int mbedcrypto_md_process( mbedcrypto_md_context_t *ctx, const unsigned char *data ); |
| 460 | |
| 461 | #ifdef __cplusplus |
| 462 | } |
| 463 | #endif |
| 464 | |
| 465 | #endif /* MBEDCRYPTO_MD_H */ |