Decrypt.h 4.75 KB
Newer Older
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1 2 3 4 5 6 7 8
//========================================================================
//
// Decrypt.h
//
// Copyright 1996-2003 Glyph & Cog, LLC
//
//========================================================================

9 10 11 12
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
13 14 15
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
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 20 21 22 23 24
//
// 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
//
//========================================================================

Kristian Høgsberg's avatar
Kristian Høgsberg committed
25 26 27 28 29 30 31 32 33
#ifndef DECRYPT_H
#define DECRYPT_H

#ifdef USE_GCC_PRAGMAS
#pragma interface
#endif

#include "goo/gtypes.h"
#include "goo/GooString.h"
34 35
#include "Object.h"
#include "Stream.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

//------------------------------------------------------------------------
// 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,
51
			   GooString *ownerEnc, GooString *userEnc,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
52 53
			   int permissions, GooString *fileID,
			   GooString *ownerPassword, GooString *userPassword,
54 55
			   Guchar *fileKey, GBool encryptMetadata,
			   GBool *ownerPasswordOk);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
56 57 58 59 60 61

private:

  static GBool makeFileKey2(int encVersion, int encRevision, int keyLength,
			    GooString *ownerKey, GooString *userKey,
			    int permissions, GooString *fileID,
62 63
			    GooString *userPassword, Guchar *fileKey,
			    GBool encryptMetadata);
64
};
Kristian Høgsberg's avatar
Kristian Høgsberg committed
65

66
//------------------------------------------------------------------------
67
// Helper classes
68 69
//------------------------------------------------------------------------

70 71 72 73 74 75 76
/* DecryptRC4State, DecryptAESState, DecryptAES256State are named like this for
 * historical reasons, but they're used for encryption too.
 * In case of decryption, the cbc field in AES and AES-256 contains the previous
 * input block or the CBC initialization vector (IV) if the stream has just been
 * reset). In case of encryption, it always contains the IV, whereas the
 * previous output is kept in buf. The paddingReached field is only used in
 * case of encryption. */
77
struct DecryptRC4State {
Kristian Høgsberg's avatar
Kristian Høgsberg committed
78 79
  Guchar state[256];
  Guchar x, y;
80 81 82 83 84 85 86
};

struct DecryptAESState {
  Guint w[44];
  Guchar state[16];
  Guchar cbc[16];
  Guchar buf[16];
87
  GBool paddingReached; // encryption only
88 89 90
  int bufIdx;
};

91 92 93 94 95
struct DecryptAES256State {
  Guint w[60];
  Guchar state[16];
  Guchar cbc[16];
  Guchar buf[16];
96
  GBool paddingReached; // encryption only
97 98 99
  int bufIdx;
};

100
class BaseCryptStream : public FilterStream {
101 102
public:

103 104 105
  BaseCryptStream(Stream *strA, Guchar *fileKey, CryptAlgorithm algoA,
                  int keyLength, int objNum, int objGen);
  virtual ~BaseCryptStream();
106 107 108
  virtual StreamKind getKind() { return strWeird; }
  virtual void reset();
  virtual int getChar();
109
  virtual int lookChar() = 0;
Adrian Johnson's avatar
Adrian Johnson committed
110
  virtual Goffset getPos();
111 112
  virtual GBool isBinary(GBool last);
  virtual Stream *getUndecodedStream() { return this; }
113
  void setAutoDelete(GBool val);
114

115
protected:
116 117
  CryptAlgorithm algo;
  int objKeyLength;
118
  Guchar objKey[32];
119
  int charactersRead; // so that getPos() can be correct
120
  int nextCharBuff;   // EOF means not read yet
121
  GBool autoDelete;
122 123 124 125

  union {
    DecryptRC4State rc4;
    DecryptAESState aes;
126
    DecryptAES256State aes256;
127
  } state;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
128
};
129 130

//------------------------------------------------------------------------
131
// EncryptStream / DecryptStream
132 133
//------------------------------------------------------------------------

134 135 136 137 138 139 140 141 142 143
class EncryptStream : public BaseCryptStream {
public:

  EncryptStream(Stream *strA, Guchar *fileKey, CryptAlgorithm algoA,
                int keyLength, int objNum, int objGen);
  ~EncryptStream();
  virtual void reset();
  virtual int lookChar();
};

144 145 146 147 148 149 150 151 152
class DecryptStream : public BaseCryptStream {
public:

  DecryptStream(Stream *strA, Guchar *fileKey, CryptAlgorithm algoA,
                int keyLength, int objNum, int objGen);
  ~DecryptStream();
  virtual void reset();
  virtual int lookChar();
};
153 154 155 156
 
//------------------------------------------------------------------------

extern void md5(Guchar *msg, int msgLen, Guchar *digest);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
157 158

#endif