Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.5 KiB

  1. // des.h - written and placed in the public domain by Wei Dai
  2. //! \file des.h
  3. //! \brief Classes for DES, 2-key Triple-DES, 3-key Triple-DES and DESX
  4. #ifndef CRYPTOPP_DES_H
  5. #define CRYPTOPP_DES_H
  6. #include "seckey.h"
  7. #include "secblock.h"
  8. NAMESPACE_BEGIN(CryptoPP)
  9. class CRYPTOPP_DLL RawDES
  10. {
  11. public:
  12. void RawSetKey(CipherDir direction, const byte *userKey);
  13. void RawProcessBlock(word32 &l, word32 &r) const;
  14. protected:
  15. static const word32 Spbox[8][64];
  16. FixedSizeSecBlock<word32, 32> k;
  17. };
  18. //! _
  19. struct DES_Info : public FixedBlockSize<8>, public FixedKeyLength<8>
  20. {
  21. // disable DES in DLL version by not exporting this function
  22. static const char * StaticAlgorithmName() {return "DES";}
  23. };
  24. /// <a href="http://www.weidai.com/scan-mirror/cs.html#DES">DES</a>
  25. /*! The DES implementation in Crypto++ ignores the parity bits
  26. (the least significant bits of each byte) in the key. However
  27. you can use CheckKeyParityBits() and CorrectKeyParityBits() to
  28. check or correct the parity bits if you wish. */
  29. class DES : public DES_Info, public BlockCipherDocumentation
  30. {
  31. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_Info>, public RawDES
  32. {
  33. public:
  34. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
  35. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  36. };
  37. public:
  38. //! check DES key parity bits
  39. static bool CheckKeyParityBits(const byte *key);
  40. //! correct DES key parity bits
  41. static void CorrectKeyParityBits(byte *key);
  42. typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
  43. typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
  44. };
  45. //! _
  46. struct DES_EDE2_Info : public FixedBlockSize<8>, public FixedKeyLength<16>
  47. {
  48. CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "DES-EDE2";}
  49. };
  50. /// <a href="http://www.weidai.com/scan-mirror/cs.html#DESede">DES-EDE2</a>
  51. class DES_EDE2 : public DES_EDE2_Info, public BlockCipherDocumentation
  52. {
  53. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE2_Info>
  54. {
  55. public:
  56. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
  57. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  58. protected:
  59. RawDES m_des1, m_des2;
  60. };
  61. public:
  62. typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
  63. typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
  64. };
  65. //! _
  66. struct DES_EDE3_Info : public FixedBlockSize<8>, public FixedKeyLength<24>
  67. {
  68. CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "DES-EDE3";}
  69. };
  70. /// <a href="http://www.weidai.com/scan-mirror/cs.html#DESede">DES-EDE3</a>
  71. class DES_EDE3 : public DES_EDE3_Info, public BlockCipherDocumentation
  72. {
  73. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE3_Info>
  74. {
  75. public:
  76. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
  77. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  78. protected:
  79. RawDES m_des1, m_des2, m_des3;
  80. };
  81. public:
  82. typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
  83. typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
  84. };
  85. //! _
  86. struct DES_XEX3_Info : public FixedBlockSize<8>, public FixedKeyLength<24>
  87. {
  88. static const char *StaticAlgorithmName() {return "DES-XEX3";}
  89. };
  90. /// <a href="http://www.weidai.com/scan-mirror/cs.html#DESX">DES-XEX3</a>, AKA DESX
  91. class DES_XEX3 : public DES_XEX3_Info, public BlockCipherDocumentation
  92. {
  93. class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_XEX3_Info>
  94. {
  95. public:
  96. void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
  97. void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
  98. protected:
  99. FixedSizeSecBlock<byte, BLOCKSIZE> m_x1, m_x3;
  100. // VS2005 workaround: calling modules compiled with /clr gets unresolved external symbol DES::Base::ProcessAndXorBlock
  101. // if we use DES::Encryption here directly without value_ptr.
  102. value_ptr<DES::Encryption> m_des;
  103. };
  104. public:
  105. typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
  106. typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
  107. };
  108. typedef DES::Encryption DESEncryption;
  109. typedef DES::Decryption DESDecryption;
  110. typedef DES_EDE2::Encryption DES_EDE2_Encryption;
  111. typedef DES_EDE2::Decryption DES_EDE2_Decryption;
  112. typedef DES_EDE3::Encryption DES_EDE3_Encryption;
  113. typedef DES_EDE3::Decryption DES_EDE3_Decryption;
  114. typedef DES_XEX3::Encryption DES_XEX3_Encryption;
  115. typedef DES_XEX3::Decryption DES_XEX3_Decryption;
  116. NAMESPACE_END
  117. #endif