1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "core/fdrm/fx_crypt.h"
8
9 #include <utility>
10
11 #define GET_UINT32(n, b, i) \
12 { \
13 (n) = (uint32_t)((uint8_t*)b)[(i)] | \
14 (((uint32_t)((uint8_t*)b)[(i) + 1]) << 8) | \
15 (((uint32_t)((uint8_t*)b)[(i) + 2]) << 16) | \
16 (((uint32_t)((uint8_t*)b)[(i) + 3]) << 24); \
17 }
18 #define PUT_UINT32(n, b, i) \
19 { \
20 (((uint8_t*)b)[(i)]) = (uint8_t)(((n)) & 0xFF); \
21 (((uint8_t*)b)[(i) + 1]) = (uint8_t)(((n) >> 8) & 0xFF); \
22 (((uint8_t*)b)[(i) + 2]) = (uint8_t)(((n) >> 16) & 0xFF); \
23 (((uint8_t*)b)[(i) + 3]) = (uint8_t)(((n) >> 24) & 0xFF); \
24 }
25
26 namespace {
27
28 const uint8_t md5_padding[64] = {
29 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
32
md5_process(CRYPT_md5_context * ctx,const uint8_t data[64])33 void md5_process(CRYPT_md5_context* ctx, const uint8_t data[64]) {
34 uint32_t A, B, C, D, X[16];
35 GET_UINT32(X[0], data, 0);
36 GET_UINT32(X[1], data, 4);
37 GET_UINT32(X[2], data, 8);
38 GET_UINT32(X[3], data, 12);
39 GET_UINT32(X[4], data, 16);
40 GET_UINT32(X[5], data, 20);
41 GET_UINT32(X[6], data, 24);
42 GET_UINT32(X[7], data, 28);
43 GET_UINT32(X[8], data, 32);
44 GET_UINT32(X[9], data, 36);
45 GET_UINT32(X[10], data, 40);
46 GET_UINT32(X[11], data, 44);
47 GET_UINT32(X[12], data, 48);
48 GET_UINT32(X[13], data, 52);
49 GET_UINT32(X[14], data, 56);
50 GET_UINT32(X[15], data, 60);
51 #define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
52 #define P(a, b, c, d, k, s, t) \
53 { \
54 a += F(b, c, d) + X[k] + t; \
55 a = S(a, s) + b; \
56 }
57 A = ctx->state[0];
58 B = ctx->state[1];
59 C = ctx->state[2];
60 D = ctx->state[3];
61 #define F(x, y, z) (z ^ (x & (y ^ z)))
62 P(A, B, C, D, 0, 7, 0xD76AA478);
63 P(D, A, B, C, 1, 12, 0xE8C7B756);
64 P(C, D, A, B, 2, 17, 0x242070DB);
65 P(B, C, D, A, 3, 22, 0xC1BDCEEE);
66 P(A, B, C, D, 4, 7, 0xF57C0FAF);
67 P(D, A, B, C, 5, 12, 0x4787C62A);
68 P(C, D, A, B, 6, 17, 0xA8304613);
69 P(B, C, D, A, 7, 22, 0xFD469501);
70 P(A, B, C, D, 8, 7, 0x698098D8);
71 P(D, A, B, C, 9, 12, 0x8B44F7AF);
72 P(C, D, A, B, 10, 17, 0xFFFF5BB1);
73 P(B, C, D, A, 11, 22, 0x895CD7BE);
74 P(A, B, C, D, 12, 7, 0x6B901122);
75 P(D, A, B, C, 13, 12, 0xFD987193);
76 P(C, D, A, B, 14, 17, 0xA679438E);
77 P(B, C, D, A, 15, 22, 0x49B40821);
78 #undef F
79 #define F(x, y, z) (y ^ (z & (x ^ y)))
80 P(A, B, C, D, 1, 5, 0xF61E2562);
81 P(D, A, B, C, 6, 9, 0xC040B340);
82 P(C, D, A, B, 11, 14, 0x265E5A51);
83 P(B, C, D, A, 0, 20, 0xE9B6C7AA);
84 P(A, B, C, D, 5, 5, 0xD62F105D);
85 P(D, A, B, C, 10, 9, 0x02441453);
86 P(C, D, A, B, 15, 14, 0xD8A1E681);
87 P(B, C, D, A, 4, 20, 0xE7D3FBC8);
88 P(A, B, C, D, 9, 5, 0x21E1CDE6);
89 P(D, A, B, C, 14, 9, 0xC33707D6);
90 P(C, D, A, B, 3, 14, 0xF4D50D87);
91 P(B, C, D, A, 8, 20, 0x455A14ED);
92 P(A, B, C, D, 13, 5, 0xA9E3E905);
93 P(D, A, B, C, 2, 9, 0xFCEFA3F8);
94 P(C, D, A, B, 7, 14, 0x676F02D9);
95 P(B, C, D, A, 12, 20, 0x8D2A4C8A);
96 #undef F
97 #define F(x, y, z) (x ^ y ^ z)
98 P(A, B, C, D, 5, 4, 0xFFFA3942);
99 P(D, A, B, C, 8, 11, 0x8771F681);
100 P(C, D, A, B, 11, 16, 0x6D9D6122);
101 P(B, C, D, A, 14, 23, 0xFDE5380C);
102 P(A, B, C, D, 1, 4, 0xA4BEEA44);
103 P(D, A, B, C, 4, 11, 0x4BDECFA9);
104 P(C, D, A, B, 7, 16, 0xF6BB4B60);
105 P(B, C, D, A, 10, 23, 0xBEBFBC70);
106 P(A, B, C, D, 13, 4, 0x289B7EC6);
107 P(D, A, B, C, 0, 11, 0xEAA127FA);
108 P(C, D, A, B, 3, 16, 0xD4EF3085);
109 P(B, C, D, A, 6, 23, 0x04881D05);
110 P(A, B, C, D, 9, 4, 0xD9D4D039);
111 P(D, A, B, C, 12, 11, 0xE6DB99E5);
112 P(C, D, A, B, 15, 16, 0x1FA27CF8);
113 P(B, C, D, A, 2, 23, 0xC4AC5665);
114 #undef F
115 #define F(x, y, z) (y ^ (x | ~z))
116 P(A, B, C, D, 0, 6, 0xF4292244);
117 P(D, A, B, C, 7, 10, 0x432AFF97);
118 P(C, D, A, B, 14, 15, 0xAB9423A7);
119 P(B, C, D, A, 5, 21, 0xFC93A039);
120 P(A, B, C, D, 12, 6, 0x655B59C3);
121 P(D, A, B, C, 3, 10, 0x8F0CCC92);
122 P(C, D, A, B, 10, 15, 0xFFEFF47D);
123 P(B, C, D, A, 1, 21, 0x85845DD1);
124 P(A, B, C, D, 8, 6, 0x6FA87E4F);
125 P(D, A, B, C, 15, 10, 0xFE2CE6E0);
126 P(C, D, A, B, 6, 15, 0xA3014314);
127 P(B, C, D, A, 13, 21, 0x4E0811A1);
128 P(A, B, C, D, 4, 6, 0xF7537E82);
129 P(D, A, B, C, 11, 10, 0xBD3AF235);
130 P(C, D, A, B, 2, 15, 0x2AD7D2BB);
131 P(B, C, D, A, 9, 21, 0xEB86D391);
132 #undef F
133 ctx->state[0] += A;
134 ctx->state[1] += B;
135 ctx->state[2] += C;
136 ctx->state[3] += D;
137 }
138
139 } // namespace
140
CRYPT_ArcFourSetup(CRYPT_rc4_context * context,pdfium::span<const uint8_t> key)141 void CRYPT_ArcFourSetup(CRYPT_rc4_context* context,
142 pdfium::span<const uint8_t> key) {
143 context->x = 0;
144 context->y = 0;
145 for (int i = 0; i < kRC4ContextPermutationLength; ++i)
146 context->m[i] = i;
147
148 int j = 0;
149 for (int i = 0; i < kRC4ContextPermutationLength; ++i) {
150 size_t size = key.size();
151 j = (j + context->m[i] + (size ? key[i % size] : 0)) & 0xFF;
152 std::swap(context->m[i], context->m[j]);
153 }
154 }
155
CRYPT_ArcFourCrypt(CRYPT_rc4_context * context,pdfium::span<uint8_t> data)156 void CRYPT_ArcFourCrypt(CRYPT_rc4_context* context,
157 pdfium::span<uint8_t> data) {
158 for (auto& datum : data) {
159 context->x = (context->x + 1) & 0xFF;
160 context->y = (context->y + context->m[context->x]) & 0xFF;
161 std::swap(context->m[context->x], context->m[context->y]);
162 datum ^=
163 context->m[(context->m[context->x] + context->m[context->y]) & 0xFF];
164 }
165 }
166
CRYPT_ArcFourCryptBlock(pdfium::span<uint8_t> data,pdfium::span<const uint8_t> key)167 void CRYPT_ArcFourCryptBlock(pdfium::span<uint8_t> data,
168 pdfium::span<const uint8_t> key) {
169 CRYPT_rc4_context s;
170 CRYPT_ArcFourSetup(&s, key);
171 CRYPT_ArcFourCrypt(&s, data);
172 }
173
CRYPT_MD5Start()174 CRYPT_md5_context CRYPT_MD5Start() {
175 CRYPT_md5_context context;
176 context.total[0] = 0;
177 context.total[1] = 0;
178 context.state[0] = 0x67452301;
179 context.state[1] = 0xEFCDAB89;
180 context.state[2] = 0x98BADCFE;
181 context.state[3] = 0x10325476;
182 return context;
183 }
184
CRYPT_MD5Update(CRYPT_md5_context * context,pdfium::span<const uint8_t> data)185 void CRYPT_MD5Update(CRYPT_md5_context* context,
186 pdfium::span<const uint8_t> data) {
187 if (data.empty())
188 return;
189
190 uint32_t left = (context->total[0] >> 3) & 0x3F;
191 uint32_t fill = 64 - left;
192 context->total[0] += data.size() << 3;
193 context->total[1] += data.size() >> 29;
194 context->total[0] &= 0xFFFFFFFF;
195 context->total[1] += context->total[0] < data.size() << 3;
196 if (left && data.size() >= fill) {
197 auto next_data = data.subspan(fill);
198 memcpy(context->buffer + left, data.data(), fill);
199 md5_process(context, context->buffer);
200 left = 0;
201 data = next_data;
202 }
203 while (data.size() >= 64) {
204 auto next_data = data.subspan(64);
205 md5_process(context, data.data());
206 data = next_data;
207 }
208 size_t remaining = data.size();
209 if (remaining)
210 memcpy(context->buffer + left, data.data(), remaining);
211 }
212
CRYPT_MD5Finish(CRYPT_md5_context * context,uint8_t digest[16])213 void CRYPT_MD5Finish(CRYPT_md5_context* context, uint8_t digest[16]) {
214 uint8_t msglen[8];
215 PUT_UINT32(context->total[0], msglen, 0);
216 PUT_UINT32(context->total[1], msglen, 4);
217 uint32_t last = (context->total[0] >> 3) & 0x3F;
218 uint32_t padn = (last < 56) ? (56 - last) : (120 - last);
219 CRYPT_MD5Update(context, {md5_padding, padn});
220 CRYPT_MD5Update(context, msglen);
221 PUT_UINT32(context->state[0], digest, 0);
222 PUT_UINT32(context->state[1], digest, 4);
223 PUT_UINT32(context->state[2], digest, 8);
224 PUT_UINT32(context->state[3], digest, 12);
225 }
226
CRYPT_MD5Generate(pdfium::span<const uint8_t> data,uint8_t digest[16])227 void CRYPT_MD5Generate(pdfium::span<const uint8_t> data, uint8_t digest[16]) {
228 CRYPT_md5_context ctx = CRYPT_MD5Start();
229 CRYPT_MD5Update(&ctx, data);
230 CRYPT_MD5Finish(&ctx, digest);
231 }
232