md5.h (3322B)
1 /* 2 This file is part of GNU libmicrohttpd 3 Copyright (C) 2022 Evgeny Grin (Karlson2k) 4 5 GNU libmicrohttpd is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with this library. 17 If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 /** 21 * @file microhttpd/md5.h 22 * @brief Calculation of MD5 digest 23 * @author Karlson2k (Evgeny Grin) 24 */ 25 26 #ifndef MHD_MD5_H 27 #define MHD_MD5_H 1 28 29 #include "mhd_options.h" 30 #include <stdint.h> 31 #ifdef HAVE_STDDEF_H 32 #include <stddef.h> /* for size_t */ 33 #endif /* HAVE_STDDEF_H */ 34 35 /** 36 * Number of bits in single MD5 word. 37 */ 38 #define MD5_WORD_SIZE_BITS 32 39 40 /** 41 * Number of bytes in single MD5 word. 42 */ 43 #define MD5_BYTES_IN_WORD (MD5_WORD_SIZE_BITS / 8) 44 45 /** 46 * Hash is kept internally as four 32-bit words. 47 * This is intermediate hash size, used during computing the final digest. 48 */ 49 #define MD5_HASH_SIZE_WORDS 4 50 51 /** 52 * Size of MD5 resulting digest in bytes. 53 * This is the final digest size, not intermediate hash. 54 */ 55 #define MD5_DIGEST_SIZE_WORDS MD5_HASH_SIZE_WORDS 56 57 /** 58 * Size of MD5 resulting digest in bytes 59 * This is the final digest size, not intermediate hash. 60 */ 61 #define MD5_DIGEST_SIZE (MD5_DIGEST_SIZE_WORDS * MD5_BYTES_IN_WORD) 62 63 /** 64 * Size of MD5 digest string in chars including termination NUL. 65 */ 66 #define MD5_DIGEST_STRING_SIZE ((MD5_DIGEST_SIZE) * 2 + 1) 67 68 /** 69 * Size of MD5 single processing block in bits. 70 */ 71 #define MD5_BLOCK_SIZE_BITS 512 72 73 /** 74 * Size of MD5 single processing block in bytes. 75 */ 76 #define MD5_BLOCK_SIZE (MD5_BLOCK_SIZE_BITS / 8) 77 78 /** 79 * Size of MD5 single processing block in words. 80 */ 81 #define MD5_BLOCK_SIZE_WORDS (MD5_BLOCK_SIZE_BITS / MD5_WORD_SIZE_BITS) 82 83 84 /** 85 * MD5 calculation context 86 */ 87 struct Md5Ctx 88 { 89 uint32_t H[MD5_HASH_SIZE_WORDS]; /**< Intermediate hash value / digest at end of calculation */ 90 uint32_t buffer[MD5_BLOCK_SIZE_WORDS]; /**< MD5 input data buffer */ 91 uint64_t count; /**< number of bytes, mod 2^64 */ 92 }; 93 94 /** 95 * Initialise structure for MD5 calculation. 96 * 97 * @param ctx the calculation context 98 */ 99 void 100 MHD_MD5_init (struct Md5Ctx *ctx); 101 102 103 /** 104 * MD5 process portion of bytes. 105 * 106 * @param ctx the calculation context 107 * @param data bytes to add to hash 108 * @param length number of bytes in @a data 109 */ 110 void 111 MHD_MD5_update (struct Md5Ctx *ctx, 112 const uint8_t *data, 113 size_t length); 114 115 116 /** 117 * Finalise MD5 calculation, return digest. 118 * 119 * @param ctx the calculation context 120 * @param[out] digest set to the hash, must be #MD5_DIGEST_SIZE bytes 121 */ 122 void 123 MHD_MD5_finish (struct Md5Ctx *ctx, 124 uint8_t digest[MD5_DIGEST_SIZE]); 125 126 /** 127 * Indicates that function MHD_MD5_finish() (without context reset) is available 128 */ 129 #define MHD_MD5_HAS_FINISH 1 130 131 #endif /* MHD_MD5_H */