Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToni Spets <toni.spets@iki.fi>2017-10-29 18:07:01 +0300
committerToni Spets <toni.spets@iki.fi>2017-11-19 15:38:59 +0300
commita81a5fa31bb50b00b7091086013952fb0332fca1 (patch)
treeb703ab671662bfb072dadd67466da8caf1611515
parent8625e2c0514e4336d1ef91ed932bd940dd98b6c8 (diff)
SymmetricCipher: Support CTR mode
Includes AES-256-CTR non-stream tests
-rw-r--r--src/crypto/SymmetricCipher.h1
-rw-r--r--src/crypto/SymmetricCipherGcrypt.cpp11
-rw-r--r--tests/TestSymmetricCipher.cpp40
-rw-r--r--tests/TestSymmetricCipher.h2
4 files changed, 53 insertions, 1 deletions
diff --git a/src/crypto/SymmetricCipher.h b/src/crypto/SymmetricCipher.h
index b85c58b7c..4f1d50c12 100644
--- a/src/crypto/SymmetricCipher.h
+++ b/src/crypto/SymmetricCipher.h
@@ -38,6 +38,7 @@ public:
enum Mode
{
Cbc,
+ Ctr,
Ecb,
Stream
};
diff --git a/src/crypto/SymmetricCipherGcrypt.cpp b/src/crypto/SymmetricCipherGcrypt.cpp
index e600a7edb..c88dbd8c2 100644
--- a/src/crypto/SymmetricCipherGcrypt.cpp
+++ b/src/crypto/SymmetricCipherGcrypt.cpp
@@ -62,6 +62,9 @@ int SymmetricCipherGcrypt::gcryptMode(SymmetricCipher::Mode mode)
case SymmetricCipher::Cbc:
return GCRY_CIPHER_MODE_CBC;
+ case SymmetricCipher::Ctr:
+ return GCRY_CIPHER_MODE_CTR;
+
case SymmetricCipher::Stream:
return GCRY_CIPHER_MODE_STREAM;
@@ -119,7 +122,13 @@ bool SymmetricCipherGcrypt::setKey(const QByteArray& key)
bool SymmetricCipherGcrypt::setIv(const QByteArray& iv)
{
m_iv = iv;
- gcry_error_t error = gcry_cipher_setiv(m_ctx, m_iv.constData(), m_iv.size());
+ gcry_error_t error;
+
+ if (m_mode == GCRY_CIPHER_MODE_CTR) {
+ error = gcry_cipher_setctr(m_ctx, m_iv.constData(), m_iv.size());
+ } else {
+ error = gcry_cipher_setiv(m_ctx, m_iv.constData(), m_iv.size());
+ }
if (error != 0) {
setErrorString(error);
diff --git a/tests/TestSymmetricCipher.cpp b/tests/TestSymmetricCipher.cpp
index 4f78693d6..74f720a7f 100644
--- a/tests/TestSymmetricCipher.cpp
+++ b/tests/TestSymmetricCipher.cpp
@@ -124,6 +124,46 @@ void TestSymmetricCipher::testAes256CbcDecryption()
plainText);
}
+void TestSymmetricCipher::testAes256CtrEncryption()
+{
+ // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
+
+ QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
+ QByteArray ctr = QByteArray::fromHex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
+ QByteArray plainText = QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
+ plainText.append(QByteArray::fromHex("ae2d8a571e03ac9c9eb76fac45af8e51"));
+ QByteArray cipherText = QByteArray::fromHex("601ec313775789a5b7a7f504bbf3d228");
+ cipherText.append(QByteArray::fromHex("f443e3ca4d62b59aca84e990cacaf5c5"));
+ bool ok;
+
+ SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Ctr, SymmetricCipher::Encrypt);
+ QVERIFY(cipher.init(key, ctr));
+ QCOMPARE(cipher.blockSize(), 16);
+
+ QCOMPARE(cipher.process(plainText, &ok),
+ cipherText);
+ QVERIFY(ok);
+}
+
+void TestSymmetricCipher::testAes256CtrDecryption()
+{
+ QByteArray key = QByteArray::fromHex("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
+ QByteArray ctr = QByteArray::fromHex("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff");
+ QByteArray cipherText = QByteArray::fromHex("601ec313775789a5b7a7f504bbf3d228");
+ cipherText.append(QByteArray::fromHex("f443e3ca4d62b59aca84e990cacaf5c5"));
+ QByteArray plainText = QByteArray::fromHex("6bc1bee22e409f96e93d7e117393172a");
+ plainText.append(QByteArray::fromHex("ae2d8a571e03ac9c9eb76fac45af8e51"));
+ bool ok;
+
+ SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Ctr, SymmetricCipher::Decrypt);
+ QVERIFY(cipher.init(key, ctr));
+ QCOMPARE(cipher.blockSize(), 16);
+
+ QCOMPARE(cipher.process(cipherText, &ok),
+ plainText);
+ QVERIFY(ok);
+}
+
void TestSymmetricCipher::testTwofish256CbcEncryption()
{
// NIST MCT Known-Answer Tests (cbc_e_m.txt)
diff --git a/tests/TestSymmetricCipher.h b/tests/TestSymmetricCipher.h
index 009989500..cad13841a 100644
--- a/tests/TestSymmetricCipher.h
+++ b/tests/TestSymmetricCipher.h
@@ -29,6 +29,8 @@ private slots:
void initTestCase();
void testAes256CbcEncryption();
void testAes256CbcDecryption();
+ void testAes256CtrEncryption();
+ void testAes256CtrDecryption();
void testTwofish256CbcEncryption();
void testTwofish256CbcDecryption();
void testSalsa20();