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