| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "gate/encode/md5hash.h" | ||
| 30 | #include "gate/memalloc.h" | ||
| 31 | |||
| 32 | /* Public information: http://en.wikipedia.org/wiki/MD5 */ | ||
| 33 | |||
| 34 | static const unsigned s[] = { | ||
| 35 | 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, | ||
| 36 | 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, | ||
| 37 | 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, | ||
| 38 | 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 | ||
| 39 | }; | ||
| 40 | |||
| 41 | static const unsigned long k[] = { | ||
| 42 | 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, | ||
| 43 | 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, | ||
| 44 | 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, | ||
| 45 | 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, | ||
| 46 | 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, | ||
| 47 | 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, | ||
| 48 | 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, | ||
| 49 | 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 | ||
| 50 | }; | ||
| 51 | |||
| 52 | 336 | static unsigned long load_uint(unsigned char const* buffer) | |
| 53 | { | ||
| 54 | 336 | return (((unsigned long)buffer[0])) | |
| 55 | 336 | | (((unsigned long)buffer[1]) << 8) | |
| 56 | 336 | | (((unsigned long)buffer[2]) << 16) | |
| 57 | 336 | | (((unsigned long)buffer[3]) << 24) | |
| 58 | ; | ||
| 59 | } | ||
| 60 | 66 | static void save_uint(unsigned long value, unsigned char* buffer) | |
| 61 | { | ||
| 62 | 66 | buffer[0] = (unsigned char)((value) & 0xff); | |
| 63 | 66 | buffer[1] = (unsigned char)((value >> 8) & 0xff); | |
| 64 | 66 | buffer[2] = (unsigned char)((value >> 16) & 0xff); | |
| 65 | 66 | buffer[3] = (unsigned char)((value >> 24) & 0xff); | |
| 66 | 66 | } | |
| 67 | |||
| 68 | 1344 | static unsigned long left_rot32(unsigned long value, unsigned count) | |
| 69 | { | ||
| 70 | 1344 | return ((value >> (32 - count)) & 0xffffffff) | ((value << count) & 0xffffffff); | |
| 71 | } | ||
| 72 | |||
| 73 | 21 | static void gate_md5_update_block(gate_md5_t* md5, unsigned char const* block) | |
| 74 | { | ||
| 75 | unsigned long f; | ||
| 76 | unsigned g; | ||
| 77 | unsigned n; | ||
| 78 | unsigned long m[16]; | ||
| 79 | 21 | unsigned long a = md5->a; | |
| 80 | 21 | unsigned long b = md5->b; | |
| 81 | 21 | unsigned long c = md5->c; | |
| 82 | 21 | unsigned long d = md5->d; | |
| 83 | |||
| 84 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 21 times.
|
357 | for (n = 0; n < 16; ++n) |
| 85 | { | ||
| 86 | 336 | m[n] = load_uint(&block[n * 4]); | |
| 87 | } | ||
| 88 | |||
| 89 |
2/2✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 21 times.
|
1365 | for (n = 0; n < 64; ++n) |
| 90 | { | ||
| 91 | unsigned long dtemp; | ||
| 92 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 1008 times.
|
1344 | if (n < 16) |
| 93 | { | ||
| 94 | 336 | f = (b & c) | ((~b) & d); | |
| 95 | 336 | g = n; | |
| 96 | } | ||
| 97 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 672 times.
|
1008 | else if (n < 32) |
| 98 | { | ||
| 99 | 336 | f = (b & d) | (c & (~d)); | |
| 100 | 336 | g = (5 * n + 1) % 16; | |
| 101 | } | ||
| 102 |
2/2✓ Branch 0 taken 336 times.
✓ Branch 1 taken 336 times.
|
672 | else if (n < 48) |
| 103 | { | ||
| 104 | 336 | f = (b ^ c ^ d); | |
| 105 | 336 | g = (3 * n + 5) % 16; | |
| 106 | } | ||
| 107 | else | ||
| 108 | { | ||
| 109 | 336 | f = c ^ (b | (~d)); | |
| 110 | 336 | g = (7 * n) % 16; | |
| 111 | } | ||
| 112 | 1344 | dtemp = d; | |
| 113 | 1344 | d = c; | |
| 114 | 1344 | c = b; | |
| 115 | 1344 | b = b + left_rot32((a + f + k[n] + m[g]) & 0xffffffff, s[n]); | |
| 116 | 1344 | a = dtemp; | |
| 117 | } | ||
| 118 | 21 | md5->a = (md5->a + a) & 0xffffffff; | |
| 119 | 21 | md5->b = (md5->b + b) & 0xffffffff; | |
| 120 | 21 | md5->c = (md5->c + c) & 0xffffffff; | |
| 121 | 21 | md5->d = (md5->d + d) & 0xffffffff; | |
| 122 | 21 | } | |
| 123 | |||
| 124 | |||
| 125 | 13 | void gate_md5_init(gate_md5_t* md5) | |
| 126 | { | ||
| 127 | 13 | md5->a = 0x67452301; | |
| 128 | 13 | md5->b = 0xEFCDAB89; | |
| 129 | 13 | md5->c = 0x98BADCFE; | |
| 130 | 13 | md5->d = 0x10325476; | |
| 131 | 13 | md5->buffer_used = 0; | |
| 132 | 13 | md5->msg_length = 0; | |
| 133 | 13 | } | |
| 134 | 11 | void gate_md5_update(gate_md5_t* md5, void const* databuffer, gate_size_t databufferlength) | |
| 135 | { | ||
| 136 | 11 | unsigned char const* ptr = (unsigned char const*)databuffer; | |
| 137 | |||
| 138 | 11 | md5->msg_length += databufferlength; | |
| 139 | |||
| 140 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
|
11 | if (md5->buffer_used > 0) |
| 141 | { | ||
| 142 | 1 | gate_size_t bufferfree = 64 - md5->buffer_used; | |
| 143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (databufferlength < bufferfree) |
| 144 | { | ||
| 145 | ✗ | gate_mem_copy(&md5->buffer[md5->buffer_used], ptr, databufferlength); | |
| 146 | ✗ | md5->buffer_used += databufferlength; | |
| 147 | ✗ | return; | |
| 148 | } | ||
| 149 | else | ||
| 150 | { | ||
| 151 | 1 | gate_mem_copy(&md5->buffer[md5->buffer_used], ptr, bufferfree); | |
| 152 | 1 | gate_md5_update_block(md5, md5->buffer); | |
| 153 | 1 | md5->buffer_used = 0; | |
| 154 | 1 | ptr += bufferfree; | |
| 155 | 1 | databufferlength -= bufferfree; | |
| 156 | } | ||
| 157 | } | ||
| 158 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 11 times.
|
20 | while (databufferlength >= 64) |
| 159 | { | ||
| 160 | 9 | gate_md5_update_block(md5, ptr); | |
| 161 | 9 | ptr += 64; | |
| 162 | 9 | databufferlength -= 64; | |
| 163 | } | ||
| 164 |
1/2✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
|
11 | if (databufferlength > 0) |
| 165 | { | ||
| 166 | 11 | gate_mem_copy(&md5->buffer[0], ptr, databufferlength); | |
| 167 | 11 | md5->buffer_used = databufferlength; | |
| 168 | } | ||
| 169 | |||
| 170 | } | ||
| 171 | 11 | void gate_md5_finish(gate_md5_t* md5, gate_md5_result_t* resulthashbuffer) | |
| 172 | { | ||
| 173 | 11 | gate_size_t bufferfree = 64 - md5->buffer_used - 1; | |
| 174 | 11 | gate_uint64_t bitlen = md5->msg_length * 8; | |
| 175 | 11 | unsigned msglen_high = (unsigned long)(bitlen >> 32); | |
| 176 | 11 | unsigned msglen_low = (unsigned long)(bitlen & 0xffffffff); | |
| 177 | |||
| 178 | 11 | md5->buffer[md5->buffer_used++] = 0x80; | |
| 179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | if (bufferfree < 8) |
| 180 | { | ||
| 181 | ✗ | gate_mem_clear(&md5->buffer[md5->buffer_used], bufferfree); | |
| 182 | ✗ | gate_md5_update_block(md5, md5->buffer); | |
| 183 | ✗ | bufferfree = 64; | |
| 184 | ✗ | md5->buffer_used = 0; | |
| 185 | } | ||
| 186 | 11 | gate_mem_clear(&md5->buffer[md5->buffer_used], bufferfree - 8); | |
| 187 | 11 | save_uint(msglen_low, &md5->buffer[56]); | |
| 188 | 11 | save_uint(msglen_high, &md5->buffer[60]); | |
| 189 | 11 | gate_md5_update_block(md5, md5->buffer); | |
| 190 | |||
| 191 | 11 | save_uint(md5->a, &resulthashbuffer->hash[0]); | |
| 192 | 11 | save_uint(md5->b, &resulthashbuffer->hash[4]); | |
| 193 | 11 | save_uint(md5->c, &resulthashbuffer->hash[8]); | |
| 194 | 11 | save_uint(md5->d, &resulthashbuffer->hash[12]); | |
| 195 | 11 | } | |
| 196 | |||
| 197 |