Botan 3.4.0
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/internal/md4.h>
9
10#include <botan/internal/bit_ops.h>
11#include <botan/internal/loadstor.h>
12#include <botan/internal/rotate.h>
13
14namespace Botan {
15
16namespace {
17
18inline 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)
19
20{
21 A += choose(B, C, D) + M0;
22 A = rotl<3>(A);
23
24 D += choose(A, B, C) + M1;
25 D = rotl<7>(D);
26
27 C += choose(D, A, B) + M2;
28 C = rotl<11>(C);
29
30 B += choose(C, D, A) + M3;
31 B = rotl<19>(B);
32}
33
34inline 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)
35
36{
37 /*
38 These are choose(D, B | C, B & C) but the below expression
39 takes advantage of the fact that B & C is a subset of B | C
40 to eliminate an and
41 */
42
43 A += ((B & C) | (D & (B | C))) + M0 + 0x5A827999;
44 A = rotl<3>(A);
45
46 D += ((A & B) | (C & (A | B))) + M1 + 0x5A827999;
47 D = rotl<5>(D);
48
49 C += ((D & A) | (B & (D | A))) + M2 + 0x5A827999;
50 C = rotl<9>(C);
51
52 B += ((C & D) | (A & (C | D))) + M3 + 0x5A827999;
53 B = rotl<13>(B);
54}
55
56inline 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)
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} // namespace
73
74/*
75* MD4 Compression Function
76*/
77void MD4::compress_n(digest_type& digest, std::span<const uint8_t> input, size_t blocks) {
78 uint32_t A = digest[0], B = digest[1], C = digest[2], D = digest[3];
79
80 BufferSlicer in(input);
81
82 for(size_t i = 0; i != blocks; ++i) {
83 const auto block = in.take(block_bytes).data();
84
85 uint32_t M00 = load_le<uint32_t>(block, 0);
86 uint32_t M01 = load_le<uint32_t>(block, 1);
87 uint32_t M02 = load_le<uint32_t>(block, 2);
88 uint32_t M03 = load_le<uint32_t>(block, 3);
89 uint32_t M04 = load_le<uint32_t>(block, 4);
90 uint32_t M05 = load_le<uint32_t>(block, 5);
91 uint32_t M06 = load_le<uint32_t>(block, 6);
92 uint32_t M07 = load_le<uint32_t>(block, 7);
93 uint32_t M08 = load_le<uint32_t>(block, 8);
94 uint32_t M09 = load_le<uint32_t>(block, 9);
95 uint32_t M10 = load_le<uint32_t>(block, 10);
96 uint32_t M11 = load_le<uint32_t>(block, 11);
97 uint32_t M12 = load_le<uint32_t>(block, 12);
98 uint32_t M13 = load_le<uint32_t>(block, 13);
99 uint32_t M14 = load_le<uint32_t>(block, 14);
100 uint32_t M15 = load_le<uint32_t>(block, 15);
101
102 FF4(A, B, C, D, M00, M01, M02, M03);
103 FF4(A, B, C, D, M04, M05, M06, M07);
104 FF4(A, B, C, D, M08, M09, M10, M11);
105 FF4(A, B, C, D, M12, M13, M14, M15);
106
107 GG4(A, B, C, D, M00, M04, M08, M12);
108 GG4(A, B, C, D, M01, M05, M09, M13);
109 GG4(A, B, C, D, M02, M06, M10, M14);
110 GG4(A, B, C, D, M03, M07, M11, M15);
111
112 HH4(A, B, C, D, M00, M08, M04, M12);
113 HH4(A, B, C, D, M02, M10, M06, M14);
114 HH4(A, B, C, D, M01, M09, M05, M13);
115 HH4(A, B, C, D, M03, M11, M07, M15);
116
117 A = (digest[0] += A);
118 B = (digest[1] += B);
119 C = (digest[2] += C);
120 D = (digest[3] += D);
121 }
122
124}
125
126void MD4::init(digest_type& digest) {
127 digest.assign({0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476});
128}
129
130std::unique_ptr<HashFunction> MD4::new_object() const {
131 return std::make_unique<MD4>();
132}
133
134std::unique_ptr<HashFunction> MD4::copy_state() const {
135 return std::make_unique<MD4>(*this);
136}
137
138void MD4::add_data(std::span<const uint8_t> input) {
139 m_md.update(input);
140}
141
142void MD4::final_result(std::span<uint8_t> output) {
143 m_md.final(output);
144}
145
146} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
bool empty() const
Definition stl_util.h:187
std::span< const uint8_t > take(const size_t count)
Definition stl_util.h:156
secure_vector< uint32_t > digest_type
Definition md4.h:20
static void init(digest_type &digest)
Definition md4.cpp:126
static void compress_n(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
Definition md4.cpp:77
std::unique_ptr< HashFunction > copy_state() const override
Definition md4.cpp:134
static constexpr size_t block_bytes
Definition md4.h:24
std::unique_ptr< HashFunction > new_object() const override
Definition md4.cpp:130
constexpr T choose(T mask, T a, T b)
Definition bit_ops.h:180