Çözüldü eterBase derlerken hata alıyorum

Bu konu çözüme ulaştırılmıştır. Çözüm için konuya yazılan tüm yorumları okumayı unutmayın. Eğer konudaki yorumlar sorununuzu çözmediyse yeni bir konu açabilirsiniz.

batuhanalby123

Üye
Üye
Mesaj
19
Beğeni
2
Puan
40
Ticaret Puanı
0
Merhaba öncelikle , Sıfırdan metin2 server files hazırlama rehberinde bir yerde takılı kalmış durumdayım. Kodlamadan anlamam öncelikle bunu belirteyim.Videoda ki gibi ilerleme yapıyordum lakin bende bir sürü hata çıktı. Bunları sebini anlamış değilim. Cryptopp sürümünü yükselttik. Resimde gözüktüğü gibi yaptım bende ama çare bulamadım. Yardımcı olacak arkadaşlara şimdiden teşekkür ederim.

Çıktı :
Yeniden derleme işlemi başladı...
1>------ Tümü Yeniden Derle başladı: Proje: eterBase, Yapılandırma: Release Win32 ------
1>Base.cpp
1>Poly.cpp
1>Symbol.cpp
1>SymTable.cpp
1>cipher.cpp
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(35,88): error C2059: sözdizimi hatası: ')'
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(38,3): error C2628: 'Cipher' arkasından 'void' gelmesi geçersizdir (bir ';' mü unuttunuz?)
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(40,10): error C2065: 'activated_': bildirimi yapılmamış tanımlayıcı
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(41,7): error C2561: 'Decrypt': işlev bir değer döndürmelidir
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(38): message : 'Decrypt' bildirimine bakın
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(43,5): error C2065: 'decoder_': bildirimi yapılmamış tanımlayıcı
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(32,10): error C2065: 'activated_': bildirimi yapılmamış tanımlayıcı
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(35,5): error C2065: 'encoder_': bildirimi yapılmamış tanımlayıcı
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(35,42): error C2059: sözdizimi hatası: ')'
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(46,26): error C2270: 'activated': üye olmayan işlevlerde değiştiricilere izin verilmez
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(46,35): error C2065: 'activated_': bildirimi yapılmamış tanımlayıcı
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(48,36): error C2065: 'activated_': bildirimi yapılmamış tanımlayıcı
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(50,2): error C2059: sözdizimi hatası: 'private'
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(59,1): error C2059: sözdizimi hatası: '}'
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(59,1): error C2143: sözdizimi hatası: ';' eksik ('}' öncesinde)
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.cpp(137,3): error C2614: 'Cipher': geçersiz üye başlatma: 'activated_' bir taban veya üye değil
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.cpp(137,22): error C2614: 'Cipher': geçersiz üye başlatma: 'encoder_' bir taban veya üye değil
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.cpp(137,38): error C2614: 'Cipher': geçersiz üye başlatma: 'decoder_' bir taban veya üye değil
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.cpp(137,54): error C2614: 'Cipher': geçersiz üye başlatma: 'key_agreement_' bir taban veya üye değil
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.cpp(203,14): error C2039: 'SetUp': bir 'Cipher' üyesi değil
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.h(16): message : 'Cipher' bildirimine bakın
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\cipher.cpp(446,17): warning C4193: #pragma warning(pop): eşleşen '#pragma warning(push)' yok
1>CPostIt.cpp
1>CRC32.cpp
1>Debug.cpp
1>error.cpp
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\error.cpp(59,23): warning C4477: 'fprintf' : '%08x' biçim dizesi 'unsigned int' türü bir bağımsız değişken gerektirir, ancak 1 değişen sayıda bağımsız değişkenin türü 'time_t'
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\error.cpp(59,23): message : biçim dizesinde '%llx' kullanmayı düşünün
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\error.cpp(59,23): message : biçim dizesinde '%I64x' kullanmayı düşünün
1>FileBase.cpp
1>FileDir.cpp
1>FileLoader.cpp
1>lzo.cpp
1>MappedFile.cpp
1>Random.cpp
1>StdAfx.cpp
1>Stl.cpp
1>tea.cpp
1>TempFile.cpp
1>Timer.cpp
1>Kod Üretiliyor...
1>Derleniyor...
1>Utils.cpp
1>Kod Üretiliyor...
1>C:\Users\batuh\OneDrive\Masaüstü\ClientSource\Client\EterBase\Utils.cpp(627): warning C4706: koşullu ifadeyle atama
1>"eterBase_VC90.vcxproj" projesini oluşturma tamamlandı -- BAŞARISIZ OLDU.
========== Tümünü Yeniden Oluşturma: 0 başarılı, 1 başarısız, 0 atlandı ==========

1692196268932.png
 
Çözüm
Cipher.cpp:
#include "stdafx.h"

#include "cipher.h"

#ifdef _IMPROVED_PACKET_ENCRYPTION_

#pragma warning(push)
//#pragma warning(disable: 4100 4127 4189 4231 4512 4706)

#include <cryptopp/modes.h>
#include <cryptopp/nbtheory.h>
#include <cryptopp/osrng.h>

// Diffie-Hellman key agreement
#include <cryptopp/dh.h>
#include <cryptopp/dh2.h>

// AES winner and candidates
#include <cryptopp/aes.h>
#include <cryptopp/cast.h>
#include <cryptopp/rc6.h>
#include <cryptopp/mars.h>
#include <cryptopp/serpent.h>
#include <cryptopp/twofish.h>
// Other block ciphers
#include <cryptopp/blowfish.h>
#include <cryptopp/camellia.h>
#include <cryptopp/des.h>
#include <cryptopp/idea.h>
#include <cryptopp/rc5.h>
#include <cryptopp/seed.h>
#include...
Cipher.cpp:
#include "stdafx.h"

#include "cipher.h"

#ifdef _IMPROVED_PACKET_ENCRYPTION_

#pragma warning(push)
//#pragma warning(disable: 4100 4127 4189 4231 4512 4706)

#include <cryptopp/modes.h>
#include <cryptopp/nbtheory.h>
#include <cryptopp/osrng.h>

// Diffie-Hellman key agreement
#include <cryptopp/dh.h>
#include <cryptopp/dh2.h>

// AES winner and candidates
#include <cryptopp/aes.h>
#include <cryptopp/cast.h>
#include <cryptopp/rc6.h>
#include <cryptopp/mars.h>
#include <cryptopp/serpent.h>
#include <cryptopp/twofish.h>
// Other block ciphers
#include <cryptopp/blowfish.h>
#include <cryptopp/camellia.h>
#include <cryptopp/des.h>
#include <cryptopp/idea.h>
#include <cryptopp/rc5.h>
#include <cryptopp/seed.h>
#include <cryptopp/shacal2.h>
#include <cryptopp/skipjack.h>
#include <cryptopp/tea.h>

#ifdef __THEMIDA__
#include <ThemidaSDK.h>
#endif

#include "Debug.h"


using namespace CryptoPP;

// Block cipher algorithm selector abstract base class.
struct BlockCipherAlgorithm {
    enum {
        kDefault, // to give more chances to default algorithm
        // AES winner and candidates
//        kAES, // Rijndael
        kRC6,
        kMARS,
        kTwofish,
        kSerpent,
        kCAST256,
        // Other block ciphers
        kIDEA,
        k3DES, // DES-EDE2
        kCamellia,
        kSEED,
        kRC5,
        kBlowfish,
        kTEA,
        //kSKIPJACK,
        kSHACAL2,
        // End sentinel
        kMaxAlgorithms
    };

    BlockCipherAlgorithm() {}
    virtual ~BlockCipherAlgorithm() {}

    static BlockCipherAlgorithm* Pick(int hint);

    virtual int GetBlockSize() const = 0;
    virtual int GetDefaultKeyLength() const = 0;
    virtual int GetIVLength() const = 0;

    virtual SymmetricCipher* CreateEncoder(const byte* key, size_t keylen,
        const byte* iv) const = 0;
    virtual SymmetricCipher* CreateDecoder(const byte* key, size_t keylen,
        const byte* iv) const = 0;
};

// Block cipher (with CTR mode) algorithm selector template class.
template<class T>
struct BlockCipherDetail : public BlockCipherAlgorithm {
    BlockCipherDetail() {}
    virtual ~BlockCipherDetail() {}

    virtual int GetBlockSize() const { return T::BLOCKSIZE; }
    virtual int GetDefaultKeyLength() const { return T::DEFAULT_KEYLENGTH; }
    virtual int GetIVLength() const { return T::IV_LENGTH; }

    virtual SymmetricCipher* CreateEncoder(const byte* key, size_t keylen,
        const byte* iv) const {
            return new CTR_Mode<T>::Encryption(key, keylen, iv);
    }
    virtual SymmetricCipher* CreateDecoder(const byte* key, size_t keylen,
        const byte* iv) const {
            return new CTR_Mode<T>::Decryption(key, keylen, iv);
    }
};

// Key agreement scheme abstract class.
class KeyAgreement {
public:
    KeyAgreement() {}
    virtual ~KeyAgreement() {}

    virtual size_t Prepare(void* buffer, size_t* length) = 0;
    virtual bool Agree(size_t agreed_length, const void* buffer, size_t length) = 0;

    const SecByteBlock& shared() const { return shared_; }

protected:
    SecByteBlock shared_;
};

// Crypto++ Unified Diffie-Hellman key agreement scheme implementation.
class DH2KeyAgreement : public KeyAgreement {
public:
    DH2KeyAgreement();
    virtual ~DH2KeyAgreement();

    virtual size_t Prepare(void* buffer, size_t* length);
    virtual bool Agree(size_t agreed_length, const void* buffer, size_t length);

private:
    DH dh_;
    DH2 dh2_;
    SecByteBlock spriv_key_;
    SecByteBlock epriv_key_;
};

Cipher::Cipher()
: activated_(false), encoder_(NULL), decoder_(NULL), key_agreement_(NULL) {
}

Cipher::~Cipher() {
    if (activated_) {
        CleanUp();
    }
}

void Cipher::CleanUp() {
    if (encoder_ != NULL) {
        delete encoder_;
        encoder_ = NULL;
    }
    if (decoder_ != NULL) {
        delete decoder_;
        decoder_ = NULL;
    }
    if (key_agreement_ != NULL) {
        delete key_agreement_;
        key_agreement_ = NULL;
    }
    activated_ = false;
}

size_t Cipher::Prepare(void* buffer, size_t* length) {
#ifdef __THEMIDA__
    VM_START
#endif

    assert(key_agreement_ == NULL);
    key_agreement_ = new DH2KeyAgreement();
    assert(key_agreement_ != NULL);
    size_t agreed_length = key_agreement_->Prepare(buffer, length);
    if (agreed_length == 0) {
        delete key_agreement_;
        key_agreement_ = NULL;
    }
#ifdef __THEMIDA__
    VM_END
#endif

    return agreed_length;
}

bool Cipher::Activate(bool polarity, size_t agreed_length,
                      const void* buffer, size_t length) {
#ifdef __THEMIDA__
    VM_START
#endif

    assert(activated_ == false);
    assert(key_agreement_ != NULL);
    bool result = false;
    if (key_agreement_->Agree(agreed_length, buffer, length)) {
        result = SetUp(polarity);
    }
    delete key_agreement_;
    key_agreement_ = NULL;
#ifdef __THEMIDA__
    VM_END
#endif

    return result;
}

bool Cipher::SetUp(bool polarity) {
#ifdef __THEMIDA__
    VM_START
#endif

    assert(key_agreement_ != NULL);
    const SecByteBlock& shared = key_agreement_->shared();

    // Pick a block cipher algorithm

    if (shared.size() < 2) {
        return false;
    }

    int hint_0 = shared.BytePtr()[*(shared.BytePtr()) % shared.size()];
    int hint_1 = shared.BytePtr()[*(shared.BytePtr() + 1) % shared.size()];
    BlockCipherAlgorithm* detail_0 = BlockCipherAlgorithm::Pick(hint_0);
    BlockCipherAlgorithm* detail_1 = BlockCipherAlgorithm::Pick(hint_1);
    assert(detail_0 != NULL);
    assert(detail_1 != NULL);
    std::auto_ptr<BlockCipherAlgorithm> algorithm_0(detail_0);
    std::auto_ptr<BlockCipherAlgorithm> algorithm_1(detail_1);

    const size_t key_length_0 = algorithm_0->GetDefaultKeyLength();
    const size_t iv_length_0 = algorithm_0->GetBlockSize();

    if (shared.size() < key_length_0 || shared.size() < iv_length_0) {
        return false;
    }

    const size_t key_length_1 = algorithm_1->GetDefaultKeyLength();
    const size_t iv_length_1 = algorithm_1->GetBlockSize();

    if (shared.size() < key_length_1 || shared.size() < iv_length_1) {
        return false;
    }

    // Pick encryption keys and initial vectors

    SecByteBlock key_0(key_length_0), iv_0(iv_length_0);
    SecByteBlock key_1(key_length_1), iv_1(iv_length_1);

    size_t offset;

    key_0.Assign(shared, key_length_0);
    offset = key_length_0;
    offset = min(key_length_0, shared.size() - key_length_1);
    key_1.Assign(shared.BytePtr() + offset, key_length_1);

    offset = shared.size() - iv_length_0;
    iv_0.Assign(shared.BytePtr() + offset, iv_length_0);
    offset = (offset < iv_length_1 ? 0 : offset - iv_length_1);
    iv_1.Assign(shared.BytePtr() + offset, iv_length_1);

    // Create encryption/decryption objects

    if (polarity) {
        encoder_ = algorithm_1->CreateEncoder(key_1, key_1.size(), iv_1);
        decoder_ = algorithm_0->CreateDecoder(key_0, key_0.size(), iv_0);
    } else {
        encoder_ = algorithm_0->CreateEncoder(key_0, key_0.size(), iv_0);
        decoder_ = algorithm_1->CreateDecoder(key_1, key_1.size(), iv_1);
    }

    assert(encoder_ != NULL);
    assert(decoder_ != NULL);
#ifdef __THEMIDA__
    VM_END
#endif

    return true;
}

BlockCipherAlgorithm* BlockCipherAlgorithm::Pick(int hint) {
    BlockCipherAlgorithm* detail;
    int selector = hint % kMaxAlgorithms;
    switch (selector) {
    //case kAES:
    //    detail = new BlockCipherDetail<AES>();
        break;
    case kRC6:
        detail = new BlockCipherDetail<RC6>();
        break;
    case kMARS:
        detail = new BlockCipherDetail<MARS>();
        break;
    case kTwofish:
        detail = new BlockCipherDetail<Twofish>();
        break;
    case kSerpent:
        detail = new BlockCipherDetail<Serpent>();
        break;
    case kCAST256:
        detail = new BlockCipherDetail<CAST256>();
        break;
    case kIDEA:
        detail = new BlockCipherDetail<IDEA>();
        break;
    case k3DES:
        detail = new BlockCipherDetail<DES_EDE2>();
        break;
    case kCamellia:
        detail = new BlockCipherDetail<Camellia>();
        break;
    case kSEED:
        detail = new BlockCipherDetail<SEED>();
        break;
    case kRC5:
        detail = new BlockCipherDetail<RC5>();
        break;
    case kBlowfish:
        detail = new BlockCipherDetail<Blowfish>();
        break;
    case kTEA:
        detail = new BlockCipherDetail<TEA>();
        break;
//    case kSKIPJACK:
//        detail = new BlockCipherDetail<SKIPJACK>();
//        break;
    case kSHACAL2:
        detail = new BlockCipherDetail<SHACAL2>();
        break;
    case kDefault:
    default:
        detail = new BlockCipherDetail<Twofish>(); // default algorithm
        break;
    }

    return detail;
}

DH2KeyAgreement::DH2KeyAgreement() : dh_(), dh2_(dh_) {
}

DH2KeyAgreement::~DH2KeyAgreement() {
}

size_t DH2KeyAgreement::Prepare(void* buffer, size_t* length) {
#ifdef __THEMIDA__
    VM_START
#endif

    // RFC 5114, 1024-bit MODP Group with 160-bit Prime Order Subgroup
    // http://tools.ietf.org/html/rfc5114#section-2.1
    Integer p("0xB10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C6"
        "9A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C0"
        "13ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD70"
        "98488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0"
        "A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708"
        "DF1FB2BC2E4A4371");

    Integer g("0xA4D1CBD5C3FD34126765A442EFB99905F8104DD258AC507F"
        "D6406CFF14266D31266FEA1E5C41564B777E690F5504F213"
        "160217B4B01B886A5E91547F9E2749F4D7FBD7D3B9A92EE1"
        "909D0D2263F80A76A6A24C087A091F531DBF0A0169B6A28A"
        "D662A4D18E73AFA32D779D5918D08BC8858F4DCEF97C2A24"
        "855E6EEB22B3B2E5");

    Integer q("0xF518AA8781A8DF278ABA4E7D64B7CB9D49462353");

    // Schnorr Group primes are of the form p = rq + 1, p and q prime. They
    // provide a subgroup order. In the case of 1024-bit MODP Group, the
    // security level is 80 bits (based on the 160-bit prime order subgroup).       

    // For a compare/contrast of using the maximum security level, see
    // dh-unified.zip. Also see http://www.cryptopp.com/wiki/Diffie-Hellman
    // and http://www.cryptopp.com/wiki/Security_level .

    AutoSeededRandomPool rnd;

    dh_.AccessGroupParameters().Initialize(p, q, g);

    if(!dh_.GetGroupParameters().ValidateGroup(rnd, 3)) {
        // Failed to validate prime and generator
        return 0;
    }

    size_t count = 0;

    p = dh_.GetGroupParameters().GetModulus();
    q = dh_.GetGroupParameters().GetSubgroupOrder();
    g = dh_.GetGroupParameters().GetGenerator();

    // http://groups.google.com/group/sci.crypt/browse_thread/thread/7dc7eeb04a09f0ce
    Integer v = ModularExponentiation(g, q, p);

    if(v != Integer::One()) {
        // Failed to verify order of the subgroup
        return 0;
    }

    //////////////////////////////////////////////////////////////

    spriv_key_.New(dh2_.StaticPrivateKeyLength());
    epriv_key_.New(dh2_.EphemeralPrivateKeyLength());
    SecByteBlock spub_key(dh2_.StaticPublicKeyLength());
    SecByteBlock epub_key(dh2_.EphemeralPublicKeyLength());

    dh2_.GenerateStaticKeyPair(rnd, spriv_key_, spub_key);
    dh2_.GenerateEphemeralKeyPair(rnd, epriv_key_, epub_key);

    // Prepare key agreement data
    const size_t spub_key_length = spub_key.size();
    const size_t epub_key_length = epub_key.size();
    const size_t data_length = spub_key_length + epub_key_length;

    if (*length < data_length) {
        // Not enough data buffer length
        return 0;
    }

    *length = data_length;
    byte* buf = (byte*)buffer;
    memcpy(buf, spub_key.BytePtr(), spub_key_length);
    memcpy(buf + spub_key_length, epub_key.BytePtr(), epub_key_length);

#ifdef __THEMIDA__
    VM_END
#endif

    return dh2_.AgreedValueLength();
}

bool DH2KeyAgreement::Agree(size_t agreed_length, const void* buffer, size_t length) {
    if (agreed_length != dh2_.AgreedValueLength()) {
        // Shared secret size mismatch
        return false;
    }
    const size_t spub_key_length = dh2_.StaticPublicKeyLength();
    const size_t epub_key_length = dh2_.EphemeralPublicKeyLength();
    if (length != (spub_key_length + epub_key_length)) {
        // Wrong data length
        return false;
    }
    shared_.New(dh2_.AgreedValueLength());
    const byte* buf = (const byte*)buffer;
    if (!dh2_.Agree(shared_, spriv_key_, epriv_key_, buf, buf + spub_key_length)) {
        // Failed to reach shared secret
        return false;
    }
    return true;
}

#pragma warning(pop)

#endif // _IMPROVED_PACKET_ENCRYPTION_

// EOF cipher.cpp

Cipher.h:
#ifndef __CIPHER_H__
#define __CIPHER_H__

#ifdef _IMPROVED_PACKET_ENCRYPTION_

#pragma warning(push)
#pragma warning(disable: 4100 4127 4189 4231 4512 4706)
#include <cryptopp/cryptlib.h>
#pragma warning(pop)

// Forward declaration
class KeyAgreement;

//THEMIDA
// Communication channel encryption handler.
class Cipher {
 public:
  explicit Cipher();
  ~Cipher();

  void CleanUp();

  // Returns agreed value length in bytes, or zero on failure.
  size_t Prepare(void* buffer, size_t* length);
  // Try to activate cipher algorithm with agreement data received from peer.
  bool Activate(bool polarity, size_t agreed_length,
                const void* buffer, size_t length);

  // Encrypts the given block of data. (no padding required)
  void Encrypt(void* buffer, size_t length) {
    assert(activated_);
    if (!activated_) {
      return;
    }
    encoder_->ProcessData((CryptoPP::byte*)buffer, (const CryptoPP::byte*)buffer, length);
  }
  // Decrypts the given block of data. (no padding required)
  void Decrypt(void* buffer, size_t length) {
    assert(activated_);
    if (!activated_) {
      return;
    }
    decoder_->ProcessData((CryptoPP::byte*)buffer, (const CryptoPP::byte*)buffer, length);
  }

  bool activated() const { return activated_; }
 
  void set_activated(bool value) { activated_ = value; }

 private:
  bool SetUp(bool polarity);

  bool activated_;

  CryptoPP::SymmetricCipher* encoder_;
  CryptoPP::SymmetricCipher* decoder_;

  KeyAgreement* key_agreement_;
};

#endif // _IMPROVED_PACKET_ENCRYPTION_

#endif // __CIPHER_H__
 
Çözüm
Cipher.cpp:
#include "stdafx.h"

#include "cipher.h"

#ifdef _IMPROVED_PACKET_ENCRYPTION_

#pragma warning(push)
//#pragma warning(disable: 4100 4127 4189 4231 4512 4706)

#include <cryptopp/modes.h>
#include <cryptopp/nbtheory.h>
#include <cryptopp/osrng.h>

// Diffie-Hellman key agreement
#include <cryptopp/dh.h>
#include <cryptopp/dh2.h>

// AES winner and candidates
#include <cryptopp/aes.h>
#include <cryptopp/cast.h>
#include <cryptopp/rc6.h>
#include <cryptopp/mars.h>
#include <cryptopp/serpent.h>
#include <cryptopp/twofish.h>
// Other block ciphers
#include <cryptopp/blowfish.h>
#include <cryptopp/camellia.h>
#include <cryptopp/des.h>
#include <cryptopp/idea.h>
#include <cryptopp/rc5.h>
#include <cryptopp/seed.h>
#include <cryptopp/shacal2.h>
#include <cryptopp/skipjack.h>
#include <cryptopp/tea.h>

#ifdef __THEMIDA__
#include <ThemidaSDK.h>
#endif

#include "Debug.h"


using namespace CryptoPP;

// Block cipher algorithm selector abstract base class.
struct BlockCipherAlgorithm {
    enum {
        kDefault, // to give more chances to default algorithm
        // AES winner and candidates
//        kAES, // Rijndael
        kRC6,
        kMARS,
        kTwofish,
        kSerpent,
        kCAST256,
        // Other block ciphers
        kIDEA,
        k3DES, // DES-EDE2
        kCamellia,
        kSEED,
        kRC5,
        kBlowfish,
        kTEA,
        //kSKIPJACK,
        kSHACAL2,
        // End sentinel
        kMaxAlgorithms
    };

    BlockCipherAlgorithm() {}
    virtual ~BlockCipherAlgorithm() {}

    static BlockCipherAlgorithm* Pick(int hint);

    virtual int GetBlockSize() const = 0;
    virtual int GetDefaultKeyLength() const = 0;
    virtual int GetIVLength() const = 0;

    virtual SymmetricCipher* CreateEncoder(const byte* key, size_t keylen,
        const byte* iv) const = 0;
    virtual SymmetricCipher* CreateDecoder(const byte* key, size_t keylen,
        const byte* iv) const = 0;
};

// Block cipher (with CTR mode) algorithm selector template class.
template<class T>
struct BlockCipherDetail : public BlockCipherAlgorithm {
    BlockCipherDetail() {}
    virtual ~BlockCipherDetail() {}

    virtual int GetBlockSize() const { return T::BLOCKSIZE; }
    virtual int GetDefaultKeyLength() const { return T::DEFAULT_KEYLENGTH; }
    virtual int GetIVLength() const { return T::IV_LENGTH; }

    virtual SymmetricCipher* CreateEncoder(const byte* key, size_t keylen,
        const byte* iv) const {
            return new CTR_Mode<T>::Encryption(key, keylen, iv);
    }
    virtual SymmetricCipher* CreateDecoder(const byte* key, size_t keylen,
        const byte* iv) const {
            return new CTR_Mode<T>::Decryption(key, keylen, iv);
    }
};

// Key agreement scheme abstract class.
class KeyAgreement {
public:
    KeyAgreement() {}
    virtual ~KeyAgreement() {}

    virtual size_t Prepare(void* buffer, size_t* length) = 0;
    virtual bool Agree(size_t agreed_length, const void* buffer, size_t length) = 0;

    const SecByteBlock& shared() const { return shared_; }

protected:
    SecByteBlock shared_;
};

// Crypto++ Unified Diffie-Hellman key agreement scheme implementation.
class DH2KeyAgreement : public KeyAgreement {
public:
    DH2KeyAgreement();
    virtual ~DH2KeyAgreement();

    virtual size_t Prepare(void* buffer, size_t* length);
    virtual bool Agree(size_t agreed_length, const void* buffer, size_t length);

private:
    DH dh_;
    DH2 dh2_;
    SecByteBlock spriv_key_;
    SecByteBlock epriv_key_;
};

Cipher::Cipher()
: activated_(false), encoder_(NULL), decoder_(NULL), key_agreement_(NULL) {
}

Cipher::~Cipher() {
    if (activated_) {
        CleanUp();
    }
}

void Cipher::CleanUp() {
    if (encoder_ != NULL) {
        delete encoder_;
        encoder_ = NULL;
    }
    if (decoder_ != NULL) {
        delete decoder_;
        decoder_ = NULL;
    }
    if (key_agreement_ != NULL) {
        delete key_agreement_;
        key_agreement_ = NULL;
    }
    activated_ = false;
}

size_t Cipher::Prepare(void* buffer, size_t* length) {
#ifdef __THEMIDA__
    VM_START
#endif

    assert(key_agreement_ == NULL);
    key_agreement_ = new DH2KeyAgreement();
    assert(key_agreement_ != NULL);
    size_t agreed_length = key_agreement_->Prepare(buffer, length);
    if (agreed_length == 0) {
        delete key_agreement_;
        key_agreement_ = NULL;
    }
#ifdef __THEMIDA__
    VM_END
#endif

    return agreed_length;
}

bool Cipher::Activate(bool polarity, size_t agreed_length,
                      const void* buffer, size_t length) {
#ifdef __THEMIDA__
    VM_START
#endif

    assert(activated_ == false);
    assert(key_agreement_ != NULL);
    bool result = false;
    if (key_agreement_->Agree(agreed_length, buffer, length)) {
        result = SetUp(polarity);
    }
    delete key_agreement_;
    key_agreement_ = NULL;
#ifdef __THEMIDA__
    VM_END
#endif

    return result;
}

bool Cipher::SetUp(bool polarity) {
#ifdef __THEMIDA__
    VM_START
#endif

    assert(key_agreement_ != NULL);
    const SecByteBlock& shared = key_agreement_->shared();

    // Pick a block cipher algorithm

    if (shared.size() < 2) {
        return false;
    }

    int hint_0 = shared.BytePtr()[*(shared.BytePtr()) % shared.size()];
    int hint_1 = shared.BytePtr()[*(shared.BytePtr() + 1) % shared.size()];
    BlockCipherAlgorithm* detail_0 = BlockCipherAlgorithm::Pick(hint_0);
    BlockCipherAlgorithm* detail_1 = BlockCipherAlgorithm::Pick(hint_1);
    assert(detail_0 != NULL);
    assert(detail_1 != NULL);
    std::auto_ptr<BlockCipherAlgorithm> algorithm_0(detail_0);
    std::auto_ptr<BlockCipherAlgorithm> algorithm_1(detail_1);

    const size_t key_length_0 = algorithm_0->GetDefaultKeyLength();
    const size_t iv_length_0 = algorithm_0->GetBlockSize();

    if (shared.size() < key_length_0 || shared.size() < iv_length_0) {
        return false;
    }

    const size_t key_length_1 = algorithm_1->GetDefaultKeyLength();
    const size_t iv_length_1 = algorithm_1->GetBlockSize();

    if (shared.size() < key_length_1 || shared.size() < iv_length_1) {
        return false;
    }

    // Pick encryption keys and initial vectors

    SecByteBlock key_0(key_length_0), iv_0(iv_length_0);
    SecByteBlock key_1(key_length_1), iv_1(iv_length_1);

    size_t offset;

    key_0.Assign(shared, key_length_0);
    offset = key_length_0;
    offset = min(key_length_0, shared.size() - key_length_1);
    key_1.Assign(shared.BytePtr() + offset, key_length_1);

    offset = shared.size() - iv_length_0;
    iv_0.Assign(shared.BytePtr() + offset, iv_length_0);
    offset = (offset < iv_length_1 ? 0 : offset - iv_length_1);
    iv_1.Assign(shared.BytePtr() + offset, iv_length_1);

    // Create encryption/decryption objects

    if (polarity) {
        encoder_ = algorithm_1->CreateEncoder(key_1, key_1.size(), iv_1);
        decoder_ = algorithm_0->CreateDecoder(key_0, key_0.size(), iv_0);
    } else {
        encoder_ = algorithm_0->CreateEncoder(key_0, key_0.size(), iv_0);
        decoder_ = algorithm_1->CreateDecoder(key_1, key_1.size(), iv_1);
    }

    assert(encoder_ != NULL);
    assert(decoder_ != NULL);
#ifdef __THEMIDA__
    VM_END
#endif

    return true;
}

BlockCipherAlgorithm* BlockCipherAlgorithm::Pick(int hint) {
    BlockCipherAlgorithm* detail;
    int selector = hint % kMaxAlgorithms;
    switch (selector) {
    //case kAES:
    //    detail = new BlockCipherDetail<AES>();
        break;
    case kRC6:
        detail = new BlockCipherDetail<RC6>();
        break;
    case kMARS:
        detail = new BlockCipherDetail<MARS>();
        break;
    case kTwofish:
        detail = new BlockCipherDetail<Twofish>();
        break;
    case kSerpent:
        detail = new BlockCipherDetail<Serpent>();
        break;
    case kCAST256:
        detail = new BlockCipherDetail<CAST256>();
        break;
    case kIDEA:
        detail = new BlockCipherDetail<IDEA>();
        break;
    case k3DES:
        detail = new BlockCipherDetail<DES_EDE2>();
        break;
    case kCamellia:
        detail = new BlockCipherDetail<Camellia>();
        break;
    case kSEED:
        detail = new BlockCipherDetail<SEED>();
        break;
    case kRC5:
        detail = new BlockCipherDetail<RC5>();
        break;
    case kBlowfish:
        detail = new BlockCipherDetail<Blowfish>();
        break;
    case kTEA:
        detail = new BlockCipherDetail<TEA>();
        break;
//    case kSKIPJACK:
//        detail = new BlockCipherDetail<SKIPJACK>();
//        break;
    case kSHACAL2:
        detail = new BlockCipherDetail<SHACAL2>();
        break;
    case kDefault:
    default:
        detail = new BlockCipherDetail<Twofish>(); // default algorithm
        break;
    }

    return detail;
}

DH2KeyAgreement::DH2KeyAgreement() : dh_(), dh2_(dh_) {
}

DH2KeyAgreement::~DH2KeyAgreement() {
}

size_t DH2KeyAgreement::Prepare(void* buffer, size_t* length) {
#ifdef __THEMIDA__
    VM_START
#endif

    // RFC 5114, 1024-bit MODP Group with 160-bit Prime Order Subgroup
    // http://tools.ietf.org/html/rfc5114#section-2.1
    Integer p("0xB10B8F96A080E01DDE92DE5EAE5D54EC52C99FBCFB06A3C6"
        "9A6A9DCA52D23B616073E28675A23D189838EF1E2EE652C0"
        "13ECB4AEA906112324975C3CD49B83BFACCBDD7D90C4BD70"
        "98488E9C219A73724EFFD6FAE5644738FAA31A4FF55BCCC0"
        "A151AF5F0DC8B4BD45BF37DF365C1A65E68CFDA76D4DA708"
        "DF1FB2BC2E4A4371");

    Integer g("0xA4D1CBD5C3FD34126765A442EFB99905F8104DD258AC507F"
        "D6406CFF14266D31266FEA1E5C41564B777E690F5504F213"
        "160217B4B01B886A5E91547F9E2749F4D7FBD7D3B9A92EE1"
        "909D0D2263F80A76A6A24C087A091F531DBF0A0169B6A28A"
        "D662A4D18E73AFA32D779D5918D08BC8858F4DCEF97C2A24"
        "855E6EEB22B3B2E5");

    Integer q("0xF518AA8781A8DF278ABA4E7D64B7CB9D49462353");

    // Schnorr Group primes are of the form p = rq + 1, p and q prime. They
    // provide a subgroup order. In the case of 1024-bit MODP Group, the
    // security level is 80 bits (based on the 160-bit prime order subgroup).      

    // For a compare/contrast of using the maximum security level, see
    // dh-unified.zip. Also see http://www.cryptopp.com/wiki/Diffie-Hellman
    // and http://www.cryptopp.com/wiki/Security_level .

    AutoSeededRandomPool rnd;

    dh_.AccessGroupParameters().Initialize(p, q, g);

    if(!dh_.GetGroupParameters().ValidateGroup(rnd, 3)) {
        // Failed to validate prime and generator
        return 0;
    }

    size_t count = 0;

    p = dh_.GetGroupParameters().GetModulus();
    q = dh_.GetGroupParameters().GetSubgroupOrder();
    g = dh_.GetGroupParameters().GetGenerator();

    // http://groups.google.com/group/sci.crypt/browse_thread/thread/7dc7eeb04a09f0ce
    Integer v = ModularExponentiation(g, q, p);

    if(v != Integer::One()) {
        // Failed to verify order of the subgroup
        return 0;
    }

    //////////////////////////////////////////////////////////////

    spriv_key_.New(dh2_.StaticPrivateKeyLength());
    epriv_key_.New(dh2_.EphemeralPrivateKeyLength());
    SecByteBlock spub_key(dh2_.StaticPublicKeyLength());
    SecByteBlock epub_key(dh2_.EphemeralPublicKeyLength());

    dh2_.GenerateStaticKeyPair(rnd, spriv_key_, spub_key);
    dh2_.GenerateEphemeralKeyPair(rnd, epriv_key_, epub_key);

    // Prepare key agreement data
    const size_t spub_key_length = spub_key.size();
    const size_t epub_key_length = epub_key.size();
    const size_t data_length = spub_key_length + epub_key_length;

    if (*length < data_length) {
        // Not enough data buffer length
        return 0;
    }

    *length = data_length;
    byte* buf = (byte*)buffer;
    memcpy(buf, spub_key.BytePtr(), spub_key_length);
    memcpy(buf + spub_key_length, epub_key.BytePtr(), epub_key_length);

#ifdef __THEMIDA__
    VM_END
#endif

    return dh2_.AgreedValueLength();
}

bool DH2KeyAgreement::Agree(size_t agreed_length, const void* buffer, size_t length) {
    if (agreed_length != dh2_.AgreedValueLength()) {
        // Shared secret size mismatch
        return false;
    }
    const size_t spub_key_length = dh2_.StaticPublicKeyLength();
    const size_t epub_key_length = dh2_.EphemeralPublicKeyLength();
    if (length != (spub_key_length + epub_key_length)) {
        // Wrong data length
        return false;
    }
    shared_.New(dh2_.AgreedValueLength());
    const byte* buf = (const byte*)buffer;
    if (!dh2_.Agree(shared_, spriv_key_, epriv_key_, buf, buf + spub_key_length)) {
        // Failed to reach shared secret
        return false;
    }
    return true;
}

#pragma warning(pop)

#endif // _IMPROVED_PACKET_ENCRYPTION_

// EOF cipher.cpp

Cipher.h:
#ifndef __CIPHER_H__
#define __CIPHER_H__

#ifdef _IMPROVED_PACKET_ENCRYPTION_

#pragma warning(push)
#pragma warning(disable: 4100 4127 4189 4231 4512 4706)
#include <cryptopp/cryptlib.h>
#pragma warning(pop)

// Forward declaration
class KeyAgreement;

//THEMIDA
// Communication channel encryption handler.
class Cipher {
 public:
  explicit Cipher();
  ~Cipher();

  void CleanUp();

  // Returns agreed value length in bytes, or zero on failure.
  size_t Prepare(void* buffer, size_t* length);
  // Try to activate cipher algorithm with agreement data received from peer.
  bool Activate(bool polarity, size_t agreed_length,
                const void* buffer, size_t length);

  // Encrypts the given block of data. (no padding required)
  void Encrypt(void* buffer, size_t length) {
    assert(activated_);
    if (!activated_) {
      return;
    }
    encoder_->ProcessData((CryptoPP::byte*)buffer, (const CryptoPP::byte*)buffer, length);
  }
  // Decrypts the given block of data. (no padding required)
  void Decrypt(void* buffer, size_t length) {
    assert(activated_);
    if (!activated_) {
      return;
    }
    decoder_->ProcessData((CryptoPP::byte*)buffer, (const CryptoPP::byte*)buffer, length);
  }

  bool activated() const { return activated_; }
 
  void set_activated(bool value) { activated_ = value; }

 private:
  bool SetUp(bool polarity);

  bool activated_;

  CryptoPP::SymmetricCipher* encoder_;
  CryptoPP::SymmetricCipher* decoder_;

  KeyAgreement* key_agreement_;
};

#endif // _IMPROVED_PACKET_ENCRYPTION_

#endif // __CIPHER_H__
Eline koluna sağlık. Problemi anlamış değilim ama cipher dosyalarının verdiklerinle değiştirdikten sonra düzledi.
 
Videodaki gibi yaptım gene de aynı hatayı verdi.

Güncelleme :
static unsigned char*dataX;
static int pos;
static int sizeX;

yapınca düzeldi bilginize. En başta 15-20. satır arası.
1692218865535.png
 
Son düzenleme:
Geri
Üst