Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // safer.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file safer.h

0004 /// \brief Classes for the SAFER and SAFER-K block ciphers

0005 
0006 #ifndef CRYPTOPP_SAFER_H
0007 #define CRYPTOPP_SAFER_H
0008 
0009 #include "seckey.h"
0010 #include "secblock.h"
0011 
0012 NAMESPACE_BEGIN(CryptoPP)
0013 
0014 /// \brief SAFER block cipher

0015 class SAFER
0016 {
0017 public:
0018     /// \brief SAFER block cipher default operation

0019     class CRYPTOPP_NO_VTABLE Base : public BlockCipher
0020     {
0021     public:
0022         unsigned int OptimalDataAlignment() const {return 1;}
0023         void UncheckedSetKey(const byte *userkey, unsigned int length, const NameValuePairs &params);
0024 
0025     protected:
0026         virtual bool Strengthened() const =0;
0027 
0028         SecByteBlock keySchedule;
0029         static const byte exp_tab[256];
0030         static const byte log_tab[256];
0031     };
0032 
0033     /// \brief SAFER block cipher encryption operation

0034     class CRYPTOPP_NO_VTABLE Enc : public Base
0035     {
0036     public:
0037         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0038     };
0039 
0040     /// \brief SAFER block cipher decryption operation

0041     class CRYPTOPP_NO_VTABLE Dec : public Base
0042     {
0043     public:
0044         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0045     };
0046 };
0047 
0048 /// \brief SAFER block cipher default implementation

0049 /// \tparam BASE SAFER::Enc or SAFER::Dec derived base class

0050 /// \tparam INFO SAFER_Info derived class

0051 /// \tparam STR flag indicating a strengthened implementation

0052 /// \details SAFER-K is not strengthened; while SAFER-SK is strengthened.

0053 template <class BASE, class INFO, bool STR>
0054 class CRYPTOPP_NO_VTABLE SAFER_Impl : public BlockCipherImpl<INFO, BASE>
0055 {
0056 protected:
0057     bool Strengthened() const {return STR;}
0058 };
0059 
0060 /// \brief SAFER-K block cipher information

0061 struct SAFER_K_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13>
0062 {
0063     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "SAFER-K";}
0064 };
0065 
0066 /// \brief SAFER-K block cipher

0067 /// \sa <a href="http://www.cryptopp.com/wiki/SAFER-K">SAFER-K</a>

0068 class SAFER_K : public SAFER_K_Info, public SAFER, public BlockCipherDocumentation
0069 {
0070 public:
0071     typedef BlockCipherFinal<ENCRYPTION, SAFER_Impl<Enc, SAFER_K_Info, false> > Encryption;
0072     typedef BlockCipherFinal<DECRYPTION, SAFER_Impl<Dec, SAFER_K_Info, false> > Decryption;
0073 };
0074 
0075 /// \brief SAFER-SK block cipher information

0076 struct SAFER_SK_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13>
0077 {
0078     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "SAFER-SK";}
0079 };
0080 
0081 /// \brief SAFER-SK block cipher

0082 /// \sa <a href="http://www.cryptopp.com/wiki/SAFER-SK">SAFER-SK</a>

0083 class SAFER_SK : public SAFER_SK_Info, public SAFER, public BlockCipherDocumentation
0084 {
0085 public:
0086     typedef BlockCipherFinal<ENCRYPTION, SAFER_Impl<Enc, SAFER_SK_Info, true> > Encryption;
0087     typedef BlockCipherFinal<DECRYPTION, SAFER_Impl<Dec, SAFER_SK_Info, true> > Decryption;
0088 };
0089 
0090 typedef SAFER_K::Encryption SAFER_K_Encryption;
0091 typedef SAFER_K::Decryption SAFER_K_Decryption;
0092 
0093 typedef SAFER_SK::Encryption SAFER_SK_Encryption;
0094 typedef SAFER_SK::Decryption SAFER_SK_Decryption;
0095 
0096 NAMESPACE_END
0097 
0098 #endif