/src/botan/src/lib/hash/md5/md5.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
16 | 0 | return std::unique_ptr<HashFunction>(new MD5(*this)); |
17 | 0 | } |
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 | 10.7k | { |
27 | 10.7k | A += (D ^ (B & (C ^ D))) + M; |
28 | 10.7k | A = rotl<S>(A) + B; |
29 | 10.7k | } md5.cpp:void Botan::(anonymous namespace)::FF<7ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 26 | 2.68k | { | 27 | 2.68k | A += (D ^ (B & (C ^ D))) + M; | 28 | 2.68k | A = rotl<S>(A) + B; | 29 | 2.68k | } |
md5.cpp:void Botan::(anonymous namespace)::FF<12ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 26 | 2.68k | { | 27 | 2.68k | A += (D ^ (B & (C ^ D))) + M; | 28 | 2.68k | A = rotl<S>(A) + B; | 29 | 2.68k | } |
md5.cpp:void Botan::(anonymous namespace)::FF<17ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 26 | 2.68k | { | 27 | 2.68k | A += (D ^ (B & (C ^ D))) + M; | 28 | 2.68k | A = rotl<S>(A) + B; | 29 | 2.68k | } |
md5.cpp:void Botan::(anonymous namespace)::FF<22ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 26 | 2.68k | { | 27 | 2.68k | A += (D ^ (B & (C ^ D))) + M; | 28 | 2.68k | A = rotl<S>(A) + B; | 29 | 2.68k | } |
|
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 | 10.7k | { |
37 | 10.7k | A += (C ^ (D & (B ^ C))) + M; |
38 | 10.7k | A = rotl<S>(A) + B; |
39 | 10.7k | } md5.cpp:void Botan::(anonymous namespace)::GG<5ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 36 | 2.68k | { | 37 | 2.68k | A += (C ^ (D & (B ^ C))) + M; | 38 | 2.68k | A = rotl<S>(A) + B; | 39 | 2.68k | } |
md5.cpp:void Botan::(anonymous namespace)::GG<9ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 36 | 2.68k | { | 37 | 2.68k | A += (C ^ (D & (B ^ C))) + M; | 38 | 2.68k | A = rotl<S>(A) + B; | 39 | 2.68k | } |
md5.cpp:void Botan::(anonymous namespace)::GG<14ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 36 | 2.68k | { | 37 | 2.68k | A += (C ^ (D & (B ^ C))) + M; | 38 | 2.68k | A = rotl<S>(A) + B; | 39 | 2.68k | } |
md5.cpp:void Botan::(anonymous namespace)::GG<20ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 36 | 2.68k | { | 37 | 2.68k | A += (C ^ (D & (B ^ C))) + M; | 38 | 2.68k | A = rotl<S>(A) + B; | 39 | 2.68k | } |
|
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 | 10.7k | { |
47 | 10.7k | A += (B ^ C ^ D) + M; |
48 | 10.7k | A = rotl<S>(A) + B; |
49 | 10.7k | } md5.cpp:void Botan::(anonymous namespace)::HH<4ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 46 | 2.68k | { | 47 | 2.68k | A += (B ^ C ^ D) + M; | 48 | 2.68k | A = rotl<S>(A) + B; | 49 | 2.68k | } |
md5.cpp:void Botan::(anonymous namespace)::HH<11ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 46 | 2.68k | { | 47 | 2.68k | A += (B ^ C ^ D) + M; | 48 | 2.68k | A = rotl<S>(A) + B; | 49 | 2.68k | } |
md5.cpp:void Botan::(anonymous namespace)::HH<16ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 46 | 2.68k | { | 47 | 2.68k | A += (B ^ C ^ D) + M; | 48 | 2.68k | A = rotl<S>(A) + B; | 49 | 2.68k | } |
md5.cpp:void Botan::(anonymous namespace)::HH<23ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 46 | 2.68k | { | 47 | 2.68k | A += (B ^ C ^ D) + M; | 48 | 2.68k | A = rotl<S>(A) + B; | 49 | 2.68k | } |
|
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 | 10.7k | { |
57 | 10.7k | A += (C ^ (B | ~D)) + M; |
58 | 10.7k | A = rotl<S>(A) + B; |
59 | 10.7k | } md5.cpp:void Botan::(anonymous namespace)::II<6ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 56 | 2.68k | { | 57 | 2.68k | A += (C ^ (B | ~D)) + M; | 58 | 2.68k | A = rotl<S>(A) + B; | 59 | 2.68k | } |
md5.cpp:void Botan::(anonymous namespace)::II<10ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 56 | 2.68k | { | 57 | 2.68k | A += (C ^ (B | ~D)) + M; | 58 | 2.68k | A = rotl<S>(A) + B; | 59 | 2.68k | } |
md5.cpp:void Botan::(anonymous namespace)::II<15ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 56 | 2.68k | { | 57 | 2.68k | A += (C ^ (B | ~D)) + M; | 58 | 2.68k | A = rotl<S>(A) + B; | 59 | 2.68k | } |
md5.cpp:void Botan::(anonymous namespace)::II<21ul>(unsigned int&, unsigned int, unsigned int, unsigned int, unsigned int) Line | Count | Source | 56 | 2.68k | { | 57 | 2.68k | A += (C ^ (B | ~D)) + M; | 58 | 2.68k | A = rotl<S>(A) + B; | 59 | 2.68k | } |
|
60 | | |
61 | | } |
62 | | |
63 | | /* |
64 | | * MD5 Compression Function |
65 | | */ |
66 | | void MD5::compress_n(const uint8_t input[], size_t blocks) |
67 | 313 | { |
68 | 313 | uint32_t A = m_digest[0], B = m_digest[1], C = m_digest[2], D = m_digest[3]; |
69 | 313 | |
70 | 985 | for(size_t i = 0; i != blocks; ++i) |
71 | 672 | { |
72 | 672 | load_le(m_M.data(), input, m_M.size()); |
73 | 672 | |
74 | 672 | FF< 7>(A,B,C,D,m_M[ 0]+0xD76AA478); FF<12>(D,A,B,C,m_M[ 1]+0xE8C7B756); |
75 | 672 | FF<17>(C,D,A,B,m_M[ 2]+0x242070DB); FF<22>(B,C,D,A,m_M[ 3]+0xC1BDCEEE); |
76 | 672 | FF< 7>(A,B,C,D,m_M[ 4]+0xF57C0FAF); FF<12>(D,A,B,C,m_M[ 5]+0x4787C62A); |
77 | 672 | FF<17>(C,D,A,B,m_M[ 6]+0xA8304613); FF<22>(B,C,D,A,m_M[ 7]+0xFD469501); |
78 | 672 | FF< 7>(A,B,C,D,m_M[ 8]+0x698098D8); FF<12>(D,A,B,C,m_M[ 9]+0x8B44F7AF); |
79 | 672 | FF<17>(C,D,A,B,m_M[10]+0xFFFF5BB1); FF<22>(B,C,D,A,m_M[11]+0x895CD7BE); |
80 | 672 | FF< 7>(A,B,C,D,m_M[12]+0x6B901122); FF<12>(D,A,B,C,m_M[13]+0xFD987193); |
81 | 672 | FF<17>(C,D,A,B,m_M[14]+0xA679438E); FF<22>(B,C,D,A,m_M[15]+0x49B40821); |
82 | 672 | |
83 | 672 | GG< 5>(A,B,C,D,m_M[ 1]+0xF61E2562); GG< 9>(D,A,B,C,m_M[ 6]+0xC040B340); |
84 | 672 | GG<14>(C,D,A,B,m_M[11]+0x265E5A51); GG<20>(B,C,D,A,m_M[ 0]+0xE9B6C7AA); |
85 | 672 | GG< 5>(A,B,C,D,m_M[ 5]+0xD62F105D); GG< 9>(D,A,B,C,m_M[10]+0x02441453); |
86 | 672 | GG<14>(C,D,A,B,m_M[15]+0xD8A1E681); GG<20>(B,C,D,A,m_M[ 4]+0xE7D3FBC8); |
87 | 672 | GG< 5>(A,B,C,D,m_M[ 9]+0x21E1CDE6); GG< 9>(D,A,B,C,m_M[14]+0xC33707D6); |
88 | 672 | GG<14>(C,D,A,B,m_M[ 3]+0xF4D50D87); GG<20>(B,C,D,A,m_M[ 8]+0x455A14ED); |
89 | 672 | GG< 5>(A,B,C,D,m_M[13]+0xA9E3E905); GG< 9>(D,A,B,C,m_M[ 2]+0xFCEFA3F8); |
90 | 672 | GG<14>(C,D,A,B,m_M[ 7]+0x676F02D9); GG<20>(B,C,D,A,m_M[12]+0x8D2A4C8A); |
91 | 672 | |
92 | 672 | HH< 4>(A,B,C,D,m_M[ 5]+0xFFFA3942); HH<11>(D,A,B,C,m_M[ 8]+0x8771F681); |
93 | 672 | HH<16>(C,D,A,B,m_M[11]+0x6D9D6122); HH<23>(B,C,D,A,m_M[14]+0xFDE5380C); |
94 | 672 | HH< 4>(A,B,C,D,m_M[ 1]+0xA4BEEA44); HH<11>(D,A,B,C,m_M[ 4]+0x4BDECFA9); |
95 | 672 | HH<16>(C,D,A,B,m_M[ 7]+0xF6BB4B60); HH<23>(B,C,D,A,m_M[10]+0xBEBFBC70); |
96 | 672 | HH< 4>(A,B,C,D,m_M[13]+0x289B7EC6); HH<11>(D,A,B,C,m_M[ 0]+0xEAA127FA); |
97 | 672 | HH<16>(C,D,A,B,m_M[ 3]+0xD4EF3085); HH<23>(B,C,D,A,m_M[ 6]+0x04881D05); |
98 | 672 | HH< 4>(A,B,C,D,m_M[ 9]+0xD9D4D039); HH<11>(D,A,B,C,m_M[12]+0xE6DB99E5); |
99 | 672 | HH<16>(C,D,A,B,m_M[15]+0x1FA27CF8); HH<23>(B,C,D,A,m_M[ 2]+0xC4AC5665); |
100 | 672 | |
101 | 672 | II< 6>(A,B,C,D,m_M[ 0]+0xF4292244); II<10>(D,A,B,C,m_M[ 7]+0x432AFF97); |
102 | 672 | II<15>(C,D,A,B,m_M[14]+0xAB9423A7); II<21>(B,C,D,A,m_M[ 5]+0xFC93A039); |
103 | 672 | II< 6>(A,B,C,D,m_M[12]+0x655B59C3); II<10>(D,A,B,C,m_M[ 3]+0x8F0CCC92); |
104 | 672 | II<15>(C,D,A,B,m_M[10]+0xFFEFF47D); II<21>(B,C,D,A,m_M[ 1]+0x85845DD1); |
105 | 672 | II< 6>(A,B,C,D,m_M[ 8]+0x6FA87E4F); II<10>(D,A,B,C,m_M[15]+0xFE2CE6E0); |
106 | 672 | II<15>(C,D,A,B,m_M[ 6]+0xA3014314); II<21>(B,C,D,A,m_M[13]+0x4E0811A1); |
107 | 672 | II< 6>(A,B,C,D,m_M[ 4]+0xF7537E82); II<10>(D,A,B,C,m_M[11]+0xBD3AF235); |
108 | 672 | II<15>(C,D,A,B,m_M[ 2]+0x2AD7D2BB); II<21>(B,C,D,A,m_M[ 9]+0xEB86D391); |
109 | 672 | |
110 | 672 | A = (m_digest[0] += A); |
111 | 672 | B = (m_digest[1] += B); |
112 | 672 | C = (m_digest[2] += C); |
113 | 672 | D = (m_digest[3] += D); |
114 | 672 | |
115 | 672 | input += hash_block_size(); |
116 | 672 | } |
117 | 313 | } |
118 | | |
119 | | /* |
120 | | * Copy out the digest |
121 | | */ |
122 | | void MD5::copy_out(uint8_t output[]) |
123 | 155 | { |
124 | 155 | copy_out_vec_le(output, output_length(), m_digest); |
125 | 155 | } |
126 | | |
127 | | /* |
128 | | * Clear memory of sensitive data |
129 | | */ |
130 | | void MD5::clear() |
131 | 310 | { |
132 | 310 | MDx_HashFunction::clear(); |
133 | 310 | zeroise(m_M); |
134 | 310 | m_digest[0] = 0x67452301; |
135 | 310 | m_digest[1] = 0xEFCDAB89; |
136 | 310 | m_digest[2] = 0x98BADCFE; |
137 | 310 | m_digest[3] = 0x10325476; |
138 | 310 | } |
139 | | |
140 | | } |