Coverage Report

Created: 2020-03-26 13:53

/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/md4.h>
9
#include <botan/loadstor.h>
10
#include <botan/rotate.h>
11
12
namespace Botan {
13
14
std::unique_ptr<HashFunction> MD4::copy_state() const
15
0
   {
16
0
   return std::unique_ptr<HashFunction>(new MD4(*this));
17
0
   }
18
19
namespace {
20
21
inline void FF4(uint32_t& A, uint32_t& B, uint32_t& C, uint32_t& D,
22
                uint32_t M0, uint32_t M1, uint32_t M2, uint32_t M3)
23
24
0
   {
25
0
   A += (D ^ (B & (C ^ D))) + M0;
26
0
   A = rotl<3>(A);
27
0
28
0
   D += (C ^ (A & (B ^ C))) + M1;
29
0
   D = rotl<7>(D);
30
0
31
0
   C += (B ^ (D & (A ^ B))) + M2;
32
0
   C = rotl<11>(C);
33
0
34
0
   B += (A ^ (C & (D ^ A))) + M3;
35
0
   B = rotl<19>(B);
36
0
   }
37
38
inline void GG4(uint32_t& A, uint32_t& B, uint32_t& C, uint32_t& D,
39
                uint32_t M0, uint32_t M1, uint32_t M2, uint32_t M3)
40
41
0
   {
42
0
   A += ((B & C) | (D & (B | C))) + M0 + 0x5A827999;
43
0
   A = rotl<3>(A);
44
0
45
0
   D += ((A & B) | (C & (A | B))) + M1 + 0x5A827999;
46
0
   D = rotl<5>(D);
47
0
48
0
   C += ((D & A) | (B & (D | A))) + M2 + 0x5A827999;
49
0
   C = rotl<9>(C);
50
0
51
0
   B += ((C & D) | (A & (C | D))) + M3 + 0x5A827999;
52
0
   B = rotl<13>(B);
53
0
   }
54
55
inline void HH4(uint32_t& A, uint32_t& B, uint32_t& C, uint32_t& D,
56
                uint32_t M0, uint32_t M1, uint32_t M2, uint32_t M3)
57
58
0
   {
59
0
   A += (B ^ C ^ D) + M0 + 0x6ED9EBA1;
60
0
   A = rotl<3>(A);
61
0
62
0
   D += (A ^ B ^ C) + M1 + 0x6ED9EBA1;
63
0
   D = rotl<9>(D);
64
0
65
0
   C += (A ^ B ^ D) + M2 + 0x6ED9EBA1;
66
0
   C = rotl<11>(C);
67
0
68
0
   B += (A ^ C ^ D) + M3 + 0x6ED9EBA1;
69
0
   B = rotl<15>(B);
70
0
   }
71
72
}
73
74
/*
75
* MD4 Compression Function
76
*/
77
void MD4::compress_n(const uint8_t input[], size_t blocks)
78
0
   {
79
0
   uint32_t A = m_digest[0], B = m_digest[1], C = m_digest[2], D = m_digest[3];
80
0
81
0
   for(size_t i = 0; i != blocks; ++i)
82
0
      {
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
0
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
0
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
0
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
0
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
0
120
0
      input += hash_block_size();
121
0
      }
122
0
   }
123
124
/*
125
* Copy out the digest
126
*/
127
void MD4::copy_out(uint8_t output[])
128
0
   {
129
0
   copy_out_vec_le(output, output_length(), m_digest);
130
0
   }
131
132
/*
133
* Clear memory of sensitive data
134
*/
135
void MD4::clear()
136
0
   {
137
0
   MDx_HashFunction::clear();
138
0
   m_digest[0] = 0x67452301;
139
0
   m_digest[1] = 0xEFCDAB89;
140
0
   m_digest[2] = 0x98BADCFE;
141
0
   m_digest[3] = 0x10325476;
142
0
   }
143
144
}