| 1 | /* |
| 2 | Copyright (c) 2011, Andre Somers |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, are permitted provided that the following conditions are met: |
| 7 | * Redistributions of source code must retain the above copyright |
| 8 | notice, this list of conditions and the following disclaimer. |
| 9 | * Redistributions in binary form must reproduce the above copyright |
| 10 | notice, this list of conditions and the following disclaimer in the |
| 11 | documentation and/or other materials provided with the distribution. |
| 12 | * Neither the name of the Rathenau Instituut, Andre Somers nor the |
| 13 | names of its contributors may be used to endorse or promote products |
| 14 | derived from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL ANDRE SOMERS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR #######; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "3rd-party/sc/simplecrypt.h" |
| 29 | |
| 30 | #include <QByteArray> |
| 31 | #include <QCryptographicHash> |
| 32 | #include <QDataStream> |
| 33 | #include <QDateTime> |
| 34 | #include <QIODevice> |
| 35 | #include <QRandomGenerator> |
| 36 | #include <QtDebug> |
| 37 | #include <QtGlobal> |
| 38 | |
| 39 | SimpleCrypt::SimpleCrypt() |
| 40 | : m_key(0), m_compressionMode(CompressionAlways), m_protectionMode(ProtectionHash), m_lastError(ErrorNoError) {} |
| 41 | |
| 42 | SimpleCrypt::SimpleCrypt(quint64 key) |
| 43 | : m_key(key), m_compressionMode(CompressionAlways), m_protectionMode(ProtectionHash), m_lastError(ErrorNoError) { |
| 44 | splitKey(); |
| 45 | } |
| 46 | |
| 47 | void SimpleCrypt::setKey(quint64 key) { |
| 48 | m_key = key; |
| 49 | splitKey(); |
| 50 | } |
| 51 | |
| 52 | void SimpleCrypt::splitKey() { |
| 53 | m_keyParts.clear(); |
| 54 | m_keyParts.resize(8); |
| 55 | |
| 56 | for (int i = 0; i < 8; i++) { |
| 57 | quint64 part = m_key; |
| 58 | |
| 59 | for (int j = i; j > 0; j--) { |
| 60 | part = part >> 8; |
| 61 | } |
| 62 | |
| 63 | part = part & 0xff; |
| 64 | m_keyParts[i] = static_cast<char>(part); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | QByteArray SimpleCrypt::encryptToByteArray(const QString& plaintext) { |
| 69 | return encryptToByteArray(plaintext.toUtf8()); |
| 70 | } |
| 71 | |
| 72 | QByteArray SimpleCrypt::encryptToByteArray(QByteArray plaintext) { |
| 73 | if (m_keyParts.isEmpty()) { |
| 74 | qWarning() << "No key set."; |
| 75 | m_lastError = ErrorNoKeySet; |
| 76 | return QByteArray(); |
| 77 | } |
| 78 | |
| 79 | QByteArray ba = plaintext; |
| 80 | CryptoFlags flags = CryptoFlagNone; |
| 81 | |
| 82 | if (m_compressionMode == CompressionAlways) { |
| 83 | ba = qCompress(ba, 9); // maximum compression |
| 84 | flags |= CryptoFlagCompression; |
| 85 | } |
| 86 | else if (m_compressionMode == CompressionAuto) { |
| 87 | QByteArray compressed = qCompress(ba, 9); |
| 88 | |
| 89 | if (compressed.size() < ba.size()) { |
| 90 | ba = compressed; |
| 91 | flags |= CryptoFlagCompression; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | QByteArray integrityProtection; |
| 96 | |
| 97 | if (m_protectionMode == ProtectionChecksum) { |
| 98 | flags |= CryptoFlagChecksum; |
| 99 | QDataStream s(&integrityProtection, QIODevice::WriteOnly); |
| 100 | |
| 101 | #if QT_VERSION_MAJOR == 6 |
| 102 | s << qChecksum(ba); |
| 103 | #else |
| 104 | s << qChecksum(ba, ba.length()); |
| 105 | #endif |
| 106 | } |
| 107 | else if (m_protectionMode == ProtectionHash) { |
| 108 | flags |= CryptoFlagHash; |
| 109 | QCryptographicHash hash(QCryptographicHash::Sha1); |
| 110 | |
| 111 | hash.addData(ba); |
| 112 | integrityProtection += hash.result(); |
| 113 | } |
| 114 | |
| 115 | // prepend a random char to the string |
| 116 | char randomChar = char(QRandomGenerator::global()->generate() & 0xFF); |
| 117 | |
| 118 | ba = randomChar + integrityProtection + ba; |
| 119 | int pos(0); |
| 120 | char lastChar(0); |
| 121 | int cnt = ba.size(); |
| 122 | |
| 123 | while (pos < cnt) { |
| 124 | ba[pos] = ba.at(pos) ^ m_keyParts.at(pos % 8) ^ lastChar; |
| 125 | lastChar = ba.at(pos); |
| 126 | ++pos; |
| 127 | } |
| 128 | |
| 129 | QByteArray resultArray; |
| 130 | |
| 131 | resultArray.append(char(0x03)); // version for future updates to algorithm |
| 132 | resultArray.append(char(flags)); // encryption flags |
| 133 | resultArray.append(ba); |
| 134 | m_lastError = ErrorNoError; |
| 135 | return resultArray; |
| 136 | } |
| 137 | |
| 138 | QString SimpleCrypt::encryptToString(const QString& plaintext) { |
| 139 | QByteArray plaintextArray = plaintext.toUtf8(); |
| 140 | QByteArray cypher = encryptToByteArray(plaintextArray); |
| 141 | QString cypherString = QString::fromLatin1(cypher.toBase64()); |
| 142 | |
| 143 | return cypherString; |
| 144 | } |
| 145 | |
| 146 | QString SimpleCrypt::encryptToString(QByteArray plaintext) { |
| 147 | QByteArray cypher = encryptToByteArray(plaintext); |
| 148 | QString cypherString = QString::fromLatin1(cypher.toBase64()); |
| 149 | |
| 150 | return cypherString; |
| 151 | } |
| 152 | |
| 153 | QString SimpleCrypt::decryptToString(const QString& cyphertext) { |
| 154 | QByteArray cyphertextArray = QByteArray::fromBase64(cyphertext.toLatin1()); |
| 155 | QByteArray plaintextArray = decryptToByteArray(cyphertextArray); |
| 156 | QString plaintext = QString::fromUtf8(plaintextArray, plaintextArray.size()); |
| 157 | |
| 158 | return plaintext; |
| 159 | } |
| 160 | |
| 161 | QString SimpleCrypt::decryptToString(QByteArray cypher) { |
| 162 | QByteArray ba = decryptToByteArray(cypher); |
| 163 | QString plaintext = QString::fromUtf8(ba, ba.size()); |
| 164 | |
| 165 | return plaintext; |
| 166 | } |
| 167 | |
| 168 | QByteArray SimpleCrypt::decryptToByteArray(const QString& cyphertext) { |
| 169 | QByteArray cyphertextArray = QByteArray::fromBase64(cyphertext.toLatin1()); |
| 170 | QByteArray ba = decryptToByteArray(cyphertextArray); |
| 171 | |
| 172 | return ba; |
| 173 | } |
| 174 | |
| 175 | QByteArray SimpleCrypt::decryptToByteArray(QByteArray cypher) { |
| 176 | if (m_keyParts.isEmpty()) { |
| 177 | qWarning() << "No key set."; |
| 178 | m_lastError = ErrorNoKeySet; |
| 179 | return QByteArray(); |
| 180 | } |
| 181 | |
| 182 | QByteArray ba = cypher; |
| 183 | |
| 184 | if (cypher.size() < 3) { |
| 185 | return QByteArray(); |
| 186 | } |
| 187 | |
| 188 | char version = ba.at(0); |
| 189 | |
| 190 | if (version != 3) { // we only work with version 3 |
| 191 | m_lastError = ErrorUnknownVersion; |
| 192 | qWarning() << "Invalid version or not a cyphertext."; |
| 193 | return QByteArray(); |
| 194 | } |
| 195 | |
| 196 | CryptoFlags flags = CryptoFlags(ba.at(1)); |
| 197 | |
| 198 | ba = ba.mid(2); |
| 199 | int pos(0); |
| 200 | int cnt(ba.size()); |
| 201 | char lastChar = 0; |
| 202 | |
| 203 | while (pos < cnt) { |
| 204 | char currentChar = ba[pos]; |
| 205 | |
| 206 | ba[pos] = ba.at(pos) ^ lastChar ^ m_keyParts.at(pos % 8); |
| 207 | lastChar = currentChar; |
| 208 | ++pos; |
| 209 | } |
| 210 | |
| 211 | ba = ba.mid(1); // chop off the random number at the start |
| 212 | bool integrityOk(true); |
| 213 | |
| 214 | if (flags.testFlag(CryptoFlagChecksum)) { |
| 215 | if (ba.length() < 2) { |
| 216 | m_lastError = ErrorIntegrityFailed; |
| 217 | return QByteArray(); |
| 218 | } |
| 219 | |
| 220 | quint16 storedChecksum; |
| 221 | |
| 222 | { |
| 223 | QDataStream s(&ba, QIODevice::ReadOnly); |
| 224 | |
| 225 | s >> storedChecksum; |
| 226 | } |
| 227 | |
| 228 | ba = ba.mid(2); |
| 229 | #if QT_VERSION_MAJOR == 6 |
| 230 | quint16 checksum = qChecksum(ba); |
| 231 | #else |
| 232 | quint16 checksum = qChecksum(ba, ba.length()); |
| 233 | #endif |
| 234 | |
| 235 | integrityOk = (checksum == storedChecksum); |
| 236 | } |
| 237 | else if (flags.testFlag(CryptoFlagHash)) { |
| 238 | if (ba.length() < 20) { |
| 239 | m_lastError = ErrorIntegrityFailed; |
| 240 | return QByteArray(); |
| 241 | } |
| 242 | |
| 243 | QByteArray storedHash = ba.left(20); |
| 244 | |
| 245 | ba = ba.mid(20); |
| 246 | QCryptographicHash hash(QCryptographicHash::Sha1); |
| 247 | |
| 248 | hash.addData(ba); |
| 249 | integrityOk = (hash.result() == storedHash); |
| 250 | } |
| 251 | |
| 252 | if (!integrityOk) { |
| 253 | m_lastError = ErrorIntegrityFailed; |
| 254 | return QByteArray(); |
| 255 | } |
| 256 | |
| 257 | if (flags.testFlag(CryptoFlagCompression)) { |
| 258 | ba = qUncompress(ba); |
| 259 | } |
| 260 | |
| 261 | m_lastError = ErrorNoError; |
| 262 | return ba; |
| 263 | } |