1
0

Encryption.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Encryption.h: interface for the CEncryption class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_ENCRYPTION_H__06C80AE2_89BD_4040_A303_D3A71F44BFBD__INCLUDED_)
  5. #define AFX_ENCRYPTION_H__06C80AE2_89BD_4040_A303_D3A71F44BFBD__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. #include "iencryption.h"
  10. #include "rijndael.h"
  11. #include "NewRandom.h"
  12. // The signature constants were chosen randomly
  13. #define TD_TLSIG_1 0x139C5AFE
  14. #define TD_TLSIG_2 0xBF3562DA
  15. #define TD_STD_KEYENCROUNDS 100000
  16. #pragma pack(1)
  17. typedef struct _TD_TLHEADER // The database header
  18. {
  19. BYTE aHeaderHash[32]; // SHA-256 hash of the rest of the header
  20. DWORD dwSignature1; // = TD_TLSIG_1
  21. DWORD dwSignature2; // = TD_TLSIG_2
  22. BYTE aMasterSeed[16]; // Seed that gets hashed with the userkey to form the final key
  23. RD_UINT8 aEncryptionIV[16]; // IV used for content encryption
  24. BYTE aContentsHash[32]; // SHA-256 hash of the database, used for integrity check
  25. BYTE aMasterSeed2[32]; // Used for the dwKeyEncRounds AES transformations
  26. DWORD dwKeyEncRounds;
  27. } TD_TLHEADER, *PTD_TLHEADER;
  28. #pragma pack()
  29. class CEncryption : public IEncryption
  30. {
  31. public:
  32. CEncryption();
  33. virtual ~CEncryption();
  34. void Release();
  35. bool Encrypt(const unsigned char* szInput, int nLenInput, const char* szPassword,
  36. unsigned char*& pOutput, int& nLenOutput);
  37. bool Decrypt(const unsigned char* pInput, int nLenInput, const char* szPassword,
  38. unsigned char*& pOutput, int& nLenOutput);
  39. void FreeBuffer(unsigned char*& pBuffer);
  40. private:
  41. // Encrypt the master key a few times to make brute-force key-search harder
  42. BOOL _TransformMasterKey(BYTE *pKeySeed);
  43. BYTE m_pMasterKey[32]; // Master key used to encrypt the whole database
  44. BYTE m_pTransformedMasterKey[32]; // Master key encrypted several times
  45. DWORD m_dwKeyEncRounds;
  46. CNewRandom m_random; // Pseudo-random number generator
  47. };
  48. #endif // !defined(AFX_ENCRYPTION_H__06C80AE2_89BD_4040_A303_D3A71F44BFBD__INCLUDED_)