From 0abb8e9b1d9d3549068c5c3432444292b713cbc6 Mon Sep 17 00:00:00 2001 From: subtly Date: Wed, 15 Oct 2014 11:58:27 +0200 Subject: cryptopp aes128-cbc --- TestHelperCrypto.h | 10 ++++++---- crypto.cpp | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/TestHelperCrypto.h b/TestHelperCrypto.h index 6feeeb97..7e38c438 100644 --- a/TestHelperCrypto.h +++ b/TestHelperCrypto.h @@ -22,11 +22,13 @@ #pragma once //#include -#include -#include -#include #include -#include +#include // secp256r1 +#include // ec domain +#include // ec prime field +#include // also for buffer +#include +#include // aes modes using namespace std; using namespace CryptoPP; diff --git a/crypto.cpp b/crypto.cpp index 25106a77..0f0a5f60 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -103,7 +103,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_ecdh_prime) cnote << "Testing cryptopp_ecdh_prime..."; using namespace CryptoPP; - OID curve = ASN1::secp256r1(); + OID curve = ASN1::secp256k1(); ECDH::Domain dhLocal(curve); SecByteBlock privLocal(dhLocal.PrivateKeyLength()); @@ -136,14 +136,51 @@ BOOST_AUTO_TEST_CASE(cryptopp_ecdh_prime) assert(ssLocal == ssRemote); } +BOOST_AUTO_TEST_CASE(cryptopp_aes128_cbc) +{ + const int aesKeyLen = 16; + assert(sizeof(char) == sizeof(byte)); + + AutoSeededRandomPool rng; + SecByteBlock key(0x00, aesKeyLen); + rng.GenerateBlock(key, key.size()); + + // Generate random IV + byte iv[AES::BLOCKSIZE]; + rng.GenerateBlock(iv, AES::BLOCKSIZE); + + string string128("AAAAAAAAAAAAAAAA"); + string plainOriginal = string128; + + CryptoPP::CBC_Mode::Encryption cbcEncryption(key, key.size(), iv); + cbcEncryption.ProcessData((byte*)&string128[0], (byte*)&string128[0], string128.size()); + assert(string128 != plainOriginal); + + CBC_Mode::Decryption cbcDecryption(key, key.size(), iv); + cbcDecryption.ProcessData((byte*)&string128[0], (byte*)&string128[0], string128.size()); + assert(plainOriginal == string128); + + + // plaintext whose size isn't divisible by block size must use stream filter for padding + string string192("AAAAAAAAAAAAAAAABBBBBBBB"); + plainOriginal = string192; + + string cipher; + StreamTransformationFilter* aesStream = new StreamTransformationFilter(cbcEncryption, new StringSink(cipher)); + StringSource source(string192, true, aesStream); + assert(cipher.size() == 32); + + cbcDecryption.ProcessData((byte*)&cipher[0], (byte*)&string192[0], cipher.size()); + assert(string192 == plainOriginal); +} + BOOST_AUTO_TEST_CASE(cryptopp_ecdh_aes128_cbc_noauth) { // ECDH gives 256-bit shared while aes uses 128-bits // Use first 128-bits of shared secret as symmetric key // IV is 0 // New connections require new ECDH keypairs - - + } BOOST_AUTO_TEST_CASE(cryptopp_eth_fbba) -- cgit From 224f43bf81b5cf7e54b0bdf95cd87e2d46bdb081 Mon Sep 17 00:00:00 2001 From: subtly Date: Sat, 18 Oct 2014 05:11:36 +0200 Subject: basic ecies class --- TestHelperCrypto.h | 2 - crypto.cpp | 106 ++++++++++++++++------------------------------------- 2 files changed, 31 insertions(+), 77 deletions(-) diff --git a/TestHelperCrypto.h b/TestHelperCrypto.h index 57e4e420..24104f11 100644 --- a/TestHelperCrypto.h +++ b/TestHelperCrypto.h @@ -21,8 +21,6 @@ #pragma once -//#include - #pragma warning(push) #pragma warning(disable:4100 4244) #pragma GCC diagnostic push diff --git a/crypto.cpp b/crypto.cpp index 1c5b9a30..428fcb27 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -27,27 +27,38 @@ #include #include #include +#include +#include #include "TestHelperCrypto.h" using namespace std; using namespace dev; +using namespace dev::crypto; +using namespace CryptoPP; -namespace dev -{ -namespace crypto +BOOST_AUTO_TEST_SUITE(devcrypto) + +BOOST_AUTO_TEST_CASE(ecies) { + ECKeyPair k = ECKeyPair::create(); + + string message("Now is the time for all good men to come to the aide of humanity."); + bytes b = bytesConstRef(message).toBytes(); + ECIESEncryptor(&k).encrypt(b); -inline CryptoPP::AutoSeededRandomPool& PRNG() { - static CryptoPP::AutoSeededRandomPool prng; - return prng; -} + bytesConstRef br(&b); + bytes plain = ECIESDecryptor(&k).decrypt(br); + assert(plain == bytesConstRef(message).toBytes()); } -} - -using namespace CryptoPP; -BOOST_AUTO_TEST_SUITE(crypto) +BOOST_AUTO_TEST_CASE(ecdhe_aes128_ctr_sha3mac) +{ + // New connections require new ECDH keypairs + // Every new connection requires a new EC keypair + // Every new trust requires a new EC keypair + // All connections should share seed for PRF (or PRNG) for nonces +} BOOST_AUTO_TEST_CASE(cryptopp_ecies_message) { @@ -55,9 +66,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_ecies_message) string const message("Now is the time for all good men to come to the aide of humanity."); - AutoSeededRandomPool prng; - - ECIES::Decryptor localDecryptor(prng, ASN1::secp256r1()); + ECIES::Decryptor localDecryptor(crypto::PRNG(), crypto::secp256k1()); SavePrivateKey(localDecryptor.GetPrivateKey()); ECIES::Encryptor localEncryptor(localDecryptor); @@ -65,31 +74,31 @@ BOOST_AUTO_TEST_CASE(cryptopp_ecies_message) ECIES::Decryptor futureDecryptor; LoadPrivateKey(futureDecryptor.AccessPrivateKey()); - futureDecryptor.GetPrivateKey().ThrowIfInvalid(prng, 3); + futureDecryptor.GetPrivateKey().ThrowIfInvalid(crypto::PRNG(), 3); ECIES::Encryptor futureEncryptor; LoadPublicKey(futureEncryptor.AccessPublicKey()); - futureEncryptor.GetPublicKey().ThrowIfInvalid(prng, 3); + futureEncryptor.GetPublicKey().ThrowIfInvalid(crypto::PRNG(), 3); // encrypt/decrypt with local string cipherLocal; - StringSource ss1 (message, true, new PK_EncryptorFilter(prng, localEncryptor, new StringSink(cipherLocal) ) ); + StringSource ss1 (message, true, new PK_EncryptorFilter(crypto::PRNG(), localEncryptor, new StringSink(cipherLocal) ) ); string plainLocal; - StringSource ss2 (cipherLocal, true, new PK_DecryptorFilter(prng, localDecryptor, new StringSink(plainLocal) ) ); + StringSource ss2 (cipherLocal, true, new PK_DecryptorFilter(crypto::PRNG(), localDecryptor, new StringSink(plainLocal) ) ); // encrypt/decrypt with future string cipherFuture; - StringSource ss3 (message, true, new PK_EncryptorFilter(prng, futureEncryptor, new StringSink(cipherFuture) ) ); + StringSource ss3 (message, true, new PK_EncryptorFilter(crypto::PRNG(), futureEncryptor, new StringSink(cipherFuture) ) ); string plainFuture; - StringSource ss4 (cipherFuture, true, new PK_DecryptorFilter(prng, futureDecryptor, new StringSink(plainFuture) ) ); + StringSource ss4 (cipherFuture, true, new PK_DecryptorFilter(crypto::PRNG(), futureDecryptor, new StringSink(plainFuture) ) ); // decrypt local w/future string plainFutureFromLocal; - StringSource ss5 (cipherLocal, true, new PK_DecryptorFilter(prng, futureDecryptor, new StringSink(plainFutureFromLocal) ) ); + StringSource ss5 (cipherLocal, true, new PK_DecryptorFilter(crypto::PRNG(), futureDecryptor, new StringSink(plainFutureFromLocal) ) ); // decrypt future w/local string plainLocalFromFuture; - StringSource ss6 (cipherFuture, true, new PK_DecryptorFilter(prng, localDecryptor, new StringSink(plainLocalFromFuture) ) ); + StringSource ss6 (cipherFuture, true, new PK_DecryptorFilter(crypto::PRNG(), localDecryptor, new StringSink(plainLocalFromFuture) ) ); assert(plainLocal == message); @@ -173,60 +182,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_aes128_cbc) cbcDecryption.ProcessData((byte*)&cipher[0], (byte*)&string192[0], cipher.size()); assert(string192 == plainOriginal); } - -BOOST_AUTO_TEST_CASE(cryptopp_ecdh_aes128_cbc_noauth) -{ - // ECDH gives 256-bit shared while aes uses 128-bits - // Use first 128-bits of shared secret as symmetric key - // IV is 0 - // New connections require new ECDH keypairs -} - -BOOST_AUTO_TEST_CASE(cryptopp_eth_fbba) -{ - // Initial Authentication: - // - // New/Known Peer: - // pubkeyL = knownR? ? myKnown : myECDH - // pubkeyR = knownR? ? theirKnown : theirECDH - // - // Initial message = hmac(k=sha3(shared-secret[128..255]), address(pubkeyL)) || ECIES encrypt(pubkeyR, pubkeyL) - // - // Key Exchange (this could occur after handshake messages): - // If peers do not know each other they will need to exchange public keys. - // - // Drop ECDH (this could occur after handshake messages): - // After authentication and/or key exchange, both sides generate shared key - // from their 'known' keys and use this to encrypt all future messages. - // - // v2: If one side doesn't trust the other then a single-use key maybe sent. - // This will need to be tracked for future connections; when non-trusting peer - // wants to trust the other, it can request that it's old, 'new', public key be - // accepted. And, if the peer *really* doesn't trust the other side, it can request - // that a new, 'new', public key be accepted. - // - // Handshake (all or nothing, padded): - // All Peers (except blacklisted): - // - // - // New Peer: - // - // - // Known Untrusted Peer: - // - // - // Known Trusted Peer: - // - // - // Blacklisted Peeer: - // Already dropped by now. - // - // - // MAC: - // ... -} - BOOST_AUTO_TEST_CASE(eth_keypairs) { cnote << "Testing Crypto..."; -- cgit From 8669468cbc18dfe0a7dd2049ff7e9662fc914fc3 Mon Sep 17 00:00:00 2001 From: subtly Date: Sat, 18 Oct 2014 23:01:42 +0200 Subject: cleanup headers --- crypto.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/crypto.cpp b/crypto.cpp index 428fcb27..484b8714 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -41,14 +41,18 @@ BOOST_AUTO_TEST_SUITE(devcrypto) BOOST_AUTO_TEST_CASE(ecies) { ECKeyPair k = ECKeyPair::create(); - + string message("Now is the time for all good men to come to the aide of humanity."); - bytes b = bytesConstRef(message).toBytes(); + bytes b = bytesRef(message).toBytes(); ECIESEncryptor(&k).encrypt(b); bytesConstRef br(&b); bytes plain = ECIESDecryptor(&k).decrypt(br); + + // ideally, decryptor will go a step further, accept a bytesRef and zero input. + assert(plain != b); + // plaintext is same as output assert(plain == bytesConstRef(message).toBytes()); } -- cgit From 9a5a6db634eba31bc1e009ef54b71c82dbc33dfb Mon Sep 17 00:00:00 2001 From: subtly Date: Wed, 22 Oct 2014 15:57:52 +0200 Subject: aes ctr mode test --- crypto.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/crypto.cpp b/crypto.cpp index 484b8714..ba5bbfa4 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -62,6 +62,11 @@ BOOST_AUTO_TEST_CASE(ecdhe_aes128_ctr_sha3mac) // Every new connection requires a new EC keypair // Every new trust requires a new EC keypair // All connections should share seed for PRF (or PRNG) for nonces + + + + + } BOOST_AUTO_TEST_CASE(cryptopp_ecies_message) @@ -149,6 +154,74 @@ BOOST_AUTO_TEST_CASE(cryptopp_ecdh_prime) assert(ssLocal == ssRemote); } +BOOST_AUTO_TEST_CASE(cryptopp_aes128_ctr) +{ + const int aesKeyLen = 16; + assert(sizeof(char) == sizeof(byte)); + + // generate test key + AutoSeededRandomPool rng; + SecByteBlock key(0x00, aesKeyLen); + rng.GenerateBlock(key, key.size()); + + // cryptopp uses IV as nonce/counter which is same as using nonce w/0 ctr + byte ctr[ AES::BLOCKSIZE ]; + rng.GenerateBlock( ctr, sizeof(ctr) ); + + string text = "Now is the time for all good men to come to the aide of humanity."; + // c++11 ftw + unsigned char const* in = (unsigned char*)&text[0]; + unsigned char* out = (unsigned char*)&text[0]; + string original = text; + + string cipherCopy; + try + { + CTR_Mode< AES >::Encryption e; + e.SetKeyWithIV( key, key.size(), ctr ); + e.ProcessData(out, in, text.size()); + assert(text!=original); + cipherCopy = text; + } + catch( CryptoPP::Exception& e ) + { + cerr << e.what() << endl; + } + + try + { + CTR_Mode< AES >::Decryption d; + d.SetKeyWithIV( key, key.size(), ctr ); + d.ProcessData(out, in, text.size()); + assert(text==original); + } + catch( CryptoPP::Exception& e ) + { + cerr << e.what() << endl; + } + + + // reencrypt ciphertext... + try + { + assert(cipherCopy!=text); + in = (unsigned char*)&cipherCopy[0]; + out = (unsigned char*)&cipherCopy[0]; + + CTR_Mode< AES >::Encryption e; + e.SetKeyWithIV( key, key.size(), ctr ); + e.ProcessData(out, in, text.size()); + + // yep, ctr mode. + assert(cipherCopy==original); + } + catch( CryptoPP::Exception& e ) + { + cerr << e.what() << endl; + } + +} + BOOST_AUTO_TEST_CASE(cryptopp_aes128_cbc) { const int aesKeyLen = 16; -- cgit From 76c29fc08addfd4cffa1773def96833f3616b4c0 Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Wed, 22 Oct 2014 16:26:10 +0200 Subject: Bugfix, get correct gas after exception --- vm.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/vm.cpp b/vm.cpp index 7306ef32..0ac21156 100644 --- a/vm.cpp +++ b/vm.cpp @@ -24,7 +24,7 @@ #include #include -#define FILL_TESTS +//#define FILL_TESTS using namespace std; using namespace json_spirit; @@ -512,12 +512,10 @@ void doTests(json_spirit::mValue& v, bool _fillin) } bytes output; - u256 gas; + VM vm(fev.gas); try { - VM vm(fev.gas); output = vm.go(fev).toVector(); - gas = vm.gas(); // Get the remaining gas } catch (Exception const& _e) { @@ -554,7 +552,7 @@ void doTests(json_spirit::mValue& v, bool _fillin) o["post"] = mValue(fev.exportState()); o["callcreates"] = fev.exportCallCreates(); o["out"] = "0x" + toHex(output); - fev.push(o, "gas", gas); + fev.push(o, "gas", vm.gas()); } else { @@ -578,7 +576,7 @@ void doTests(json_spirit::mValue& v, bool _fillin) else BOOST_CHECK(output == fromHex(o["out"].get_str())); - BOOST_CHECK(test.toInt(o["gas"]) == gas); + BOOST_CHECK(test.toInt(o["gas"]) == vm.gas()); BOOST_CHECK(test.addresses == fev.addresses); BOOST_CHECK(test.callcreates == fev.callcreates); } @@ -621,11 +619,13 @@ void executeTests(const string& _name) if (ptestPath == NULL) { cnote << " could not find environment variable ETHEREUM_TEST_PATH \n"; - testPath = "../../../tests/vmtests"; + testPath = "../../../tests"; } else testPath = ptestPath; + testPath += "/vmtests"; + #ifdef FILL_TESTS try { @@ -654,7 +654,7 @@ void executeTests(const string& _name) cnote << "Testing VM..." << _name; json_spirit::mValue v; string s = asString(contents(testPath + "/" + _name + ".json")); - BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of " + _name + ".json is empty. Have you cloned the 'tests' repo branch develop and set ETHEREUM_TEST_PATH to its path?"); + BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of " + testPath + "/" + _name + ".json is empty. Have you cloned the 'tests' repo branch develop and set ETHEREUM_TEST_PATH to its path?"); json_spirit::read_string(s, v); dev::test::doTests(v, false); } @@ -715,4 +715,3 @@ BOOST_AUTO_TEST_CASE(vmSystemOperationsTest) { dev::test::executeTests("vmSystemOperationsTest"); } - -- cgit From 52e3b15735a71ed527d538b82ba135bd9c29d18e Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Wed, 22 Oct 2014 21:21:18 +0200 Subject: Added call depth tests --- vm.cpp | 2 +- vmIOandFlowOperationsTestFiller.json | 28 ++++++++++++++ vmSystemOperationsTestFiller.json | 71 ++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) diff --git a/vm.cpp b/vm.cpp index 0ac21156..fd23829f 100644 --- a/vm.cpp +++ b/vm.cpp @@ -24,7 +24,7 @@ #include #include -//#define FILL_TESTS +#define FILL_TESTS using namespace std; using namespace json_spirit; diff --git a/vmIOandFlowOperationsTestFiller.json b/vmIOandFlowOperationsTestFiller.json index 79d162c8..a470b9c8 100644 --- a/vmIOandFlowOperationsTestFiller.json +++ b/vmIOandFlowOperationsTestFiller.json @@ -670,6 +670,34 @@ } }, + "jumpi2": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "1000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "1000000000000000000", + "nonce" : 0, + "code" : "0x60017fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff596002600357", + "storage": {} + } + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "value" : "1000000000000000000", + "data" : "", + "gasPrice" : "100000000000000", + "gas" : "10000" + } + }, + "pc0": { "env" : { "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", diff --git a/vmSystemOperationsTestFiller.json b/vmSystemOperationsTestFiller.json index c948f043..c0dae74d 100644 --- a/vmSystemOperationsTestFiller.json +++ b/vmSystemOperationsTestFiller.json @@ -949,6 +949,77 @@ } }, + "ABAcalls2": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "10000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "1000000000000000000", + "nonce" : 0, + "code" : "{ [[ 0 ]] (ADD (SLOAD 0) 1) (CALL (- (GAS) 1000) 0x945304eb96065b2a98b57a48a06ae28d285a71b5 1 0 0 0 0) }", + "storage": {} + }, + "945304eb96065b2a98b57a48a06ae28d285a71b5" : { + "balance" : "0", + "code" : " { [[ 0 ]] (ADD (SLOAD 0) 1) (CALL (- (GAS) 1000) 0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6 0 0 0 0 0) } ", + "nonce" : "0", + "storage" : { + } + } + + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "value" : "100000", + "data" : "", + "gasPrice" : "100000000000000", + "gas" : "10000000000000" + } + }, + + "ABAcalls3": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "10000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "1025000", + "nonce" : 0, + "code" : "{ [[ 0 ]] (ADD (SLOAD 0) 1) (CALL (- (GAS) 1000) 0x945304eb96065b2a98b57a48a06ae28d285a71b5 1 0 0 0 0) }", + "storage": {} + }, + "945304eb96065b2a98b57a48a06ae28d285a71b5" : { + "balance" : "0", + "code" : " { [[ 0 ]] (ADD (SLOAD 0) 1) (CALL (- (GAS) 1000) 0x0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6 0 0 0 0 0) } ", + "nonce" : "0", + "storage" : { + } + } + + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "value" : "100000", + "data" : "", + "gasPrice" : "100000000000000", + "gas" : "1000000" + } + }, "ABAcallsSuicide0": { "env" : { -- cgit From 29450c0fab3b093962e32c36fa0b1c5c237b3998 Mon Sep 17 00:00:00 2001 From: subtly Date: Wed, 22 Oct 2014 22:57:41 +0200 Subject: abstract cryptopp. add/test encrypt/decrypt for key. --- crypto.cpp | 66 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/crypto.cpp b/crypto.cpp index ba5bbfa4..48c6fc70 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -38,22 +38,36 @@ using namespace CryptoPP; BOOST_AUTO_TEST_SUITE(devcrypto) -BOOST_AUTO_TEST_CASE(ecies) +BOOST_AUTO_TEST_CASE(eckeypair_encrypt) { ECKeyPair k = ECKeyPair::create(); - - string message("Now is the time for all good men to come to the aide of humanity."); - bytes b = bytesRef(message).toBytes(); - ECIESEncryptor(&k).encrypt(b); - - bytesConstRef br(&b); - bytes plain = ECIESDecryptor(&k).decrypt(br); - - // ideally, decryptor will go a step further, accept a bytesRef and zero input. - assert(plain != b); + string message("Now is the time for all good persons to come to the aide of humanity."); + string original = message; + + bytes b = asBytes(message); + k.encrypt(b); + assert(b != asBytes(original)); - // plaintext is same as output - assert(plain == bytesConstRef(message).toBytes()); + bytes p = k.decrypt(&b); + assert(p == asBytes(original)); +} + +BOOST_AUTO_TEST_CASE(ecies) +{ +// ECKeyPair k = ECKeyPair::create(); +// +// string message("Now is the time for all good persons to come to the aide of humanity."); +// bytes b = bytesRef(message).toBytes(); +// ECIESEncryptor(&k).encrypt(b); +// +// bytesConstRef br(&b); +// bytes plain = ECIESDecryptor(&k).decrypt(br); +// +// // ideally, decryptor will go a step further, accept a bytesRef and zero input. +// assert(plain != b); +// +// // plaintext is same as output +// assert(plain == bytesConstRef(message).toBytes()); } BOOST_AUTO_TEST_CASE(ecdhe_aes128_ctr_sha3mac) @@ -73,9 +87,9 @@ BOOST_AUTO_TEST_CASE(cryptopp_ecies_message) { cnote << "Testing cryptopp_ecies_message..."; - string const message("Now is the time for all good men to come to the aide of humanity."); + string const message("Now is the time for all good persons to come to the aide of humanity."); - ECIES::Decryptor localDecryptor(crypto::PRNG(), crypto::secp256k1()); + ECIES::Decryptor localDecryptor(pp::PRNG(), pp::secp256k1()); SavePrivateKey(localDecryptor.GetPrivateKey()); ECIES::Encryptor localEncryptor(localDecryptor); @@ -83,31 +97,31 @@ BOOST_AUTO_TEST_CASE(cryptopp_ecies_message) ECIES::Decryptor futureDecryptor; LoadPrivateKey(futureDecryptor.AccessPrivateKey()); - futureDecryptor.GetPrivateKey().ThrowIfInvalid(crypto::PRNG(), 3); + futureDecryptor.GetPrivateKey().ThrowIfInvalid(pp::PRNG(), 3); ECIES::Encryptor futureEncryptor; LoadPublicKey(futureEncryptor.AccessPublicKey()); - futureEncryptor.GetPublicKey().ThrowIfInvalid(crypto::PRNG(), 3); + futureEncryptor.GetPublicKey().ThrowIfInvalid(pp::PRNG(), 3); // encrypt/decrypt with local string cipherLocal; - StringSource ss1 (message, true, new PK_EncryptorFilter(crypto::PRNG(), localEncryptor, new StringSink(cipherLocal) ) ); + StringSource ss1 (message, true, new PK_EncryptorFilter(pp::PRNG(), localEncryptor, new StringSink(cipherLocal) ) ); string plainLocal; - StringSource ss2 (cipherLocal, true, new PK_DecryptorFilter(crypto::PRNG(), localDecryptor, new StringSink(plainLocal) ) ); + StringSource ss2 (cipherLocal, true, new PK_DecryptorFilter(pp::PRNG(), localDecryptor, new StringSink(plainLocal) ) ); // encrypt/decrypt with future string cipherFuture; - StringSource ss3 (message, true, new PK_EncryptorFilter(crypto::PRNG(), futureEncryptor, new StringSink(cipherFuture) ) ); + StringSource ss3 (message, true, new PK_EncryptorFilter(pp::PRNG(), futureEncryptor, new StringSink(cipherFuture) ) ); string plainFuture; - StringSource ss4 (cipherFuture, true, new PK_DecryptorFilter(crypto::PRNG(), futureDecryptor, new StringSink(plainFuture) ) ); + StringSource ss4 (cipherFuture, true, new PK_DecryptorFilter(pp::PRNG(), futureDecryptor, new StringSink(plainFuture) ) ); // decrypt local w/future string plainFutureFromLocal; - StringSource ss5 (cipherLocal, true, new PK_DecryptorFilter(crypto::PRNG(), futureDecryptor, new StringSink(plainFutureFromLocal) ) ); + StringSource ss5 (cipherLocal, true, new PK_DecryptorFilter(pp::PRNG(), futureDecryptor, new StringSink(plainFutureFromLocal) ) ); // decrypt future w/local string plainLocalFromFuture; - StringSource ss6 (cipherFuture, true, new PK_DecryptorFilter(crypto::PRNG(), localDecryptor, new StringSink(plainLocalFromFuture) ) ); + StringSource ss6 (cipherFuture, true, new PK_DecryptorFilter(pp::PRNG(), localDecryptor, new StringSink(plainLocalFromFuture) ) ); assert(plainLocal == message); @@ -126,12 +140,12 @@ BOOST_AUTO_TEST_CASE(cryptopp_ecdh_prime) ECDH::Domain dhLocal(curve); SecByteBlock privLocal(dhLocal.PrivateKeyLength()); SecByteBlock pubLocal(dhLocal.PublicKeyLength()); - dhLocal.GenerateKeyPair(dev::crypto::PRNG(), privLocal, pubLocal); + dhLocal.GenerateKeyPair(pp::PRNG(), privLocal, pubLocal); ECDH::Domain dhRemote(curve); SecByteBlock privRemote(dhRemote.PrivateKeyLength()); SecByteBlock pubRemote(dhRemote.PublicKeyLength()); - dhRemote.GenerateKeyPair(dev::crypto::PRNG(), privRemote, pubRemote); + dhRemote.GenerateKeyPair(pp::PRNG(), privRemote, pubRemote); assert(dhLocal.AgreedValueLength() == dhRemote.AgreedValueLength()); @@ -168,7 +182,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_aes128_ctr) byte ctr[ AES::BLOCKSIZE ]; rng.GenerateBlock( ctr, sizeof(ctr) ); - string text = "Now is the time for all good men to come to the aide of humanity."; + string text = "Now is the time for all good persons to come to the aide of humanity."; // c++11 ftw unsigned char const* in = (unsigned char*)&text[0]; unsigned char* out = (unsigned char*)&text[0]; -- cgit From 9308d04a4cc1b93a879f542aa9c3e3e8297410e4 Mon Sep 17 00:00:00 2001 From: subtly Date: Wed, 22 Oct 2014 23:59:00 +0200 Subject: Export and encrypt via dev::Public. more cryptopp abstraction. --- crypto.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/crypto.cpp b/crypto.cpp index 48c6fc70..7e3ce81a 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -28,7 +28,7 @@ #include #include #include -#include +//#include #include "TestHelperCrypto.h" using namespace std; @@ -38,6 +38,21 @@ using namespace CryptoPP; BOOST_AUTO_TEST_SUITE(devcrypto) +BOOST_AUTO_TEST_CASE(cryptopp_public_export_import) +{ + ECIES::Decryptor d(pp::PRNG(), pp::secp256k1()); + ECIES::Encryptor e(d.GetKey()); + + Public p = pp::exportPublicKey(e.GetKey()); + Integer x(&p[0], 32); + Integer y(&p[32], 32); + + DL_PublicKey_EC pub; + pub.Initialize(pp::secp256k1(), ECP::Point(x,y)); + + assert(pub == e.GetKey()); +} + BOOST_AUTO_TEST_CASE(eckeypair_encrypt) { ECKeyPair k = ECKeyPair::create(); @@ -50,6 +65,11 @@ BOOST_AUTO_TEST_CASE(eckeypair_encrypt) bytes p = k.decrypt(&b); assert(p == asBytes(original)); + + encrypt(p, k.publicKey()); + assert(p != asBytes(original)); + + // todo: test decrypt w/Secret } BOOST_AUTO_TEST_CASE(ecies) -- cgit From 6513d4312962f7d99d52572f02b5ff4277272496 Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Oct 2014 02:40:02 +0200 Subject: import/export cryptopp keys. begin to separate operations and public/secret. --- crypto.cpp | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/crypto.cpp b/crypto.cpp index 7e3ce81a..a9b9904c 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -28,7 +28,6 @@ #include #include #include -//#include #include "TestHelperCrypto.h" using namespace std; @@ -38,22 +37,32 @@ using namespace CryptoPP; BOOST_AUTO_TEST_SUITE(devcrypto) +BOOST_AUTO_TEST_CASE(cryptopp_private_secret_import) +{ + ECKeyPair k = ECKeyPair::create(); + Integer e = k.m_decryptor.AccessKey().GetPrivateExponent(); + assert(pp::ExponentFromSecret(k.secret()) == e); +} + BOOST_AUTO_TEST_CASE(cryptopp_public_export_import) { ECIES::Decryptor d(pp::PRNG(), pp::secp256k1()); ECIES::Encryptor e(d.GetKey()); - Public p = pp::exportPublicKey(e.GetKey()); + Public p; + pp::exportDL_PublicKey_EC(e.GetKey(), p); Integer x(&p[0], 32); Integer y(&p[32], 32); DL_PublicKey_EC pub; pub.Initialize(pp::secp256k1(), ECP::Point(x,y)); - assert(pub == e.GetKey()); + + DL_PublicKey_EC pub2; + pub.Initialize(pp::secp256k1(), ECP::Point(x,y)); } -BOOST_AUTO_TEST_CASE(eckeypair_encrypt) +BOOST_AUTO_TEST_CASE(ecies_eckeypair) { ECKeyPair k = ECKeyPair::create(); string message("Now is the time for all good persons to come to the aide of humanity."); @@ -62,32 +71,16 @@ BOOST_AUTO_TEST_CASE(eckeypair_encrypt) bytes b = asBytes(message); k.encrypt(b); assert(b != asBytes(original)); - - bytes p = k.decrypt(&b); - assert(p == asBytes(original)); - encrypt(p, k.publicKey()); - assert(p != asBytes(original)); - - // todo: test decrypt w/Secret -} + Secret s = k.secret(); + decrypt(s, b); + assert(b == asBytes(original)); -BOOST_AUTO_TEST_CASE(ecies) -{ -// ECKeyPair k = ECKeyPair::create(); -// -// string message("Now is the time for all good persons to come to the aide of humanity."); -// bytes b = bytesRef(message).toBytes(); -// ECIESEncryptor(&k).encrypt(b); -// -// bytesConstRef br(&b); -// bytes plain = ECIESDecryptor(&k).decrypt(br); -// -// // ideally, decryptor will go a step further, accept a bytesRef and zero input. -// assert(plain != b); -// -// // plaintext is same as output -// assert(plain == bytesConstRef(message).toBytes()); + // Fix Me! +// encrypt(k.publicKey(), b); +// assert(b != asBytes(original)); +// bytes plain = k.decrypt(&b); +// assert(plain == asBytes(original)); } BOOST_AUTO_TEST_CASE(ecdhe_aes128_ctr_sha3mac) -- cgit From 19e169694c7ae60acc8eb923fe71c4a971dc32fd Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Oct 2014 04:50:04 +0200 Subject: fix public import/export --- crypto.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/crypto.cpp b/crypto.cpp index a9b9904c..bb8db2b6 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -51,15 +51,10 @@ BOOST_AUTO_TEST_CASE(cryptopp_public_export_import) Public p; pp::exportDL_PublicKey_EC(e.GetKey(), p); - Integer x(&p[0], 32); - Integer y(&p[32], 32); - + DL_PublicKey_EC pub; - pub.Initialize(pp::secp256k1(), ECP::Point(x,y)); - assert(pub == e.GetKey()); - - DL_PublicKey_EC pub2; - pub.Initialize(pp::secp256k1(), ECP::Point(x,y)); + pub.Initialize(pp::secp256k1(), pp::PointFromPublic(p)); + assert(pub.GetPublicElement() == e.GetKey().GetPublicElement()); } BOOST_AUTO_TEST_CASE(ecies_eckeypair) @@ -78,9 +73,10 @@ BOOST_AUTO_TEST_CASE(ecies_eckeypair) // Fix Me! // encrypt(k.publicKey(), b); -// assert(b != asBytes(original)); -// bytes plain = k.decrypt(&b); -// assert(plain == asBytes(original)); + k.encrypt(b); + assert(b != asBytes(original)); + k.decrypt(b); + assert(b == asBytes(original)); } BOOST_AUTO_TEST_CASE(ecdhe_aes128_ctr_sha3mac) -- cgit From 4dcfbd05c6218e8cd38a36eacdc09b0dabf5e9f0 Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Oct 2014 05:04:25 +0200 Subject: cryptopp_vs_secp256k1 --- crypto.cpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/crypto.cpp b/crypto.cpp index bb8db2b6..55ee1e17 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -37,6 +37,37 @@ using namespace CryptoPP; BOOST_AUTO_TEST_SUITE(devcrypto) +BOOST_AUTO_TEST_CASE(cryptopp_vs_secp256k1) +{ + ECIES::Decryptor d(pp::PRNG(), pp::secp256k1()); + ECIES::Encryptor e(d.GetKey()); + + Secret s; + pp::SecretFromDL_PrivateKey_EC(d.GetKey(), s); + + Public p; + pp::PublicFromDL_PublicKey_EC(e.GetKey(), p); + + assert(dev::toAddress(s) == right160(dev::sha3(p.ref()))); + + Secret previous = s; + for (auto i = 0; i < 30; i++) + { + ECIES::Decryptor d(pp::PRNG(), pp::secp256k1()); + ECIES::Encryptor e(d.GetKey()); + + Secret s; + pp::SecretFromDL_PrivateKey_EC(d.GetKey(), s); + assert(s!=previous); + + Public p; + pp::PublicFromDL_PublicKey_EC(e.GetKey(), p); + + /// wow, this worked. the first time. + assert(dev::toAddress(s) == right160(dev::sha3(p.ref()))); + } +} + BOOST_AUTO_TEST_CASE(cryptopp_private_secret_import) { ECKeyPair k = ECKeyPair::create(); @@ -50,7 +81,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_public_export_import) ECIES::Encryptor e(d.GetKey()); Public p; - pp::exportDL_PublicKey_EC(e.GetKey(), p); + pp::PublicFromDL_PublicKey_EC(e.GetKey(), p); DL_PublicKey_EC pub; pub.Initialize(pp::secp256k1(), pp::PointFromPublic(p)); -- cgit From 9e28ef7cf013205dbd59be66bcd93d8fa2eb5cff Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Thu, 23 Oct 2014 16:07:13 +0200 Subject: Added new recursive bombs to vm tests --- vmPushDupSwapTestFiller.json | 28 +++++++++++++ vmSystemOperationsTestFiller.json | 86 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/vmPushDupSwapTestFiller.json b/vmPushDupSwapTestFiller.json index 1bb3e663..52c704d4 100644 --- a/vmPushDupSwapTestFiller.json +++ b/vmPushDupSwapTestFiller.json @@ -27,6 +27,34 @@ } }, + "push1_missingStack": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "1000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "1000000000000000000", + "nonce" : 0, + "code" : "0x60", + "storage": {} + } + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "value" : "1000000000000000000", + "data" : "", + "gasPrice" : "100000000000000", + "gas" : "10000" + } + }, + "push2": { "env" : { "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", diff --git a/vmSystemOperationsTestFiller.json b/vmSystemOperationsTestFiller.json index c0dae74d..1df2697e 100644 --- a/vmSystemOperationsTestFiller.json +++ b/vmSystemOperationsTestFiller.json @@ -509,7 +509,7 @@ } }, - "CallRecursiveBomb": { + "CallRecursiveBomb0": { "env" : { "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", "currentNumber" : "0", @@ -543,6 +543,90 @@ } }, + "CallRecursiveBomb1": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "10000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "20000000", + "nonce" : 0, + "code" : "{ [[ 0 ]] (+ (SLOAD 0) 1) [[ 1 ]] (CALL (- (GAS) 224) (ADDRESS) 0 0 0 0 0) }", + "storage": {} + } + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "value" : "100000", + "data" : "", + "gasPrice" : "1", + "gas" : "364723" + } + }, + + "CallRecursiveBomb2": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "10000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "20000000", + "nonce" : 0, + "code" : "{ [[ 0 ]] (+ (SLOAD 0) 1) [[ 1 ]] (CALL (- (GAS) 224) (ADDRESS) 0 0 0 0 0) }", + "storage": {} + } + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "value" : "100000", + "data" : "", + "gasPrice" : "1", + "gas" : "364724" + } + }, + + "CallRecursiveBomb3": { + "env" : { + "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", + "currentNumber" : "0", + "currentGasLimit" : "10000000", + "currentDifficulty" : "256", + "currentTimestamp" : 1, + "currentCoinbase" : "2adc25665018aa1fe0e6bc666dac8fc2697ff9ba" + }, + "pre" : { + "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6" : { + "balance" : "20000000", + "nonce" : 0, + "code" : "{ [[ 0 ]] (+ (SLOAD 0) 1) [[ 1 ]] (CALL (- (GAS) 224) (ADDRESS) 0 0 0 0 0) }", + "storage": {} + } + }, + "exec" : { + "address" : "0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6", + "origin" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "caller" : "cd1722f3947def4cf144679da39c4c32bdc35681", + "value" : "100000", + "data" : "", + "gasPrice" : "1", + "gas" : "1000000" + } + }, + "suicide0": { "env" : { "previousHash" : "5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6", -- cgit From 9604a5a4d9cb48d01538f9199988a901f56c9b1d Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Oct 2014 16:38:50 +0200 Subject: commit before narrowing down import-public key issue w/cryptopp --- crypto.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 18 deletions(-) diff --git a/crypto.cpp b/crypto.cpp index 55ee1e17..ccfa1003 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -62,52 +62,80 @@ BOOST_AUTO_TEST_CASE(cryptopp_vs_secp256k1) Public p; pp::PublicFromDL_PublicKey_EC(e.GetKey(), p); - - /// wow, this worked. the first time. + assert(dev::toAddress(s) == right160(dev::sha3(p.ref()))); } } -BOOST_AUTO_TEST_CASE(cryptopp_private_secret_import) +BOOST_AUTO_TEST_CASE(cryptopp_is_bad) { - ECKeyPair k = ECKeyPair::create(); - Integer e = k.m_decryptor.AccessKey().GetPrivateExponent(); - assert(pp::ExponentFromSecret(k.secret()) == e); + SecretKeyRef k; + Secret s = k.sec(); + + /// Convert secret to exponent used by pp + Integer e = pp::ExponentFromSecret(k.sec()); + + ECIES::Decryptor d; +// k.AccessGroupParameters().Initialize(ASN1::secp256r1()); +// k.SetPrivateExponent(_e); + + pp::SecretFromDL_PrivateKey_EC(d.GetKey(), s); + } BOOST_AUTO_TEST_CASE(cryptopp_public_export_import) { ECIES::Decryptor d(pp::PRNG(), pp::secp256k1()); ECIES::Encryptor e(d.GetKey()); - + + Secret s; + pp::SecretFromDL_PrivateKey_EC(d.GetKey(), s); Public p; pp::PublicFromDL_PublicKey_EC(e.GetKey(), p); - + Address addr = right160(dev::sha3(p.ref())); + assert(toAddress(s) == addr); + + KeyPair l(s); + assert(l.address() == addr); + DL_PublicKey_EC pub; pub.Initialize(pp::secp256k1(), pp::PointFromPublic(p)); assert(pub.GetPublicElement() == e.GetKey().GetPublicElement()); + + + //// + SecretKeyRef k; + Public p2; + pp::PublicFromExponent(pp::ExponentFromSecret(k.sec()), p2); + assert(k.pub() == p2); + + // Fix me: + Address a = k.address(); + Address a2 = toAddress(k.sec()); + assert(a2 == a); } BOOST_AUTO_TEST_CASE(ecies_eckeypair) { - ECKeyPair k = ECKeyPair::create(); + KeyPair l = KeyPair::create(); + SecretKeyRef k(l.sec()); + string message("Now is the time for all good persons to come to the aide of humanity."); string original = message; bytes b = asBytes(message); - k.encrypt(b); + encrypt(k.pub(), b); assert(b != asBytes(original)); - Secret s = k.secret(); - decrypt(s, b); + decrypt(k.sec(), b); assert(b == asBytes(original)); - // Fix Me! -// encrypt(k.publicKey(), b); - k.encrypt(b); - assert(b != asBytes(original)); - k.decrypt(b); - assert(b == asBytes(original)); +// // Fix Me! +//// encrypt(k.publicKey(), b); +// k.encrypt(b); +// assert(b != asBytes(original)); +// k.decrypt(b); +// assert(b == asBytes(original)); } BOOST_AUTO_TEST_CASE(ecdhe_aes128_ctr_sha3mac) -- cgit From 3726c120e9ede1a5cd835ee4beb516a9aca6e669 Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Oct 2014 16:59:01 +0200 Subject: two ec curves walked into a bar... --- crypto.cpp | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/crypto.cpp b/crypto.cpp index ccfa1003..40e0a6a4 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -67,20 +67,39 @@ BOOST_AUTO_TEST_CASE(cryptopp_vs_secp256k1) } } -BOOST_AUTO_TEST_CASE(cryptopp_is_bad) +BOOST_AUTO_TEST_CASE(cryptopp_keys_cryptor_sipaseckp256k1) { SecretKeyRef k; Secret s = k.sec(); - /// Convert secret to exponent used by pp - Integer e = pp::ExponentFromSecret(k.sec()); + // Convert secret to exponent used by pp + Integer e = pp::ExponentFromSecret(s); + // Test that exported DL_EC private is same as exponent from Secret + CryptoPP::DL_PrivateKey_EC privatek; + privatek.AccessGroupParameters().Initialize(pp::secp256k1()); + privatek.SetPrivateExponent(e); + assert(e == privatek.GetPrivateExponent()); + + // Test that exported secret is same as decryptor(privatek) secret ECIES::Decryptor d; -// k.AccessGroupParameters().Initialize(ASN1::secp256r1()); -// k.SetPrivateExponent(_e); + d.AccessKey().AccessGroupParameters().Initialize(pp::secp256k1()); + d.AccessKey().SetPrivateExponent(e); + assert(d.AccessKey().GetPrivateExponent() == e); - pp::SecretFromDL_PrivateKey_EC(d.GetKey(), s); + // Test that decryptor->encryptor->public == private->makepublic->public + CryptoPP::DL_PublicKey_EC pubk; + pubk.AccessGroupParameters().Initialize(pp::secp256k1()); + privatek.MakePublicKey(pubk); + ECIES::Encryptor enc(d); + assert(pubk.GetPublicElement() == enc.AccessKey().GetPublicElement()); + + // Test against sipa/seckp256k1 + Public p; + pp::PublicFromExponent(pp::ExponentFromSecret(s), p); + assert(toAddress(s) == dev::right160(dev::sha3(p.ref()))); + assert(k.pub() == p); } BOOST_AUTO_TEST_CASE(cryptopp_public_export_import) @@ -101,9 +120,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_public_export_import) DL_PublicKey_EC pub; pub.Initialize(pp::secp256k1(), pp::PointFromPublic(p)); assert(pub.GetPublicElement() == e.GetKey().GetPublicElement()); - - - //// + SecretKeyRef k; Public p2; pp::PublicFromExponent(pp::ExponentFromSecret(k.sec()), p2); -- cgit From a203fb7ae8fd84d576ef530c83e1fe5b09ad364e Mon Sep 17 00:00:00 2001 From: Christoph Jentzsch Date: Thu, 23 Oct 2014 18:58:59 +0200 Subject: merge --- vm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm.cpp b/vm.cpp index 54f3b8ce..2e4571be 100644 --- a/vm.cpp +++ b/vm.cpp @@ -91,7 +91,7 @@ bool FakeExtVM::call(Address _receiveAddress, u256 _value, bytesConstRef _data, if (!m_s.addresses().count(myAddress)) { m_ms.internal.resize(m_ms.internal.size() + 1); - auto na = m_s.createNewAddress(myAddress, myAddress, balance(myAddress), gasPrice, &contractgas, init, origin, &suicides, &m_ms ? &(m_ms.internal.back()) : nullptr, OnOpFunc(), 1); + auto na = m_s.createNewAddress(myAddress, myAddress, balance(myAddress), gasPrice, &contractgas, init, origin, &suicides, &m_ms ? &(m_ms.internal.back()) : nullptr, {}, 1); if (!m_ms.internal.back().from) m_ms.internal.pop_back(); if (na != myAddress) -- cgit From ee062e564b4cac38a14d755abcde1c68b3ad8b53 Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Oct 2014 19:06:31 +0200 Subject: add ecies encrypt/decrypt support to common crypto --- TestHelperCrypto.h | 18 +----------------- crypto.cpp | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/TestHelperCrypto.h b/TestHelperCrypto.h index 24104f11..01e97c21 100644 --- a/TestHelperCrypto.h +++ b/TestHelperCrypto.h @@ -21,23 +21,7 @@ #pragma once -#pragma warning(push) -#pragma warning(disable:4100 4244) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wconversion" -#pragma GCC diagnostic ignored "-Wunused-parameter" -#pragma GCC diagnostic ignored "-Wunused-variable" -#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor" -#pragma GCC diagnostic ignored "-Wextra" -#include -#include // secp256k1 -#include // ec domain -#include // ec prime field -#include // cryptopp buffer -#include -#include // aes modes -#pragma warning(pop) -#pragma GCC diagnostic pop +#include using namespace std; using namespace CryptoPP; diff --git a/crypto.cpp b/crypto.cpp index 40e0a6a4..3662bb83 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -37,6 +37,24 @@ using namespace CryptoPP; BOOST_AUTO_TEST_SUITE(devcrypto) +BOOST_AUTO_TEST_CASE(common_crypt) +{ + string message("Now is the time for all good persons to come to the aide of humanity."); + bytes m = asBytes(message); + bytesConstRef bcr(&m); + + SecretKeyRef k; + bytes cipher; + encrypt(k.pub(), bcr, cipher); + assert(cipher != asBytes(message) && cipher.size() > 0); + + bytes plain; + decrypt(k.sec(), bytesConstRef(&cipher), plain); + + assert(asString(plain) == message); + assert(plain == asBytes(message)); +} + BOOST_AUTO_TEST_CASE(cryptopp_vs_secp256k1) { ECIES::Decryptor d(pp::PRNG(), pp::secp256k1()); -- cgit From 2b66efb72d2b58c9f165766cb427928525601a47 Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Oct 2014 19:19:02 +0200 Subject: cleanup more headers --- crypto.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto.cpp b/crypto.cpp index 3662bb83..ba17b9fe 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -37,7 +37,7 @@ using namespace CryptoPP; BOOST_AUTO_TEST_SUITE(devcrypto) -BOOST_AUTO_TEST_CASE(common_crypt) +BOOST_AUTO_TEST_CASE(common_encrypt_decrypt) { string message("Now is the time for all good persons to come to the aide of humanity."); bytes m = asBytes(message); -- cgit From f02987e2199de3b6296c24cc5746c7857fbed60e Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Oct 2014 19:25:12 +0200 Subject: remove irrelevant test code --- crypto.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/crypto.cpp b/crypto.cpp index ba17b9fe..8e56d607 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -144,7 +144,6 @@ BOOST_AUTO_TEST_CASE(cryptopp_public_export_import) pp::PublicFromExponent(pp::ExponentFromSecret(k.sec()), p2); assert(k.pub() == p2); - // Fix me: Address a = k.address(); Address a2 = toAddress(k.sec()); assert(a2 == a); @@ -164,13 +163,6 @@ BOOST_AUTO_TEST_CASE(ecies_eckeypair) decrypt(k.sec(), b); assert(b == asBytes(original)); - -// // Fix Me! -//// encrypt(k.publicKey(), b); -// k.encrypt(b); -// assert(b != asBytes(original)); -// k.decrypt(b); -// assert(b == asBytes(original)); } BOOST_AUTO_TEST_CASE(ecdhe_aes128_ctr_sha3mac) -- cgit From 1c8ef381247ae249a4dd9f7567a3aa5aaa0e4417 Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Oct 2014 21:59:05 +0200 Subject: coding standards --- crypto.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/crypto.cpp b/crypto.cpp index 8e56d607..57c992e5 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -76,7 +76,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_vs_secp256k1) Secret s; pp::SecretFromDL_PrivateKey_EC(d.GetKey(), s); - assert(s!=previous); + assert(s != previous); Public p; pp::PublicFromDL_PublicKey_EC(e.GetKey(), p); @@ -274,8 +274,8 @@ BOOST_AUTO_TEST_CASE(cryptopp_aes128_ctr) rng.GenerateBlock(key, key.size()); // cryptopp uses IV as nonce/counter which is same as using nonce w/0 ctr - byte ctr[ AES::BLOCKSIZE ]; - rng.GenerateBlock( ctr, sizeof(ctr) ); + byte ctr[AES::BLOCKSIZE]; + rng.GenerateBlock(ctr, sizeof(ctr)); string text = "Now is the time for all good persons to come to the aide of humanity."; // c++11 ftw @@ -286,13 +286,13 @@ BOOST_AUTO_TEST_CASE(cryptopp_aes128_ctr) string cipherCopy; try { - CTR_Mode< AES >::Encryption e; - e.SetKeyWithIV( key, key.size(), ctr ); + CTR_Mode::Encryption e; + e.SetKeyWithIV(key, key.size(), ctr); e.ProcessData(out, in, text.size()); - assert(text!=original); + assert(text != original); cipherCopy = text; } - catch( CryptoPP::Exception& e ) + catch(CryptoPP::Exception& e) { cerr << e.what() << endl; } @@ -300,11 +300,11 @@ BOOST_AUTO_TEST_CASE(cryptopp_aes128_ctr) try { CTR_Mode< AES >::Decryption d; - d.SetKeyWithIV( key, key.size(), ctr ); + d.SetKeyWithIV(key, key.size(), ctr); d.ProcessData(out, in, text.size()); - assert(text==original); + assert(text == original); } - catch( CryptoPP::Exception& e ) + catch(CryptoPP::Exception& e) { cerr << e.what() << endl; } @@ -313,18 +313,18 @@ BOOST_AUTO_TEST_CASE(cryptopp_aes128_ctr) // reencrypt ciphertext... try { - assert(cipherCopy!=text); + assert(cipherCopy != text); in = (unsigned char*)&cipherCopy[0]; out = (unsigned char*)&cipherCopy[0]; - CTR_Mode< AES >::Encryption e; - e.SetKeyWithIV( key, key.size(), ctr ); + CTR_Mode::Encryption e; + e.SetKeyWithIV(key, key.size(), ctr); e.ProcessData(out, in, text.size()); // yep, ctr mode. - assert(cipherCopy==original); + assert(cipherCopy == original); } - catch( CryptoPP::Exception& e ) + catch(CryptoPP::Exception& e) { cerr << e.what() << endl; } -- cgit From 9a6b5fd613b8fbe208ee9e8eecf7571371ce8beb Mon Sep 17 00:00:00 2001 From: subtly Date: Thu, 23 Oct 2014 22:12:47 +0200 Subject: style. remove unused code. --- crypto.cpp | 47 ++++------------------------------------------- 1 file changed, 4 insertions(+), 43 deletions(-) diff --git a/crypto.cpp b/crypto.cpp index 57c992e5..0d3b6202 100644 --- a/crypto.cpp +++ b/crypto.cpp @@ -43,7 +43,7 @@ BOOST_AUTO_TEST_CASE(common_encrypt_decrypt) bytes m = asBytes(message); bytesConstRef bcr(&m); - SecretKeyRef k; + KeyPair k = KeyPair::create(); bytes cipher; encrypt(k.pub(), bcr, cipher); assert(cipher != asBytes(message) && cipher.size() > 0); @@ -87,7 +87,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_vs_secp256k1) BOOST_AUTO_TEST_CASE(cryptopp_keys_cryptor_sipaseckp256k1) { - SecretKeyRef k; + KeyPair k = KeyPair::create(); Secret s = k.sec(); // Convert secret to exponent used by pp @@ -139,7 +139,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_public_export_import) pub.Initialize(pp::secp256k1(), pp::PointFromPublic(p)); assert(pub.GetPublicElement() == e.GetKey().GetPublicElement()); - SecretKeyRef k; + KeyPair k = KeyPair::create(); Public p2; pp::PublicFromExponent(pp::ExponentFromSecret(k.sec()), p2); assert(k.pub() == p2); @@ -151,8 +151,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_public_export_import) BOOST_AUTO_TEST_CASE(ecies_eckeypair) { - KeyPair l = KeyPair::create(); - SecretKeyRef k(l.sec()); + KeyPair k = KeyPair::create(); string message("Now is the time for all good persons to come to the aide of humanity."); string original = message; @@ -225,44 +224,6 @@ BOOST_AUTO_TEST_CASE(cryptopp_ecies_message) assert(plainLocalFromFuture == plainLocal); } -BOOST_AUTO_TEST_CASE(cryptopp_ecdh_prime) -{ - cnote << "Testing cryptopp_ecdh_prime..."; - - using namespace CryptoPP; - OID curve = ASN1::secp256k1(); - - ECDH::Domain dhLocal(curve); - SecByteBlock privLocal(dhLocal.PrivateKeyLength()); - SecByteBlock pubLocal(dhLocal.PublicKeyLength()); - dhLocal.GenerateKeyPair(pp::PRNG(), privLocal, pubLocal); - - ECDH::Domain dhRemote(curve); - SecByteBlock privRemote(dhRemote.PrivateKeyLength()); - SecByteBlock pubRemote(dhRemote.PublicKeyLength()); - dhRemote.GenerateKeyPair(pp::PRNG(), privRemote, pubRemote); - - assert(dhLocal.AgreedValueLength() == dhRemote.AgreedValueLength()); - - // local: send public to remote; remote: send public to local - - // Local - SecByteBlock sharedLocal(dhLocal.AgreedValueLength()); - assert(dhLocal.Agree(sharedLocal, privLocal, pubRemote)); - - // Remote - SecByteBlock sharedRemote(dhRemote.AgreedValueLength()); - assert(dhRemote.Agree(sharedRemote, privRemote, pubLocal)); - - // Test - Integer ssLocal, ssRemote; - ssLocal.Decode(sharedLocal.BytePtr(), sharedLocal.SizeInBytes()); - ssRemote.Decode(sharedRemote.BytePtr(), sharedRemote.SizeInBytes()); - - assert(ssLocal != 0); - assert(ssLocal == ssRemote); -} - BOOST_AUTO_TEST_CASE(cryptopp_aes128_ctr) { const int aesKeyLen = 16; -- cgit