diff --git a/.gitmodules b/.gitmodules index c987653..8d73891 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,3 @@ -[submodule "uint256_t"] - path = uint256_t - url = git@github.com:QuasarApp/uint256_t.git [submodule "src/Qt-AES"] path = src/Qt-AES url = git@github.com:QuasarApp/Qt-AES.git diff --git a/src/Qt-RSA/qrsaencryption.cpp b/src/Qt-RSA/qrsaencryption.cpp index afaad9a..65659d2 100644 --- a/src/Qt-RSA/qrsaencryption.cpp +++ b/src/Qt-RSA/qrsaencryption.cpp @@ -170,15 +170,22 @@ QByteArray toArray(INT i, short sizeBlok = -1) { if (sizeBlok < 0) { return res; } + + while (res.rbegin() != res.rend() && !*res.rbegin()) { + res.remove(res.size() -1, 1); + } + return res.left(sizeBlok); } template INT fromArray(const QByteArray& array) { + INT res = 0; - return *(reinterpret_cast(const_cast(array.data()))); - - return 0; + memcpy(&res, array.data(), + static_cast(std::min(array.size(), + static_cast(sizeof(INT))))); + return res; } template @@ -200,16 +207,12 @@ bool keyGenerator(QByteArray &pubKey, } while((!isMutuallyPrime(eilor, e))); - INT d = 0; - - d = ExtEuclid(eilor , e); + INT d = ExtEuclid(eilor , e);; while(d < 0 ) { d += eilor; } - - pubKey.append(toArray(e)); pubKey.append(toArray(modul)); privKey.append(toArray(d)); @@ -239,7 +242,7 @@ QByteArray decodeBlok(const INT& block, const INT &d, const INT &m) { } template -QByteArray encodeArray(const QByteArray &rawData, const QByteArray &pubKey) { +QByteArray encodeArray(QByteArray rawData, const QByteArray &pubKey) { int index = 0; QByteArray block; @@ -255,14 +258,10 @@ QByteArray encodeArray(const QByteArray &rawData, const QByteArray &pubKey) { QByteArray res; + rawData.append(ENDLINE); + while ((block = rawData.mid(index, blockSize)).size()) { - auto i = fromArray(block); - QByteArray j = toArray(i, blockSize); - - auto i2 = fromArray(j); - auto j2 = toArray(i2, blockSize); - res.append(encodeBlok(fromArray(block), e, m)); index += blockSize; } @@ -286,8 +285,7 @@ QByteArray decodeArray(const QByteArray &rawData, const QByteArray &privKey) { res.append(decodeBlok(fromArray(block), d, m)); index += blockSize; } - - return res; + return res.remove(res.lastIndexOf(ENDLINE), res.size()); } QRSAEncryption::QRSAEncryption() { @@ -325,10 +323,31 @@ QByteArray QRSAEncryption::decode(const QByteArray &rawData, const QByteArray &p } } +QByteArray QRSAEncryption::signMessage(QByteArray rawData, const QByteArray &privKey) { + auto msg = encode(rawData, privKey); + int size = rawData.size(); + rawData.insert(0, reinterpret_cast(&size), sizeof (int)); + rawData.append(msg); + + return rawData; +} + +bool QRSAEncryption::checkSignMessage(const QByteArray &rawData, const QByteArray &pubKey) { + int mSize = 0; + memcpy(&mSize, rawData.left(sizeof (int)), sizeof (int)); + + auto message = rawData.mid(sizeof (int), mSize); + auto sig = rawData.mid(mSize + static_cast(sizeof (int))); + + return message == decode(sig, pubKey); +} + bool QRSAEncryption::generatePairKey(QByteArray &pubKey, QByteArray &privKey, - QRSAEncryption::Rsa rsa) -{ + QRSAEncryption::Rsa rsa) { + + pubKey.clear(); + privKey.clear(); switch (rsa) { case RSA_64: { diff --git a/src/Qt-RSA/qrsaencryption.h b/src/Qt-RSA/qrsaencryption.h index d3a2c36..5629410 100644 --- a/src/Qt-RSA/qrsaencryption.h +++ b/src/Qt-RSA/qrsaencryption.h @@ -4,20 +4,25 @@ #include #include +#define ENDLINE "#_end_#" + class QRSAEncryption { public: enum Rsa { - RSA_64 = 64, // long time of key generate : 1 ms in processor (2.0 KHz) - RSA_128 = 128, // long time of key generate : 5 s in processor (2.0 KHz) -// RSA_256 = 256, // long time of key generate : 1 m in processor (2.0 KHz) + RSA_64 = 64, + RSA_128 = 128 }; QRSAEncryption(); QByteArray encode(const QByteArray &rawData, const QByteArray &pubKey); QByteArray decode(const QByteArray &rawData, const QByteArray &privKey); + + QByteArray signMessage(QByteArray rawData, const QByteArray &privKey); + bool checkSignMessage(const QByteArray &rawData, const QByteArray &pubKey); + bool generatePairKey(QByteArray &pubKey, QByteArray &privKey, Rsa = RSA_128); }; diff --git a/tests/main.cpp b/tests/main.cpp index 3a60bd6..3109eee 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -2,31 +2,71 @@ #include #include #include +#include + +QByteArray randomArray() { + srand(static_cast(time(nullptr))); + QByteArray res; + + int length = rand() % 1024 * 1024; + + for (int i = 0; i < length; ++i) { + res.push_back(static_cast(rand() % 0xFF)); + } + + return res; +} + +bool testCrypto(QRSAEncryption::Rsa rsa) { + QByteArray pub, priv; + QRSAEncryption e; + + + for (int i = 0; i < 100; i++) { + e.generatePairKey(pub, priv, rsa); + + if (pub.size() != rsa / 4) { + qCritical() << "pubKey size wrong RSA" << rsa; + return false; + } + + + if (priv.size() != rsa / 4) { + qCritical() << "privKey size wrong RSA" << rsa; + return false; + } + + for (int i = 0; i < 100; i++) { + auto base = randomArray(); + + if ( base != e.decode(e.encode(base, pub), priv)) { + qCritical() << "encode decode data error RSA" << rsa; + return false; + } + + if (!e.checkSignMessage(e.signMessage(base, pub), priv)) { + qCritical() << "sig message error RSA" << rsa; + return false; + } + + } + } + + return true; +} + int main() { + if(!testCrypto(QRSAEncryption::Rsa::RSA_64)) { + return 1; + } - QRSAEncryption e; + if(!testCrypto(QRSAEncryption::Rsa::RSA_128)) { + return 1; + } - QByteArray pub, priv; + qInfo() << "Tests passed successfully"; - auto t = QDateTime::currentMSecsSinceEpoch(); - e.generatePairKey(pub, priv, QRSAEncryption::Rsa::RSA_64); - - QByteArray message = "L"; - - auto data = e.encode(message, pub); - - auto res = e.decode(data, priv); - qDebug() << "key generate time RSA_64: " << (QDateTime::currentMSecsSinceEpoch() - t); - - t = QDateTime::currentMSecsSinceEpoch(); - e.generatePairKey(pub, priv, QRSAEncryption::Rsa::RSA_128); - - qDebug() << "key generate time RSA_128: " << (QDateTime::currentMSecsSinceEpoch() - t); - - auto data2 = e.encode(message, pub); - - auto res2 = e.decode(data2, priv); return 0; } diff --git a/uint256_t b/uint256_t deleted file mode 160000 index a176a96..0000000 --- a/uint256_t +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a176a96ab181388d19c3415ba0225a645da43221