Botan 2.19.5 Crypto and TLS for C&

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