Skip to content

Commit

Permalink
Remove non ascii test (FISCO-BCOS#1940)
Browse files Browse the repository at this point in the history
* remove non ascii characters test

* add sdf sm4 tests
  • Loading branch information
MaggieNgWu authored May 20, 2021
1 parent 3a4db44 commit 1c36739
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
40 changes: 38 additions & 2 deletions fisco-bcos/crypto/mini-crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <libdevcrypto/Hash.h>
#include <libdevcrypto/SM2Signature.h>
#include <libdevcrypto/SM3Hash.h>
#include <libdevcrypto/SM4Crypto.h>
#include <libdevcrypto/hsm/HSMCrypto.h>
#include <libdevcrypto/hsm/HSMHash.h>
#include <libdevcrypto/hsm/HSMSignature.h>
using namespace dev::crypto;
Expand All @@ -40,19 +42,53 @@ int main(int, const char* argv[])

std::cout << "#### begin function test" << std::endl;
KeyPair kp = KeyPair::create();

std::string pubHex = toHex(kp.pub().data(), kp.pub().data() + 64, "04");
h256 h(dev::fromHex("0x68b5bae5fe19851624298fd1e9b4d788627ac27c13aad3240102ffd292a17911"));
std::shared_ptr<crypto::Signature> swResult = sm2Sign(kp, h);
std::shared_ptr<crypto::Signature> sdfResult = SDFSM2Sign(kp, h);
bool result1 = sm2Verify(kp.pub(), swResult, h);
cout << "*** soft sign, soft verify :" << result1 << endl << endl;
cout << "*** soft sign, soft verify :" << result1 << endl;
bool result2 = sm2Verify(kp.pub(), sdfResult, h);
cout << "*** hardware sign, soft verify: " << result2 << endl;
bool result3 = SDFSM2Verify(kp.pub(), sdfResult, h);
cout << "*** hardware sign, hardware verify: " << result3 << endl;
bool result4 = SDFSM2Verify(kp.pub(), swResult, h);
cout << "*** soft sign, hardware verify: " << result4 << endl;


const std::string key = "0B780F0F13CE7F3F1383053CAC817ABF";
const std::string plainData =
"1E59A21C02EBE011A33A36F497A0D5927EE96088EF3232DA09AD25BE3B1F1C9EF24D357E25B4A15472C41682BE"
"ACE94E";
const std::string iv = "B47B07085D258F6EE60790A786C7DFBD";
const std::string endata = sm4Encrypt((const unsigned char*)plainData.data(), plainData.size(),
(const unsigned char*)key.data(), key.size(), (const unsigned char*)iv.data());

const std::string dedata = SDFSM4Decrypt((const unsigned char*)endata.data(), endata.size(),
(const unsigned char*)key.data(), key.size(), (const unsigned char*)iv.data());
int softHardSM4 = plainData.compare(dedata);
cout << "*** soft sm4 enc, hardware decrypt: " << softHardSM4 == 0 << endl;


// const std::string dedata = sm4Decrypt((const unsigned char*)endata.data(),
// endata.size(),(const unsigned char*)key.data(), key.size(), (const unsigned char*)iv.data());
// BOOST_CHECK_EQUAL(plainData, dedata);

const std::string keyWithoutIv = "00000000000000000000000000000000";
const std::string plainDataWithoutIv =
"5942D6DA31CD0A93E46CF382468710888F4393C6D734B3C6C6C44B1F6F34B08AEE0386B91831A268C4E9815BB6"
"1375F4CDA913BA80C37CE6F4971977319CCD7D23502328A45130D0FDF5B63A77EA601F806733FBCADF969B08AA"
"9AA56A8B509FAA7E95FC3706E3482EF1532A91DB2EB3EDF234D1E2E57F75B5EACC81A391";
const std::string endataWithoutIv = sm4Encrypt((const unsigned char*)plainDataWithoutIv.data(),
plainDataWithoutIv.size(), (const unsigned char*)keyWithoutIv.data(), keyWithoutIv.size(),
(const unsigned char*)keyWithoutIv.data());
const std::string dedataWithoutIv = SDFSM4Decrypt((const unsigned char*)endataWithoutIv.data(),
endataWithoutIv.size(), (const unsigned char*)keyWithoutIv.data(), endataWithoutIv.size(),
(const unsigned char*)keyWithoutIv.data());

int softHardSM4WithoutIv = plainDataWithoutIv.compare(dedataWithoutIv);
cout << "*** soft sm4 enc, hardware decrypt without iv: " << softHardSM4WithoutIv == 0 << endl;

getchar();
std::cout << "#### begin performance test" << std::endl;

Expand Down
2 changes: 1 addition & 1 deletion test/unittests/libdevcore/CommonJS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ BOOST_AUTO_TEST_CASE(test_fromRaw)
{
// non ascii characters means empty string
h256 a("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
BOOST_CHECK("" == fromRaw(a));
//BOOST_CHECK("" == fromRaw(a));
h256 b("");
BOOST_CHECK("" == fromRaw(b));
h256 c("0x4173636969436861726163746572730000000000000000000000000000000000");
Expand Down
1 change: 1 addition & 0 deletions test/unittests/libnetwork/FakeASIOInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <libnetwork/ASIOInterface.h>
#include <boost/asio.hpp>
#include <boost/asio/ssl.hpp>
#include <thread>

#define ERROR_SOCKET_PORT 8889
#define EMPTY_CERT_SOCKET_PORT 8887
Expand Down

0 comments on commit 1c36739

Please sign in to comment.