Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:04

0001 // kalyna.h - written and placed in the public domain by Jeffrey Walton

0002 //            Based on public domain code by Keru Kuro.

0003 
0004 /// \file kalyna.h

0005 /// \brief Classes for the Kalyna block cipher

0006 /// \details The Crypto++ implementation relied upon three sources. First was Oliynykov, Gorbenko, Kazymyrov,

0007 ///   Ruzhentsev, Kuznetsov, Gorbenko, Dyrda, Dolgov, Pushkaryov, Mordvinov and Kaidalov's "A New Encryption

0008 ///   Standard of Ukraine: The Kalyna Block Cipher" (http://eprint.iacr.org/2015/650.pdf). Second was Roman

0009 ///   Oliynykov and Oleksandr Kazymyrov's GitHub with the reference implementation

0010 ///   (http://github.com/Roman-Oliynykov/Kalyna-reference). The third resource was Keru Kuro's implementation

0011 ///   of Kalyna in CppCrypto (http://sourceforge.net/projects/cppcrypto/). Kuro has an outstanding

0012 ///   implementation that performed better than the reference implementation and our initial attempts.

0013 
0014 #ifndef CRYPTOPP_KALYNA_H
0015 #define CRYPTOPP_KALYNA_H
0016 
0017 #include "config.h"
0018 #include "seckey.h"
0019 #include "secblock.h"
0020 
0021 NAMESPACE_BEGIN(CryptoPP)
0022 
0023 /// \brief Kalyna-128 block cipher information

0024 /// \since Crypto++ 6.0

0025 struct CRYPTOPP_NO_VTABLE Kalyna128_Info : public FixedBlockSize<16>, VariableKeyLength<16, 16, 32>
0026 {
0027     static const char* StaticAlgorithmName()
0028     {
0029         // Format is Cipher-Blocksize(Keylength)

0030         return "Kalyna-128";
0031     }
0032 };
0033 
0034 /// \brief Kalyna-256 block cipher information

0035 /// \since Crypto++ 6.0

0036 struct CRYPTOPP_NO_VTABLE Kalyna256_Info : public FixedBlockSize<32>, VariableKeyLength<32, 32, 64>
0037 {
0038     static const char* StaticAlgorithmName()
0039     {
0040         // Format is Cipher-Blocksize(Keylength)

0041         return "Kalyna-256";
0042     }
0043 };
0044 
0045 /// \brief Kalyna-512 block cipher information

0046 /// \since Crypto++ 6.0

0047 struct CRYPTOPP_NO_VTABLE Kalyna512_Info : public FixedBlockSize<64>, FixedKeyLength<64>
0048 {
0049     static const char* StaticAlgorithmName()
0050     {
0051         // Format is Cipher-Blocksize(Keylength)

0052         return "Kalyna-512";
0053     }
0054 };
0055 
0056 /// \brief Kalyna block cipher base class

0057 /// \since Crypto++ 6.0

0058 class CRYPTOPP_NO_VTABLE Kalyna_Base
0059 {
0060 public:
0061     virtual ~Kalyna_Base() {}
0062 
0063 protected:
0064     typedef SecBlock<word64, AllocatorWithCleanup<word64, true> > AlignedSecBlock64;
0065     mutable AlignedSecBlock64 m_wspace;  // work space

0066     AlignedSecBlock64         m_mkey;    // master key

0067     AlignedSecBlock64         m_rkeys;   // round keys

0068     unsigned int     m_kl, m_nb, m_nk;   // number 64-bit blocks and keys

0069 };
0070 
0071 /// \brief Kalyna 128-bit block cipher

0072 /// \details Kalyna128 provides 128-bit block size. The valid key sizes are 128-bit and 256-bit.

0073 /// \since Crypto++ 6.0

0074 class Kalyna128 : public Kalyna128_Info, public BlockCipherDocumentation
0075 {
0076 public:
0077     class CRYPTOPP_NO_VTABLE Base : public Kalyna_Base, public BlockCipherImpl<Kalyna128_Info>
0078     {
0079     public:
0080         /// \brief Provides the name of this algorithm

0081         /// \return the standard algorithm name

0082         /// \details If the object is unkeyed, then the generic name "Kalyna" is returned

0083         ///   to the caller. If the algorithm is keyed, then a two or three part name is

0084         ///   returned to the caller. The name follows DSTU 7624:2014, where block size is

0085         ///   provided first and then key length. The library uses a dash to identify block size

0086         ///   and parenthesis to identify key length. For example, Kalyna-128(256) is Kalyna

0087         ///   with a 128-bit block size and a 256-bit key length. If a mode is associated

0088         ///   with the object, then it follows as expected. For example, Kalyna-128(256)/ECB.

0089         ///   DSTU is a little more complex with more parameters, dashes, underscores, but the

0090         ///   library does not use the delimiters or full convention.

0091         std::string AlgorithmName() const {
0092             return std::string("Kalyna-128") + "(" + IntToString(m_kl*8) + ")";
0093         }
0094 
0095         /// \brief Provides input and output data alignment for optimal performance.

0096         /// \return the input data alignment that provides optimal performance

0097         /// \sa GetAlignment() and OptimalBlockSize()

0098         unsigned int OptimalDataAlignment() const {
0099             return GetAlignmentOf<word64>();
0100         }
0101 
0102     protected:
0103         void UncheckedSetKey(const byte *key, unsigned int keylen, const NameValuePairs &params);
0104         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0105 
0106     protected:
0107         void SetKey_22(const word64 key[2]);
0108         void SetKey_24(const word64 key[4]);
0109         void ProcessBlock_22(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0110         void ProcessBlock_24(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0111     };
0112 
0113     typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
0114     typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
0115 };
0116 
0117 /// \brief Kalyna 256-bit block cipher

0118 /// \details Kalyna256 provides 256-bit block size. The valid key sizes are 256-bit and 512-bit.

0119 /// \since Crypto++ 6.0

0120 class Kalyna256 : public Kalyna256_Info, public BlockCipherDocumentation
0121 {
0122 public:
0123     class CRYPTOPP_NO_VTABLE Base : public Kalyna_Base, public BlockCipherImpl<Kalyna256_Info>
0124     {
0125     public:
0126         /// \brief Provides the name of this algorithm

0127         /// \return the standard algorithm name

0128         /// \details If the object is unkeyed, then the generic name "Kalyna" is returned

0129         ///   to the caller. If the algorithm is keyed, then a two or three part name is

0130         ///   returned to the caller. The name follows DSTU 7624:2014, where block size is

0131         ///   provided first and then key length. The library uses a dash to identify block size

0132         ///   and parenthesis to identify key length. For example, Kalyna-128(256) is Kalyna

0133         ///   with a 128-bit block size and a 256-bit key length. If a mode is associated

0134         ///   with the object, then it follows as expected. For example, Kalyna-128(256)/ECB.

0135         ///   DSTU is a little more complex with more parameters, dashes, underscores, but the

0136         ///   library does not use the delimiters or full convention.

0137         std::string AlgorithmName() const {
0138             return std::string("Kalyna-256") + "(" + IntToString(m_kl*8) + ")";
0139         }
0140 
0141         /// \brief Provides input and output data alignment for optimal performance.

0142         /// \return the input data alignment that provides optimal performance

0143         /// \sa GetAlignment() and OptimalBlockSize()

0144         unsigned int OptimalDataAlignment() const {
0145             return GetAlignmentOf<word64>();
0146         }
0147 
0148     protected:
0149         void UncheckedSetKey(const byte *key, unsigned int keylen, const NameValuePairs &params);
0150         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0151 
0152     protected:
0153         void SetKey_44(const word64 key[4]);
0154         void SetKey_48(const word64 key[8]);
0155         void ProcessBlock_44(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0156         void ProcessBlock_48(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0157     };
0158 
0159     typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
0160     typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
0161 };
0162 
0163 /// \brief Kalyna 512-bit block cipher

0164 /// \details Kalyna512 provides 512-bit block size. The valid key size is 512-bit.

0165 /// \since Crypto++ 6.0

0166 class Kalyna512 : public Kalyna512_Info, public BlockCipherDocumentation
0167 {
0168 public:
0169     class CRYPTOPP_NO_VTABLE Base : public Kalyna_Base, public BlockCipherImpl<Kalyna512_Info>
0170     {
0171     public:
0172         /// \brief Provides the name of this algorithm

0173         /// \return the standard algorithm name

0174         /// \details If the object is unkeyed, then the generic name "Kalyna" is returned

0175         ///   to the caller. If the algorithm is keyed, then a two or three part name is

0176         ///   returned to the caller. The name follows DSTU 7624:2014, where block size is

0177         ///   provided first and then key length. The library uses a dash to identify block size

0178         ///   and parenthesis to identify key length. For example, Kalyna-128(256) is Kalyna

0179         ///   with a 128-bit block size and a 256-bit key length. If a mode is associated

0180         ///   with the object, then it follows as expected. For example, Kalyna-128(256)/ECB.

0181         ///   DSTU is a little more complex with more parameters, dashes, underscores, but the

0182         ///   library does not use the delimiters or full convention.

0183         std::string AlgorithmName() const {
0184             return std::string("Kalyna-512") + "(" + IntToString(m_kl*8) + ")";
0185         }
0186 
0187         /// \brief Provides input and output data alignment for optimal performance.

0188         /// \return the input data alignment that provides optimal performance

0189         /// \sa GetAlignment() and OptimalBlockSize()

0190         unsigned int OptimalDataAlignment() const {
0191             return GetAlignmentOf<word64>();
0192         }
0193 
0194     protected:
0195         void UncheckedSetKey(const byte *key, unsigned int keylen, const NameValuePairs &params);
0196         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0197 
0198     protected:
0199         void SetKey_88(const word64 key[8]);
0200         void ProcessBlock_88(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0201     };
0202 
0203     typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
0204     typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
0205 };
0206 
0207 typedef Kalyna128::Encryption Kalyna128Encryption;
0208 typedef Kalyna128::Decryption Kalyna128Decryption;
0209 
0210 typedef Kalyna256::Encryption Kalyna256Encryption;
0211 typedef Kalyna256::Decryption Kalyna256Decryption;
0212 
0213 typedef Kalyna512::Encryption Kalyna512Encryption;
0214 typedef Kalyna512::Decryption Kalyna512Decryption;
0215 
0216 NAMESPACE_END
0217 
0218 #endif  // CRYPTOPP_KALYNA_H