SymmetricCipher: Support CTR mode

Includes AES-256-CTR non-stream tests
This commit is contained in:
Toni Spets 2017-10-29 17:07:01 +02:00
parent 8625e2c051
commit a81a5fa31b
4 changed files with 53 additions and 1 deletions

View File

@ -38,6 +38,7 @@ public:
enum Mode enum Mode
{ {
Cbc, Cbc,
Ctr,
Ecb, Ecb,
Stream Stream
}; };

View File

@ -62,6 +62,9 @@ int SymmetricCipherGcrypt::gcryptMode(SymmetricCipher::Mode mode)
case SymmetricCipher::Cbc: case SymmetricCipher::Cbc:
return GCRY_CIPHER_MODE_CBC; return GCRY_CIPHER_MODE_CBC;
case SymmetricCipher::Ctr:
return GCRY_CIPHER_MODE_CTR;
case SymmetricCipher::Stream: case SymmetricCipher::Stream:
return GCRY_CIPHER_MODE_STREAM; return GCRY_CIPHER_MODE_STREAM;
@ -119,7 +122,13 @@ bool SymmetricCipherGcrypt::setKey(const QByteArray& key)
bool SymmetricCipherGcrypt::setIv(const QByteArray& iv) bool SymmetricCipherGcrypt::setIv(const QByteArray& iv)
{ {
m_iv = 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) { if (error != 0) {
setErrorString(error); setErrorString(error);

View File

@ -124,6 +124,46 @@ void TestSymmetricCipher::testAes256CbcDecryption()
plainText); 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() void TestSymmetricCipher::testTwofish256CbcEncryption()
{ {
// NIST MCT Known-Answer Tests (cbc_e_m.txt) // NIST MCT Known-Answer Tests (cbc_e_m.txt)

View File

@ -29,6 +29,8 @@ private slots:
void initTestCase(); void initTestCase();
void testAes256CbcEncryption(); void testAes256CbcEncryption();
void testAes256CbcDecryption(); void testAes256CbcDecryption();
void testAes256CtrEncryption();
void testAes256CtrDecryption();
void testTwofish256CbcEncryption(); void testTwofish256CbcEncryption();
void testTwofish256CbcDecryption(); void testTwofish256CbcDecryption();
void testSalsa20(); void testSalsa20();