Coverage Report

Created: 2026-04-10 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.4.0/src/lib/hash/md5/md5.cpp
Line
Count
Source
1
/*
2
* MD5
3
* (C) 1999-2008 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/internal/md5.h>
9
10
#include <botan/internal/bit_ops.h>
11
#include <botan/internal/loadstor.h>
12
#include <botan/internal/rotate.h>
13
#include <botan/internal/stl_util.h>
14
15
#include <array>
16
17
namespace Botan {
18
namespace {
19
20
/*
21
* MD5 FF Function
22
*/
23
template <size_t S>
24
55.2M
inline void FF(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
25
55.2M
   A += choose(B, C, D) + M;
26
55.2M
   A = rotl<S>(A) + B;
27
55.2M
}
md5.cpp:void Botan::(anonymous namespace)::FF<7ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
24
13.8M
inline void FF(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
25
13.8M
   A += choose(B, C, D) + M;
26
13.8M
   A = rotl<S>(A) + B;
27
13.8M
}
md5.cpp:void Botan::(anonymous namespace)::FF<12ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
24
13.8M
inline void FF(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
25
13.8M
   A += choose(B, C, D) + M;
26
13.8M
   A = rotl<S>(A) + B;
27
13.8M
}
md5.cpp:void Botan::(anonymous namespace)::FF<17ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
24
13.8M
inline void FF(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
25
13.8M
   A += choose(B, C, D) + M;
26
13.8M
   A = rotl<S>(A) + B;
27
13.8M
}
md5.cpp:void Botan::(anonymous namespace)::FF<22ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
24
13.8M
inline void FF(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
25
13.8M
   A += choose(B, C, D) + M;
26
13.8M
   A = rotl<S>(A) + B;
27
13.8M
}
28
29
/*
30
* MD5 GG Function
31
*/
32
template <size_t S>
33
55.2M
inline void GG(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
34
55.2M
   A += choose(D, B, C) + M;
35
55.2M
   A = rotl<S>(A) + B;
36
55.2M
}
md5.cpp:void Botan::(anonymous namespace)::GG<5ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
33
13.8M
inline void GG(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
34
13.8M
   A += choose(D, B, C) + M;
35
13.8M
   A = rotl<S>(A) + B;
36
13.8M
}
md5.cpp:void Botan::(anonymous namespace)::GG<9ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
33
13.8M
inline void GG(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
34
13.8M
   A += choose(D, B, C) + M;
35
13.8M
   A = rotl<S>(A) + B;
36
13.8M
}
md5.cpp:void Botan::(anonymous namespace)::GG<14ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
33
13.8M
inline void GG(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
34
13.8M
   A += choose(D, B, C) + M;
35
13.8M
   A = rotl<S>(A) + B;
36
13.8M
}
md5.cpp:void Botan::(anonymous namespace)::GG<20ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
33
13.8M
inline void GG(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
34
13.8M
   A += choose(D, B, C) + M;
35
13.8M
   A = rotl<S>(A) + B;
36
13.8M
}
37
38
/*
39
* MD5 HH Function
40
*/
41
template <size_t S>
42
55.2M
inline void HH(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
43
55.2M
   A += (B ^ C ^ D) + M;
44
55.2M
   A = rotl<S>(A) + B;
45
55.2M
}
md5.cpp:void Botan::(anonymous namespace)::HH<4ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
42
13.8M
inline void HH(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
43
13.8M
   A += (B ^ C ^ D) + M;
44
13.8M
   A = rotl<S>(A) + B;
45
13.8M
}
md5.cpp:void Botan::(anonymous namespace)::HH<11ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
42
13.8M
inline void HH(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
43
13.8M
   A += (B ^ C ^ D) + M;
44
13.8M
   A = rotl<S>(A) + B;
45
13.8M
}
md5.cpp:void Botan::(anonymous namespace)::HH<16ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
42
13.8M
inline void HH(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
43
13.8M
   A += (B ^ C ^ D) + M;
44
13.8M
   A = rotl<S>(A) + B;
45
13.8M
}
md5.cpp:void Botan::(anonymous namespace)::HH<23ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
42
13.8M
inline void HH(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
43
13.8M
   A += (B ^ C ^ D) + M;
44
13.8M
   A = rotl<S>(A) + B;
45
13.8M
}
46
47
/*
48
* MD5 II Function
49
*/
50
template <size_t S>
51
55.2M
inline void II(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
52
   // This expr is choose(D, B ^ C, ~C), but that is slower
53
55.2M
   A += (C ^ (B | ~D)) + M;
54
55.2M
   A = rotl<S>(A) + B;
55
55.2M
}
md5.cpp:void Botan::(anonymous namespace)::II<6ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
51
13.8M
inline void II(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
52
   // This expr is choose(D, B ^ C, ~C), but that is slower
53
13.8M
   A += (C ^ (B | ~D)) + M;
54
13.8M
   A = rotl<S>(A) + B;
55
13.8M
}
md5.cpp:void Botan::(anonymous namespace)::II<10ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
51
13.8M
inline void II(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
52
   // This expr is choose(D, B ^ C, ~C), but that is slower
53
13.8M
   A += (C ^ (B | ~D)) + M;
54
13.8M
   A = rotl<S>(A) + B;
55
13.8M
}
md5.cpp:void Botan::(anonymous namespace)::II<15ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
51
13.8M
inline void II(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
52
   // This expr is choose(D, B ^ C, ~C), but that is slower
53
13.8M
   A += (C ^ (B | ~D)) + M;
54
13.8M
   A = rotl<S>(A) + B;
55
13.8M
}
md5.cpp:void Botan::(anonymous namespace)::II<21ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
51
13.8M
inline void II(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M) {
52
   // This expr is choose(D, B ^ C, ~C), but that is slower
53
13.8M
   A += (C ^ (B | ~D)) + M;
54
13.8M
   A = rotl<S>(A) + B;
55
13.8M
}
56
57
}  // namespace
58
59
/*
60
* MD5 Compression Function
61
*/
62
244k
void MD5::compress_n(MD5::digest_type& digest, std::span<const uint8_t> input, size_t blocks) {
63
244k
   uint32_t A = digest[0], B = digest[1], C = digest[2], D = digest[3];
64
244k
   std::array<uint32_t, 16> M;
65
66
244k
   BufferSlicer in(input);
67
68
3.69M
   for(size_t i = 0; i != blocks; ++i) {
69
3.45M
      load_le(M.data(), in.take(block_bytes).data(), M.size());
70
71
3.45M
      FF<7>(A, B, C, D, M[0] + 0xD76AA478);
72
3.45M
      FF<12>(D, A, B, C, M[1] + 0xE8C7B756);
73
3.45M
      FF<17>(C, D, A, B, M[2] + 0x242070DB);
74
3.45M
      FF<22>(B, C, D, A, M[3] + 0xC1BDCEEE);
75
3.45M
      FF<7>(A, B, C, D, M[4] + 0xF57C0FAF);
76
3.45M
      FF<12>(D, A, B, C, M[5] + 0x4787C62A);
77
3.45M
      FF<17>(C, D, A, B, M[6] + 0xA8304613);
78
3.45M
      FF<22>(B, C, D, A, M[7] + 0xFD469501);
79
3.45M
      FF<7>(A, B, C, D, M[8] + 0x698098D8);
80
3.45M
      FF<12>(D, A, B, C, M[9] + 0x8B44F7AF);
81
3.45M
      FF<17>(C, D, A, B, M[10] + 0xFFFF5BB1);
82
3.45M
      FF<22>(B, C, D, A, M[11] + 0x895CD7BE);
83
3.45M
      FF<7>(A, B, C, D, M[12] + 0x6B901122);
84
3.45M
      FF<12>(D, A, B, C, M[13] + 0xFD987193);
85
3.45M
      FF<17>(C, D, A, B, M[14] + 0xA679438E);
86
3.45M
      FF<22>(B, C, D, A, M[15] + 0x49B40821);
87
88
3.45M
      GG<5>(A, B, C, D, M[1] + 0xF61E2562);
89
3.45M
      GG<9>(D, A, B, C, M[6] + 0xC040B340);
90
3.45M
      GG<14>(C, D, A, B, M[11] + 0x265E5A51);
91
3.45M
      GG<20>(B, C, D, A, M[0] + 0xE9B6C7AA);
92
3.45M
      GG<5>(A, B, C, D, M[5] + 0xD62F105D);
93
3.45M
      GG<9>(D, A, B, C, M[10] + 0x02441453);
94
3.45M
      GG<14>(C, D, A, B, M[15] + 0xD8A1E681);
95
3.45M
      GG<20>(B, C, D, A, M[4] + 0xE7D3FBC8);
96
3.45M
      GG<5>(A, B, C, D, M[9] + 0x21E1CDE6);
97
3.45M
      GG<9>(D, A, B, C, M[14] + 0xC33707D6);
98
3.45M
      GG<14>(C, D, A, B, M[3] + 0xF4D50D87);
99
3.45M
      GG<20>(B, C, D, A, M[8] + 0x455A14ED);
100
3.45M
      GG<5>(A, B, C, D, M[13] + 0xA9E3E905);
101
3.45M
      GG<9>(D, A, B, C, M[2] + 0xFCEFA3F8);
102
3.45M
      GG<14>(C, D, A, B, M[7] + 0x676F02D9);
103
3.45M
      GG<20>(B, C, D, A, M[12] + 0x8D2A4C8A);
104
105
3.45M
      HH<4>(A, B, C, D, M[5] + 0xFFFA3942);
106
3.45M
      HH<11>(D, A, B, C, M[8] + 0x8771F681);
107
3.45M
      HH<16>(C, D, A, B, M[11] + 0x6D9D6122);
108
3.45M
      HH<23>(B, C, D, A, M[14] + 0xFDE5380C);
109
3.45M
      HH<4>(A, B, C, D, M[1] + 0xA4BEEA44);
110
3.45M
      HH<11>(D, A, B, C, M[4] + 0x4BDECFA9);
111
3.45M
      HH<16>(C, D, A, B, M[7] + 0xF6BB4B60);
112
3.45M
      HH<23>(B, C, D, A, M[10] + 0xBEBFBC70);
113
3.45M
      HH<4>(A, B, C, D, M[13] + 0x289B7EC6);
114
3.45M
      HH<11>(D, A, B, C, M[0] + 0xEAA127FA);
115
3.45M
      HH<16>(C, D, A, B, M[3] + 0xD4EF3085);
116
3.45M
      HH<23>(B, C, D, A, M[6] + 0x04881D05);
117
3.45M
      HH<4>(A, B, C, D, M[9] + 0xD9D4D039);
118
3.45M
      HH<11>(D, A, B, C, M[12] + 0xE6DB99E5);
119
3.45M
      HH<16>(C, D, A, B, M[15] + 0x1FA27CF8);
120
3.45M
      HH<23>(B, C, D, A, M[2] + 0xC4AC5665);
121
122
3.45M
      II<6>(A, B, C, D, M[0] + 0xF4292244);
123
3.45M
      II<10>(D, A, B, C, M[7] + 0x432AFF97);
124
3.45M
      II<15>(C, D, A, B, M[14] + 0xAB9423A7);
125
3.45M
      II<21>(B, C, D, A, M[5] + 0xFC93A039);
126
3.45M
      II<6>(A, B, C, D, M[12] + 0x655B59C3);
127
3.45M
      II<10>(D, A, B, C, M[3] + 0x8F0CCC92);
128
3.45M
      II<15>(C, D, A, B, M[10] + 0xFFEFF47D);
129
3.45M
      II<21>(B, C, D, A, M[1] + 0x85845DD1);
130
3.45M
      II<6>(A, B, C, D, M[8] + 0x6FA87E4F);
131
3.45M
      II<10>(D, A, B, C, M[15] + 0xFE2CE6E0);
132
3.45M
      II<15>(C, D, A, B, M[6] + 0xA3014314);
133
3.45M
      II<21>(B, C, D, A, M[13] + 0x4E0811A1);
134
3.45M
      II<6>(A, B, C, D, M[4] + 0xF7537E82);
135
3.45M
      II<10>(D, A, B, C, M[11] + 0xBD3AF235);
136
3.45M
      II<15>(C, D, A, B, M[2] + 0x2AD7D2BB);
137
3.45M
      II<21>(B, C, D, A, M[9] + 0xEB86D391);
138
139
3.45M
      A = (digest[0] += A);
140
3.45M
      B = (digest[1] += B);
141
3.45M
      C = (digest[2] += C);
142
3.45M
      D = (digest[3] += D);
143
3.45M
   }
144
244k
}
145
146
86.2k
void MD5::init(digest_type& digest) {
147
86.2k
   digest.assign({0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476});
148
86.2k
}
149
150
0
std::unique_ptr<HashFunction> MD5::new_object() const {
151
0
   return std::make_unique<MD5>();
152
0
}
153
154
0
std::unique_ptr<HashFunction> MD5::copy_state() const {
155
0
   return std::make_unique<MD5>(*this);
156
0
}
157
158
6.60M
void MD5::add_data(std::span<const uint8_t> input) {
159
6.60M
   m_md.update(input);
160
6.60M
}
161
162
38.4k
void MD5::final_result(std::span<uint8_t> output) {
163
38.4k
   m_md.final(output);
164
38.4k
}
165
166
}  // namespace Botan