OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "../../../include/fdrm/fx_crypt.h" | 7 #include "../../../include/fdrm/fx_crypt.h" |
8 #include "../../../include/fxcrt/fx_basic.h" | 8 #include "../../../include/fxcrt/fx_basic.h" |
9 #include "../../../include/fxcrt/fx_safe_types.h" | 9 #include "../../../include/fxcrt/fx_safe_types.h" |
10 | 10 |
11 #ifdef __cplusplus | 11 #ifdef __cplusplus |
12 extern "C" { | 12 extern "C" { |
13 #endif | 13 #endif |
14 struct rc4_state { | 14 struct rc4_state { |
15 int x, y, m[256]; | 15 int x, y, m[256]; |
16 }; | 16 }; |
17 void CRYPT_ArcFourSetup(void* context, FX_LPCBYTE key, FX_DWORD length ) | 17 void CRYPT_ArcFourSetup(void* context, const uint8_t* key, FX_DWORD length ) |
18 { | 18 { |
19 rc4_state *s = (rc4_state*)context; | 19 rc4_state *s = (rc4_state*)context; |
20 int i, j, k, *m, a; | 20 int i, j, k, *m, a; |
21 s->x = 0; | 21 s->x = 0; |
22 s->y = 0; | 22 s->y = 0; |
23 m = s->m; | 23 m = s->m; |
24 for( i = 0; i < 256; i++ ) { | 24 for( i = 0; i < 256; i++ ) { |
25 m[i] = i; | 25 m[i] = i; |
26 } | 26 } |
27 j = k = 0; | 27 j = k = 0; |
(...skipping 18 matching lines...) Expand all Loading... |
46 x = ( x + 1 ) & 0xFF; | 46 x = ( x + 1 ) & 0xFF; |
47 a = m[x]; | 47 a = m[x]; |
48 y = ( y + a ) & 0xFF; | 48 y = ( y + a ) & 0xFF; |
49 m[x] = b = m[y]; | 49 m[x] = b = m[y]; |
50 m[y] = a; | 50 m[y] = a; |
51 data[i] ^= m[( a + b ) & 0xFF]; | 51 data[i] ^= m[( a + b ) & 0xFF]; |
52 } | 52 } |
53 s->x = x; | 53 s->x = x; |
54 s->y = y; | 54 s->y = y; |
55 } | 55 } |
56 void CRYPT_ArcFourCryptBlock(FX_LPBYTE pData, FX_DWORD size, FX_LPCBYTE key, FX_
DWORD keylen) | 56 void CRYPT_ArcFourCryptBlock(uint8_t* pData, FX_DWORD size, const uint8_t* key,
FX_DWORD keylen) |
57 { | 57 { |
58 rc4_state s; | 58 rc4_state s; |
59 CRYPT_ArcFourSetup(&s, key, keylen); | 59 CRYPT_ArcFourSetup(&s, key, keylen); |
60 CRYPT_ArcFourCrypt(&s, pData, size); | 60 CRYPT_ArcFourCrypt(&s, pData, size); |
61 } | 61 } |
62 struct md5_context { | 62 struct md5_context { |
63 FX_DWORD total[2]; | 63 FX_DWORD total[2]; |
64 FX_DWORD state[4]; | 64 FX_DWORD state[4]; |
65 uint8_t buffer[64]; | 65 uint8_t buffer[64]; |
66 }; | 66 }; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 void CRYPT_MD5Start(void* context) | 186 void CRYPT_MD5Start(void* context) |
187 { | 187 { |
188 struct md5_context *ctx = (struct md5_context*)context; | 188 struct md5_context *ctx = (struct md5_context*)context; |
189 ctx->total[0] = 0; | 189 ctx->total[0] = 0; |
190 ctx->total[1] = 0; | 190 ctx->total[1] = 0; |
191 ctx->state[0] = 0x67452301; | 191 ctx->state[0] = 0x67452301; |
192 ctx->state[1] = 0xEFCDAB89; | 192 ctx->state[1] = 0xEFCDAB89; |
193 ctx->state[2] = 0x98BADCFE; | 193 ctx->state[2] = 0x98BADCFE; |
194 ctx->state[3] = 0x10325476; | 194 ctx->state[3] = 0x10325476; |
195 } | 195 } |
196 void CRYPT_MD5Update(FX_LPVOID pctx, FX_LPCBYTE input, FX_DWORD length ) | 196 void CRYPT_MD5Update(void* pctx, const uint8_t* input, FX_DWORD length ) |
197 { | 197 { |
198 struct md5_context *ctx = (struct md5_context *)pctx; | 198 struct md5_context *ctx = (struct md5_context *)pctx; |
199 FX_DWORD left, fill; | 199 FX_DWORD left, fill; |
200 if( ! length ) { | 200 if( ! length ) { |
201 return; | 201 return; |
202 } | 202 } |
203 left = ( ctx->total[0] >> 3 ) & 0x3F; | 203 left = ( ctx->total[0] >> 3 ) & 0x3F; |
204 fill = 64 - left; | 204 fill = 64 - left; |
205 ctx->total[0] += length << 3; | 205 ctx->total[0] += length << 3; |
206 ctx->total[1] += length >> 29; | 206 ctx->total[1] += length >> 29; |
(...skipping 14 matching lines...) Expand all Loading... |
221 if( length ) { | 221 if( length ) { |
222 FXSYS_memcpy32( (void *) (ctx->buffer + left), (void *) input, length ); | 222 FXSYS_memcpy32( (void *) (ctx->buffer + left), (void *) input, length ); |
223 } | 223 } |
224 } | 224 } |
225 const uint8_t md5_padding[64] = { | 225 const uint8_t md5_padding[64] = { |
226 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 226 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
227 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 227 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
228 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | 228 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
229 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | 229 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
230 }; | 230 }; |
231 void CRYPT_MD5Finish(FX_LPVOID pctx, uint8_t digest[16] ) | 231 void CRYPT_MD5Finish(void* pctx, uint8_t digest[16] ) |
232 { | 232 { |
233 struct md5_context *ctx = (struct md5_context *)pctx; | 233 struct md5_context *ctx = (struct md5_context *)pctx; |
234 FX_DWORD last, padn; | 234 FX_DWORD last, padn; |
235 uint8_t msglen[8]; | 235 uint8_t msglen[8]; |
236 PUT_FX_DWORD( ctx->total[0], msglen, 0 ); | 236 PUT_FX_DWORD( ctx->total[0], msglen, 0 ); |
237 PUT_FX_DWORD( ctx->total[1], msglen, 4 ); | 237 PUT_FX_DWORD( ctx->total[1], msglen, 4 ); |
238 last = ( ctx->total[0] >> 3 ) & 0x3F; | 238 last = ( ctx->total[0] >> 3 ) & 0x3F; |
239 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); | 239 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last ); |
240 CRYPT_MD5Update( ctx, md5_padding, padn ); | 240 CRYPT_MD5Update( ctx, md5_padding, padn ); |
241 CRYPT_MD5Update( ctx, msglen, 8 ); | 241 CRYPT_MD5Update( ctx, msglen, 8 ); |
242 PUT_FX_DWORD( ctx->state[0], digest, 0 ); | 242 PUT_FX_DWORD( ctx->state[0], digest, 0 ); |
243 PUT_FX_DWORD( ctx->state[1], digest, 4 ); | 243 PUT_FX_DWORD( ctx->state[1], digest, 4 ); |
244 PUT_FX_DWORD( ctx->state[2], digest, 8 ); | 244 PUT_FX_DWORD( ctx->state[2], digest, 8 ); |
245 PUT_FX_DWORD( ctx->state[3], digest, 12 ); | 245 PUT_FX_DWORD( ctx->state[3], digest, 12 ); |
246 } | 246 } |
247 void CRYPT_MD5Generate(FX_LPCBYTE input, FX_DWORD length, uint8_t digest[16]) | 247 void CRYPT_MD5Generate(const uint8_t* input, FX_DWORD length, uint8_t digest[16]
) |
248 { | 248 { |
249 md5_context ctx; | 249 md5_context ctx; |
250 CRYPT_MD5Start(&ctx); | 250 CRYPT_MD5Start(&ctx); |
251 CRYPT_MD5Update(&ctx, input, length); | 251 CRYPT_MD5Update(&ctx, input, length); |
252 CRYPT_MD5Finish(&ctx, digest); | 252 CRYPT_MD5Finish(&ctx, digest); |
253 } | 253 } |
254 static FX_BOOL (*g_PubKeyDecryptor)(FX_LPCBYTE pData, FX_DWORD size, FX_LPBYTE d
ata_buf, FX_DWORD& data_len) = NULL; | 254 static FX_BOOL (*g_PubKeyDecryptor)(const uint8_t* pData, FX_DWORD size, uint8_t
* data_buf, FX_DWORD& data_len) = NULL; |
255 void CRYPT_SetPubKeyDecryptor(FX_BOOL (*func)(FX_LPCBYTE pData, FX_DWORD size, F
X_LPBYTE data_buf, FX_DWORD& data_len)) | 255 void CRYPT_SetPubKeyDecryptor(FX_BOOL (*func)(const uint8_t* pData, FX_DWORD siz
e, uint8_t* data_buf, FX_DWORD& data_len)) |
256 { | 256 { |
257 g_PubKeyDecryptor = func; | 257 g_PubKeyDecryptor = func; |
258 } | 258 } |
259 #ifdef __cplusplus | 259 #ifdef __cplusplus |
260 }; | 260 }; |
261 #endif | 261 #endif |
OLD | NEW |