Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:54

0001 // cham.h - written and placed in the public domain by Kim Sung Hee and Jeffrey Walton

0002 //          Based on "CHAM: A Family of Lightweight Block Ciphers for

0003 //          Resource-Constrained Devices" by Bonwook Koo, Dongyoung Roh,

0004 //          Hyeonjin Kim, Younghoon Jung, Dong-Geon Lee, and Daesung Kwon

0005 
0006 /// \file cham.h

0007 /// \brief Classes for the CHAM block cipher

0008 /// \since Crypto++ 8.0

0009 
0010 #ifndef CRYPTOPP_CHAM_H
0011 #define CRYPTOPP_CHAM_H
0012 
0013 #include "config.h"
0014 #include "seckey.h"
0015 #include "secblock.h"
0016 #include "algparam.h"
0017 
0018 #if (CRYPTOPP_BOOL_X64 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X86)
0019 # define CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS 1
0020 #endif
0021 
0022 // Yet another SunStudio/SunCC workaround. Failed self tests

0023 // in SSE code paths on i386 for SunStudio 12.3 and below.

0024 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x5120)
0025 # undef CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
0026 #endif
0027 
0028 NAMESPACE_BEGIN(CryptoPP)
0029 
0030 /// \brief CHAM block cipher information

0031 /// \since Crypto++ 8.0

0032 struct CHAM64_Info : public FixedBlockSize<8>, public FixedKeyLength<16>
0033 {
0034     /// \brief The algorithm name

0035     /// \return the algorithm name

0036     /// \details StaticAlgorithmName returns the algorithm's name as a static

0037     ///   member function.

0038     static const std::string StaticAlgorithmName()
0039     {
0040         // Format is Cipher-Blocksize

0041         return "CHAM-64";
0042     }
0043 };
0044 
0045 /// \brief CHAM block cipher information

0046 /// \since Crypto++ 8.0

0047 struct CHAM128_Info : public FixedBlockSize<16>, public VariableKeyLength<16,16,32,16>
0048 {
0049     /// \brief The algorithm name

0050     /// \return the algorithm name

0051     /// \details StaticAlgorithmName returns the algorithm's name as a static

0052     ///   member function.

0053     static const std::string StaticAlgorithmName()
0054     {
0055         // Format is Cipher-Blocksize

0056         return "CHAM-128";
0057     }
0058 };
0059 
0060 /// \brief CHAM 64-bit block cipher

0061 /// \details CHAM64 provides 64-bit block size. The valid key size is 128-bit.

0062 /// \note Crypto++ provides a byte oriented implementation

0063 /// \sa CHAM128, <a href="http://www.cryptopp.com/wiki/CHAM">CHAM</a>,

0064 ///   <a href="https://pdfs.semanticscholar.org/2f57/61b5c2614cffd58a09cc83c375a2b32a2ed3.pdf">

0065 ///   CHAM: A Family of Lightweight Block Ciphers for Resource-Constrained Devices</a>

0066 /// \since Crypto++ 8.0

0067 class CRYPTOPP_NO_VTABLE CHAM64 : public CHAM64_Info, public BlockCipherDocumentation
0068 {
0069 public:
0070     /// \brief CHAM block cipher transformation functions

0071     /// \details Provides implementation common to encryption and decryption

0072     /// \since Crypto++ 8.0

0073     class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<CHAM64_Info>
0074     {
0075     protected:
0076         void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
0077 
0078         SecBlock<word16> m_rk;
0079         mutable FixedSizeSecBlock<word16, 4> m_x;
0080         unsigned int m_kw;
0081     };
0082 
0083     /// \brief Encryption transformation

0084     /// \details Enc provides implementation for encryption transformation. All key and block

0085     ///   sizes are supported.

0086     /// \since Crypto++ 8.0

0087     class CRYPTOPP_NO_VTABLE Enc : public Base
0088     {
0089     public:
0090         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0091     };
0092 
0093     /// \brief Decryption transformation

0094     /// \details Dec provides implementation for decryption transformation. All key and block

0095     ///   sizes are supported.

0096     /// \since Crypto++ 8.0

0097     class CRYPTOPP_NO_VTABLE Dec : public Base
0098     {
0099     public:
0100         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0101     };
0102 
0103     /// \brief CHAM64 encryption

0104     typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
0105     /// \brief CHAM64 decryption

0106     typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
0107 };
0108 
0109 /// \brief CHAM64 encryption

0110 typedef CHAM64::Encryption CHAM64Encryption;
0111 /// \brief CHAM64 decryption

0112 typedef CHAM64::Decryption CHAM64Decryption;
0113 
0114 /// \brief CHAM 128-bit block cipher

0115 /// \details CHAM128 provides 128-bit block size. The valid key size is 128-bit and 256-bit.

0116 /// \note Crypto++ provides a byte oriented implementation

0117 /// \sa CHAM64, <a href="http://www.cryptopp.com/wiki/CHAM">CHAM</a>,

0118 ///   <a href="https://pdfs.semanticscholar.org/2f57/61b5c2614cffd58a09cc83c375a2b32a2ed3.pdf">

0119 ///   CHAM: A Family of Lightweight Block Ciphers for Resource-Constrained Devices</a>

0120 /// \since Crypto++ 8.0

0121 class CRYPTOPP_NO_VTABLE CHAM128 : public CHAM128_Info, public BlockCipherDocumentation
0122 {
0123 public:
0124     /// \brief CHAM block cipher transformation functions

0125     /// \details Provides implementation common to encryption and decryption

0126     /// \since Crypto++ 8.0

0127     class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<CHAM128_Info>
0128     {
0129     protected:
0130         void UncheckedSetKey(const byte *userKey, unsigned int keyLength, const NameValuePairs &params);
0131         std::string AlgorithmProvider() const;
0132 
0133         SecBlock<word32> m_rk;
0134         mutable FixedSizeSecBlock<word32, 4> m_x;
0135         unsigned int m_kw;
0136     };
0137 
0138     /// \brief Encryption transformation

0139     /// \details Enc provides implementation for encryption transformation. All key and block

0140     ///   sizes are supported.

0141     /// \since Crypto++ 8.0

0142     class CRYPTOPP_NO_VTABLE Enc : public Base
0143     {
0144     public:
0145         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0146 
0147 #if CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
0148         size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
0149 #endif
0150     };
0151 
0152     /// \brief Decryption transformation

0153     /// \details Dec provides implementation for decryption transformation. All key and block

0154     ///   sizes are supported.

0155     /// \since Crypto++ 8.0

0156     class CRYPTOPP_NO_VTABLE Dec : public Base
0157     {
0158     public:
0159         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0160 
0161 #if CRYPTOPP_CHAM128_ADVANCED_PROCESS_BLOCKS
0162         size_t AdvancedProcessBlocks(const byte *inBlocks, const byte *xorBlocks, byte *outBlocks, size_t length, word32 flags) const;
0163 #endif
0164     };
0165 
0166     /// \brief CHAM128 encryption

0167     typedef BlockCipherFinal<ENCRYPTION, Enc> Encryption;
0168     /// \brief CHAM128 decryption

0169     typedef BlockCipherFinal<DECRYPTION, Dec> Decryption;
0170 };
0171 
0172 /// \brief CHAM128 encryption

0173 typedef CHAM128::Encryption CHAM128Encryption;
0174 /// \brief CHAM128 decryption

0175 typedef CHAM128::Decryption CHAM128Decryption;
0176 
0177 NAMESPACE_END
0178 
0179 #endif  // CRYPTOPP_CHAM_H