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 #ifndef CORE_FDRM_FX_CRYPT_H_ 8 #define CORE_FDRM_FX_CRYPT_H_ 9 10 #include "core/fxcrt/fx_system.h" 11 #include "third_party/base/span.h" 12 13 constexpr int32_t kRC4ContextPermutationLength = 256; 14 struct CRYPT_rc4_context { 15 int32_t x; 16 int32_t y; 17 int32_t m[kRC4ContextPermutationLength]; 18 }; 19 20 #define MAX_NR 14 21 #define MAX_NB 8 22 struct CRYPT_aes_context { 23 int Nb; 24 int Nr; 25 unsigned int keysched[(MAX_NR + 1) * MAX_NB]; 26 unsigned int invkeysched[(MAX_NR + 1) * MAX_NB]; 27 unsigned int iv[MAX_NB]; 28 }; 29 30 struct CRYPT_md5_context { 31 uint32_t total[2]; 32 uint32_t state[4]; 33 uint8_t buffer[64]; 34 }; 35 36 struct CRYPT_sha1_context { 37 uint64_t total_bytes; 38 uint32_t blkused; // Constrained to [0, 64). 39 uint32_t h[5]; 40 uint8_t block[64]; 41 }; 42 43 struct CRYPT_sha2_context { 44 uint64_t total_bytes; 45 uint64_t state[8]; 46 uint8_t buffer[128]; 47 }; 48 49 void CRYPT_ArcFourCryptBlock(pdfium::span<uint8_t> data, 50 pdfium::span<const uint8_t> key); 51 void CRYPT_ArcFourSetup(CRYPT_rc4_context* context, 52 pdfium::span<const uint8_t> key); 53 void CRYPT_ArcFourCrypt(CRYPT_rc4_context* context, pdfium::span<uint8_t> data); 54 55 void CRYPT_AESSetKey(CRYPT_aes_context* context, 56 const uint8_t* key, 57 uint32_t keylen, 58 bool bEncrypt); 59 void CRYPT_AESSetIV(CRYPT_aes_context* context, const uint8_t* iv); 60 void CRYPT_AESDecrypt(CRYPT_aes_context* context, 61 uint8_t* dest, 62 const uint8_t* src, 63 uint32_t size); 64 void CRYPT_AESEncrypt(CRYPT_aes_context* context, 65 uint8_t* dest, 66 const uint8_t* src, 67 uint32_t size); 68 69 CRYPT_md5_context CRYPT_MD5Start(); 70 void CRYPT_MD5Update(CRYPT_md5_context* context, 71 pdfium::span<const uint8_t> data); 72 void CRYPT_MD5Finish(CRYPT_md5_context* context, uint8_t digest[16]); 73 void CRYPT_MD5Generate(pdfium::span<const uint8_t> data, uint8_t digest[16]); 74 75 void CRYPT_SHA1Start(CRYPT_sha1_context* context); 76 void CRYPT_SHA1Update(CRYPT_sha1_context* context, 77 const uint8_t* data, 78 uint32_t size); 79 void CRYPT_SHA1Finish(CRYPT_sha1_context* context, uint8_t digest[20]); 80 void CRYPT_SHA1Generate(const uint8_t* data, uint32_t size, uint8_t digest[20]); 81 82 void CRYPT_SHA256Start(CRYPT_sha2_context* context); 83 void CRYPT_SHA256Update(CRYPT_sha2_context* context, 84 const uint8_t* data, 85 uint32_t size); 86 void CRYPT_SHA256Finish(CRYPT_sha2_context* context, uint8_t digest[32]); 87 void CRYPT_SHA256Generate(const uint8_t* data, 88 uint32_t size, 89 uint8_t digest[32]); 90 91 void CRYPT_SHA384Start(CRYPT_sha2_context* context); 92 void CRYPT_SHA384Update(CRYPT_sha2_context* context, 93 const uint8_t* data, 94 uint32_t size); 95 void CRYPT_SHA384Finish(CRYPT_sha2_context* context, uint8_t digest[48]); 96 void CRYPT_SHA384Generate(const uint8_t* data, 97 uint32_t size, 98 uint8_t digest[48]); 99 100 void CRYPT_SHA512Start(CRYPT_sha2_context* context); 101 void CRYPT_SHA512Update(CRYPT_sha2_context* context, 102 const uint8_t* data, 103 uint32_t size); 104 void CRYPT_SHA512Finish(CRYPT_sha2_context* context, uint8_t digest[64]); 105 void CRYPT_SHA512Generate(const uint8_t* data, 106 uint32_t size, 107 uint8_t digest[64]); 108 109 #endif // CORE_FDRM_FX_CRYPT_H_ 110