• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/crypto/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 * s,const uint8_t * key,uint32_t length)141 void CRYPT_ArcFourSetup(CRYPT_rc4_context* s,
142                         const uint8_t* key,
143                         uint32_t length) {
144   s->x = 0;
145   s->y = 0;
146   for (int i = 0; i < kRC4ContextPermutationLength; ++i)
147     s->m[i] = i;
148 
149   int j = 0;
150   for (int i = 0; i < kRC4ContextPermutationLength; ++i) {
151     j = (j + s->m[i] + (length ? key[i % length] : 0)) & 0xFF;
152     std::swap(s->m[i], s->m[j]);
153   }
154 }
155 
CRYPT_ArcFourCrypt(CRYPT_rc4_context * s,uint8_t * data,uint32_t length)156 void CRYPT_ArcFourCrypt(CRYPT_rc4_context* s, uint8_t* data, uint32_t length) {
157   for (uint32_t i = 0; i < length; ++i) {
158     s->x = (s->x + 1) & 0xFF;
159     s->y = (s->y + s->m[s->x]) & 0xFF;
160     std::swap(s->m[s->x], s->m[s->y]);
161     data[i] ^= s->m[(s->m[s->x] + s->m[s->y]) & 0xFF];
162   }
163 }
164 
CRYPT_ArcFourCryptBlock(uint8_t * pData,uint32_t size,const uint8_t * key,uint32_t keylen)165 void CRYPT_ArcFourCryptBlock(uint8_t* pData,
166                              uint32_t size,
167                              const uint8_t* key,
168                              uint32_t keylen) {
169   CRYPT_rc4_context s;
170   CRYPT_ArcFourSetup(&s, key, keylen);
171   CRYPT_ArcFourCrypt(&s, pData, size);
172 }
173 
CRYPT_MD5Start(CRYPT_md5_context * ctx)174 void CRYPT_MD5Start(CRYPT_md5_context* ctx) {
175   ctx->total[0] = 0;
176   ctx->total[1] = 0;
177   ctx->state[0] = 0x67452301;
178   ctx->state[1] = 0xEFCDAB89;
179   ctx->state[2] = 0x98BADCFE;
180   ctx->state[3] = 0x10325476;
181 }
182 
CRYPT_MD5Update(CRYPT_md5_context * ctx,const uint8_t * input,uint32_t length)183 void CRYPT_MD5Update(CRYPT_md5_context* ctx,
184                      const uint8_t* input,
185                      uint32_t length) {
186   uint32_t left, fill;
187   if (!length) {
188     return;
189   }
190   left = (ctx->total[0] >> 3) & 0x3F;
191   fill = 64 - left;
192   ctx->total[0] += length << 3;
193   ctx->total[1] += length >> 29;
194   ctx->total[0] &= 0xFFFFFFFF;
195   ctx->total[1] += ctx->total[0] < length << 3;
196   if (left && length >= fill) {
197     memcpy(ctx->buffer + left, input, fill);
198     md5_process(ctx, ctx->buffer);
199     length -= fill;
200     input += fill;
201     left = 0;
202   }
203   while (length >= 64) {
204     md5_process(ctx, input);
205     length -= 64;
206     input += 64;
207   }
208   if (length) {
209     memcpy(ctx->buffer + left, input, length);
210   }
211 }
212 
CRYPT_MD5Finish(CRYPT_md5_context * ctx,uint8_t digest[16])213 void CRYPT_MD5Finish(CRYPT_md5_context* ctx, uint8_t digest[16]) {
214   uint32_t last, padn;
215   uint8_t msglen[8];
216   PUT_UINT32(ctx->total[0], msglen, 0);
217   PUT_UINT32(ctx->total[1], msglen, 4);
218   last = (ctx->total[0] >> 3) & 0x3F;
219   padn = (last < 56) ? (56 - last) : (120 - last);
220   CRYPT_MD5Update(ctx, md5_padding, padn);
221   CRYPT_MD5Update(ctx, msglen, 8);
222   PUT_UINT32(ctx->state[0], digest, 0);
223   PUT_UINT32(ctx->state[1], digest, 4);
224   PUT_UINT32(ctx->state[2], digest, 8);
225   PUT_UINT32(ctx->state[3], digest, 12);
226 }
227 
CRYPT_MD5Generate(const uint8_t * input,uint32_t length,uint8_t digest[16])228 void CRYPT_MD5Generate(const uint8_t* input,
229                        uint32_t length,
230                        uint8_t digest[16]) {
231   CRYPT_md5_context ctx;
232   CRYPT_MD5Start(&ctx);
233   CRYPT_MD5Update(&ctx, input, length);
234   CRYPT_MD5Finish(&ctx, digest);
235 }
236