21 #include <tox/toxencryptsave.h>
29 static QString getKeyDerivationError(Tox_Err_Key_Derivation error);
30 static QString getEncryptionError(Tox_Err_Encryption error);
31 static QString getDecryptionError(Tox_Err_Decryption error);
32 static QString getSaltError(Tox_Err_Get_Salt error);
66 return TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
76 if (ciphertext.length() < TOX_PASS_ENCRYPTION_EXTRA_LENGTH) {
80 return tox_is_data_encrypted(
reinterpret_cast<const uint8_t*
>(ciphertext.constData()));
92 if (password.length() == 0) {
93 qWarning() <<
"Empty password supplied, probably not what you intended.";
96 QByteArray pass = password.toUtf8();
97 QByteArray ciphertext(plaintext.length() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, 0x00);
98 Tox_Err_Encryption error;
99 tox_pass_encrypt(
reinterpret_cast<const uint8_t*
>(plaintext.constData()),
100 static_cast<size_t>(plaintext.size()),
101 reinterpret_cast<const uint8_t*
>(pass.constData()),
102 static_cast<size_t>(pass.size()),
103 reinterpret_cast<uint8_t*
>(ciphertext.data()), &error);
105 if (error != TOX_ERR_ENCRYPTION_OK) {
106 qCritical() << getEncryptionError(error);
123 qWarning() <<
"The data was not encrypted using this module, or it's corrupted.";
127 if (password.length() == 0) {
128 qDebug() <<
"Empty password supplied, probably not what you intended.";
131 QByteArray pass = password.toUtf8();
132 QByteArray plaintext(ciphertext.length() - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, 0x00);
133 Tox_Err_Decryption error;
134 tox_pass_decrypt(
reinterpret_cast<const uint8_t*
>(ciphertext.constData()),
135 static_cast<size_t>(ciphertext.size()),
136 reinterpret_cast<const uint8_t*
>(pass.constData()),
137 static_cast<size_t>(pass.size()),
reinterpret_cast<uint8_t*
>(plaintext.data()),
140 if (error != TOX_ERR_DECRYPTION_OK) {
141 qWarning() << getDecryptionError(error);
158 const QByteArray pass = password.toUtf8();
159 Tox_Err_Key_Derivation error;
160 Tox_Pass_Key*
const passKey = tox_pass_key_derive(
161 reinterpret_cast<const uint8_t*
>(pass.constData()),
162 static_cast<size_t>(pass.length()), &error);
164 if (error != TOX_ERR_KEY_DERIVATION_OK) {
166 qCritical() << getKeyDerivationError(error);
167 return std::unique_ptr<ToxEncrypt>{};
185 qWarning() <<
"The data was not encrypted using this module, or it's corrupted.";
186 return std::unique_ptr<ToxEncrypt>{};
189 Tox_Err_Get_Salt saltError;
190 uint8_t salt[TOX_PASS_SALT_LENGTH];
191 tox_get_salt(
reinterpret_cast<const uint8_t*
>(toxSave.constData()), salt, &saltError);
193 if (saltError != TOX_ERR_GET_SALT_OK) {
194 qWarning() << getSaltError(saltError);
195 return std::unique_ptr<ToxEncrypt>{};
198 QByteArray pass = password.toUtf8();
199 Tox_Err_Key_Derivation keyError;
200 Tox_Pass_Key*
const passKey = tox_pass_key_derive_with_salt(
201 reinterpret_cast<const uint8_t*
>(pass.constData()),
202 static_cast<size_t>(pass.length()), salt, &keyError);
204 if (keyError != TOX_ERR_KEY_DERIVATION_OK) {
206 qWarning() << getKeyDerivationError(keyError);
207 return std::unique_ptr<ToxEncrypt>{};
221 qCritical() <<
"The passkey is invalid.";
225 QByteArray ciphertext(plaintext.length() + TOX_PASS_ENCRYPTION_EXTRA_LENGTH, 0x00);
226 Tox_Err_Encryption error;
227 tox_pass_key_encrypt(
passKey,
reinterpret_cast<const uint8_t*
>(plaintext.constData()),
228 static_cast<size_t>(plaintext.size()),
229 reinterpret_cast<uint8_t*
>(ciphertext.data()), &error);
231 if (error != TOX_ERR_ENCRYPTION_OK) {
232 qCritical() << getEncryptionError(error);
248 qWarning() <<
"The data was not encrypted using this module, or it's corrupted.";
252 QByteArray plaintext(ciphertext.length() - TOX_PASS_ENCRYPTION_EXTRA_LENGTH, 0x00);
253 Tox_Err_Decryption error;
254 tox_pass_key_decrypt(
passKey,
reinterpret_cast<const uint8_t*
>(ciphertext.constData()),
255 static_cast<size_t>(ciphertext.size()),
256 reinterpret_cast<uint8_t*
>(plaintext.data()), &error);
258 if (error != TOX_ERR_DECRYPTION_OK) {
259 qWarning() << getDecryptionError(error);
271 QString getKeyDerivationError(Tox_Err_Key_Derivation error)
274 case TOX_ERR_KEY_DERIVATION_OK:
275 return QStringLiteral(
"The function returned successfully.");
276 case TOX_ERR_KEY_DERIVATION_NULL:
277 return QStringLiteral(
278 "One of the arguments to the function was NULL when it was not expected.");
279 case TOX_ERR_KEY_DERIVATION_FAILED:
280 return QStringLiteral(
281 "The crypto lib was unable to derive a key from the given passphrase.");
283 return QStringLiteral(
"Unknown key derivation error.");
292 QString getEncryptionError(Tox_Err_Encryption error)
295 case TOX_ERR_ENCRYPTION_OK:
296 return QStringLiteral(
"The function returned successfully.");
297 case TOX_ERR_ENCRYPTION_NULL:
298 return QStringLiteral(
299 "One of the arguments to the function was NULL when it was not expected.");
300 case TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED:
301 return QStringLiteral(
302 "The crypto lib was unable to derive a key from the given passphrase.");
303 case TOX_ERR_ENCRYPTION_FAILED:
304 return QStringLiteral(
"The encryption itself failed.");
306 return QStringLiteral(
"Unknown encryption error.");
315 QString getDecryptionError(Tox_Err_Decryption error)
318 case TOX_ERR_DECRYPTION_OK:
319 return QStringLiteral(
"The function returned successfully.");
320 case TOX_ERR_DECRYPTION_NULL:
321 return QStringLiteral(
322 "One of the arguments to the function was NULL when it was not expected.");
323 case TOX_ERR_DECRYPTION_INVALID_LENGTH:
324 return QStringLiteral(
325 "The input data was shorter than TOX_PASS_ENCRYPTION_EXTRA_LENGTH bytes.");
326 case TOX_ERR_DECRYPTION_BAD_FORMAT:
327 return QStringLiteral(
"The input data is missing the magic number or is corrupted.");
328 case TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED:
329 return QStringLiteral(
"The crypto lib was unable to derive a key from the given passphrase.");
330 case TOX_ERR_DECRYPTION_FAILED:
331 return QStringLiteral(
"Decryption failed. Either the data was corrupted or the password/key was incorrect.");
333 return QStringLiteral(
"Unknown decryption error.");
342 QString getSaltError(Tox_Err_Get_Salt error)
345 case TOX_ERR_GET_SALT_OK:
346 return QStringLiteral(
"The function returned successfully.");
347 case TOX_ERR_GET_SALT_NULL:
348 return QStringLiteral(
349 "One of the arguments to the function was NULL when it was not expected.");
350 case TOX_ERR_GET_SALT_BAD_FORMAT:
351 return QStringLiteral(
"The input data is missing the magic number or is corrupted.");
353 return QStringLiteral(
"Unknown salt error.");