From b21162cf8e06f40baa1f58be6a8c17435cebc34d Mon Sep 17 00:00:00 2001 From: weidai Date: Fri, 4 Oct 2002 17:31:41 +0000 Subject: Initial revision git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@2 57ff6487-cd31-0410-9ec3-f628ee90f5f0 --- des.h | 133 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 des.h (limited to 'des.h') diff --git a/des.h b/des.h new file mode 100644 index 0000000..65b4d33 --- /dev/null +++ b/des.h @@ -0,0 +1,133 @@ +#ifndef CRYPTOPP_DES_H +#define CRYPTOPP_DES_H + +/** \file +*/ + +#include "seckey.h" +#include "secblock.h" + +NAMESPACE_BEGIN(CryptoPP) + +struct DES_Info : public FixedBlockSize<8>, public FixedKeyLength<8> +{ + static const char *StaticAlgorithmName() {return "DES";} +}; + +/// DES +/*! The DES implementation in Crypto++ ignores the parity bits + (the least significant bits of each byte) in the key. However + you can use CheckKeyParityBits() and CorrectKeyParityBits() to + check or correct the parity bits if you wish. */ +class DES : public DES_Info, public BlockCipherDocumentation +{ + class Base : public BlockCipherBaseTemplate + { + public: + void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length = 8); + void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; + + // exposed for faster Triple-DES + void RawProcessBlock(word32 &l, word32 &r) const; + + protected: + static const word32 Spbox[8][64]; + + FixedSizeSecBlock k; + }; + +public: + //! check DES key parity bits + static bool CheckKeyParityBits(const byte *key); + //! correct DES key parity bits + static void CorrectKeyParityBits(byte *key); + + typedef BlockCipherTemplate Encryption; + typedef BlockCipherTemplate Decryption; +}; + +struct DES_EDE2_Info : public FixedBlockSize<8>, public FixedKeyLength<16> +{ + static const char *StaticAlgorithmName() {return "DES-EDE2";} +}; + +/// DES-EDE2 +class DES_EDE2 : public DES_EDE2_Info, public BlockCipherDocumentation +{ + class Base : public BlockCipherBaseTemplate + { + public: + void UncheckedSetKey(CipherDir direction, const byte *userKey, unsigned int length); + void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; + + protected: + DES::Encryption m_des1, m_des2; + }; + +public: + typedef BlockCipherTemplate Encryption; + typedef BlockCipherTemplate Decryption; +}; + +struct DES_EDE3_Info : public FixedBlockSize<8>, public FixedKeyLength<24> +{ + static const char *StaticAlgorithmName() {return "DES-EDE3";} +}; + +/// DES-EDE3 +class DES_EDE3 : public DES_EDE3_Info, public BlockCipherDocumentation +{ + class Base : public BlockCipherBaseTemplate + { + public: + void UncheckedSetKey(CipherDir dir, const byte *key, unsigned int length); + void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; + + protected: + DES::Encryption m_des1, m_des2, m_des3; + }; + +public: + typedef BlockCipherTemplate Encryption; + typedef BlockCipherTemplate Decryption; +}; + +struct DES_XEX3_Info : public FixedBlockSize<8>, public FixedKeyLength<24> +{ + static const char *StaticAlgorithmName() {return "DES-XEX3";} +}; + +/// DES-XEX3, AKA DESX +class DES_XEX3 : public DES_XEX3_Info, public BlockCipherDocumentation +{ + class Base : public BlockCipherBaseTemplate + { + public: + void UncheckedSetKey(CipherDir dir, const byte *key, unsigned int length); + void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; + + protected: + FixedSizeSecBlock m_x1, m_x3; + DES::Encryption m_des; + }; + +public: + typedef BlockCipherTemplate Encryption; + typedef BlockCipherTemplate Decryption; +}; + +typedef DES::Encryption DESEncryption; +typedef DES::Decryption DESDecryption; + +typedef DES_EDE2::Encryption DES_EDE2_Encryption; +typedef DES_EDE2::Decryption DES_EDE2_Decryption; + +typedef DES_EDE3::Encryption DES_EDE3_Encryption; +typedef DES_EDE3::Decryption DES_EDE3_Decryption; + +typedef DES_XEX3::Encryption DES_XEX3_Encryption; +typedef DES_XEX3::Decryption DES_XEX3_Decryption; + +NAMESPACE_END + +#endif -- cgit v1.2.1