Decrypt.h 5.03 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
// Copyright (C) 2013 Adrian Johnson <ajohnson@redneon.com>
20
// Copyright (C) 2013, 2018, 2019 Albert Astals Cid <aacid@kde.org>
Thomas Freitag's avatar
Thomas Freitag committed
21
// Copyright (C) 2013 Thomas Freitag <Thomas.Freitag@alfa.de>
22 23 24 25 26 27
//
// 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
28 29 30 31
#ifndef DECRYPT_H
#define DECRYPT_H

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

//------------------------------------------------------------------------
// 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.
47
  static bool makeFileKey(int encVersion, int encRevision, int keyLength,
Albert Astals Cid's avatar
Albert Astals Cid committed
48 49 50 51
			   const GooString *ownerKey, const GooString *userKey,
			   const GooString *ownerEnc, const GooString *userEnc,
			   int permissions, const GooString *fileID,
			   const GooString *ownerPassword, const GooString *userPassword,
52
			   unsigned char *fileKey, bool encryptMetadata,
53
			   bool *ownerPasswordOk);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
54 55 56

private:

57
  static bool makeFileKey2(int encVersion, int encRevision, int keyLength,
Albert Astals Cid's avatar
Albert Astals Cid committed
58 59
			    const GooString *ownerKey, const GooString *userKey,
			    int permissions, const GooString *fileID,
60
			    const GooString *userPassword, unsigned char *fileKey,
61
			    bool encryptMetadata);
62
};
Kristian Høgsberg's avatar
Kristian Høgsberg committed
63

64
//------------------------------------------------------------------------
65
// Helper classes
66 67
//------------------------------------------------------------------------

68 69 70 71 72 73 74
/* 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. */
75
struct DecryptRC4State {
76 77
  unsigned char state[256];
  unsigned char x, y;
78 79 80
};

struct DecryptAESState {
81
  unsigned int w[44];
82 83 84
  unsigned char state[16];
  unsigned char cbc[16];
  unsigned char buf[16];
85
  bool paddingReached; // encryption only
86 87 88
  int bufIdx;
};

89
struct DecryptAES256State {
90
  unsigned int w[60];
91 92 93
  unsigned char state[16];
  unsigned char cbc[16];
  unsigned char buf[16];
94
  bool paddingReached; // encryption only
95 96 97
  int bufIdx;
};

98
class BaseCryptStream : public FilterStream {
99 100
public:

101
  BaseCryptStream(Stream *strA, const unsigned char *fileKey, CryptAlgorithm algoA,
102
                  int keyLength, Ref ref);
Albert Astals Cid's avatar
Albert Astals Cid committed
103
  ~BaseCryptStream();
104
  StreamKind getKind() const override { return strCrypt; }
Albert Astals Cid's avatar
Albert Astals Cid committed
105 106 107 108
  void reset() override;
  int getChar() override;
  int lookChar() override = 0;
  Goffset getPos() override;
109
  bool isBinary(bool last) override;
Albert Astals Cid's avatar
Albert Astals Cid committed
110
  Stream *getUndecodedStream() override { return this; }
111
  void setAutoDelete(bool val);
112

113
protected:
114 115
  CryptAlgorithm algo;
  int objKeyLength;
116
  unsigned char objKey[32];
Albert Astals Cid's avatar
Albert Astals Cid committed
117
  Goffset charactersRead; // so that getPos() can be correct
118
  int nextCharBuff;   // EOF means not read yet
119
  bool autoDelete;
120 121 122 123

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

//------------------------------------------------------------------------
129
// EncryptStream / DecryptStream
130 131
//------------------------------------------------------------------------

132 133 134
class EncryptStream : public BaseCryptStream {
public:

135
  EncryptStream(Stream *strA, const unsigned char *fileKey, CryptAlgorithm algoA,
136
                int keyLength, Ref ref);
137
  ~EncryptStream();
Albert Astals Cid's avatar
Albert Astals Cid committed
138 139
  void reset() override;
  int lookChar() override;
140 141
};

142 143 144
class DecryptStream : public BaseCryptStream {
public:

145
  DecryptStream(Stream *strA, const unsigned char *fileKey, CryptAlgorithm algoA,
146
                int keyLength, Ref ref);
147
  ~DecryptStream();
Albert Astals Cid's avatar
Albert Astals Cid committed
148 149
  void reset() override;
  int lookChar() override;
150
};
151 152 153
 
//------------------------------------------------------------------------

154
extern void md5(const unsigned char *msg, int msgLen, unsigned char *digest);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
155 156

#endif