1 //======================================================================== 2 // 3 // Decrypt.h 4 // 5 // Copyright 1996-2003 Glyph & Cog, LLC 6 // 7 //======================================================================== 8 9 //======================================================================== 10 // 11 // Modified under the Poppler project - http://poppler.freedesktop.org 12 // 13 // All changes made under the Poppler project to this file are licensed 14 // under GPL version 2 or later 15 // 16 // Copyright (C) 2008 Julien Rebetez <julien@fhtagn.net> 17 // Copyright (C) 2009 David Benjamin <davidben@mit.edu> 18 // Copyright (C) 2012 Fabio D'Urso <fabiodurso@hotmail.it> 19 // Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com> 20 // Copyright (C) 2013 Albert Astals Cid <aacid@kde.org> 21 // Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de> 22 // 23 // To see a description of the changes please see the Changelog file that 24 // came with your tarball or type make ChangeLog if you are building from git 25 // 26 //======================================================================== 27 28 #ifndef DECRYPT_H 29 #define DECRYPT_H 30 31 #ifdef USE_GCC_PRAGMAS 32 #pragma interface 33 #endif 34 35 #include "goo/gtypes.h" 36 #include "goo/GooString.h" 37 #include "Object.h" 38 #include "Stream.h" 39 40 //------------------------------------------------------------------------ 41 // Decrypt 42 //------------------------------------------------------------------------ 43 44 class Decrypt { 45 public: 46 47 // Generate a file key. The <fileKey> buffer must have space for at 48 // least 16 bytes. Checks <ownerPassword> and then <userPassword> 49 // and returns true if either is correct. Sets <ownerPasswordOk> if 50 // the owner password was correct. Either or both of the passwords 51 // may be NULL, which is treated as an empty string. 52 static GBool makeFileKey(int encVersion, int encRevision, int keyLength, 53 GooString *ownerKey, GooString *userKey, 54 GooString *ownerEnc, GooString *userEnc, 55 int permissions, GooString *fileID, 56 GooString *ownerPassword, GooString *userPassword, 57 Guchar *fileKey, GBool encryptMetadata, 58 GBool *ownerPasswordOk); 59 60 private: 61 62 static GBool makeFileKey2(int encVersion, int encRevision, int keyLength, 63 GooString *ownerKey, GooString *userKey, 64 int permissions, GooString *fileID, 65 GooString *userPassword, Guchar *fileKey, 66 GBool encryptMetadata); 67 }; 68 69 //------------------------------------------------------------------------ 70 // Helper classes 71 //------------------------------------------------------------------------ 72 73 /* DecryptRC4State, DecryptAESState, DecryptAES256State are named like this for 74 * historical reasons, but they're used for encryption too. 75 * In case of decryption, the cbc field in AES and AES-256 contains the previous 76 * input block or the CBC initialization vector (IV) if the stream has just been 77 * reset). In case of encryption, it always contains the IV, whereas the 78 * previous output is kept in buf. The paddingReached field is only used in 79 * case of encryption. */ 80 struct DecryptRC4State { 81 Guchar state[256]; 82 Guchar x, y; 83 }; 84 85 struct DecryptAESState { 86 Guint w[44]; 87 Guchar state[16]; 88 Guchar cbc[16]; 89 Guchar buf[16]; 90 GBool paddingReached; // encryption only 91 int bufIdx; 92 }; 93 94 struct DecryptAES256State { 95 Guint w[60]; 96 Guchar state[16]; 97 Guchar cbc[16]; 98 Guchar buf[16]; 99 GBool paddingReached; // encryption only 100 int bufIdx; 101 }; 102 103 class BaseCryptStream : public FilterStream { 104 public: 105 106 BaseCryptStream(Stream *strA, Guchar *fileKey, CryptAlgorithm algoA, 107 int keyLength, int objNum, int objGen); 108 virtual ~BaseCryptStream(); getKind()109 virtual StreamKind getKind() { return strCrypt; } 110 virtual void reset(); 111 virtual int getChar(); 112 virtual int lookChar() = 0; 113 virtual Goffset getPos(); 114 virtual GBool isBinary(GBool last); getUndecodedStream()115 virtual Stream *getUndecodedStream() { return this; } 116 void setAutoDelete(GBool val); 117 118 protected: 119 CryptAlgorithm algo; 120 int objKeyLength; 121 Guchar objKey[32]; 122 Goffset charactersRead; // so that getPos() can be correct 123 int nextCharBuff; // EOF means not read yet 124 GBool autoDelete; 125 126 union { 127 DecryptRC4State rc4; 128 DecryptAESState aes; 129 DecryptAES256State aes256; 130 } state; 131 }; 132 133 //------------------------------------------------------------------------ 134 // EncryptStream / DecryptStream 135 //------------------------------------------------------------------------ 136 137 class EncryptStream : public BaseCryptStream { 138 public: 139 140 EncryptStream(Stream *strA, Guchar *fileKey, CryptAlgorithm algoA, 141 int keyLength, int objNum, int objGen); 142 ~EncryptStream(); 143 virtual void reset(); 144 virtual int lookChar(); 145 }; 146 147 class DecryptStream : public BaseCryptStream { 148 public: 149 150 DecryptStream(Stream *strA, Guchar *fileKey, CryptAlgorithm algoA, 151 int keyLength, int objNum, int objGen); 152 ~DecryptStream(); 153 virtual void reset(); 154 virtual int lookChar(); 155 }; 156 157 //------------------------------------------------------------------------ 158 159 extern void md5(Guchar *msg, int msgLen, Guchar *digest); 160 161 #endif 162