Botan 1.10.17

md4.cpp
Go to the documentation of this file.
1 /*
2 * MD4
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/md4.h>
9 #include <botan/loadstor.h>
10 #include <botan/rotate.h>
11
12 namespace Botan {
13
14 namespace {
15
16 /*
17 * MD4 FF Function
18 */
19 inline void FF(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit M, byte S)
20 {
21 A += (D ^ (B & (C ^ D))) + M;
22 A = rotate_left(A, S);
23 }
24
25 /*
26 * MD4 GG Function
27 */
28 inline void GG(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit M, byte S)
29 {
30 A += ((B & C) | (D & (B | C))) + M + 0x5A827999;
31 A = rotate_left(A, S);
32 }
33
34 /*
35 * MD4 HH Function
36 */
37 inline void HH(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit M, byte S)
38 {
39 A += (B ^ C ^ D) + M + 0x6ED9EBA1;
40 A = rotate_left(A, S);
41 }
42
43 }
44
45 /*
46 * MD4 Compression Function
47 */
48 void MD4::compress_n(const byte input[], size_t blocks)
49 {
50 u32bit A = digest[0], B = digest[1], C = digest[2], D = digest[3];
51
52 for(size_t i = 0; i != blocks; ++i)
53 {
54 load_le(&M[0], input, M.size());
55
56 FF(A,B,C,D,M[ 0], 3); FF(D,A,B,C,M[ 1], 7);
57 FF(C,D,A,B,M[ 2],11); FF(B,C,D,A,M[ 3],19);
58 FF(A,B,C,D,M[ 4], 3); FF(D,A,B,C,M[ 5], 7);
59 FF(C,D,A,B,M[ 6],11); FF(B,C,D,A,M[ 7],19);
60 FF(A,B,C,D,M[ 8], 3); FF(D,A,B,C,M[ 9], 7);
61 FF(C,D,A,B,M[10],11); FF(B,C,D,A,M[11],19);
62 FF(A,B,C,D,M[12], 3); FF(D,A,B,C,M[13], 7);
63 FF(C,D,A,B,M[14],11); FF(B,C,D,A,M[15],19);
64
65 GG(A,B,C,D,M[ 0], 3); GG(D,A,B,C,M[ 4], 5);
66 GG(C,D,A,B,M[ 8], 9); GG(B,C,D,A,M[12],13);
67 GG(A,B,C,D,M[ 1], 3); GG(D,A,B,C,M[ 5], 5);
68 GG(C,D,A,B,M[ 9], 9); GG(B,C,D,A,M[13],13);
69 GG(A,B,C,D,M[ 2], 3); GG(D,A,B,C,M[ 6], 5);
70 GG(C,D,A,B,M[10], 9); GG(B,C,D,A,M[14],13);
71 GG(A,B,C,D,M[ 3], 3); GG(D,A,B,C,M[ 7], 5);
72 GG(C,D,A,B,M[11], 9); GG(B,C,D,A,M[15],13);
73
74 HH(A,B,C,D,M[ 0], 3); HH(D,A,B,C,M[ 8], 9);
75 HH(C,D,A,B,M[ 4],11); HH(B,C,D,A,M[12],15);
76 HH(A,B,C,D,M[ 2], 3); HH(D,A,B,C,M[10], 9);
77 HH(C,D,A,B,M[ 6],11); HH(B,C,D,A,M[14],15);
78 HH(A,B,C,D,M[ 1], 3); HH(D,A,B,C,M[ 9], 9);
79 HH(C,D,A,B,M[ 5],11); HH(B,C,D,A,M[13],15);
80 HH(A,B,C,D,M[ 3], 3); HH(D,A,B,C,M[11], 9);
81 HH(C,D,A,B,M[ 7],11); HH(B,C,D,A,M[15],15);
82
83 A = (digest[0] += A);
84 B = (digest[1] += B);
85 C = (digest[2] += C);
86 D = (digest[3] += D);
87
88 input += hash_block_size();
89 }
90 }
91
92 /*
93 * Copy out the digest
94 */
95 void MD4::copy_out(byte output[])
96 {
97 for(size_t i = 0; i != output_length(); i += 4)
98 store_le(digest[i/4], output + i);
99 }
100
101 /*
102 * Clear memory of sensitive data
103 */
104 void MD4::clear()
105 {
106 MDx_HashFunction::clear();
107 zeroise(M);
108 digest[0] = 0x67452301;
109 digest[1] = 0xEFCDAB89;
110 digest[2] = 0x98BADCFE;
111 digest[3] = 0x10325476;
112 }
113
114 }
Botan::MD4::digest
SecureVector< u32bit > digest
Definition: md4.h:41
Botan::load_le
T load_le(const byte in[], size_t off)
Definition: loadstor.h:116
Botan::MD4::M
SecureVector< u32bit > M
Definition: md4.h:36
Botan::store_le
void store_le(u16bit in, byte out[2])
Definition: loadstor.h:427
Botan::rotate_left
T rotate_left(T input, size_t rot)
Definition: rotate.h:21
Botan::byte
unsigned char byte
Definition: types.h:22
Botan
Definition: algo_base.h:14
Botan::MD4::compress_n
void compress_n(const byte input[], size_t blocks)
Definition: md4.cpp:48
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::MD4::output_length
size_t output_length() const
Definition: md4.h:22
Botan::MD4::clear
void clear()
Definition: md4.cpp:104
Botan::MDx_HashFunction::clear
void clear()
Definition: mdx_hash.cpp:32
Botan::MDx_HashFunction::hash_block_size
size_t hash_block_size() const
Definition: mdx_hash.h:32
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428
Botan::MD4::copy_out
void copy_out(byte[])
Definition: md4.cpp:95
Botan::u32bit
unsigned int u32bit
Definition: types.h:32