diff --git a/.gitignore b/.gitignore index 767679d..6801d49 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,7 @@ *.a *.la *.lai -*.so +*.so* *.dll *.dylib @@ -42,3 +42,5 @@ target_wrapper.* # QtCreator CMake CMakeLists.txt.user* + +deployTests/ diff --git a/Qt-Secret.pro b/Qt-Secret.pro index 108d60b..dca77f9 100644 --- a/Qt-Secret.pro +++ b/Qt-Secret.pro @@ -10,11 +10,12 @@ CONFIG += ordered SUBDIRS += \ src \ - tests + tests \ + qaesencryption include($$PWD/test.pri) src.file = src/Qt-Secret.pro tests.file = tests/Qt-SecretTest.pro - +qaesencryption.file = src/Qt-AES/qaesencryption.pro diff --git a/src/Qt-AES b/src/Qt-AES index b296606..1b3ddbd 160000 --- a/src/Qt-AES +++ b/src/Qt-AES @@ -1 +1 @@ -Subproject commit b296606aabc611df5cda4aece14d0f7df1607a24 +Subproject commit 1b3ddbd2d22ed826758896cfc67825ae75c4ccc0 diff --git a/src/Qt-RSA/qrsaencryption.cpp b/src/Qt-RSA/qrsaencryption.cpp index 65659d2..7e70dd3 100644 --- a/src/Qt-RSA/qrsaencryption.cpp +++ b/src/Qt-RSA/qrsaencryption.cpp @@ -12,18 +12,14 @@ INT eulerFunc(const INT &p, const INT &q) { } template -INT mul(const INT &a, const INT &b, const INT &m) { - if (b == 0) { - return 0 % m; +INT mul(INT a, INT b, const INT &m) { + INT res = 0; + while (a != 0) { + if (a & 1) res = (res + b) % m; + a >>= 1; + b = (b << 1) % m; } - - if (b == 1) - return a; - if (b % 2 == 0){ - INT t = mul(a, b / 2, m); - return (2 * t) % m; - } - return (mul(a, b - 1, m) + a) % m; + return res; } template @@ -171,9 +167,9 @@ QByteArray toArray(INT i, short sizeBlok = -1) { return res; } - while (res.rbegin() != res.rend() && !*res.rbegin()) { - res.remove(res.size() -1, 1); - } + // while (res.rbegin() != res.rend() && !*res.rbegin()) { + // res.remove(res.size() -1, 1); + // } return res.left(sizeBlok); } @@ -288,6 +284,17 @@ QByteArray decodeArray(const QByteArray &rawData, const QByteArray &privKey) { return res.remove(res.lastIndexOf(ENDLINE), res.size()); } +bool QRSAEncryption::testKeyPair(const QByteArray &pubKey, const QByteArray &privKey) { + QByteArray tesVal = "Test message of encrypkey"; + bool result = tesVal == decode(encode(tesVal, pubKey), privKey); + + if (!result) { + qWarning() << "(Warning): Testkey Fail, try generate new key pair!"; + } + + return result; +} + QRSAEncryption::QRSAEncryption() { } @@ -296,11 +303,11 @@ QByteArray QRSAEncryption::encode(const QByteArray &rawData, const QByteArray &p switch (pubKey.size()) { case RSA_64 / 4: { - return encodeArray(rawData, pubKey); + return encodeArray(rawData, pubKey); } case RSA_128 / 4: { - return encodeArray(rawData, pubKey); + return encodeArray(rawData, pubKey); } default: return QByteArray(); @@ -312,11 +319,11 @@ QByteArray QRSAEncryption::decode(const QByteArray &rawData, const QByteArray &p switch (privKey.size()) { case RSA_64 / 4: { - return decodeArray(rawData, privKey); + return decodeArray(rawData, privKey); } case RSA_128 / 4: { - return decodeArray(rawData, privKey); + return decodeArray(rawData, privKey); } default: return QByteArray(); @@ -346,25 +353,31 @@ bool QRSAEncryption::generatePairKey(QByteArray &pubKey, QByteArray &privKey, QRSAEncryption::Rsa rsa) { - pubKey.clear(); - privKey.clear(); + do { - switch (rsa) { - case RSA_64: { - if (!keyGenerator(pubKey, privKey)) { - return false; + pubKey.clear(); + privKey.clear(); + + switch (rsa) { + case RSA_64: { + + if (!keyGenerator(pubKey, privKey)) { + return false; + } + + + break; } - break; - } - case RSA_128: { - if (!keyGenerator(pubKey, privKey)) { - return false; + case RSA_128: { + if (!keyGenerator(pubKey, privKey)) { + return false; + } + break; } - break; - } + } - } + } while (!testKeyPair(pubKey, privKey)); return true; diff --git a/src/Qt-RSA/qrsaencryption.h b/src/Qt-RSA/qrsaencryption.h index 5629410..7f759df 100644 --- a/src/Qt-RSA/qrsaencryption.h +++ b/src/Qt-RSA/qrsaencryption.h @@ -8,6 +8,8 @@ class QRSAEncryption { +private: + bool testKeyPair(const QByteArray &pubKey, const QByteArray &privKey); public: enum Rsa { diff --git a/src/Qt-Secret.pro b/src/Qt-Secret.pro index 3dfa269..7d83aac 100644 --- a/src/Qt-Secret.pro +++ b/src/Qt-Secret.pro @@ -24,7 +24,7 @@ CONFIG(release, debug|release): { #include($$PWD/../uint256_t/uint256.pri) -VERSION = 0.0.0.1 +VERSION = 0.1.0 HEADERS += \ Qt-AES/qaesencryption.h \ diff --git a/test.pri b/test.pri index f21c333..5e4e406 100644 --- a/test.pri +++ b/test.pri @@ -1,5 +1,5 @@ -unix:exec = $$PWD/Stests/build/release/Qt-SecretTest -win32:exec = $$PWD/Stests/build/release/Qt-SecretTest.exe +unix:exec = $$PWD/tests/build/release/Qt-SecretTest,$$PWD/src/Qt-AES/build/release/QAESEncryption +win32:exec = $$PWD/tests/build/release/Qt-SecretTest.exe,$$PWD/src/Qt-AES/build/release/QAESEncryption.exe contains(QMAKE_HOST.os, Linux):{ @@ -8,12 +8,21 @@ contains(QMAKE_HOST.os, Linux):{ DEPLOYER=%cqtdeployer% } + deployTest.commands = $$DEPLOYER -bin $$exec clear -qmake $$QMAKE_QMAKE -targetDir $$PWD/deployTests -libDir $$PWD -recursiveDepth 5 -test.depends = deployTest -unix:test.commands = $$PWD/deployTests/serverTests.sh -win32:test.commands = $$PWD/deployTests/serverTests.exe +unix:testRSA.commands = $$PWD/deployTests/Qt-SecretTest.sh +win32:testRSA.commands = $$PWD/deployTests/Qt-SecretTest.exe + +unix:testAES.commands = $$PWD/deployTests/QAESEncryption.sh +win32:testAES.commands = $$PWD/deployTests/QAESEncryption.exe + +test.depends += deployTest +test.depends += testRSA +test.depends += testAES QMAKE_EXTRA_TARGETS += \ deployTest \ + testAES \ + testRSA \ test diff --git a/tests/main.cpp b/tests/main.cpp index 3109eee..a4da5ae 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -4,11 +4,14 @@ #include #include +const int testSize = 100; + + QByteArray randomArray() { srand(static_cast(time(nullptr))); QByteArray res; - int length = rand() % 1024 * 1024; + int length = rand() % 124 * 1; for (int i = 0; i < length; ++i) { res.push_back(static_cast(rand() % 0xFF)); @@ -22,9 +25,13 @@ bool testCrypto(QRSAEncryption::Rsa rsa) { QRSAEncryption e; - for (int i = 0; i < 100; i++) { + for (int i = 0; i < testSize; i++) { e.generatePairKey(pub, priv, rsa); + qInfo() << QString("Test keys (%0/%1):").arg(i).arg(testSize); + qInfo() << QString("Private key: %0").arg(QString(priv.toHex())); + qInfo() << QString("Public key: %0").arg(QString(pub.toHex())); + if (pub.size() != rsa / 4) { qCritical() << "pubKey size wrong RSA" << rsa; return false; @@ -36,19 +43,23 @@ bool testCrypto(QRSAEncryption::Rsa rsa) { return false; } - for (int i = 0; i < 100; i++) { + for (int i = 0; i < testSize; i++) { auto base = randomArray(); - if ( base != e.decode(e.encode(base, pub), priv)) { - qCritical() << "encode decode data error RSA" << rsa; + auto encodeData = e.encode(base, pub); + auto decodeData = e.decode(encodeData, priv); + + if ( base != decodeData) { + qCritical() << "encode/decode data error RSA" << rsa; return false; } - if (!e.checkSignMessage(e.signMessage(base, pub), priv)) { + encodeData = e.signMessage(base, priv); + + if (!e.checkSignMessage(encodeData, pub)) { qCritical() << "sig message error RSA" << rsa; return false; } - } }