/*! * * Copyright (c) 2023-2023 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file encryption.cpp * \author (last) Behrouz NematiPour * \date (last) 18-Jul-2023 * \author (original) Behrouz NematiPour * \date (original) 18-Jul-2023 * */ #include "encryption.h" // Linux // Qt #include #include // Project //#include "DeviceController.h" QString encryption::__salt__ = ""; // Additional int encryption::cryptOpen(const QString vDevice, const QString vFolder) { qDebug() << vDevice << vFolder; return 0; } int encryption::cryptFormat(const QString vDevice) { qDebug() << vDevice; return 0; } int encryption::cryptClose(const QString vFolder) { qDebug() << vFolder; return 0; } int encryption::mkfsExt4(const QString vDevice) { qDebug() << vDevice; return 0; } void encryption::varSalt(const QString &vSalt) { __salt__ = vSalt; } QString encryption::fixSalt() { const char c [ ] = { 56, 51, 104, 114, 48, 117, 50, 78, 51, 109, 94, 43, 105, 80, 48, 117, 114, 0 }; return QString(c); } /*! * \brief encryption::hashedString * \details encrypts the string vString with an algorithm * \param vString - the string to be encrypted. * \param vSalt - the encryption salt. * \return encrypted string */ QString encryption::hashedString(const QString &vString, bool &ok, bool vAddMoreSalt) { int iter = 10000; // https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#pbkdf2 int len = 16; ok = vAddMoreSalt ? ! __salt__.isEmpty() : true; auto shuffle = [](const QString &vString) -> QByteArray { auto reverse = [](const QString &vString) -> QString { if ( vString.trimmed().isEmpty() ) return QString(); QString mReverse; int end = vString.size() - 1; int mid = end / 2; for(int i = mid; i >= 0; i-- ) { mReverse += vString.at(i); } for(int i = end; i > mid; i-- ) { mReverse += vString.at(i); } return mReverse; }; if ( vString.trimmed().isEmpty() ) return QByteArray(); //DEBUG: qDebug()<<"string" << vString; QString reversed = reverse(vString); //DEBUG: qDebug()<<"reversed" << reversed; QByteArray hexed = reversed.toUtf8().toHex(); //DEBUG: qDebug()<<"hexed" << hexed; return hexed; }; QCryptographicHash::Algorithm algorithm = QCryptographicHash::Sha512; QString salt = fixSalt() + (vAddMoreSalt ? __salt__ : ""); //DEBUG: qDebug() << "salt:" << salt; QByteArray hashed = QPasswordDigestor::deriveKeyPbkdf2(algorithm, vString.toUtf8(), shuffle( salt ), iter, len).toHex(); //DEBUG: qDebug() << "hashed:" << vString << hashed; return hashed; } /*! * \brief encryption::isDefaultServicePassword * \details Checks if the current service password is the default password * \note It is being used to force the user/manufacturer to set the service password. * \param vPassword - the service pasword entered by user. * \return true if the service password is the default. */ bool encryption::isDefaultServicePassword(const QString &vPassword, bool &ok) { //DEBUG qDebug() << __FUNCTION__ << vPassword; return vPassword == defaultServicePassword(ok); } /*! * \brief encryption::defaultPassword * \details The default password which will be used as an indication that the service password has not been set yet. * \return the default password will be saved as hashed string. */ QString encryption::defaultServicePassword(bool &ok) { QString _A1 = "tal"; QString _S1 = QString("%1").arg(_A1 ).prepend("A" ); QString _S2 = QString("%1").arg(_A1 ).prepend("Ma" ); QString _S3 = QString("%1").arg(10*2+2 ).append ("leh" ); QString hashed = hashedString(( QStringList() << _S1 << _S2 << _S3 ).join('.'), ok, false); //DEBUG qDebug() << __FUNCTION__ << hashed; return hashed; } /*! * \brief encryption::configurationsPassword * \details The password which will be used for encrypt partition. * \return the password will be saved as hashed string. */ QString encryption::configurationsPassword(bool &ok, bool vReset) { if ( vReset ) { // "Diality.2023" const char d [ ] = { 68, 105, 97, 108, 105, 116, 121, 46, 50, 48, 50, 51, 0 }; return QString(d); } const char c [ ] = { 50, 75, 102, 98, 106, 67, 46, 68, 89, 115, 116, 109, 46, 71, 50, 75, 106, 90, 46, 105, 78, 105, 120, 73, 46, 78, 105, 51, 50, 89, 46, 84, 89, 112, 57, 117, 46, 77, 50, 52, 119, 103, 46, 50, 89, 98, 98, 106, 46, 78, 105, 48, 73, 78, 46, 109, 70, 50, 52, 119, 46, 103, 50, 76, 76, 90, 46, 104, 116, 117, 77, 73, 46, 78, 105, 111, 50, 89, 46, 84, 89, 112, 57, 117, 46, 77, 50, 52, 119, 0 }; QString hashed = hashedString(QString(c), ok, true); //DEBUG qDebug() << __FUNCTION__ << hashed << ok; return hashed; }