Back to index

tetex-bin  3.0
Public Member Functions | Static Public Member Functions | Static Private Member Functions | Private Attributes
Decrypt Class Reference

#include <Decrypt.h>

List of all members.

Public Member Functions

 Decrypt (Guchar *fileKey, int keyLength, int objNum, int objGen)
void reset ()
Guchar decryptByte (Guchar c)

Static Public Member Functions

static GBool makeFileKey (int encVersion, int encRevision, int keyLength, GString *ownerKey, GString *userKey, int permissions, GString *fileID, GString *ownerPassword, GString *userPassword, Guchar *fileKey, GBool *ownerPasswordOk)

Static Private Member Functions

static GBool makeFileKey2 (int encVersion, int encRevision, int keyLength, GString *ownerKey, GString *userKey, int permissions, GString *fileID, GString *userPassword, Guchar *fileKey)

Private Attributes

int objKeyLength
Guchar objKey [21]
Guchar state [256]
Guchar x
Guchar y

Detailed Description

Definition at line 25 of file Decrypt.h.


Constructor & Destructor Documentation

Decrypt::Decrypt ( Guchar fileKey,
int  keyLength,
int  objNum,
int  objGen 
)

Definition at line 33 of file Decrypt.cc.

                                                                       {
  int i;

  // construct object key
  for (i = 0; i < keyLength; ++i) {
    objKey[i] = fileKey[i];
  }
  objKey[keyLength] = objNum & 0xff;
  objKey[keyLength + 1] = (objNum >> 8) & 0xff;
  objKey[keyLength + 2] = (objNum >> 16) & 0xff;
  objKey[keyLength + 3] = objGen & 0xff;
  objKey[keyLength + 4] = (objGen >> 8) & 0xff;
  md5(objKey, keyLength + 5, objKey);

  // set up for decryption
  x = y = 0;
  if ((objKeyLength = keyLength + 5) > 16) {
    objKeyLength = 16;
  }
  rc4InitKey(objKey, objKeyLength, state);
}

Here is the call graph for this function:


Member Function Documentation

Definition at line 60 of file Decrypt.cc.

                                    {
  return rc4DecryptByte(state, &x, &y, c);
}

Here is the call graph for this function:

Here is the caller graph for this function:

GBool Decrypt::makeFileKey ( int  encVersion,
int  encRevision,
int  keyLength,
GString ownerKey,
GString userKey,
int  permissions,
GString fileID,
GString ownerPassword,
GString userPassword,
Guchar fileKey,
GBool ownerPasswordOk 
) [static]

Definition at line 64 of file Decrypt.cc.

                                                                 {
  Guchar test[32], test2[32];
  GString *userPassword2;
  Guchar fState[256];
  Guchar tmpKey[16];
  Guchar fx, fy;
  int len, i, j;

  // try using the supplied owner password to generate the user password
  *ownerPasswordOk = gFalse;
  if (ownerPassword) {
    len = ownerPassword->getLength();
    if (len < 32) {
      memcpy(test, ownerPassword->getCString(), len);
      memcpy(test + len, passwordPad, 32 - len);
    } else {
      memcpy(test, ownerPassword->getCString(), 32);
    }
    md5(test, 32, test);
    if (encRevision == 3) {
      for (i = 0; i < 50; ++i) {
       md5(test, 16, test);
      }
    }
    if (encRevision == 2) {
      rc4InitKey(test, keyLength, fState);
      fx = fy = 0;
      for (i = 0; i < 32; ++i) {
       test2[i] = rc4DecryptByte(fState, &fx, &fy, ownerKey->getChar(i));
      }
    } else {
      memcpy(test2, ownerKey->getCString(), 32);
      for (i = 19; i >= 0; --i) {
       for (j = 0; j < keyLength; ++j) {
         tmpKey[j] = test[j] ^ i;
       }
       rc4InitKey(tmpKey, keyLength, fState);
       fx = fy = 0;
       for (j = 0; j < 32; ++j) {
         test2[j] = rc4DecryptByte(fState, &fx, &fy, test2[j]);
       }
      }
    }
    userPassword2 = new GString((char *)test2, 32);
    if (makeFileKey2(encVersion, encRevision, keyLength, ownerKey, userKey,
                   permissions, fileID, userPassword2, fileKey)) {
      *ownerPasswordOk = gTrue;
      delete userPassword2;
      return gTrue;
    }
    delete userPassword2;
  }

  // try using the supplied user password
  return makeFileKey2(encVersion, encRevision, keyLength, ownerKey, userKey,
                    permissions, fileID, userPassword, fileKey);
}

Here is the call graph for this function:

Here is the caller graph for this function:

GBool Decrypt::makeFileKey2 ( int  encVersion,
int  encRevision,
int  keyLength,
GString ownerKey,
GString userKey,
int  permissions,
GString fileID,
GString userPassword,
Guchar fileKey 
) [static, private]

Definition at line 126 of file Decrypt.cc.

                                                                 {
  Guchar *buf;
  Guchar test[32];
  Guchar fState[256];
  Guchar tmpKey[16];
  Guchar fx, fy;
  int len, i, j;
  GBool ok;

  // generate file key
  buf = (Guchar *)gmalloc(68 + fileID->getLength());
  if (userPassword) {
    len = userPassword->getLength();
    if (len < 32) {
      memcpy(buf, userPassword->getCString(), len);
      memcpy(buf + len, passwordPad, 32 - len);
    } else {
      memcpy(buf, userPassword->getCString(), 32);
    }
  } else {
    memcpy(buf, passwordPad, 32);
  }
  memcpy(buf + 32, ownerKey->getCString(), 32);
  buf[64] = permissions & 0xff;
  buf[65] = (permissions >> 8) & 0xff;
  buf[66] = (permissions >> 16) & 0xff;
  buf[67] = (permissions >> 24) & 0xff;
  memcpy(buf + 68, fileID->getCString(), fileID->getLength());
  md5(buf, 68 + fileID->getLength(), fileKey);
  if (encRevision == 3) {
    for (i = 0; i < 50; ++i) {
      md5(fileKey, keyLength, fileKey);
    }
  }

  // test user password
  if (encRevision == 2) {
    rc4InitKey(fileKey, keyLength, fState);
    fx = fy = 0;
    for (i = 0; i < 32; ++i) {
      test[i] = rc4DecryptByte(fState, &fx, &fy, userKey->getChar(i));
    }
    ok = memcmp(test, passwordPad, 32) == 0;
  } else if (encRevision == 3) {
    memcpy(test, userKey->getCString(), 32);
    for (i = 19; i >= 0; --i) {
      for (j = 0; j < keyLength; ++j) {
       tmpKey[j] = fileKey[j] ^ i;
      }
      rc4InitKey(tmpKey, keyLength, fState);
      fx = fy = 0;
      for (j = 0; j < 32; ++j) {
       test[j] = rc4DecryptByte(fState, &fx, &fy, test[j]);
      }
    }
    memcpy(buf, passwordPad, 32);
    memcpy(buf + 32, fileID->getCString(), fileID->getLength());
    md5(buf, 32 + fileID->getLength(), buf);
    ok = memcmp(test, buf, 16) == 0;
  } else {
    ok = gFalse;
  }

  gfree(buf);
  return ok;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 55 of file Decrypt.cc.

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Guchar Decrypt::objKey[21] [private]

Definition at line 56 of file Decrypt.h.

Definition at line 55 of file Decrypt.h.

Guchar Decrypt::state[256] [private]

Definition at line 57 of file Decrypt.h.

Guchar Decrypt::x [private]

Definition at line 58 of file Decrypt.h.

Guchar Decrypt::y [private]

Definition at line 58 of file Decrypt.h.


The documentation for this class was generated from the following files: