2015-11-23 00:17:15 +00:00
|
|
|
// safer.h - written and placed in the public domain by Wei Dai
|
|
|
|
|
|
|
|
//! \file safer.h
|
|
|
|
//! \brief Classes for the SAFER block cipher
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#ifndef CRYPTOPP_SAFER_H
|
|
|
|
#define CRYPTOPP_SAFER_H
|
|
|
|
|
|
|
|
#include "seckey.h"
|
|
|
|
#include "secblock.h"
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2015-12-14 04:53:50 +00:00
|
|
|
//! \class SAFER
|
|
|
|
//! \brief SAFER base class
|
2015-11-05 06:59:46 +00:00
|
|
|
class SAFER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
class CRYPTOPP_NO_VTABLE Base : public BlockCipher
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
unsigned int OptimalDataAlignment() const {return 1;}
|
|
|
|
void UncheckedSetKey(const byte *userkey, unsigned int length, const NameValuePairs ¶ms);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual bool Strengthened() const =0;
|
|
|
|
|
|
|
|
SecByteBlock keySchedule;
|
|
|
|
static const byte exp_tab[256];
|
|
|
|
static const byte log_tab[256];
|
|
|
|
};
|
|
|
|
|
|
|
|
class CRYPTOPP_NO_VTABLE Enc : public Base
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CRYPTOPP_NO_VTABLE Dec : public Base
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class BASE, class INFO, bool STR>
|
|
|
|
class CRYPTOPP_NO_VTABLE SAFER_Impl : public BlockCipherImpl<INFO, BASE>
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
bool Strengthened() const {return STR;}
|
|
|
|
};
|
|
|
|
|
2015-12-14 04:53:50 +00:00
|
|
|
//! \class SAFER_K_Info
|
|
|
|
//! \brief SAFER-K block cipher information
|
2015-11-05 06:59:46 +00:00
|
|
|
struct SAFER_K_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13>
|
|
|
|
{
|
|
|
|
static const char *StaticAlgorithmName() {return "SAFER-K";}
|
|
|
|
};
|
|
|
|
|
2015-12-14 04:53:50 +00:00
|
|
|
//! \class SAFER_K
|
|
|
|
//! \brief SAFER-K block cipher
|
|
|
|
//! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#SAFER-K">SAFER-K</a>
|
2015-11-05 06:59:46 +00:00
|
|
|
class SAFER_K : public SAFER_K_Info, public SAFER, public BlockCipherDocumentation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef BlockCipherFinal<ENCRYPTION, SAFER_Impl<Enc, SAFER_K_Info, false> > Encryption;
|
|
|
|
typedef BlockCipherFinal<DECRYPTION, SAFER_Impl<Dec, SAFER_K_Info, false> > Decryption;
|
|
|
|
};
|
|
|
|
|
2015-12-14 04:53:50 +00:00
|
|
|
//! \class SAFER_SK_Info
|
|
|
|
//! \brief SAFER-SK block cipher information
|
2015-11-05 06:59:46 +00:00
|
|
|
struct SAFER_SK_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13>
|
|
|
|
{
|
|
|
|
static const char *StaticAlgorithmName() {return "SAFER-SK";}
|
|
|
|
};
|
|
|
|
|
2015-12-14 04:53:50 +00:00
|
|
|
//! \class SAFER_SK
|
|
|
|
//! \brief SAFER-SK block cipher
|
|
|
|
//! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#SAFER-SK">SAFER-SK</a>
|
2015-11-05 06:59:46 +00:00
|
|
|
class SAFER_SK : public SAFER_SK_Info, public SAFER, public BlockCipherDocumentation
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef BlockCipherFinal<ENCRYPTION, SAFER_Impl<Enc, SAFER_SK_Info, true> > Encryption;
|
|
|
|
typedef BlockCipherFinal<DECRYPTION, SAFER_Impl<Dec, SAFER_SK_Info, true> > Decryption;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef SAFER_K::Encryption SAFER_K_Encryption;
|
|
|
|
typedef SAFER_K::Decryption SAFER_K_Decryption;
|
|
|
|
|
|
|
|
typedef SAFER_SK::Encryption SAFER_SK_Encryption;
|
|
|
|
typedef SAFER_SK::Decryption SAFER_SK_Decryption;
|
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|