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