Coverage Report

Created: 2020-11-21 08:34

/src/botan/src/lib/hash/md5/md5.cpp
Line
Count
Source (jump to first uncovered line)
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
#include <botan/internal/loadstor.h>
10
#include <botan/internal/rotate.h>
11
12
namespace Botan {
13
14
std::unique_ptr<HashFunction> MD5::copy_state() const
15
0
   {
16
0
   return std::unique_ptr<HashFunction>(new MD5(*this));
17
0
   }
18
19
namespace {
20
21
/*
22
* MD5 FF Function
23
*/
24
template<size_t S>
25
inline void FF(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M)
26
8.27k
   {
27
8.27k
   A += (D ^ (B & (C ^ D))) + M;
28
8.27k
   A  = rotl<S>(A) + B;
29
8.27k
   }
md5.cpp:void Botan::(anonymous namespace)::FF<7ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
26
2.06k
   {
27
2.06k
   A += (D ^ (B & (C ^ D))) + M;
28
2.06k
   A  = rotl<S>(A) + B;
29
2.06k
   }
md5.cpp:void Botan::(anonymous namespace)::FF<12ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
26
2.06k
   {
27
2.06k
   A += (D ^ (B & (C ^ D))) + M;
28
2.06k
   A  = rotl<S>(A) + B;
29
2.06k
   }
md5.cpp:void Botan::(anonymous namespace)::FF<17ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
26
2.06k
   {
27
2.06k
   A += (D ^ (B & (C ^ D))) + M;
28
2.06k
   A  = rotl<S>(A) + B;
29
2.06k
   }
md5.cpp:void Botan::(anonymous namespace)::FF<22ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
26
2.06k
   {
27
2.06k
   A += (D ^ (B & (C ^ D))) + M;
28
2.06k
   A  = rotl<S>(A) + B;
29
2.06k
   }
30
31
/*
32
* MD5 GG Function
33
*/
34
template<size_t S>
35
inline void GG(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M)
36
8.27k
   {
37
8.27k
   A += (C ^ (D & (B ^ C))) + M;
38
8.27k
   A  = rotl<S>(A) + B;
39
8.27k
   }
md5.cpp:void Botan::(anonymous namespace)::GG<5ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
36
2.06k
   {
37
2.06k
   A += (C ^ (D & (B ^ C))) + M;
38
2.06k
   A  = rotl<S>(A) + B;
39
2.06k
   }
md5.cpp:void Botan::(anonymous namespace)::GG<9ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
36
2.06k
   {
37
2.06k
   A += (C ^ (D & (B ^ C))) + M;
38
2.06k
   A  = rotl<S>(A) + B;
39
2.06k
   }
md5.cpp:void Botan::(anonymous namespace)::GG<14ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
36
2.06k
   {
37
2.06k
   A += (C ^ (D & (B ^ C))) + M;
38
2.06k
   A  = rotl<S>(A) + B;
39
2.06k
   }
md5.cpp:void Botan::(anonymous namespace)::GG<20ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
36
2.06k
   {
37
2.06k
   A += (C ^ (D & (B ^ C))) + M;
38
2.06k
   A  = rotl<S>(A) + B;
39
2.06k
   }
40
41
/*
42
* MD5 HH Function
43
*/
44
template<size_t S>
45
inline void HH(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M)
46
8.27k
   {
47
8.27k
   A += (B ^ C ^ D) + M;
48
8.27k
   A  = rotl<S>(A) + B;
49
8.27k
   }
md5.cpp:void Botan::(anonymous namespace)::HH<4ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
46
2.06k
   {
47
2.06k
   A += (B ^ C ^ D) + M;
48
2.06k
   A  = rotl<S>(A) + B;
49
2.06k
   }
md5.cpp:void Botan::(anonymous namespace)::HH<11ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
46
2.06k
   {
47
2.06k
   A += (B ^ C ^ D) + M;
48
2.06k
   A  = rotl<S>(A) + B;
49
2.06k
   }
md5.cpp:void Botan::(anonymous namespace)::HH<16ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
46
2.06k
   {
47
2.06k
   A += (B ^ C ^ D) + M;
48
2.06k
   A  = rotl<S>(A) + B;
49
2.06k
   }
md5.cpp:void Botan::(anonymous namespace)::HH<23ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
46
2.06k
   {
47
2.06k
   A += (B ^ C ^ D) + M;
48
2.06k
   A  = rotl<S>(A) + B;
49
2.06k
   }
50
51
/*
52
* MD5 II Function
53
*/
54
template<size_t S>
55
inline void II(uint32_t& A, uint32_t B, uint32_t C, uint32_t D, uint32_t M)
56
8.27k
   {
57
8.27k
   A += (C ^ (B | ~D)) + M;
58
8.27k
   A  = rotl<S>(A) + B;
59
8.27k
   }
md5.cpp:void Botan::(anonymous namespace)::II<6ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
56
2.06k
   {
57
2.06k
   A += (C ^ (B | ~D)) + M;
58
2.06k
   A  = rotl<S>(A) + B;
59
2.06k
   }
md5.cpp:void Botan::(anonymous namespace)::II<10ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
56
2.06k
   {
57
2.06k
   A += (C ^ (B | ~D)) + M;
58
2.06k
   A  = rotl<S>(A) + B;
59
2.06k
   }
md5.cpp:void Botan::(anonymous namespace)::II<15ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
56
2.06k
   {
57
2.06k
   A += (C ^ (B | ~D)) + M;
58
2.06k
   A  = rotl<S>(A) + B;
59
2.06k
   }
md5.cpp:void Botan::(anonymous namespace)::II<21ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int)
Line
Count
Source
56
2.06k
   {
57
2.06k
   A += (C ^ (B | ~D)) + M;
58
2.06k
   A  = rotl<S>(A) + B;
59
2.06k
   }
60
61
}
62
63
/*
64
* MD5 Compression Function
65
*/
66
void MD5::compress_n(const uint8_t input[], size_t blocks)
67
202
   {
68
202
   uint32_t A = m_digest[0], B = m_digest[1], C = m_digest[2], D = m_digest[3];
69
70
719
   for(size_t i = 0; i != blocks; ++i)
71
517
      {
72
517
      load_le(m_M.data(), input, m_M.size());
73
74
517
      FF< 7>(A,B,C,D,m_M[ 0]+0xD76AA478);   FF<12>(D,A,B,C,m_M[ 1]+0xE8C7B756);
75
517
      FF<17>(C,D,A,B,m_M[ 2]+0x242070DB);   FF<22>(B,C,D,A,m_M[ 3]+0xC1BDCEEE);
76
517
      FF< 7>(A,B,C,D,m_M[ 4]+0xF57C0FAF);   FF<12>(D,A,B,C,m_M[ 5]+0x4787C62A);
77
517
      FF<17>(C,D,A,B,m_M[ 6]+0xA8304613);   FF<22>(B,C,D,A,m_M[ 7]+0xFD469501);
78
517
      FF< 7>(A,B,C,D,m_M[ 8]+0x698098D8);   FF<12>(D,A,B,C,m_M[ 9]+0x8B44F7AF);
79
517
      FF<17>(C,D,A,B,m_M[10]+0xFFFF5BB1);   FF<22>(B,C,D,A,m_M[11]+0x895CD7BE);
80
517
      FF< 7>(A,B,C,D,m_M[12]+0x6B901122);   FF<12>(D,A,B,C,m_M[13]+0xFD987193);
81
517
      FF<17>(C,D,A,B,m_M[14]+0xA679438E);   FF<22>(B,C,D,A,m_M[15]+0x49B40821);
82
83
517
      GG< 5>(A,B,C,D,m_M[ 1]+0xF61E2562);   GG< 9>(D,A,B,C,m_M[ 6]+0xC040B340);
84
517
      GG<14>(C,D,A,B,m_M[11]+0x265E5A51);   GG<20>(B,C,D,A,m_M[ 0]+0xE9B6C7AA);
85
517
      GG< 5>(A,B,C,D,m_M[ 5]+0xD62F105D);   GG< 9>(D,A,B,C,m_M[10]+0x02441453);
86
517
      GG<14>(C,D,A,B,m_M[15]+0xD8A1E681);   GG<20>(B,C,D,A,m_M[ 4]+0xE7D3FBC8);
87
517
      GG< 5>(A,B,C,D,m_M[ 9]+0x21E1CDE6);   GG< 9>(D,A,B,C,m_M[14]+0xC33707D6);
88
517
      GG<14>(C,D,A,B,m_M[ 3]+0xF4D50D87);   GG<20>(B,C,D,A,m_M[ 8]+0x455A14ED);
89
517
      GG< 5>(A,B,C,D,m_M[13]+0xA9E3E905);   GG< 9>(D,A,B,C,m_M[ 2]+0xFCEFA3F8);
90
517
      GG<14>(C,D,A,B,m_M[ 7]+0x676F02D9);   GG<20>(B,C,D,A,m_M[12]+0x8D2A4C8A);
91
92
517
      HH< 4>(A,B,C,D,m_M[ 5]+0xFFFA3942);   HH<11>(D,A,B,C,m_M[ 8]+0x8771F681);
93
517
      HH<16>(C,D,A,B,m_M[11]+0x6D9D6122);   HH<23>(B,C,D,A,m_M[14]+0xFDE5380C);
94
517
      HH< 4>(A,B,C,D,m_M[ 1]+0xA4BEEA44);   HH<11>(D,A,B,C,m_M[ 4]+0x4BDECFA9);
95
517
      HH<16>(C,D,A,B,m_M[ 7]+0xF6BB4B60);   HH<23>(B,C,D,A,m_M[10]+0xBEBFBC70);
96
517
      HH< 4>(A,B,C,D,m_M[13]+0x289B7EC6);   HH<11>(D,A,B,C,m_M[ 0]+0xEAA127FA);
97
517
      HH<16>(C,D,A,B,m_M[ 3]+0xD4EF3085);   HH<23>(B,C,D,A,m_M[ 6]+0x04881D05);
98
517
      HH< 4>(A,B,C,D,m_M[ 9]+0xD9D4D039);   HH<11>(D,A,B,C,m_M[12]+0xE6DB99E5);
99
517
      HH<16>(C,D,A,B,m_M[15]+0x1FA27CF8);   HH<23>(B,C,D,A,m_M[ 2]+0xC4AC5665);
100
101
517
      II< 6>(A,B,C,D,m_M[ 0]+0xF4292244);   II<10>(D,A,B,C,m_M[ 7]+0x432AFF97);
102
517
      II<15>(C,D,A,B,m_M[14]+0xAB9423A7);   II<21>(B,C,D,A,m_M[ 5]+0xFC93A039);
103
517
      II< 6>(A,B,C,D,m_M[12]+0x655B59C3);   II<10>(D,A,B,C,m_M[ 3]+0x8F0CCC92);
104
517
      II<15>(C,D,A,B,m_M[10]+0xFFEFF47D);   II<21>(B,C,D,A,m_M[ 1]+0x85845DD1);
105
517
      II< 6>(A,B,C,D,m_M[ 8]+0x6FA87E4F);   II<10>(D,A,B,C,m_M[15]+0xFE2CE6E0);
106
517
      II<15>(C,D,A,B,m_M[ 6]+0xA3014314);   II<21>(B,C,D,A,m_M[13]+0x4E0811A1);
107
517
      II< 6>(A,B,C,D,m_M[ 4]+0xF7537E82);   II<10>(D,A,B,C,m_M[11]+0xBD3AF235);
108
517
      II<15>(C,D,A,B,m_M[ 2]+0x2AD7D2BB);   II<21>(B,C,D,A,m_M[ 9]+0xEB86D391);
109
110
517
      A = (m_digest[0] += A);
111
517
      B = (m_digest[1] += B);
112
517
      C = (m_digest[2] += C);
113
517
      D = (m_digest[3] += D);
114
115
517
      input += hash_block_size();
116
517
      }
117
202
   }
118
119
/*
120
* Copy out the digest
121
*/
122
void MD5::copy_out(uint8_t output[])
123
100
   {
124
100
   copy_out_vec_le(output, output_length(), m_digest);
125
100
   }
126
127
/*
128
* Clear memory of sensitive data
129
*/
130
void MD5::clear()
131
200
   {
132
200
   MDx_HashFunction::clear();
133
200
   zeroise(m_M);
134
200
   m_digest[0] = 0x67452301;
135
200
   m_digest[1] = 0xEFCDAB89;
136
200
   m_digest[2] = 0x98BADCFE;
137
200
   m_digest[3] = 0x10325476;
138
200
   }
139
140
}