Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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