Skip to content

Commit

Permalink
Backed out use of "static const" to declare constant; switch to "enum…
Browse files Browse the repository at this point in the history
…" (Issue 255)
  • Loading branch information
noloader committed Sep 6, 2016
1 parent 45323bd commit a62aee4
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 66 deletions.
5 changes: 0 additions & 5 deletions 3way.cpp
Expand Up @@ -15,11 +15,6 @@ void ThreeWay_TestInstantiations()
}
#endif

// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255
static const size_t s_unused1 = ThreeWay::KEYLENGTH;
static const size_t s_unused2 = ThreeWayEncryption::KEYLENGTH;
static const size_t s_unused3 = ThreeWayDecryption::KEYLENGTH;

static const word32 START_E = 0x0b0b; // round constant of first encryption round
static const word32 START_D = 0xb1b1; // round constant of first decryption round
#ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
Expand Down
10 changes: 10 additions & 0 deletions cast.h
Expand Up @@ -11,6 +11,8 @@

NAMESPACE_BEGIN(CryptoPP)

//! \class CAST
//! \brief CAST block cipher base
class CAST
{
protected:
Expand All @@ -29,6 +31,8 @@ struct CAST128_Info : public FixedBlockSize<8>, public VariableKeyLength<16, 5,
//! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#CAST-128">CAST-128</a>
class CAST128 : public CAST128_Info, public BlockCipherDocumentation
{
//! \class Base
//! \brief CAST128 block cipher default operation
class CRYPTOPP_NO_VTABLE Base : public CAST, public BlockCipherImpl<CAST128_Info>
{
public:
Expand All @@ -39,12 +43,16 @@ class CAST128 : public CAST128_Info, public BlockCipherDocumentation
FixedSizeSecBlock<word32, 32> K;
};

//! \class Enc
//! \brief CAST128 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 CAST128 block cipher decryption operation
class CRYPTOPP_NO_VTABLE Dec : public Base
{
public:
Expand All @@ -68,6 +76,8 @@ struct CAST256_Info : public FixedBlockSize<16>, public VariableKeyLength<16, 16
//! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#CAST-256">CAST-256</a>
class CAST256 : public CAST256_Info, public BlockCipherDocumentation
{
//! \class Base
//! \brief CAST256 block cipher default operation
class CRYPTOPP_NO_VTABLE Base : public CAST, public BlockCipherImpl<CAST256_Info>
{
public:
Expand Down
5 changes: 3 additions & 2 deletions config.h
Expand Up @@ -544,8 +544,9 @@ NAMESPACE_END
# define CRYPTOPP_NOINLINE
#endif

// how to declare class constants
#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__)
// How to declare class constants
// Use enum for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255
#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) || (defined(__APPLE__) && (__GNUC__ == 4) && (__GNUC_MINOR__ <= 2))
# define CRYPTOPP_CONSTANT(x) enum {x};
#else
# define CRYPTOPP_CONSTANT(x) static const int x;
Expand Down
5 changes: 3 additions & 2 deletions config.recommend
Expand Up @@ -544,8 +544,9 @@ NAMESPACE_END
# define CRYPTOPP_NOINLINE
#endif

// how to declare class constants
#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__)
// How to declare class constants
// Use enum for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255
#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) || (defined(__APPLE__) && (__GNUC__ == 4) && (__GNUC_MINOR__ <= 2))
# define CRYPTOPP_CONSTANT(x) enum {x};
#else
# define CRYPTOPP_CONSTANT(x) static const int x;
Expand Down
6 changes: 0 additions & 6 deletions des.cpp
Expand Up @@ -20,12 +20,6 @@

NAMESPACE_BEGIN(CryptoPP)

// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255
static const size_t s_unused1 = DES::KEYLENGTH;
static const size_t s_unused2 = DES_EDE2::KEYLENGTH;
static const size_t s_unused3 = DES_EDE3::KEYLENGTH;
static const size_t s_unused4 = DES_XEX3::KEYLENGTH;

typedef BlockGetAndPut<word32, BigEndian> Block;

// Richard Outerbridge's initial permutation algorithm
Expand Down
3 changes: 0 additions & 3 deletions gost.cpp
Expand Up @@ -4,9 +4,6 @@

NAMESPACE_BEGIN(CryptoPP)

// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255
static const size_t s_unused = GOST::KEYLENGTH;

// these are the S-boxes given in Applied Cryptography 2nd Ed., p. 333
const byte GOST::Base::sBox[8][16]={
{4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3},
Expand Down
3 changes: 0 additions & 3 deletions idea.cpp
Expand Up @@ -7,9 +7,6 @@

NAMESPACE_BEGIN(CryptoPP)

// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255
static const size_t s_unused = IDEA::KEYLENGTH;

static const int IDEA_KEYLEN=(6*IDEA::ROUNDS+4); // key schedule length in # of word16s

#define low16(x) ((x)&0xffff) // compiler should be able to optimize this away if word is 16 bits
Expand Down
4 changes: 2 additions & 2 deletions modes.cpp
Expand Up @@ -7,9 +7,9 @@
#include "modes.h"
#include "misc.h"

#ifndef NDEBUG
//#ifndef NDEBUG
#include "des.h"
#endif
//#endif

NAMESPACE_BEGIN(CryptoPP)

Expand Down
44 changes: 35 additions & 9 deletions modes.h
Expand Up @@ -16,8 +16,8 @@
NAMESPACE_BEGIN(CryptoPP)

//! \class CipherModeDocumentation
//! \brief Classes for operating block cipher modes of operation
//! \details Each class derived from this one defines two types, Encryption and Decryption,
//! \brief Block cipher mode of operation information
//! \details Each class derived from this one defines two types, Encryption and Decryption,
//! both of which implement the SymmetricCipher interface.
//! For each mode there are two classes, one of which is a template class,
//! and the other one has a name that ends in "_ExternalCipher".
Expand All @@ -31,6 +31,8 @@ struct CipherModeDocumentation : public SymmetricCipherDocumentation
{
};

//! \class CipherModeBase
//! \brief Block cipher mode of operation information
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CipherModeBase : public SymmetricCipher
{
public:
Expand Down Expand Up @@ -70,7 +72,7 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CipherModeBase : public SymmetricCipher
if (!(feedbackSize == 0 || feedbackSize == BlockSize()))
throw InvalidArgument("CipherModeBase: feedback size cannot be specified for this cipher mode");
}

// Thanks to Zireael, http://github.com/weidai11/cryptopp/pull/46
#ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
virtual void ResizeBuffers();
Expand All @@ -85,6 +87,9 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CipherModeBase : public SymmetricCipher
AlignedSecByteBlock m_register;
};

//! \class ModePolicyCommonTemplate
//! \brief Block cipher mode of operation common operations
//! \tparam POLICY_INTERFACE common operations
template <class POLICY_INTERFACE>
class CRYPTOPP_NO_VTABLE ModePolicyCommonTemplate : public CipherModeBase, public POLICY_INTERFACE
{
Expand All @@ -101,6 +106,8 @@ void ModePolicyCommonTemplate<POLICY_INTERFACE>::CipherSetKey(const NameValuePai
SetFeedbackSize(feedbackSize);
}

//! \class CFB_ModePolicy
//! \brief CFB block cipher mode of operation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CFB_ModePolicy : public ModePolicyCommonTemplate<CFB_CipherAbstractPolicy>
{
public:
Expand Down Expand Up @@ -129,6 +136,8 @@ inline void CopyOrZero(void *dest, const void *src, size_t s)
memset(dest, 0, s);
}

//! \class OFB_ModePolicy
//! \brief OFB block cipher mode of operation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE OFB_ModePolicy : public ModePolicyCommonTemplate<AdditiveCipherAbstractPolicy>
{
public:
Expand All @@ -143,6 +152,8 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE OFB_ModePolicy : public ModePolicyCommonTe
void CipherResynchronize(byte *keystreamBuffer, const byte *iv, size_t length);
};

//! \class CTR_ModePolicy
//! \brief CTR block cipher mode of operation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CTR_ModePolicy : public ModePolicyCommonTemplate<AdditiveCipherAbstractPolicy>
{
public:
Expand All @@ -166,6 +177,8 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CTR_ModePolicy : public ModePolicyCommonTe
AlignedSecByteBlock m_counterArray;
};

//! \class BlockOrientedCipherModeBase
//! \brief Block cipher mode of operation default implementation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BlockOrientedCipherModeBase : public CipherModeBase
{
public:
Expand All @@ -178,7 +191,7 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BlockOrientedCipherModeBase : public Ciphe

protected:
bool RequireAlignedInput() const {return true;}

// Thanks to Zireael, http://github.com/weidai11/cryptopp/pull/46
#ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
void ResizeBuffers();
Expand All @@ -193,6 +206,8 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE BlockOrientedCipherModeBase : public Ciphe
SecByteBlock m_buffer;
};

//! \class ECB_OneWay
//! \brief ECB block cipher mode of operation default implementation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE ECB_OneWay : public BlockOrientedCipherModeBase
{
public:
Expand All @@ -204,6 +219,8 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE ECB_OneWay : public BlockOrientedCipherMod
static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECB";}
};

//! \class CBC_ModeBase
//! \brief CBC block cipher mode of operation default implementation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_ModeBase : public BlockOrientedCipherModeBase
{
public:
Expand All @@ -213,12 +230,16 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_ModeBase : public BlockOrientedCipherM
static const char * CRYPTOPP_API StaticAlgorithmName() {return "CBC";}
};

//! \class CBC_Encryption
//! \brief CBC block cipher mode of operation encryption operation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Encryption : public CBC_ModeBase
{
public:
void ProcessData(byte *outString, const byte *inString, size_t length);
};

//! \class CBC_CTS_Encryption
//! \brief CBC-CTS block cipher mode of operation encryption operation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Encryption : public CBC_Encryption
{
public:
Expand All @@ -237,13 +258,15 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Encryption : public CBC_Encryption
byte *m_stolenIV;
};

//! \class CBC_Decryption
//! \brief CBC block cipher mode of operation decryption operation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Decryption : public CBC_ModeBase
{
public:
void ProcessData(byte *outString, const byte *inString, size_t length);

protected:

// Thanks to Zireael, http://github.com/weidai11/cryptopp/pull/46
#ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562
void ResizeBuffers();
Expand All @@ -258,14 +281,17 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_Decryption : public CBC_ModeBase
AlignedSecByteBlock m_temp;
};

//! \class CBC_CTS_Decryption
//! \brief CBC-CTS block cipher mode of operation decryption operation
class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE CBC_CTS_Decryption : public CBC_Decryption
{
public:
unsigned int MinLastBlockSize() const {return BlockSize()+1;}
void ProcessLastBlock(byte *outString, const byte *inString, size_t length);
};

//! _
//! \class CipherModeFinalTemplate_CipherHolder
//! \brief Block cipher mode of operation aggregate
template <class CIPHER, class BASE>
class CipherModeFinalTemplate_CipherHolder : protected ObjectHolder<CIPHER>, public AlgorithmImpl<BASE, CipherModeFinalTemplate_CipherHolder<CIPHER, BASE> >
{
Expand Down Expand Up @@ -296,8 +322,8 @@ class CipherModeFinalTemplate_CipherHolder : protected ObjectHolder<CIPHER>, pub
};

//! \class CipherModeFinalTemplate_ExternalCipher
//! \tparam BASE CipherModeFinalTemplate_CipherHolder class
//! \brief OFB block cipher mode of operation.
//! \tparam BASE CipherModeFinalTemplate_CipherHolder base class
//! \details
template <class BASE>
class CipherModeFinalTemplate_ExternalCipher : public BASE
{
Expand Down
3 changes: 0 additions & 3 deletions panama.cpp
Expand Up @@ -17,9 +17,6 @@ NAMESPACE_BEGIN(CryptoPP)
# pragma warning(disable: 4731)
#endif

// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255
static const size_t s_unused = PanamaCipher<>::KEYLENGTH;

template <class B>
void Panama<B>::Reset()
{
Expand Down
10 changes: 8 additions & 2 deletions safer.h
@@ -1,7 +1,7 @@
// safer.h - written and placed in the public domain by Wei Dai

//! \file safer.h
//! \brief Classes for the SAFER block cipher
//! \brief Classes for the SAFER and SAFER-K block ciphers

#ifndef CRYPTOPP_SAFER_H
#define CRYPTOPP_SAFER_H
Expand All @@ -12,10 +12,12 @@
NAMESPACE_BEGIN(CryptoPP)

//! \class SAFER
//! \brief SAFER base class
//! \brief SAFER block cipher
class SAFER
{
public:
//! \class Base
//! \brief SAFER block cipher default operation
class CRYPTOPP_NO_VTABLE Base : public BlockCipher
{
public:
Expand All @@ -30,12 +32,16 @@ class SAFER
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:
Expand Down
4 changes: 0 additions & 4 deletions salsa.cpp
Expand Up @@ -40,10 +40,6 @@ void Salsa20_TestInstantiations()
}
#endif

// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255
// static const size_t s_unused1 = Salsa20::KEYLENGTH;
static const size_t s_unused2 = XSalsa20::KEYLENGTH;

void Salsa20_Policy::CipherSetKey(const NameValuePairs &params, const byte *key, size_t length)
{
m_rounds = params.GetIntValueWithDefault(Name::Rounds(), 20);
Expand Down
3 changes: 0 additions & 3 deletions seal.cpp
Expand Up @@ -17,9 +17,6 @@ void SEAL_TestInstantiations()
}
#endif

// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255
static const size_t s_unused = SEAL<>::KEYLENGTH;

struct SEAL_Gamma
{
SEAL_Gamma(const byte *key)
Expand Down
3 changes: 0 additions & 3 deletions seed.cpp
Expand Up @@ -6,9 +6,6 @@

NAMESPACE_BEGIN(CryptoPP)

// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255
static const size_t s_unused = SEED::KEYLENGTH;

static const word32 s_kc[16] = {
0x9e3779b9, 0x3c6ef373, 0x78dde6e6, 0xf1bbcdcc, 0xe3779b99, 0xc6ef3733, 0x8dde6e67, 0x1bbcdccf,
0x3779b99e, 0x6ef3733c, 0xdde6e678, 0xbbcdccf1, 0x779b99e3, 0xef3733c6, 0xde6e678d, 0xbcdccf1b};
Expand Down
3 changes: 0 additions & 3 deletions shark.cpp
Expand Up @@ -12,9 +12,6 @@

NAMESPACE_BEGIN(CryptoPP)

// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255
static const size_t s_unused = SHARK::KEYLENGTH;

static word64 SHARKTransform(word64 a)
{
static const byte iG[8][8] = {
Expand Down
3 changes: 0 additions & 3 deletions skipjack.cpp
Expand Up @@ -17,9 +17,6 @@

NAMESPACE_BEGIN(CryptoPP)

// Hack for OS X 10.5 ld, http://github.com/weidai11/cryptopp/issues/255
static const size_t s_unused = SKIPJACK::KEYLENGTH;

/**
* The F-table byte permutation (see description of the G-box permutation)
*/
Expand Down

1 comment on commit a62aee4

@noloader
Copy link
Collaborator Author

@noloader noloader commented on a62aee4 Sep 6, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial attempt to fix this issue was to perform a quasi-explicit instantiation, but it was removed. An example of one that was removed:

// 3way.cpp
static const size_t s_unused1 = ThreeWay::KEYLENGTH;

The quasi-explicit instantiation was removed because we could not remediate the undefined references present in validat1.cpp. The missing symbol for ThreeWay::KEYLENGTH was especially frustrating - it was the only FixedKeySize<12>, so we knew exactly where it was coming from. We were able to clear it from 3way.o, but not validat1.o.

The switch from static const to enum occurred in config.h (and config.recommend). In the test below, defined(__APPLE__) && (__GNUC__ == 4) && (__GNUC_MINOR__ <= 2) is a proxy or stand-in for "older Apple ld". It may mildly miss the mark, but it probably won't cause any problems in 2016 on modern Intel systems and older PPC systems.

#if (defined(_MSC_VER) && _MSC_VER <= 1300) || defined(__INTEL_COMPILER) || defined(__BORLANDC__) || (defined(__APPLE__) && (__GNUC__ == 4) && (__GNUC_MINOR__ <= 2))
#   define CRYPTOPP_CONSTANT(x) enum {x};
#else
#   define CRYPTOPP_CONSTANT(x) static const int x;
#endif

Also see Issue 255: OS X 10.5 and Missing symbols for FixedKeyLength::KEYLENGTH.

Please sign in to comment.