1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
//========================================================================
//
// Decrypt.h
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
// Copyright (C) 2008 Julien Rebetez <julien@fhtagn.net>
// Copyright (C) 2009 David Benjamin <davidben@mit.edu>
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================
#ifndef DECRYPT_H
#define DECRYPT_H
#ifdef USE_GCC_PRAGMAS
#pragma interface
#endif
#include "goo/gtypes.h"
#include "goo/GooString.h"
#include "Object.h"
#include "Stream.h"
//------------------------------------------------------------------------
// Decrypt
//------------------------------------------------------------------------
class Decrypt {
public:
// Generate a file key. The <fileKey> buffer must have space for at
// least 16 bytes. Checks <ownerPassword> and then <userPassword>
// and returns true if either is correct. Sets <ownerPasswordOk> if
// the owner password was correct. Either or both of the passwords
// may be NULL, which is treated as an empty string.
static GBool makeFileKey(int encVersion, int encRevision, int keyLength,
GooString *ownerKey, GooString *userKey,
GooString *ownerEnc, GooString *userEnc,
int permissions, GooString *fileID,
GooString *ownerPassword, GooString *userPassword,
Guchar *fileKey, GBool encryptMetadata,
GBool *ownerPasswordOk);
private:
static GBool makeFileKey2(int encVersion, int encRevision, int keyLength,
GooString *ownerKey, GooString *userKey,
int permissions, GooString *fileID,
GooString *userPassword, Guchar *fileKey,
GBool encryptMetadata);
};
//------------------------------------------------------------------------
// DecryptStream
//------------------------------------------------------------------------
struct DecryptRC4State {
Guchar state[256];
Guchar x, y;
int buf;
};
struct DecryptAESState {
Guint w[44];
Guchar state[16];
Guchar cbc[16];
Guchar buf[16];
int bufIdx;
};
struct DecryptAES256State {
Guint w[60];
Guchar state[16];
Guchar cbc[16];
Guchar buf[16];
int bufIdx;
};
class DecryptStream: public FilterStream {
public:
DecryptStream(Stream *strA, Guchar *fileKey,
CryptAlgorithm algoA, int keyLength,
int objNum, int objGen);
virtual ~DecryptStream();
virtual StreamKind getKind() { return strWeird; }
virtual void reset();
virtual int getChar();
virtual int lookChar();
virtual int getPos();
virtual GBool isBinary(GBool last);
virtual Stream *getUndecodedStream() { return this; }
private:
CryptAlgorithm algo;
int objKeyLength;
Guchar objKey[32];
int charactersRead; // so that getPos() can be correct
union {
DecryptRC4State rc4;
DecryptAESState aes;
DecryptAES256State aes256;
} state;
};
//------------------------------------------------------------------------
extern void rc4InitKey(Guchar *key, int keyLen, Guchar *state);
extern Guchar rc4DecryptByte(Guchar *state, Guchar *x, Guchar *y, Guchar c);
extern void md5(Guchar *msg, int msgLen, Guchar *digest);
#endif
|