From c554712d08efbb7cb745509858213265b3d76d39 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Fri, 14 Jan 2005 03:05:41 +0000 Subject: [PATCH] more unused --- stepmania/src/crypto51/cryptlib.cpp | 8 -- stepmania/src/crypto51/cryptlib.h | 21 ---- stepmania/src/crypto51/filters.cpp | 34 ------ stepmania/src/crypto51/filters.h | 46 -------- stepmania/src/crypto51/misc.h | 168 ---------------------------- stepmania/src/crypto51/modes.h | 17 --- stepmania/src/crypto51/pkcspad.cpp | 61 ---------- stepmania/src/crypto51/pkcspad.h | 14 +-- stepmania/src/crypto51/pubkey.cpp | 20 ---- stepmania/src/crypto51/pubkey.h | 109 ------------------ stepmania/src/crypto51/rsa.cpp | 4 - stepmania/src/crypto51/rsa.h | 11 -- stepmania/src/crypto51/seckey.h | 127 --------------------- stepmania/src/crypto51/test.cpp | 166 --------------------------- 14 files changed, 1 insertion(+), 805 deletions(-) delete mode 100644 stepmania/src/crypto51/test.cpp diff --git a/stepmania/src/crypto51/cryptlib.cpp b/stepmania/src/crypto51/cryptlib.cpp index 5e545f54e6..9d4088b2fd 100644 --- a/stepmania/src/crypto51/cryptlib.cpp +++ b/stepmania/src/crypto51/cryptlib.cpp @@ -596,14 +596,6 @@ unsigned int PK_FixedLengthCryptoSystem::CiphertextLength(unsigned int plainText return 0; } -DecodingResult PK_FixedLengthDecryptor::Decrypt(RandomNumberGenerator &rng, const byte *cipherText, unsigned int cipherTextLength, byte *plainText) const -{ - if (cipherTextLength != FixedCiphertextLength()) - return DecodingResult(); - - return FixedLengthDecrypt(rng, cipherText, plainText); -} - unsigned int PK_Signer::Sign(RandomNumberGenerator &rng, PK_MessageAccumulator *messageAccumulator, byte *signature) const { std::auto_ptr m(messageAccumulator); diff --git a/stepmania/src/crypto51/cryptlib.h b/stepmania/src/crypto51/cryptlib.h index 4dc2631cea..f6dcb5cced 100644 --- a/stepmania/src/crypto51/cryptlib.h +++ b/stepmania/src/crypto51/cryptlib.h @@ -1151,27 +1151,6 @@ public: #endif }; -//! interface for encryptors with fixed length ciphertext - -class PK_FixedLengthEncryptor : public PK_Encryptor, virtual public PK_FixedLengthCryptoSystem -{ -}; - -//! interface for decryptors with fixed length ciphertext - -class PK_FixedLengthDecryptor : public PK_Decryptor, virtual public PK_FixedLengthCryptoSystem -{ -public: - //! decrypt a byte string, and return the length of plaintext - /*! \pre length of cipherText == CipherTextLength() - \pre size of plainText == MaxPlainTextLength() - \return the actual length of the plaintext, or 0 if decryption fails. - */ - virtual DecodingResult FixedLengthDecrypt(RandomNumberGenerator &rng, const byte *cipherText, byte *plainText) const =0; - - DecodingResult Decrypt(RandomNumberGenerator &rng, const byte *cipherText, unsigned int cipherTextLength, byte *plainText) const; -}; - //! interface for public-key signers and verifiers /*! This class provides an interface common to signers and verifiers diff --git a/stepmania/src/crypto51/filters.cpp b/stepmania/src/crypto51/filters.cpp index 7139451610..60a42a996c 100644 --- a/stepmania/src/crypto51/filters.cpp +++ b/stepmania/src/crypto51/filters.cpp @@ -605,38 +605,4 @@ unsigned int StringStore::CopyRangeTo2(BufferedTransformation &target, unsigned return blockedBytes; } -unsigned int RandomNumberStore::TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel, bool blocking) -{ - if (!blocking) - throw NotImplemented("RandomNumberStore: nonblocking transfer is not implemented by this object"); - - unsigned long transferMax = transferBytes; - for (transferBytes = 0; transferBytes { @@ -570,14 +532,6 @@ public: : SourceTemplate(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));} }; -//! . -class RandomNumberSource : public SourceTemplate -{ -public: - RandomNumberSource(RandomNumberGenerator &rng, unsigned int length, bool pumpAll, BufferedTransformation *attachment = NULL) - : SourceTemplate(attachment, RandomNumberStore(rng, length)) {if (pumpAll) PumpAll();} -}; - NAMESPACE_END #endif diff --git a/stepmania/src/crypto51/misc.h b/stepmania/src/crypto51/misc.h index 153eabd4a2..f54cdebf36 100644 --- a/stepmania/src/crypto51/misc.h +++ b/stepmania/src/crypto51/misc.h @@ -369,64 +369,6 @@ inline word64 ByteReverse(word64 value) } #endif -inline byte BitReverse(byte value) -{ - value = ((value & 0xAA) >> 1) | ((value & 0x55) << 1); - value = ((value & 0xCC) >> 2) | ((value & 0x33) << 2); - return rotlFixed(value, 4); -} - -inline word16 BitReverse(word16 value) -{ - value = ((value & 0xAAAA) >> 1) | ((value & 0x5555) << 1); - value = ((value & 0xCCCC) >> 2) | ((value & 0x3333) << 2); - value = ((value & 0xF0F0) >> 4) | ((value & 0x0F0F) << 4); - return ByteReverse(value); -} - -inline word32 BitReverse(word32 value) -{ - value = ((value & 0xAAAAAAAA) >> 1) | ((value & 0x55555555) << 1); - value = ((value & 0xCCCCCCCC) >> 2) | ((value & 0x33333333) << 2); - value = ((value & 0xF0F0F0F0) >> 4) | ((value & 0x0F0F0F0F) << 4); - return ByteReverse(value); -} - -#ifdef WORD64_AVAILABLE -inline word64 BitReverse(word64 value) -{ -#ifdef SLOW_WORD64 - return (word64(BitReverse(word32(value))) << 32) | BitReverse(word32(value>>32)); -#else - value = ((value & W64LIT(0xAAAAAAAAAAAAAAAA)) >> 1) | ((value & W64LIT(0x5555555555555555)) << 1); - value = ((value & W64LIT(0xCCCCCCCCCCCCCCCC)) >> 2) | ((value & W64LIT(0x3333333333333333)) << 2); - value = ((value & W64LIT(0xF0F0F0F0F0F0F0F0)) >> 4) | ((value & W64LIT(0x0F0F0F0F0F0F0F0F)) << 4); - return ByteReverse(value); -#endif -} -#endif - -template -inline T BitReverse(T value) -{ - if (sizeof(T) == 1) - return (T)BitReverse((byte)value); - else if (sizeof(T) == 2) - return (T)BitReverse((word16)value); - else if (sizeof(T) == 4) - return (T)BitReverse((word32)value); - else - { -#ifdef WORD64_AVAILABLE - assert(sizeof(T) == 8); - return (T)BitReverse((word64)value); -#else - assert(false); - return 0; -#endif - } -} - template inline T ConditionalByteReverse(ByteOrder order, T value) { @@ -461,31 +403,6 @@ inline void GetUserKey(ByteOrder order, T *out, unsigned int outlen, const byte ConditionalByteReverse(order, out, out, RoundUpToMultipleOf(inlen, U)); } -inline byte UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, byte*) -{ - return block[0]; -} - -inline word16 UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, word16*) -{ - return (order == BIG_ENDIAN_ORDER) - ? block[1] | (block[0] << 8) - : block[0] | (block[1] << 8); -} - -inline word32 UnalignedGetWordNonTemplate(ByteOrder order, const byte *block, word32*) -{ - return (order == BIG_ENDIAN_ORDER) - ? word32(block[3]) | (word32(block[2]) << 8) | (word32(block[1]) << 16) | (word32(block[0]) << 24) - : word32(block[0]) | (word32(block[1]) << 8) | (word32(block[2]) << 16) | (word32(block[3]) << 24); -} - -template -inline T UnalignedGetWord(ByteOrder order, const byte *block, T*dummy=NULL) -{ - return UnalignedGetWordNonTemplate(order, block, dummy); -} - inline void UnalignedPutWord(ByteOrder order, byte *block, byte value, const byte *xorBlock = NULL) { block[0] = xorBlock ? (value ^ xorBlock[0]) : value; @@ -537,24 +454,6 @@ inline void UnalignedPutWord(ByteOrder order, byte *block, word32 value, const b } } -template -inline T GetWord(bool assumeAligned, ByteOrder order, const byte *block) -{ - if (assumeAligned) - { - assert(IsAligned(block)); - return ConditionalByteReverse(order, *reinterpret_cast(block)); - } - else - return UnalignedGetWord(order, block); -} - -template -inline void GetWord(bool assumeAligned, ByteOrder order, T &result, const byte *block) -{ - result = GetWord(assumeAligned, order, block); -} - template inline void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, const byte *xorBlock = NULL) { @@ -570,73 +469,6 @@ inline void PutWord(bool assumeAligned, ByteOrder order, byte *block, T value, c UnalignedPutWord(order, block, value, xorBlock); } -template -class GetBlock -{ -public: - GetBlock(const void *block) - : m_block((const byte *)block) {} - - template - inline GetBlock & operator()(U &x) - { - CRYPTOPP_COMPILE_ASSERT(sizeof(U) >= sizeof(T)); - x = GetWord(A, B::ToEnum(), m_block); - m_block += sizeof(T); - return *this; - } - -private: - const byte *m_block; -}; - -template -class PutBlock -{ -public: - PutBlock(const void *xorBlock, void *block) - : m_xorBlock((const byte *)xorBlock), m_block((byte *)block) {} - - template - inline PutBlock & operator()(U x) - { - PutWord(A, B::ToEnum(), m_block, (T)x, m_xorBlock); - m_block += sizeof(T); - if (m_xorBlock) - m_xorBlock += sizeof(T); - return *this; - } - -private: - const byte *m_xorBlock; - byte *m_block; -}; - -template -struct BlockGetAndPut -{ - // function needed because of C++ grammatical ambiguity between expression-statements and declarations - static inline GetBlock Get(const void *block) {return GetBlock(block);} - typedef PutBlock Put; -}; - -template -std::string WordToString(T value, ByteOrder order = BIG_ENDIAN_ORDER) -{ - if (!NativeByteOrderIs(order)) - value = ByteReverse(value); - - return std::string((char *)&value, sizeof(value)); -} - -template -T StringToWord(const std::string &str, ByteOrder order = BIG_ENDIAN_ORDER) -{ - T value = 0; - memcpy(&value, str.data(), STDMIN(sizeof(value), str.size())); - return NativeByteOrderIs(order) ? value : ByteReverse(value); -} - // ************** help remove warning on g++ *************** template struct SafeShifter; diff --git a/stepmania/src/crypto51/modes.h b/stepmania/src/crypto51/modes.h index 3585c82178..90cedce144 100644 --- a/stepmania/src/crypto51/modes.h +++ b/stepmania/src/crypto51/modes.h @@ -150,23 +150,6 @@ public: void ProcessBlocks(byte *outString, const byte *inString, unsigned int numberOfBlocks); }; -class CBC_CTS_Encryption : public CBC_Encryption -{ -public: - void SetStolenIV(byte *iv) {m_stolenIV = iv;} - unsigned int MinLastBlockSize() const {return BlockSize()+1;} - void ProcessLastBlock(byte *outString, const byte *inString, unsigned int length); - -protected: - void UncheckedSetKey(const NameValuePairs ¶ms, const byte *key, unsigned int length) - { - CBC_Encryption::UncheckedSetKey(params, key, length); - m_stolenIV = params.GetValueWithDefault(Name::StolenIV(), (byte *)NULL); - } - - byte *m_stolenIV; -}; - class CBC_Decryption : public CBC_ModeBase { public: diff --git a/stepmania/src/crypto51/pkcspad.cpp b/stepmania/src/crypto51/pkcspad.cpp index 574baf9e69..facbc3589c 100644 --- a/stepmania/src/crypto51/pkcspad.cpp +++ b/stepmania/src/crypto51/pkcspad.cpp @@ -9,67 +9,6 @@ NAMESPACE_BEGIN(CryptoPP) template<> const byte PKCS_DigestDecoration::decoration[] = {0x30,0x21,0x30,0x09,0x06,0x05,0x2B,0x0E,0x03,0x02,0x1A,0x05,0x00,0x04,0x14}; template<> const unsigned int PKCS_DigestDecoration::length = sizeof(PKCS_DigestDecoration::decoration); -unsigned int PKCS_EncryptionPaddingScheme::MaxUnpaddedLength(unsigned int paddedLength) const -{ - return SaturatingSubtract(paddedLength/8, 10U); -} - -void PKCS_EncryptionPaddingScheme::Pad(RandomNumberGenerator &rng, const byte *input, unsigned int inputLen, byte *pkcsBlock, unsigned int pkcsBlockLen) const -{ - assert (inputLen <= MaxUnpaddedLength(pkcsBlockLen)); // this should be checked by caller - - // convert from bit length to byte length - if (pkcsBlockLen % 8 != 0) - { - pkcsBlock[0] = 0; - pkcsBlock++; - } - pkcsBlockLen /= 8; - - pkcsBlock[0] = 2; // block type 2 - - // pad with non-zero random bytes - for (unsigned i = 1; i < pkcsBlockLen-inputLen-1; i++) - pkcsBlock[i] = (byte)rng.GenerateWord32(1, 0xff); - - pkcsBlock[pkcsBlockLen-inputLen-1] = 0; // separator - memcpy(pkcsBlock+pkcsBlockLen-inputLen, input, inputLen); -} - -DecodingResult PKCS_EncryptionPaddingScheme::Unpad(const byte *pkcsBlock, unsigned int pkcsBlockLen, byte *output) const -{ - bool invalid = false; - unsigned int maxOutputLen = MaxUnpaddedLength(pkcsBlockLen); - - // convert from bit length to byte length - if (pkcsBlockLen % 8 != 0) - { - invalid = (pkcsBlock[0] != 0) || invalid; - pkcsBlock++; - } - pkcsBlockLen /= 8; - - // Require block type 2. - invalid = (pkcsBlock[0] != 2) || invalid; - - // skip past the padding until we find the separator - unsigned i=1; - while (i maxOutputLen) || invalid; - - if (invalid) - return DecodingResult(); - - memcpy (output, pkcsBlock+i, outputLen); - return DecodingResult(outputLen); -} - -// ******************************************************** - void PKCS1v15_SignatureMessageEncodingMethod::ComputeMessageRepresentative(RandomNumberGenerator &rng, const byte *recoverableMessage, unsigned int recoverableMessageLength, HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty, diff --git a/stepmania/src/crypto51/pkcspad.h b/stepmania/src/crypto51/pkcspad.h index 611aa07ab1..8b0a6b0abc 100644 --- a/stepmania/src/crypto51/pkcspad.h +++ b/stepmania/src/crypto51/pkcspad.h @@ -7,15 +7,6 @@ NAMESPACE_BEGIN(CryptoPP) //! EME-PKCS1-v1_5 -class PKCS_EncryptionPaddingScheme : public PK_EncryptionMessageEncodingMethod -{ -public: - static const char * StaticAlgorithmName() {return "EME-PKCS1-v1_5";} - - unsigned int MaxUnpaddedLength(unsigned int paddedLength) const; - void Pad(RandomNumberGenerator &rng, const byte *raw, unsigned int inputLength, byte *padded, unsigned int paddedLength) const; - DecodingResult Unpad(const byte *padded, unsigned int paddedLength, byte *raw) const; -}; template struct PKCS_DigestDecoration { @@ -48,14 +39,11 @@ public: //! PKCS #1 version 1.5, for use with RSAES and RSASS /*! The following hash functions are supported for signature: SHA, MD2, MD5, RIPEMD160, SHA256, SHA384, SHA512. */ -struct PKCS1v15 : public SignatureStandard, public EncryptionStandard +struct PKCS1v15 : public SignatureStandard { - typedef PKCS_EncryptionPaddingScheme EncryptionMessageEncodingMethod; typedef PKCS1v15_SignatureMessageEncodingMethod SignatureMessageEncodingMethod; }; -// PKCS_DecoratedHashModule can be instantiated with the following -// classes as specified in PKCS#1 v2.0 and P1363a class SHA; NAMESPACE_END diff --git a/stepmania/src/crypto51/pubkey.cpp b/stepmania/src/crypto51/pubkey.cpp index 000258997a..d7e0955d4c 100644 --- a/stepmania/src/crypto51/pubkey.cpp +++ b/stepmania/src/crypto51/pubkey.cpp @@ -99,24 +99,4 @@ DecodingResult TF_VerifierBase::RecoverAndRestart(byte *recoveredMessage, PK_Mes return result; } -DecodingResult TF_DecryptorBase::FixedLengthDecrypt(RandomNumberGenerator &rng, const byte *cipherText, byte *plainText) const -{ - SecByteBlock paddedBlock(PaddedBlockByteLength()); - Integer x = GetTrapdoorFunctionInterface().CalculateInverse(rng, Integer(cipherText, FixedCiphertextLength())); - if (x.ByteCount() > paddedBlock.size()) - x = Integer::Zero(); // don't return false here to prevent timing attack - x.Encode(paddedBlock, paddedBlock.size()); - return GetMessageEncodingInterface().Unpad(paddedBlock, PaddedBlockBitLength(), plainText); -} - -void TF_EncryptorBase::Encrypt(RandomNumberGenerator &rng, const byte *plainText, unsigned int plainTextLength, byte *cipherText) const -{ - if (plainTextLength > FixedMaxPlaintextLength()) - throw InvalidArgument(AlgorithmName() + ": message too long for this public key"); - - SecByteBlock paddedBlock(PaddedBlockByteLength()); - GetMessageEncodingInterface().Pad(rng, plainText, plainTextLength, paddedBlock, PaddedBlockBitLength()); - GetTrapdoorFunctionInterface().ApplyRandomizedFunction(rng, Integer(paddedBlock, paddedBlock.size())).Encode(cipherText, FixedCiphertextLength()); -} - NAMESPACE_END diff --git a/stepmania/src/crypto51/pubkey.h b/stepmania/src/crypto51/pubkey.h index 0cab6e5383..5bb3cadbca 100644 --- a/stepmania/src/crypto51/pubkey.h +++ b/stepmania/src/crypto51/pubkey.h @@ -105,22 +105,6 @@ public: // ******************************************************** -//! . -class PK_EncryptionMessageEncodingMethod -{ -public: - virtual ~PK_EncryptionMessageEncodingMethod() {} - - //! max size of unpadded message in bytes, given max size of padded message in bits (1 less than size of modulus) - virtual unsigned int MaxUnpaddedLength(unsigned int paddedLength) const =0; - - virtual void Pad(RandomNumberGenerator &rng, const byte *raw, unsigned int inputLength, byte *padded, unsigned int paddedBitLength) const =0; - - virtual DecodingResult Unpad(const byte *padded, unsigned int paddedBitLength, byte *raw) const =0; -}; - -// ******************************************************** - //! . template class TF_Base @@ -137,35 +121,6 @@ protected: // ******************************************************** -//! . -template -class TF_CryptoSystemBase : public INTERFACE, protected BASE -{ -public: - unsigned int FixedMaxPlaintextLength() const {return this->GetMessageEncodingInterface().MaxUnpaddedLength(PaddedBlockBitLength());} - unsigned int FixedCiphertextLength() const {return this->GetTrapdoorFunctionBounds().MaxImage().ByteCount();} - -protected: - unsigned int PaddedBlockByteLength() const {return BitsToBytes(PaddedBlockBitLength());} - unsigned int PaddedBlockBitLength() const {return this->GetTrapdoorFunctionBounds().PreimageBound().BitCount()-1;} -}; - -//! . -class TF_DecryptorBase : public TF_CryptoSystemBase > -{ -public: - DecodingResult FixedLengthDecrypt(RandomNumberGenerator &rng, const byte *cipherText, byte *plainText) const; -}; - -//! . -class TF_EncryptorBase : public TF_CryptoSystemBase > -{ -public: - void Encrypt(RandomNumberGenerator &rng, const byte *plainText, unsigned int plainTextLength, byte *cipherText) const; -}; - -// ******************************************************** - typedef std::pair HashIdentifier; //! . @@ -462,18 +417,6 @@ public: void CopyKeyInto(typename SCHEME_OPTIONS::PublicKey &key) const {key = this->GetKey();} }; -//! . -template -class TF_DecryptorImpl : public TF_PrivateObjectImpl -{ -}; - -//! . -template -class TF_EncryptorImpl : public TF_PublicObjectImpl -{ -}; - //! . template class TF_SignerImpl : public TF_PrivateObjectImpl @@ -496,35 +439,8 @@ class TF_VerifierImpl : public TF_PublicObjectImpl - static void GenerateAndMaskTemplate(byte *output, unsigned int outputLength, const byte *input, unsigned int inputLength, H* dummy=NULL) - { - H h; - P1363_MGF1KDF2_Common(h, output, outputLength, input, inputLength, mask, 0); - } -#endif - void GenerateAndMask(HashTransformation &hash, byte *output, unsigned int outputLength, const byte *input, unsigned int inputLength, bool mask = true) const - { - P1363_MGF1KDF2_Common(hash, output, outputLength, input, inputLength, mask, 0); - } -}; - // ******************************************************** //! . @@ -653,34 +569,9 @@ public: #endif }; -//! Base class for public key encryption standard classes. These classes are used to select from variants of algorithms. Note that not all standards apply to all algorithms. -struct EncryptionStandard {}; - //! Base class for public key signature standard classes. These classes are used to select from variants of algorithms. Note that not all standards apply to all algorithms. struct SignatureStandard {}; -template -class TF_ES; - -//! Trapdoor Function Based Encryption Scheme -template > -class TF_ES : public KEYS -{ - typedef typename STANDARD::EncryptionMessageEncodingMethod MessageEncodingMethod; - -public: - //! see EncryptionStandard for a list of standards - typedef STANDARD Standard; - typedef TF_CryptoSchemeOptions SchemeOptions; - - static std::string StaticAlgorithmName() {return KEYS::StaticAlgorithmName() + "/" + MessageEncodingMethod::StaticAlgorithmName();} - - //! implements PK_Decryptor interface - typedef PK_FinalTemplate > Decryptor; - //! implements PK_Encryptor interface - typedef PK_FinalTemplate > Encryptor; -}; - template // VC60 workaround: doesn't work if KEYS is first parameter class TF_SS; diff --git a/stepmania/src/crypto51/rsa.cpp b/stepmania/src/crypto51/rsa.cpp index 8ca748c8d6..c61f7b4ecc 100644 --- a/stepmania/src/crypto51/rsa.cpp +++ b/stepmania/src/crypto51/rsa.cpp @@ -10,13 +10,9 @@ #include "algparam.h" -#include "oaep.cpp" - NAMESPACE_BEGIN(CryptoPP) -template class OAEP; - OID RSAFunction::GetAlgorithmID() const { return ASN1::rsaEncryption(); diff --git a/stepmania/src/crypto51/rsa.h b/stepmania/src/crypto51/rsa.h index 2c1713b7ea..711678ce23 100644 --- a/stepmania/src/crypto51/rsa.h +++ b/stepmania/src/crypto51/rsa.h @@ -7,7 +7,6 @@ */ #include "pkcspad.h" -#include "oaep.h" #include "integer.h" #include "asn.h" @@ -108,12 +107,6 @@ struct RSA typedef InvertibleRSAFunction PrivateKey; }; -//! RSA cryptosystem -template -struct RSAES : public TF_ES -{ -}; - //! RSA signature scheme with appendix /*! See documentation of PKCS1v15 for a list of hash functions that can be used with it. */ template @@ -121,10 +114,6 @@ struct RSASS : public TF_SS { }; -// The two RSA encryption schemes defined in PKCS #1 v2.0 -typedef RSAES >::Decryptor RSAES_OAEP_SHA_Decryptor; -typedef RSAES >::Encryptor RSAES_OAEP_SHA_Encryptor; - // The three RSA signature schemes defined in PKCS #1 v2.0 typedef RSASS::Signer RSASSA_PKCS1v15_SHA_Signer; typedef RSASS::Verifier RSASSA_PKCS1v15_SHA_Verifier; diff --git a/stepmania/src/crypto51/seckey.h b/stepmania/src/crypto51/seckey.h index 56f589975e..b1543a820d 100644 --- a/stepmania/src/crypto51/seckey.h +++ b/stepmania/src/crypto51/seckey.h @@ -11,11 +11,6 @@ NAMESPACE_BEGIN(CryptoPP) -inline CipherDir ReverseCipherDir(CipherDir dir) -{ - return (dir == ENCRYPTION) ? DECRYPTION : ENCRYPTION; -} - //! . template class FixedBlockSize @@ -24,52 +19,6 @@ public: enum {BLOCKSIZE = N}; }; -// ************** rounds *************** - -//! . -template -class FixedRounds -{ -public: - enum {ROUNDS = R}; - -protected: - template - static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, unsigned int length, const NameValuePairs ¶m) - { - obj->ThrowIfInvalidKeyLength(length); - int rounds = param.GetIntValueWithDefault("Rounds", ROUNDS); - if (rounds != ROUNDS) - throw InvalidRounds(obj->StaticAlgorithmName(), rounds); - obj->UncheckedSetKey(dir, key, length); - } -}; - -//! . -template // use INT_MAX here because enums are treated as signed ints -class VariableRounds -{ -public: - enum {DEFAULT_ROUNDS = D, MIN_ROUNDS = N, MAX_ROUNDS = M}; - static unsigned int StaticGetDefaultRounds(unsigned int keylength) {return DEFAULT_ROUNDS;} - -protected: - static inline void AssertValidRounds(unsigned int rounds) - { - assert(rounds >= MIN_ROUNDS && rounds <= MAX_ROUNDS); - } - - template - static inline void CheckedSetKey(T *obj, CipherDir dir, const byte *key, unsigned int length, const NameValuePairs ¶m) - { - obj->ThrowIfInvalidKeyLength(length); - int rounds = param.GetIntValueWithDefault("Rounds", obj->StaticGetDefaultRounds(length)); - if (rounds < (unsigned int)MIN_ROUNDS || rounds > (unsigned int)MAX_ROUNDS) - throw InvalidRounds(obj->AlgorithmName(), rounds); - obj->UncheckedSetKey(dir, key, length, rounds); - } -}; - // ************** key length *************** //! . @@ -82,45 +31,6 @@ public: static unsigned int StaticGetValidKeyLength(unsigned int) {return KEYLENGTH;} }; -/// support query of variable key length, template parameters are default, min, max, multiple (default multiple 1) -template -class VariableKeyLength -{ - // make these private to avoid Doxygen documenting them in all derived classes - CRYPTOPP_COMPILE_ASSERT(Q > 0); - CRYPTOPP_COMPILE_ASSERT(N % Q == 0); - CRYPTOPP_COMPILE_ASSERT(M % Q == 0); - CRYPTOPP_COMPILE_ASSERT(N < M); - CRYPTOPP_COMPILE_ASSERT(D >= N && M >= D); - -public: - enum {MIN_KEYLENGTH=N, MAX_KEYLENGTH=M, DEFAULT_KEYLENGTH=D, KEYLENGTH_MULTIPLE=Q}; - enum {IV_REQUIREMENT = IV_REQ}; - static unsigned int StaticGetValidKeyLength(unsigned int n) - { - if (n < (unsigned int)MIN_KEYLENGTH) - return MIN_KEYLENGTH; - else if (n > (unsigned int)MAX_KEYLENGTH) - return (unsigned int)MAX_KEYLENGTH; - else - { - n += KEYLENGTH_MULTIPLE-1; - return n - n%KEYLENGTH_MULTIPLE; - } - } -}; - -/// support query of key length that's the same as another class -template -class SameKeyLengthAs -{ -public: - enum {MIN_KEYLENGTH=T::MIN_KEYLENGTH, MAX_KEYLENGTH=T::MAX_KEYLENGTH, DEFAULT_KEYLENGTH=T::DEFAULT_KEYLENGTH}; - enum {IV_REQUIREMENT = T::IV_REQUIREMENT}; - static unsigned int StaticGetValidKeyLength(unsigned int keylength) - {return T::StaticGetValidKeyLength(keylength);} -}; - // ************** implementation helper for SimpledKeyed *************** template @@ -182,45 +92,8 @@ public: Clonable * Clone() const {return new BlockCipherTemplate(*this);} }; -//! . -template -class MessageAuthenticationCodeTemplate : public -#ifdef CRYPTOPP_DOXYGEN_PROCESSING - MessageAuthenticationCode -#else - SimpleKeyingInterfaceImpl -#endif -{ -public: - MessageAuthenticationCodeTemplate() {} - MessageAuthenticationCodeTemplate(const byte *key) - {SetKey(key, this->DEFAULT_KEYLENGTH);} - MessageAuthenticationCodeTemplate(const byte *key, unsigned int length) - {SetKey(key, length);} - - std::string AlgorithmName() const {return this->StaticAlgorithmName();} - - void SetKey(const byte *key, unsigned int length, const NameValuePairs ¶m = g_nullNameValuePairs) - { - CheckedSetKey(this, Empty(), key, length, param); - } - - Clonable * Clone() const {return new MessageAuthenticationCodeTemplate(*this);} -}; - // ************** documentation *************** -//! These objects usually should not be used directly. See CipherModeDocumentation instead. -/*! Each class derived from this one defines two types, Encryption and Decryption, - both of which implement the BlockCipher interface. */ -struct BlockCipherDocumentation -{ - //! implements the BlockCipher interface - typedef BlockCipher Encryption; - //! implements the BlockCipher interface - typedef BlockCipher Decryption; -}; - /*! \brief Each class derived from this one defines two types, Encryption and Decryption, both of which implement the SymmetricCipher interface. See CipherModeDocumentation for information about using block ciphers. */ diff --git a/stepmania/src/crypto51/test.cpp b/stepmania/src/crypto51/test.cpp deleted file mode 100644 index 1b832192d0..0000000000 --- a/stepmania/src/crypto51/test.cpp +++ /dev/null @@ -1,166 +0,0 @@ -// test.cpp - written and placed in the public domain by Wei Dai - -#include "sha.h" -#include "files.h" -#include "rng.h" -#include "rsa.h" -#include "randpool.h" - -#include -#include - -#ifdef CRYPTOPP_WIN32_AVAILABLE -#include -#endif - -#if (_MSC_VER >= 1000) -#include // for the debug heap -#endif - -#if defined(__MWERKS__) && defined(macintosh) -#include -#endif - -USING_NAMESPACE(CryptoPP) -USING_NAMESPACE(std) - -void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed); -string RSAEncryptString(const char *pubFilename, const char *seed, const char *message); -string RSADecryptString(const char *privFilename, const char *ciphertext); -void RSASignFile(const char *privFilename, const char *messageFilename, const char *signatureFilename); -bool RSAVerifyFile(const char *pubFilename, const char *messageFilename, const char *signatureFilename); - -int (*AdhocTest)(int argc, char *argv[]) = NULL; - -#ifdef __BCPLUSPLUS__ -int cmain(int argc, char *argv[]) -#elif defined(_MSC_VER) -int __cdecl main(int argc, char *argv[]) -#else -int main(int argc, char *argv[]) -#endif -{ -#ifdef _CRTDBG_LEAK_CHECK_DF - // Turn on leak-checking - int tempflag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); - tempflag |= _CRTDBG_LEAK_CHECK_DF; - _CrtSetDbgFlag( tempflag ); -#endif - -#if defined(__MWERKS__) && defined(macintosh) - argc = ccommand(&argv); -#endif - - try - { - std::string command, executableName, edcFilename; - - if (argc < 2) - command = 'h'; - else - command = argv[1]; - - switch (command[0]) - { - case 'g': - { - char seed[1024], privFilename[128], pubFilename[128]; - unsigned int keyLength; - - cout << "Key length in bits: "; - cin >> keyLength; - - cout << "\nSave private key to file: "; - cin >> privFilename; - - cout << "\nSave public key to file: "; - cin >> pubFilename; - - cout << "\nRandom Seed: "; - ws(cin); - cin.getline(seed, 1024); - - GenerateRSAKey(keyLength, privFilename, pubFilename, seed); - return 0; - } - case 'r': - { - switch (argv[1][1]) - { - case 's': - RSASignFile(argv[2], argv[3], argv[4]); - return 0; - case 'v': - { - bool verified = RSAVerifyFile(argv[2], argv[3], argv[4]); - cout << (verified ? "valid signature" : "invalid signature") << endl; - return 0; - } - } - } - default: - FileSource usage("usage.dat", true, new FileSink(cout)); - return 1; - } - } - catch(CryptoPP::Exception &e) - { - cout << "\nCryptoPP::Exception caught: " << e.what() << endl; - return -1; - } - catch(std::exception &e) - { - cout << "\nstd::exception caught: " << e.what() << endl; - return -2; - } -} - - -RandomPool & GlobalRNG() -{ - static RandomPool randomPool; - return randomPool; -} - -void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed) -{ - RandomPool randPool; - randPool.Put((byte *)seed, strlen(seed)); - - RSAES_PKCS1v15_Decryptor priv(randPool, keyLength); - FileSink privFile(privFilename); - priv.DEREncode(privFile); - privFile.MessageEnd(); - - RSAES_PKCS1v15_Encryptor pub(priv); - FileSink pubFile(pubFilename); - pub.DEREncode(pubFile); - pubFile.MessageEnd(); -} - -void RSASignFile(const char *privFilename, const char *messageFilename, const char *signatureFilename) -{ - FileSource privFile(privFilename, true); - RSASSA_PKCS1v15_SHA_Signer priv(privFile); - // RSASSA_PKCS1v15_SHA_Signer ignores the rng. Use a real RNG for other signature schemes! - FileSource f(messageFilename, true, new SignerFilter(GlobalRNG(), priv, new FileSink(signatureFilename))); -} - -bool RSAVerifyFile(const char *pubFilename, const char *messageFilename, const char *signatureFilename) -{ - FileSource pubFile(pubFilename, true); - RSASSA_PKCS1v15_SHA_Verifier pub(pubFile); - - FileSource signatureFile(signatureFilename, true); - if (signatureFile.MaxRetrievable() != pub.SignatureLength()) - return false; - SecByteBlock signature(pub.SignatureLength()); - signatureFile.Get(signature, signature.size()); - - VerifierFilter *verifierFilter = new VerifierFilter(pub); - verifierFilter->Put(signature, pub.SignatureLength()); - FileSource f(messageFilename, true, verifierFilter); - - return verifierFilter->GetLastResult(); -} -