Merge pull request #9 from bricke/OFB_dev

Ofb dev
This commit is contained in:
Matteo Brichese 2018-04-05 16:09:41 -07:00 committed by GitHub
commit e5f942d924
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 52 deletions

View File

@ -323,7 +323,8 @@ QByteArray QAESEncryption::byteXor(const QByteArray &a, const QByteArray &b)
QByteArray::const_iterator it_b = b.begin();
QByteArray ret;
for(int i = 0; i < m_blocklen; i++)
//for(int i = 0; i < m_blocklen; i++)
for(int i = 0; i < std::min(a.size(), b.size()); i++)
ret.insert(i,it_a[i] ^ it_b[i]);
return ret;
@ -395,36 +396,44 @@ QByteArray QAESEncryption::encode(const QByteArray &rawText, const QByteArray &k
QByteArray ret;
QByteArray expandedKey = expandKey(key);
QByteArray alignedText(rawText);
QByteArray ivTemp(iv);
//Fill array with padding
alignedText.append(getPadding(rawText.size(), m_blocklen));
//Preparation for CFB
if (m_mode == CFB)
ret.append(byteXor(alignedText.mid(0, m_blocklen), cipher(expandedKey, iv)));
//Looping thru all blocks
for(int i=0; i < alignedText.size(); i+= m_blocklen){
switch(m_mode)
{
case ECB:
switch(m_mode)
{
case ECB:
for(int i=0; i < alignedText.size(); i+= m_blocklen)
ret.append(cipher(expandedKey, alignedText.mid(i, m_blocklen)));
break;
case CBC:
alignedText.replace(i, m_blocklen, byteXor(alignedText.mid(i, m_blocklen),ivTemp));
ret.append(cipher(expandedKey, alignedText.mid(i, m_blocklen)));
ivTemp = ret.mid(i, m_blocklen);
break;
case CFB:
if (i+m_blocklen < alignedText.size())
ret.append(byteXor(alignedText.mid(i+m_blocklen, m_blocklen),
cipher(expandedKey, ret.mid(i, m_blocklen))));
break;
default:
//do nothing
break;
break;
case CBC: {
QByteArray ivTemp(iv);
for(int i=0; i < alignedText.size(); i+= m_blocklen) {
alignedText.replace(i, m_blocklen, byteXor(alignedText.mid(i, m_blocklen),ivTemp));
ret.append(cipher(expandedKey, alignedText.mid(i, m_blocklen)));
ivTemp = ret.mid(i, m_blocklen);
}
}
break;
case CFB: {
ret.append(byteXor(alignedText.left(m_blocklen), cipher(expandedKey, iv)));
for(int i=0; i < alignedText.size(); i+= m_blocklen) {
if (i+m_blocklen < alignedText.size())
ret.append(byteXor(alignedText.mid(i+m_blocklen, m_blocklen),
cipher(expandedKey, ret.mid(i, m_blocklen))));
}
}
break;
case OFB: {
QByteArray ofbTemp;
ofbTemp.append(cipher(expandedKey, iv));
for (int i=m_blocklen; i < alignedText.size(); i += m_blocklen){
ofbTemp.append(cipher(expandedKey, ofbTemp.right(m_blocklen)));
}
ret.append(byteXor(alignedText, ofbTemp));
}
break;
default: break;
}
return ret;
}
@ -436,33 +445,44 @@ QByteArray QAESEncryption::decode(const QByteArray &rawText, const QByteArray &k
QByteArray ret;
QByteArray expandedKey = expandKey(key);
QByteArray ivTemp(iv);
//Preparation for CFB
if (m_mode == CFB)
ret.append(byteXor(rawText.mid(0, m_blocklen), cipher(expandedKey, iv)));
for(int i=0; i < rawText.size(); i+= m_blocklen){
switch(m_mode)
{
case ECB:
switch(m_mode)
{
case ECB:
for(int i=0; i < rawText.size(); i+= m_blocklen)
ret.append(invCipher(expandedKey, rawText.mid(i, m_blocklen)));
break;
case CBC:
ret.append(invCipher(expandedKey, rawText.mid(i, m_blocklen)));
ret.replace(i, m_blocklen, byteXor(ret.mid(i, m_blocklen),ivTemp));
ivTemp = rawText.mid(i, m_blocklen);
break;
case CFB:
if (i+m_blocklen < rawText.size()){
ret.append(byteXor(rawText.mid(i+m_blocklen, m_blocklen),
cipher(expandedKey, rawText.mid(i, m_blocklen))));
break;
case CBC: {
QByteArray ivTemp(iv);
for(int i=0; i < rawText.size(); i+= m_blocklen){
ret.append(invCipher(expandedKey, rawText.mid(i, m_blocklen)));
ret.replace(i, m_blocklen, byteXor(ret.mid(i, m_blocklen),ivTemp));
ivTemp = rawText.mid(i, m_blocklen);
}
break;
default:
//do nothing
break;
}
break;
case CFB: {
ret.append(byteXor(rawText.mid(0, m_blocklen), cipher(expandedKey, iv)));
for(int i=0; i < rawText.size(); i+= m_blocklen){
if (i+m_blocklen < rawText.size()) {
ret.append(byteXor(rawText.mid(i+m_blocklen, m_blocklen),
cipher(expandedKey, rawText.mid(i, m_blocklen))));
}
}
}
break;
case OFB: {
QByteArray ofbTemp;
ofbTemp.append(cipher(expandedKey, iv));
for (int i=m_blocklen; i < rawText.size(); i += m_blocklen){
ofbTemp.append(cipher(expandedKey, ofbTemp.right(m_blocklen)));
}
ret.append(byteXor(rawText, ofbTemp));
}
break;
default:
//do nothing
break;
}
return ret;
}

View File

@ -17,7 +17,8 @@ public:
enum Mode {
ECB,
CBC,
CFB
CFB,
OFB
};
enum Padding {
@ -132,9 +133,9 @@ private:
// The round constant word array, Rcon[i], contains the values given by
// x to th e power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8)
//Only the first 14 elements are needed
// Only the first 14 elements are needed
const quint8 Rcon[256] = {
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, /*0x4d, 0x9a,
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab/*, 0x4d, 0x9a,
0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, 0x39,
0x72, 0xe4, 0xd3, 0xbd, 0x61, 0xc2, 0x9f, 0x25, 0x4a, 0x94, 0x33, 0x66, 0xcc, 0x83, 0x1d, 0x3a,
0x74, 0xe8, 0xcb, 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8,

View File

@ -1,5 +1,6 @@
#include "aestest.h"
#include <QDebug>
#include <QByteArray>
#include <QCryptographicHash>
#include <QFile>
@ -139,7 +140,7 @@ void AesTest::CBC128Decrypt()
void AesTest::CFB256String()
{
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::CFB);
QAESEncryption encryption(QAESEncryption::AES_256, QAESEncryption::CFB, QAESEncryption::PKCS7);
QString inputStr("The Advanced Encryption Standard (AES), also known by its original name Rijndael "
"is a specification for the encryption of electronic data established by the U.S. "
@ -171,3 +172,19 @@ void AesTest::CFB256LongText()
QByteArray decodedText = encryption.removePadding(encryption.decode(encodeText, hashKey, iv));
QCOMPARE(decodedText, input);
}
void AesTest::OFB256String()
{
QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::OFB, QAESEncryption::PKCS7);
QString inputStr("The Advanced Encryption Standard (AES), also known by its original name Rijndael "
"is a specification for the encryption of electronic data established by the U.S. "
"National Institute of Standards and Technology (NIST) in 2001");
QString key("123456789123");
QByteArray hashKey = QCryptographicHash::hash(key.toLocal8Bit(), QCryptographicHash::Sha256);
QByteArray encodeText = encryption.encode(inputStr.toLocal8Bit(), hashKey, iv);
QByteArray decodedText = encryption.removePadding(encryption.decode(encodeText, hashKey, iv));
QCOMPARE(inputStr, decodedText);
}

View File

@ -29,6 +29,8 @@ private slots:
void CFB256LongText();
void OFB256String();
void cleanupTestCase(){}
private: