Catch and handle all errors from libgcrypt.

This commit is contained in:
Felix Geyer
2015-05-09 19:47:53 +02:00
parent a7f4e2d0cd
commit a762cef0a9
29 changed files with 622 additions and 194 deletions

View File

@@ -39,8 +39,8 @@ void TestKeePass2RandomStream::test()
const int Size = 128;
SymmetricCipher cipher(SymmetricCipher::Salsa20, SymmetricCipher::Stream, SymmetricCipher::Encrypt,
CryptoHash::hash(key, CryptoHash::Sha256), KeePass2::INNER_STREAM_SALSA20_IV);
SymmetricCipher cipher(SymmetricCipher::Salsa20, SymmetricCipher::Stream, SymmetricCipher::Encrypt);
QVERIFY(cipher.init(CryptoHash::hash(key, CryptoHash::Sha256), KeePass2::INNER_STREAM_SALSA20_IV));
const QByteArray data(QByteArray::fromHex("601ec313775789a5b7a7f504bbf3d228f443e3ca4d62b59aca84e990cacaf5c5"
"2b0930daa23de94ce87017ba2d84988ddfc9c58db67aada613c2dd08457941a6"
@@ -59,20 +59,27 @@ void TestKeePass2RandomStream::test()
}
KeePass2RandomStream randomStream(key);
KeePass2RandomStream randomStream;
bool ok;
QVERIFY(randomStream.init(key));
QByteArray randomStreamData;
randomStreamData.append(randomStream.process(data.mid(0, 7)));
randomStreamData.append(randomStream.process(data.mid(7, 1)));
randomStreamData.append(randomStream.process(data.mid(0, 7), &ok));
QVERIFY(ok);
randomStreamData.append(randomStream.process(data.mid(7, 1), &ok));
QVERIFY(ok);
QByteArray tmpData = data.mid(8, 12);
randomStream.processInPlace(tmpData);
randomStreamData.append(tmpData);
randomStreamData.append(randomStream.process(data.mid(20, 44)));
randomStreamData.append(randomStream.process(data.mid(64, 64)));
randomStreamData.append(randomStream.process(data.mid(20, 44), &ok));
QVERIFY(ok);
randomStreamData.append(randomStream.process(data.mid(64, 64), &ok));
QVERIFY(ok);
SymmetricCipher cipherEncrypt(SymmetricCipher::Salsa20, SymmetricCipher::Stream, SymmetricCipher::Encrypt,
CryptoHash::hash(key, CryptoHash::Sha256), KeePass2::INNER_STREAM_SALSA20_IV);
QByteArray cipherDataEncrypt = cipherEncrypt.process(data);
SymmetricCipher cipherEncrypt(SymmetricCipher::Salsa20, SymmetricCipher::Stream, SymmetricCipher::Encrypt);
QVERIFY(cipherEncrypt.init(CryptoHash::hash(key, CryptoHash::Sha256), KeePass2::INNER_STREAM_SALSA20_IV));
QByteArray cipherDataEncrypt = cipherEncrypt.process(data, &ok);
QVERIFY(ok);
QCOMPARE(randomStreamData.size(), Size);

View File

@@ -43,6 +43,8 @@ void TestKeys::testComposite()
CompositeKey* compositeKey1 = new CompositeKey();
PasswordKey* passwordKey1 = new PasswordKey();
PasswordKey* passwordKey2 = new PasswordKey("test");
bool ok;
QString errorString;
// make sure that addKey() creates a copy of the keys
compositeKey1->addKey(*passwordKey1);
@@ -50,13 +52,15 @@ void TestKeys::testComposite()
delete passwordKey1;
delete passwordKey2;
QByteArray transformed = compositeKey1->transform(QByteArray(32, '\0'), 1);
QByteArray transformed = compositeKey1->transform(QByteArray(32, '\0'), 1, &ok, &errorString);
QVERIFY(ok);
QCOMPARE(transformed.size(), 32);
// make sure the subkeys are copied
CompositeKey* compositeKey2 = compositeKey1->clone();
delete compositeKey1;
QCOMPARE(compositeKey2->transform(QByteArray(32, '\0'), 1), transformed);
QCOMPARE(compositeKey2->transform(QByteArray(32, '\0'), 1, &ok, &errorString), transformed);
QVERIFY(ok);
delete compositeKey2;
CompositeKey* compositeKey3 = new CompositeKey();
@@ -130,7 +134,7 @@ void TestKeys::testCreateFileKey()
compositeKey.addKey(fileKey);
Database* dbOrg = new Database();
dbOrg->setKey(compositeKey);
QVERIFY(dbOrg->setKey(compositeKey));
dbOrg->metadata()->setName(dbName);
QBuffer dbBuffer;
@@ -182,7 +186,10 @@ void TestKeys::benchmarkTransformKey()
QByteArray seed(32, '\x4B');
bool ok;
QString errorString;
QBENCHMARK {
compositeKey.transform(seed, 1e6);
compositeKey.transform(seed, 1e6, &ok, &errorString);
}
}

View File

@@ -42,17 +42,20 @@ void TestSymmetricCipher::testAes256CbcEncryption()
plainText.append(QByteArray::fromHex("ae2d8a571e03ac9c9eb76fac45af8e51"));
QByteArray cipherText = QByteArray::fromHex("f58c4c04d6e5f1ba779eabfb5f7bfbd6");
cipherText.append(QByteArray::fromHex("9cfc4e967edb808d679f777bc6702c7d"));
bool ok;
SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Encrypt,
key, iv);
SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Encrypt);
QVERIFY(cipher.init(key, iv));
QCOMPARE(cipher.blockSize(), 16);
QCOMPARE(cipher.process(plainText),
QCOMPARE(cipher.process(plainText, &ok),
cipherText);
QVERIFY(ok);
QBuffer buffer;
SymmetricCipherStream stream(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc,
SymmetricCipher::Encrypt, key, iv);
SymmetricCipher::Encrypt);
QVERIFY(stream.init(key, iv));
buffer.open(QIODevice::WriteOnly);
stream.open(QIODevice::WriteOnly);
QVERIFY(stream.reset());
@@ -86,18 +89,22 @@ void TestSymmetricCipher::testAes256CbcDecryption()
cipherText.append(QByteArray::fromHex("9cfc4e967edb808d679f777bc6702c7d"));
QByteArray plainText = QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
plainText.append(QByteArray::fromHex("ae2d8a571e03ac9c9eb76fac45af8e51"));
bool ok;
SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Decrypt, key, iv);
SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Cbc, SymmetricCipher::Decrypt);
QVERIFY(cipher.init(key, iv));
QCOMPARE(cipher.blockSize(), 16);
QCOMPARE(cipher.process(cipherText),
QCOMPARE(cipher.process(cipherText, &ok),
plainText);
QVERIFY(ok);
// padded with 16 0x16 bytes
QByteArray cipherTextPadded = cipherText + QByteArray::fromHex("3a3aa5e0213db1a9901f9036cf5102d2");
QBuffer buffer(&cipherTextPadded);
SymmetricCipherStream stream(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc,
SymmetricCipher::Decrypt, key, iv);
SymmetricCipher::Decrypt);
QVERIFY(stream.init(key, iv));
buffer.open(QIODevice::ReadOnly);
stream.open(QIODevice::ReadOnly);
@@ -123,16 +130,20 @@ void TestSymmetricCipher::testSalsa20()
QByteArray key = QByteArray::fromHex("F3F4F5F6F7F8F9FAFBFCFDFEFF000102030405060708090A0B0C0D0E0F101112");
QByteArray iv = QByteArray::fromHex("0000000000000000");
bool ok;
SymmetricCipher cipher(SymmetricCipher::Salsa20, SymmetricCipher::Stream, SymmetricCipher::Encrypt, key, iv);
SymmetricCipher cipher(SymmetricCipher::Salsa20, SymmetricCipher::Stream, SymmetricCipher::Encrypt);
QVERIFY(cipher.init(key, iv));
QByteArray cipherTextA;
for (int i = 0; i < 8; i++) {
cipherTextA.append(cipher.process(QByteArray(64, '\0')));
cipherTextA.append(cipher.process(QByteArray(64, '\0'), &ok));
QVERIFY(ok);
}
cipher.reset();
QByteArray cipherTextB = cipher.process(QByteArray(512, '\0'));
QByteArray cipherTextB = cipher.process(QByteArray(512, '\0'), &ok);
QVERIFY(ok);
cipher.reset();
QByteArray expectedCipherText1;
@@ -180,7 +191,8 @@ void TestSymmetricCipher::testPadding()
buffer.open(QIODevice::ReadWrite);
SymmetricCipherStream streamEnc(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc,
SymmetricCipher::Encrypt, key, iv);
SymmetricCipher::Encrypt);
QVERIFY(streamEnc.init(key, iv));
streamEnc.open(QIODevice::WriteOnly);
streamEnc.write(plainText);
streamEnc.close();
@@ -189,7 +201,8 @@ void TestSymmetricCipher::testPadding()
QCOMPARE(buffer.buffer().size(), 16);
SymmetricCipherStream streamDec(&buffer, SymmetricCipher::Aes256, SymmetricCipher::Cbc,
SymmetricCipher::Decrypt, key, iv);
SymmetricCipher::Decrypt);
QVERIFY(streamDec.init(key, iv));
streamDec.open(QIODevice::ReadOnly);
QByteArray decrypted = streamDec.readAll();
QCOMPARE(decrypted, plainText);