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