2010-09-13 17:24:36 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 or (at your option)
|
|
|
|
* version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CompositeKey.h"
|
|
|
|
|
|
|
|
#include "crypto/CryptoHash.h"
|
|
|
|
#include "crypto/SymmetricCipher.h"
|
|
|
|
|
2011-11-11 19:49:19 -05:00
|
|
|
#include <QtCore/QtConcurrentRun>
|
|
|
|
|
2011-11-13 08:52:43 -05:00
|
|
|
CompositeKey::CompositeKey()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositeKey::CompositeKey(const CompositeKey& key)
|
|
|
|
{
|
|
|
|
*this = key;
|
|
|
|
}
|
|
|
|
|
2010-09-13 17:24:36 -04:00
|
|
|
CompositeKey::~CompositeKey()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_keys);
|
|
|
|
}
|
|
|
|
|
2012-01-11 17:59:50 -05:00
|
|
|
void CompositeKey::clear()
|
|
|
|
{
|
|
|
|
qDeleteAll(m_keys);
|
|
|
|
m_keys.clear();
|
|
|
|
}
|
|
|
|
|
2010-09-13 17:24:36 -04:00
|
|
|
CompositeKey* CompositeKey::clone() const
|
|
|
|
{
|
|
|
|
return new CompositeKey(*this);
|
|
|
|
}
|
|
|
|
|
2011-11-13 08:52:43 -05:00
|
|
|
CompositeKey& CompositeKey::operator=(const CompositeKey& key)
|
|
|
|
{
|
|
|
|
Q_FOREACH (Key* subKey, key.m_keys) {
|
|
|
|
m_keys.append(subKey->clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
2010-09-13 17:24:36 -04:00
|
|
|
|
|
|
|
QByteArray CompositeKey::rawKey() const
|
|
|
|
{
|
|
|
|
CryptoHash cryptoHash(CryptoHash::Sha256);
|
|
|
|
|
|
|
|
Q_FOREACH (Key* key, m_keys) {
|
|
|
|
cryptoHash.addData(key->rawKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
return cryptoHash.result();
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray CompositeKey::transform(const QByteArray& seed, int rounds) const
|
|
|
|
{
|
2011-11-13 08:52:43 -05:00
|
|
|
Q_ASSERT(seed.size() == 32);
|
|
|
|
Q_ASSERT(rounds > 0);
|
|
|
|
|
2010-09-13 17:24:36 -04:00
|
|
|
QByteArray key = rawKey();
|
|
|
|
|
2011-11-11 19:49:19 -05:00
|
|
|
QFuture<QByteArray> future1 = QtConcurrent::run(transformKeyRaw, key.left(16), seed, rounds);
|
|
|
|
QFuture<QByteArray> future2 = QtConcurrent::run(transformKeyRaw, key.right(16), seed, rounds);
|
2010-09-13 17:24:36 -04:00
|
|
|
|
|
|
|
QByteArray transformed;
|
2011-11-11 19:49:19 -05:00
|
|
|
transformed.append(future1.result());
|
|
|
|
transformed.append(future2.result());
|
2010-09-13 17:24:36 -04:00
|
|
|
|
|
|
|
return CryptoHash::hash(transformed, CryptoHash::Sha256);
|
|
|
|
}
|
|
|
|
|
2011-11-11 19:49:19 -05:00
|
|
|
QByteArray CompositeKey::transformKeyRaw(const QByteArray& key, const QByteArray& seed, int rounds) {
|
2010-09-13 17:24:36 -04:00
|
|
|
QByteArray iv(16, 0);
|
2011-11-11 19:49:19 -05:00
|
|
|
SymmetricCipher cipher(SymmetricCipher::Aes256, SymmetricCipher::Ecb, SymmetricCipher::Encrypt, seed, iv);
|
2010-09-13 17:24:36 -04:00
|
|
|
|
2011-11-11 19:49:19 -05:00
|
|
|
QByteArray result = key;
|
|
|
|
|
|
|
|
for (int i=0; i<rounds; i++) {
|
|
|
|
cipher.processInPlace(result);
|
2010-09-13 17:24:36 -04:00
|
|
|
}
|
2011-11-11 19:49:19 -05:00
|
|
|
|
|
|
|
return result;
|
2010-09-13 17:24:36 -04:00
|
|
|
}
|
|
|
|
|
2011-11-11 19:49:19 -05:00
|
|
|
void CompositeKey::addKey(const Key& key)
|
2010-09-13 17:24:36 -04:00
|
|
|
{
|
2011-11-11 19:49:19 -05:00
|
|
|
m_keys.append(key.clone());
|
2010-09-13 17:24:36 -04:00
|
|
|
}
|