Botan 1.10.17

md5.cpp
Go to the documentation of this file.
1 /*
2 * MD5
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/md5.h>
9 #include <botan/loadstor.h>
10 #include <botan/rotate.h>
11
12 namespace Botan {
13
14 namespace {
15
16 /*
17 * MD5 FF Function
18 */
19 inline void FF(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg,
20 byte S, u32bit magic)
21 {
22 A += (D ^ (B & (C ^ D))) + msg + magic;
23 A = rotate_left(A, S) + B;
24 }
25
26 /*
27 * MD5 GG Function
28 */
29 inline void GG(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg,
30 byte S, u32bit magic)
31 {
32 A += (C ^ (D & (B ^ C))) + msg + magic;
33 A = rotate_left(A, S) + B;
34 }
35
36 /*
37 * MD5 HH Function
38 */
39 inline void HH(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg,
40 byte S, u32bit magic)
41 {
42 A += (B ^ C ^ D) + msg + magic;
43 A = rotate_left(A, S) + B;
44 }
45
46 /*
47 * MD5 II Function
48 */
49 inline void II(u32bit& A, u32bit B, u32bit C, u32bit D, u32bit msg,
50 byte S, u32bit magic)
51 {
52 A += (C ^ (B | ~D)) + msg + magic;
53 A = rotate_left(A, S) + B;
54 }
55
56 }
57
58 /*
59 * MD5 Compression Function
60 */
61 void MD5::compress_n(const byte input[], size_t blocks)
62 {
63 u32bit A = digest[0], B = digest[1], C = digest[2], D = digest[3];
64
65 for(size_t i = 0; i != blocks; ++i)
66 {
67 load_le(&M[0], input, M.size());
68
69 FF(A,B,C,D,M[ 0], 7,0xD76AA478); FF(D,A,B,C,M[ 1],12,0xE8C7B756);
70 FF(C,D,A,B,M[ 2],17,0x242070DB); FF(B,C,D,A,M[ 3],22,0xC1BDCEEE);
71 FF(A,B,C,D,M[ 4], 7,0xF57C0FAF); FF(D,A,B,C,M[ 5],12,0x4787C62A);
72 FF(C,D,A,B,M[ 6],17,0xA8304613); FF(B,C,D,A,M[ 7],22,0xFD469501);
73 FF(A,B,C,D,M[ 8], 7,0x698098D8); FF(D,A,B,C,M[ 9],12,0x8B44F7AF);
74 FF(C,D,A,B,M[10],17,0xFFFF5BB1); FF(B,C,D,A,M[11],22,0x895CD7BE);
75 FF(A,B,C,D,M[12], 7,0x6B901122); FF(D,A,B,C,M[13],12,0xFD987193);
76 FF(C,D,A,B,M[14],17,0xA679438E); FF(B,C,D,A,M[15],22,0x49B40821);
77
78 GG(A,B,C,D,M[ 1], 5,0xF61E2562); GG(D,A,B,C,M[ 6], 9,0xC040B340);
79 GG(C,D,A,B,M[11],14,0x265E5A51); GG(B,C,D,A,M[ 0],20,0xE9B6C7AA);
80 GG(A,B,C,D,M[ 5], 5,0xD62F105D); GG(D,A,B,C,M[10], 9,0x02441453);
81 GG(C,D,A,B,M[15],14,0xD8A1E681); GG(B,C,D,A,M[ 4],20,0xE7D3FBC8);
82 GG(A,B,C,D,M[ 9], 5,0x21E1CDE6); GG(D,A,B,C,M[14], 9,0xC33707D6);
83 GG(C,D,A,B,M[ 3],14,0xF4D50D87); GG(B,C,D,A,M[ 8],20,0x455A14ED);
84 GG(A,B,C,D,M[13], 5,0xA9E3E905); GG(D,A,B,C,M[ 2], 9,0xFCEFA3F8);
85 GG(C,D,A,B,M[ 7],14,0x676F02D9); GG(B,C,D,A,M[12],20,0x8D2A4C8A);
86
87 HH(A,B,C,D,M[ 5], 4,0xFFFA3942); HH(D,A,B,C,M[ 8],11,0x8771F681);
88 HH(C,D,A,B,M[11],16,0x6D9D6122); HH(B,C,D,A,M[14],23,0xFDE5380C);
89 HH(A,B,C,D,M[ 1], 4,0xA4BEEA44); HH(D,A,B,C,M[ 4],11,0x4BDECFA9);
90 HH(C,D,A,B,M[ 7],16,0xF6BB4B60); HH(B,C,D,A,M[10],23,0xBEBFBC70);
91 HH(A,B,C,D,M[13], 4,0x289B7EC6); HH(D,A,B,C,M[ 0],11,0xEAA127FA);
92 HH(C,D,A,B,M[ 3],16,0xD4EF3085); HH(B,C,D,A,M[ 6],23,0x04881D05);
93 HH(A,B,C,D,M[ 9], 4,0xD9D4D039); HH(D,A,B,C,M[12],11,0xE6DB99E5);
94 HH(C,D,A,B,M[15],16,0x1FA27CF8); HH(B,C,D,A,M[ 2],23,0xC4AC5665);
95
96 II(A,B,C,D,M[ 0], 6,0xF4292244); II(D,A,B,C,M[ 7],10,0x432AFF97);
97 II(C,D,A,B,M[14],15,0xAB9423A7); II(B,C,D,A,M[ 5],21,0xFC93A039);
98 II(A,B,C,D,M[12], 6,0x655B59C3); II(D,A,B,C,M[ 3],10,0x8F0CCC92);
99 II(C,D,A,B,M[10],15,0xFFEFF47D); II(B,C,D,A,M[ 1],21,0x85845DD1);
100 II(A,B,C,D,M[ 8], 6,0x6FA87E4F); II(D,A,B,C,M[15],10,0xFE2CE6E0);
101 II(C,D,A,B,M[ 6],15,0xA3014314); II(B,C,D,A,M[13],21,0x4E0811A1);
102 II(A,B,C,D,M[ 4], 6,0xF7537E82); II(D,A,B,C,M[11],10,0xBD3AF235);
103 II(C,D,A,B,M[ 2],15,0x2AD7D2BB); II(B,C,D,A,M[ 9],21,0xEB86D391);
104
105 A = (digest[0] += A);
106 B = (digest[1] += B);
107 C = (digest[2] += C);
108 D = (digest[3] += D);
109
110 input += hash_block_size();
111 }
112 }
113
114 /*
115 * Copy out the digest
116 */
117 void MD5::copy_out(byte output[])
118 {
119 for(size_t i = 0; i != output_length(); i += 4)
120 store_le(digest[i/4], output + i);
121 }
122
123 /*
124 * Clear memory of sensitive data
125 */
126 void MD5::clear()
127 {
128 MDx_HashFunction::clear();
129 zeroise(M);
130 digest[0] = 0x67452301;
131 digest[1] = 0xEFCDAB89;
132 digest[2] = 0x98BADCFE;
133 digest[3] = 0x10325476;
134 }
135
136 }
Botan::load_le
T load_le(const byte in[], size_t off)
Definition: loadstor.h:116
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::MD5::copy_out
void copy_out(byte[])
Definition: md5.cpp:117
Botan::byte
unsigned char byte
Definition: types.h:22
Botan
Definition: algo_base.h:14
Botan::MD5::compress_n
void compress_n(const byte[], size_t blocks)
Definition: md5.cpp:61
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::MD5::M
SecureVector< u32bit > M
Definition: md5.h:36
Botan::MD5::output_length
size_t output_length() const
Definition: md5.h:22
Botan::MD5::clear
void clear()
Definition: md5.cpp:126
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::MD5::digest
SecureVector< u32bit > digest
Definition: md5.h:41
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428
Botan::u32bit
unsigned int u32bit
Definition: types.h:32