/src/xpdf-4.05/xpdf/Decrypt.h
Line | Count | Source |
1 | | //======================================================================== |
2 | | // |
3 | | // Decrypt.h |
4 | | // |
5 | | // Copyright 1996-2003 Glyph & Cog, LLC |
6 | | // |
7 | | //======================================================================== |
8 | | |
9 | | #ifndef DECRYPT_H |
10 | | #define DECRYPT_H |
11 | | |
12 | | #include <aconf.h> |
13 | | |
14 | | #include "gtypes.h" |
15 | | #include "GString.h" |
16 | | #include "Object.h" |
17 | | #include "Stream.h" |
18 | | |
19 | | //------------------------------------------------------------------------ |
20 | | // Decrypt |
21 | | //------------------------------------------------------------------------ |
22 | | |
23 | | class Decrypt { |
24 | | public: |
25 | | |
26 | | // Generate a file key. The <fileKey> buffer must have space for at |
27 | | // least 16 bytes. Checks <ownerPassword> and then <userPassword> |
28 | | // and returns true if either is correct. Sets <ownerPasswordOk> if |
29 | | // the owner password was correct. Either or both of the passwords |
30 | | // may be NULL, which is treated as an empty string. |
31 | | static GBool makeFileKey(int encVersion, int encRevision, int keyLength, |
32 | | GString *ownerKey, GString *userKey, |
33 | | GString *ownerEnc, GString *userEnc, |
34 | | int permissions, GString *fileID, |
35 | | GString *ownerPassword, GString *userPassword, |
36 | | Guchar *fileKey, GBool encryptMetadata, |
37 | | GBool *ownerPasswordOk); |
38 | | |
39 | | private: |
40 | | |
41 | | static void r6Hash(Guchar *key, int keyLen, const char *pwd, int pwdLen, |
42 | | char *userKey); |
43 | | static GBool makeFileKey2(int encVersion, int encRevision, int keyLength, |
44 | | GString *ownerKey, GString *userKey, |
45 | | int permissions, GString *fileID, |
46 | | GString *userPassword, Guchar *fileKey, |
47 | | GBool encryptMetadata); |
48 | | }; |
49 | | |
50 | | //------------------------------------------------------------------------ |
51 | | // DecryptStream |
52 | | //------------------------------------------------------------------------ |
53 | | |
54 | | struct DecryptRC4State { |
55 | | Guchar state[256]; |
56 | | Guchar x, y; |
57 | | int buf; |
58 | | }; |
59 | | |
60 | | struct DecryptAESState { |
61 | | Guint w[44]; |
62 | | Guchar state[16]; |
63 | | Guchar cbc[16]; |
64 | | Guchar buf[16]; |
65 | | int bufIdx; |
66 | | }; |
67 | | |
68 | | struct DecryptAES256State { |
69 | | Guint w[60]; |
70 | | Guchar state[16]; |
71 | | Guchar cbc[16]; |
72 | | Guchar buf[16]; |
73 | | int bufIdx; |
74 | | }; |
75 | | |
76 | | class DecryptStream: public FilterStream { |
77 | | public: |
78 | | |
79 | | DecryptStream(Stream *strA, Guchar *fileKeyA, |
80 | | CryptAlgorithm algoA, int keyLengthA, |
81 | | int objNumA, int objGenA); |
82 | | virtual ~DecryptStream(); |
83 | | virtual Stream *copy(); |
84 | 0 | virtual StreamKind getKind() { return strWeird; } |
85 | | virtual void reset(); |
86 | | virtual int getChar(); |
87 | | virtual int lookChar(); |
88 | | virtual GBool isBinary(GBool last); |
89 | 0 | virtual Stream *getUndecodedStream() { return this; } |
90 | | |
91 | | private: |
92 | | |
93 | | Guchar fileKey[32]; |
94 | | CryptAlgorithm algo; |
95 | | int keyLength; |
96 | | int objNum, objGen; |
97 | | int objKeyLength; |
98 | | Guchar objKey[32]; |
99 | | |
100 | | union { |
101 | | DecryptRC4State rc4; |
102 | | DecryptAESState aes; |
103 | | DecryptAES256State aes256; |
104 | | } state; |
105 | | }; |
106 | | |
107 | | //------------------------------------------------------------------------ |
108 | | |
109 | | struct MD5State { |
110 | | Gulong a, b, c, d; |
111 | | Guchar buf[64]; |
112 | | int bufLen; |
113 | | int msgLen; |
114 | | Guchar digest[16]; |
115 | | }; |
116 | | |
117 | | extern void rc4InitKey(Guchar *key, int keyLen, Guchar *state); |
118 | | extern Guchar rc4DecryptByte(Guchar *state, Guchar *x, Guchar *y, Guchar c); |
119 | | void md5Start(MD5State *state); |
120 | | void md5Append(MD5State *state, Guchar *data, int dataLen); |
121 | | void md5Finish(MD5State *state); |
122 | | extern void md5(Guchar *msg, int msgLen, Guchar *digest); |
123 | | extern void aesKeyExpansion(DecryptAESState *s, |
124 | | Guchar *objKey, int objKeyLen, |
125 | | GBool decrypt); |
126 | | extern void aesEncryptBlock(DecryptAESState *s, Guchar *in); |
127 | | extern void aesDecryptBlock(DecryptAESState *s, Guchar *in, GBool last); |
128 | | |
129 | | #endif |