Botan  2.9.0
Crypto and TLS for C++11
md4.cpp
Go to the documentation of this file.
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/rotate.h>
10 
11 namespace Botan {
12 
13 std::unique_ptr<HashFunction> MD4::copy_state() const
14  {
15  return std::unique_ptr<HashFunction>(new MD4(*this));
16  }
17 
18 namespace {
19 
20 inline void FF4(uint32_t& A, uint32_t& B, uint32_t& C, uint32_t& D,
21  uint32_t M0, uint32_t M1, uint32_t M2, uint32_t M3)
22 
23  {
24  A += (D ^ (B & (C ^ D))) + M0;
25  A = rotl<3>(A);
26 
27  D += (C ^ (A & (B ^ C))) + M1;
28  D = rotl<7>(D);
29 
30  C += (B ^ (D & (A ^ B))) + M2;
31  C = rotl<11>(C);
32 
33  B += (A ^ (C & (D ^ A))) + M3;
34  B = rotl<19>(B);
35  }
36 
37 inline void GG4(uint32_t& A, uint32_t& B, uint32_t& C, uint32_t& D,
38  uint32_t M0, uint32_t M1, uint32_t M2, uint32_t M3)
39 
40  {
41  A += ((B & C) | (D & (B | C))) + M0 + 0x5A827999;
42  A = rotl<3>(A);
43 
44  D += ((A & B) | (C & (A | B))) + M1 + 0x5A827999;
45  D = rotl<5>(D);
46 
47  C += ((D & A) | (B & (D | A))) + M2 + 0x5A827999;
48  C = rotl<9>(C);
49 
50  B += ((C & D) | (A & (C | D))) + M3 + 0x5A827999;
51  B = rotl<13>(B);
52  }
53 
54 inline void HH4(uint32_t& A, uint32_t& B, uint32_t& C, uint32_t& D,
55  uint32_t M0, uint32_t M1, uint32_t M2, uint32_t M3)
56 
57  {
58  A += (B ^ C ^ D) + M0 + 0x6ED9EBA1;
59  A = rotl<3>(A);
60 
61  D += (A ^ B ^ C) + M1 + 0x6ED9EBA1;
62  D = rotl<9>(D);
63 
64  C += (A ^ B ^ D) + M2 + 0x6ED9EBA1;
65  C = rotl<11>(C);
66 
67  B += (A ^ C ^ D) + M3 + 0x6ED9EBA1;
68  B = rotl<15>(B);
69  }
70 
71 }
72 
73 /*
74 * MD4 Compression Function
75 */
76 void MD4::compress_n(const uint8_t input[], size_t blocks)
77  {
78  uint32_t A = m_digest[0], B = m_digest[1], C = m_digest[2], D = m_digest[3];
79 
80  for(size_t i = 0; i != blocks; ++i)
81  {
82  uint32_t M00 = load_le<uint32_t>(input, 0);
83  uint32_t M01 = load_le<uint32_t>(input, 1);
84  uint32_t M02 = load_le<uint32_t>(input, 2);
85  uint32_t M03 = load_le<uint32_t>(input, 3);
86  uint32_t M04 = load_le<uint32_t>(input, 4);
87  uint32_t M05 = load_le<uint32_t>(input, 5);
88  uint32_t M06 = load_le<uint32_t>(input, 6);
89  uint32_t M07 = load_le<uint32_t>(input, 7);
90  uint32_t M08 = load_le<uint32_t>(input, 8);
91  uint32_t M09 = load_le<uint32_t>(input, 9);
92  uint32_t M10 = load_le<uint32_t>(input, 10);
93  uint32_t M11 = load_le<uint32_t>(input, 11);
94  uint32_t M12 = load_le<uint32_t>(input, 12);
95  uint32_t M13 = load_le<uint32_t>(input, 13);
96  uint32_t M14 = load_le<uint32_t>(input, 14);
97  uint32_t M15 = load_le<uint32_t>(input, 15);
98 
99  FF4(A, B, C, D, M00, M01, M02, M03);
100  FF4(A, B, C, D, M04, M05, M06, M07);
101  FF4(A, B, C, D, M08, M09, M10, M11);
102  FF4(A, B, C, D, M12, M13, M14, M15);
103 
104  GG4(A, B, C, D, M00, M04, M08, M12);
105  GG4(A, B, C, D, M01, M05, M09, M13);
106  GG4(A, B, C, D, M02, M06, M10, M14);
107  GG4(A, B, C, D, M03, M07, M11, M15);
108 
109  HH4(A, B, C, D, M00, M08, M04, M12);
110  HH4(A, B, C, D, M02, M10, M06, M14);
111  HH4(A, B, C, D, M01, M09, M05, M13);
112  HH4(A, B, C, D, M03, M11, M07, M15);
113 
114  A = (m_digest[0] += A);
115  B = (m_digest[1] += B);
116  C = (m_digest[2] += C);
117  D = (m_digest[3] += D);
118 
119  input += hash_block_size();
120  }
121  }
122 
123 /*
124 * Copy out the digest
125 */
126 void MD4::copy_out(uint8_t output[])
127  {
128  copy_out_vec_le(output, output_length(), m_digest);
129  }
130 
131 /*
132 * Clear memory of sensitive data
133 */
135  {
137  m_digest[0] = 0x67452301;
138  m_digest[1] = 0xEFCDAB89;
139  m_digest[2] = 0x98BADCFE;
140  m_digest[3] = 0x10325476;
141  }
142 
143 }
void clear() override
Definition: mdx_hash.cpp:41
MD4()
Definition: md4.h:28
uint32_t load_le< uint32_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:196
size_t output_length() const override
Definition: md4.h:22
size_t hash_block_size() const override final
Definition: mdx_hash.h:33
void clear() override
Definition: md4.cpp:134
Definition: alg_id.cpp:13
std::unique_ptr< HashFunction > copy_state() const override
Definition: md4.cpp:13
void copy_out_vec_le(uint8_t out[], size_t out_bytes, const std::vector< T, Alloc > &in)
Definition: loadstor.h:692