GCC Code Coverage Report


Directory: src/gate/
File: src/gate/encode/md5hash.c
Date: 2025-09-14 13:10:38
Exec Total Coverage
Lines: 84 91 92.3%
Functions: 7 7 100.0%
Branches: 17 20 85.0%

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 208 static unsigned long load_uint(unsigned char const* buffer)
53 {
54 208 return (((unsigned long)buffer[0]))
55 208 | (((unsigned long)buffer[1]) << 8)
56 208 | (((unsigned long)buffer[2]) << 16)
57 208 | (((unsigned long)buffer[3]) << 24)
58 ;
59 }
60 42 static void save_uint(unsigned long value, unsigned char* buffer)
61 {
62 42 buffer[0] = (unsigned char)((value) & 0xff);
63 42 buffer[1] = (unsigned char)((value >> 8) & 0xff);
64 42 buffer[2] = (unsigned char)((value >> 16) & 0xff);
65 42 buffer[3] = (unsigned char)((value >> 24) & 0xff);
66 42 }
67
68 832 static unsigned long left_rot32(unsigned long value, unsigned count)
69 {
70 832 return ((value >> (32 - count)) & 0xffffffff) | ((value << count) & 0xffffffff);
71 }
72
73 13 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 13 unsigned long a = md5->a;
80 13 unsigned long b = md5->b;
81 13 unsigned long c = md5->c;
82 13 unsigned long d = md5->d;
83 unsigned long dtemp;
84
85
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 13 times.
221 for (n = 0; n < 16; ++n)
86 {
87 208 m[n] = load_uint(&block[n * 4]);
88 }
89
90
2/2
✓ Branch 0 taken 832 times.
✓ Branch 1 taken 13 times.
845 for (n = 0; n < 64; ++n)
91 {
92
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 624 times.
832 if (n < 16)
93 {
94 208 f = (b & c) | ((~b) & d);
95 208 g = n;
96 }
97
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 416 times.
624 else if (n < 32)
98 {
99 208 f = (b & d) | (c & (~d));
100 208 g = (5 * n + 1) % 16;
101 }
102
2/2
✓ Branch 0 taken 208 times.
✓ Branch 1 taken 208 times.
416 else if (n < 48)
103 {
104 208 f = (b ^ c ^ d);
105 208 g = (3 * n + 5) % 16;
106 }
107 else
108 {
109 208 f = c ^ (b | (~d));
110 208 g = (7 * n) % 16;
111 }
112 832 dtemp = d;
113 832 d = c;
114 832 c = b;
115 832 b = b + left_rot32((a + f + k[n] + m[g]) & 0xffffffff, s[n]);
116 832 a = dtemp;
117 }
118 13 md5->a = (md5->a + a) & 0xffffffff;
119 13 md5->b = (md5->b + b) & 0xffffffff;
120 13 md5->c = (md5->c + c) & 0xffffffff;
121 13 md5->d = (md5->d + d) & 0xffffffff;
122 13 }
123
124
125 8 void gate_md5_init(gate_md5_t* md5)
126 {
127 8 md5->a = 0x67452301;
128 8 md5->b = 0xEFCDAB89;
129 8 md5->c = 0x98BADCFE;
130 8 md5->d = 0x10325476;
131 8 md5->buffer_used = 0;
132 8 md5->msg_length = 0;
133 8 }
134 7 void gate_md5_update(gate_md5_t* md5, void const* databuffer, gate_size_t databufferlength)
135 {
136 7 unsigned char const* ptr = (unsigned char const*)databuffer;
137
138 7 md5->msg_length += databufferlength;
139
140
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 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 5 times.
✓ Branch 1 taken 7 times.
12 while (databufferlength >= 64)
159 {
160 5 gate_md5_update_block(md5, ptr);
161 5 ptr += 64;
162 5 databufferlength -= 64;
163 }
164
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (databufferlength > 0)
165 {
166 7 gate_mem_copy(&md5->buffer[0], ptr, databufferlength);
167 7 md5->buffer_used = databufferlength;
168 }
169
170 }
171 7 void gate_md5_finish(gate_md5_t* md5, gate_md5_result_t* resulthashbuffer)
172 {
173 7 gate_size_t bufferfree = 64 - md5->buffer_used - 1;
174 7 gate_uint64_t bitlen = md5->msg_length * 8;
175 7 unsigned msglen_high = (unsigned long)(bitlen >> 32);
176 7 unsigned msglen_low = (unsigned long)(bitlen & 0xffffffff);
177
178 7 md5->buffer[md5->buffer_used++] = 0x80;
179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 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 7 gate_mem_clear(&md5->buffer[md5->buffer_used], bufferfree - 8);
187 7 save_uint(msglen_low, &md5->buffer[56]);
188 7 save_uint(msglen_high, &md5->buffer[60]);
189 7 gate_md5_update_block(md5, md5->buffer);
190
191 7 save_uint(md5->a, &resulthashbuffer->hash[0]);
192 7 save_uint(md5->b, &resulthashbuffer->hash[4]);
193 7 save_uint(md5->c, &resulthashbuffer->hash[8]);
194 7 save_uint(md5->d, &resulthashbuffer->hash[12]);
195 7 }
196
197