safer.h
Go to the documentation of this file.
// safer.h - written and placed in the public domain by Wei Dai
//! \file safer.h
//! \brief Classes for the SAFER and SAFER-K block ciphers
#ifndef CRYPTOPP_SAFER_H
#define CRYPTOPP_SAFER_H
#include "seckey.h"
#include "secblock.h"
NAMESPACE_BEGIN(CryptoPP)
//! \class SAFER
//! \brief SAFER block cipher
class SAFER
{
public:
//! \class Base
//! \brief SAFER block cipher default operation
class CRYPTOPP_NO_VTABLE Base : public BlockCipher
{
public:
unsigned int OptimalDataAlignment() const {return 1;}
void UncheckedSetKey(const byte *userkey, unsigned int length, const NameValuePairs &params);
protected:
virtual bool Strengthened() const =0;
SecByteBlock keySchedule;
static const byte exp_tab[256];
static const byte log_tab[256];
};
//! \class Enc
//! \brief SAFER block cipher encryption operation
class CRYPTOPP_NO_VTABLE Enc : public Base
{
public:
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
};
//! \class Dec
//! \brief SAFER block cipher decryption operation
class CRYPTOPP_NO_VTABLE Dec : public Base
{
public:
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
};
};
//! \class SAFER_Impl
//! \brief SAFER block cipher default implementation
//! \tparam BASE SAFER::Enc or SAFER::Dec derived base class
//! \tparam INFO SAFER_Info derived class
//! \tparam STR flag indicating a strengthened implementation
//! \details SAFER-K is not strengthened; while SAFER-SK is strengthened.
template <class BASE, class INFO, bool STR>
class CRYPTOPP_NO_VTABLE SAFER_Impl : public BlockCipherImpl<INFO, BASE>
{
protected:
bool Strengthened() const {return STR;}
};
//! \class SAFER_K_Info
//! \brief SAFER-K block cipher information
struct SAFER_K_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13>
{
CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "SAFER-K";}
};
//! \class SAFER_K
//! \brief SAFER-K block cipher
//! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#SAFER-K">SAFER-K</a>
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;
};
//! \class SAFER_SK_Info
//! \brief SAFER-SK block cipher information
struct SAFER_SK_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 8, 16, 8>, public VariableRounds<10, 1, 13>
{
CRYPTOPP_CONSTEXPR static const char *StaticAlgorithmName() {return "SAFER-SK";}
};
//! \class SAFER_SK
//! \brief SAFER-SK block cipher
//! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#SAFER-SK">SAFER-SK</a>
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
SAFER_SK
SAFER-SK block cipher.
Definition: safer.h:92
secblock.h
Classes and functions for secure memory allocations.
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:731
SAFER_SK_Info
SAFER-SK block cipher information.
Definition: safer.h:84
SAFER::Dec
SAFER block cipher decryption operation.
Definition: safer.h:45
FixedBlockSize
Inherited by algorithms with fixed block size.
Definition: seckey.h:34
BlockCipherImpl
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:326
SAFER::Base
SAFER block cipher default operation.
Definition: safer.h:21
SAFER_K
SAFER-K block cipher.
Definition: safer.h:75
SAFER
SAFER block cipher.
Definition: safer.h:16
BlockCipherDocumentation
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher.
Definition: seckey.h:423
seckey.h
Classes and functions for implementing secret key algorithms.
BlockCipherFinal
Provides class member functions to key a block cipher.
Definition: seckey.h:339
SAFER::Base::OptimalDataAlignment
unsigned int OptimalDataAlignment() const
Provides input and output data alignment for optimal performance.
Definition: safer.h:24
CryptoPP
Crypto++ library namespace.
BlockCipher
Interface for one direction (encryption or decryption) of a block cipher.
Definition: cryptlib.h:1091
SAFER_Impl
SAFER block cipher default implementation.
Definition: safer.h:59
VariableRounds
Inherited by algorithms with variable number of rounds.
Definition: seckey.h:60
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:277
BlockTransformation::ProcessAndXorBlock
virtual void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const =0
Encrypt or decrypt a block.
SAFER::Enc
SAFER block cipher encryption operation.
Definition: safer.h:37
SAFER_K_Info
SAFER-K block cipher information.
Definition: safer.h:67
VariableKeyLength
Inherited by keyed algorithms with variable key length.
Definition: seckey.h:177