Coverage Report

Created: 2023-06-07 07:00

/src/botan/src/lib/hash/md4/md4.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* MD4
3
* (C) 1999-2007 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#include <botan/internal/md4.h>
9
10
#include <botan/internal/bit_ops.h>
11
#include <botan/internal/loadstor.h>
12
#include <botan/internal/rotate.h>
13
14
namespace Botan {
15
16
0
std::unique_ptr<HashFunction> MD4::copy_state() const { return std::make_unique<MD4>(*this); }
17
18
namespace {
19
20
inline void FF4(uint32_t& A, uint32_t& B, uint32_t& C, uint32_t& D, uint32_t M0, uint32_t M1, uint32_t M2, uint32_t M3)
21
22
0
{
23
0
   A += choose(B, C, D) + M0;
24
0
   A = rotl<3>(A);
25
26
0
   D += choose(A, B, C) + M1;
27
0
   D = rotl<7>(D);
28
29
0
   C += choose(D, A, B) + M2;
30
0
   C = rotl<11>(C);
31
32
0
   B += choose(C, D, A) + M3;
33
0
   B = rotl<19>(B);
34
0
}
35
36
inline void GG4(uint32_t& A, uint32_t& B, uint32_t& C, uint32_t& D, uint32_t M0, uint32_t M1, uint32_t M2, uint32_t M3)
37
38
0
{
39
   /*
40
   These are choose(D, B | C, B & C) but the below expression
41
   takes advantage of the fact that B & C is a subset of B | C
42
   to eliminate an and
43
   */
44
45
0
   A += ((B & C) | (D & (B | C))) + M0 + 0x5A827999;
46
0
   A = rotl<3>(A);
47
48
0
   D += ((A & B) | (C & (A | B))) + M1 + 0x5A827999;
49
0
   D = rotl<5>(D);
50
51
0
   C += ((D & A) | (B & (D | A))) + M2 + 0x5A827999;
52
0
   C = rotl<9>(C);
53
54
0
   B += ((C & D) | (A & (C | D))) + M3 + 0x5A827999;
55
0
   B = rotl<13>(B);
56
0
}
57
58
inline void HH4(uint32_t& A, uint32_t& B, uint32_t& C, uint32_t& D, uint32_t M0, uint32_t M1, uint32_t M2, uint32_t M3)
59
60
0
{
61
0
   A += (B ^ C ^ D) + M0 + 0x6ED9EBA1;
62
0
   A = rotl<3>(A);
63
64
0
   D += (A ^ B ^ C) + M1 + 0x6ED9EBA1;
65
0
   D = rotl<9>(D);
66
67
0
   C += (A ^ B ^ D) + M2 + 0x6ED9EBA1;
68
0
   C = rotl<11>(C);
69
70
0
   B += (A ^ C ^ D) + M3 + 0x6ED9EBA1;
71
0
   B = rotl<15>(B);
72
0
}
73
74
}  // namespace
75
76
/*
77
* MD4 Compression Function
78
*/
79
0
void MD4::compress_n(const uint8_t input[], size_t blocks) {
80
0
   uint32_t A = m_digest[0], B = m_digest[1], C = m_digest[2], D = m_digest[3];
81
82
0
   for(size_t i = 0; i != blocks; ++i) {
83
0
      uint32_t M00 = load_le<uint32_t>(input, 0);
84
0
      uint32_t M01 = load_le<uint32_t>(input, 1);
85
0
      uint32_t M02 = load_le<uint32_t>(input, 2);
86
0
      uint32_t M03 = load_le<uint32_t>(input, 3);
87
0
      uint32_t M04 = load_le<uint32_t>(input, 4);
88
0
      uint32_t M05 = load_le<uint32_t>(input, 5);
89
0
      uint32_t M06 = load_le<uint32_t>(input, 6);
90
0
      uint32_t M07 = load_le<uint32_t>(input, 7);
91
0
      uint32_t M08 = load_le<uint32_t>(input, 8);
92
0
      uint32_t M09 = load_le<uint32_t>(input, 9);
93
0
      uint32_t M10 = load_le<uint32_t>(input, 10);
94
0
      uint32_t M11 = load_le<uint32_t>(input, 11);
95
0
      uint32_t M12 = load_le<uint32_t>(input, 12);
96
0
      uint32_t M13 = load_le<uint32_t>(input, 13);
97
0
      uint32_t M14 = load_le<uint32_t>(input, 14);
98
0
      uint32_t M15 = load_le<uint32_t>(input, 15);
99
100
0
      FF4(A, B, C, D, M00, M01, M02, M03);
101
0
      FF4(A, B, C, D, M04, M05, M06, M07);
102
0
      FF4(A, B, C, D, M08, M09, M10, M11);
103
0
      FF4(A, B, C, D, M12, M13, M14, M15);
104
105
0
      GG4(A, B, C, D, M00, M04, M08, M12);
106
0
      GG4(A, B, C, D, M01, M05, M09, M13);
107
0
      GG4(A, B, C, D, M02, M06, M10, M14);
108
0
      GG4(A, B, C, D, M03, M07, M11, M15);
109
110
0
      HH4(A, B, C, D, M00, M08, M04, M12);
111
0
      HH4(A, B, C, D, M02, M10, M06, M14);
112
0
      HH4(A, B, C, D, M01, M09, M05, M13);
113
0
      HH4(A, B, C, D, M03, M11, M07, M15);
114
115
0
      A = (m_digest[0] += A);
116
0
      B = (m_digest[1] += B);
117
0
      C = (m_digest[2] += C);
118
0
      D = (m_digest[3] += D);
119
120
0
      input += hash_block_size();
121
0
   }
122
0
}
123
124
/*
125
* Copy out the digest
126
*/
127
0
void MD4::copy_out(uint8_t output[]) { copy_out_vec_le(output, output_length(), m_digest); }
128
129
/*
130
* Clear memory of sensitive data
131
*/
132
0
void MD4::clear() {
133
0
   MDx_HashFunction::clear();
134
0
   m_digest[0] = 0x67452301;
135
0
   m_digest[1] = 0xEFCDAB89;
136
0
   m_digest[2] = 0x98BADCFE;
137
0
   m_digest[3] = 0x10325476;
138
0
}
139
140
}  // namespace Botan