des.h 5.3 KB

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