bool AES_CBC_Decode(const unsigned char* key, int keyLen, unsigned char* initVector, const unsigned char* input, int inputOctets, unsigned char* outBuffer) { Rijndael oRijndael; oRijndael.init(Rijndael::CBC, Rijndael::Decrypt, key, static_cast<Rijndael::KeyLength>(keyLen), initVector); int nLength = oRijndael.padDecrypt(input, inputOctets, outBuffer); return (nLength > 0)?true:false; }
bool CEncryption::Decrypt(const unsigned char* pInput, int nLenInput, const char* szPassword, unsigned char*& pOutput, int& nLenOutput) { bool bResult = false; TD_TLHEADER hdr; RD_UINT8 uFinalKey[32]; sha256_ctx sha32; ASSERT(NULL != pInput); if(NULL == pInput) return FALSE; ASSERT(0 != nLenInput); if(0 == nLenInput) return FALSE; ASSERT(NULL != szPassword); if(NULL == szPassword) return FALSE; ASSERT(sizeof(TD_TLHEADER) <= nLenInput); if(sizeof(TD_TLHEADER) > nLenInput) return FALSE; // Extract header structure from memory file memcpy(&hdr, pInput, sizeof(TD_TLHEADER)); // Hash the header sha256_begin(&sha32); sha256_hash((unsigned char *)&hdr + 32, sizeof(TD_TLHEADER) - 32, &sha32); sha256_end((unsigned char *)uFinalKey, &sha32); // Check if hash of header is the same as stored hash // to verify integrity of header if(0 == memcmp(hdr.aHeaderHash, uFinalKey, 32)) { // Check if we can open this if((hdr.dwSignature1 == TD_TLSIG_1) && (hdr.dwSignature2 == TD_TLSIG_2)) { // Allocate enough memory pOutput = new unsigned char[nLenInput]; if(NULL != pOutput) { unsigned long uKeyLen = strlen(szPassword); // Create MasterKey by hashing szPassword ASSERT(0 != uKeyLen); if(0 != uKeyLen) { sha256_begin(&sha32); sha256_hash((unsigned char *)szPassword, uKeyLen, &sha32); sha256_end(m_pMasterKey, &sha32); m_dwKeyEncRounds = hdr.dwKeyEncRounds; // Generate m_pTransformedMasterKey from m_pMasterKey if(TRUE == _TransformMasterKey(hdr.aMasterSeed2)) { // Hash the master password with the generated hash salt sha256_begin(&sha32); sha256_hash(hdr.aMasterSeed, 16, &sha32); sha256_hash(m_pTransformedMasterKey, 32, &sha32); sha256_end((unsigned char *)uFinalKey, &sha32); bResult = true; } } } } } if (bResult) { bResult = false; Rijndael aes; // Initialize Rijndael/AES if(RIJNDAEL_SUCCESS == aes.init(Rijndael::CBC, Rijndael::Decrypt, uFinalKey, Rijndael::Key32Bytes, hdr.aEncryptionIV) ) { nLenOutput = aes.padDecrypt((RD_UINT8 *)pInput + sizeof(TD_TLHEADER), nLenInput - sizeof(TD_TLHEADER), (RD_UINT8 *)pOutput); // Check if all went correct ASSERT(0 <= nLenOutput); if(0 <= nLenOutput) { // Check contents correct (with high probability) sha256_begin(&sha32); sha256_hash((unsigned char *)pOutput, nLenOutput, &sha32); sha256_end((unsigned char *)uFinalKey, &sha32); if(0 == memcmp(hdr.aContentsHash, uFinalKey, 32)) { bResult = true; // data decrypted successfully } } } } if (!bResult) { SAFE_DELETE_ARRAY(pOutput); } return (bResult); }
bool App::openTree(const QString &file) { NotesFileParser handler; connect(&handler, SIGNAL(backupLoaded(bool, const QString &)), this, SLOT(setBackupLocation(bool, const QString &))); connect(&handler, SIGNAL(notesShowReminderAtStartUpLoaded(bool)), this, SLOT(setNotesShowReminderAtStartUp(bool))); // connect(&handler, SIGNAL(notesWordWrapLoaded(bool)), // this, SLOT(setNotesWordWrap(bool))); // connect(&handler, SIGNAL(notesRootNodeDecorationLoaded(bool)), // this, SLOT(setNotesRootNodeDecoration(bool))); // connect(&handler, SIGNAL(notesShowScrollBarsLoaded(bool)), // this, SLOT(setNotesShowScrollBars(bool))); connect(&handler, SIGNAL(entryLoaded(Entry *)), this, SLOT(addEntry(Entry *))); connect(&handler, SIGNAL(noteLoaded(const QString &, QList<QString> *, const QString &, const QString &, int, QDateTime, const QString &, QDateTime)), notes, SLOT(addNote(const QString &, QList<QString> *, const QString &, const QString &, int, QDateTime, const QString &, QDateTime))); connect(&handler, SIGNAL(securityPasswdLoaded(const QString &)), this, SLOT(setSecurityPasswd(const QString &))); connect(&handler, SIGNAL(noteLoaded(const QString &, Strokes *, const QString &, const QString &, int, QDateTime, const QString &, QDateTime)), notes, SLOT(addNote(const QString &, Strokes *, const QString &, const QString &, int, QDateTime, const QString &, QDateTime))); connect(&handler, SIGNAL(noNoteChild()), notes, SLOT(noNoteChild())); QFile xmlFile(Global::applicationFileName("iqnotes", file + ".xml")), xmlRijndaelFile(Global::applicationFileName("iqnotes", file + ".rijn")); QXmlInputSource *source; // Init xml file if (!xmlFile.exists() && !xmlRijndaelFile.exists()) { xmlFile.open(IO_WriteOnly); QTextStream ts(&xmlFile); ts << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<iqnotes>\n<entries>\n" << "<entry name=\"Company\">\n" << "<property name=\"Name\" type=\"oneLine\"/>\n" << "<property name=\"Address\" type=\"multiLine\"/>\n" << "<property name=\"Tels\" type=\"oneLine\"/>\n" << "<property name=\"Emails\" type=\"oneLine\"/>\n" << "<property name=\"Note\" type=\"multiLine\"/>\n" << "</entry>\n" << "<entry name=\"Person\">\n" << "<property name=\"Name\" type=\"oneLine\"/>\n" << "<property name=\"Nick\" type=\"oneLine\"/>\n" << "<property name=\"Work Address\" type=\"multiLine\"/>\n" << "<property name=\"Work Tel\" type=\"oneLine\"/>\n" << "<property name=\"Work Emails\" type=\"oneLine\"/>\n" << "<property name=\"Home Address\" type=\"multiLine\"/>\n" << "<property name=\"Home Tel\" type=\"oneLine\"/>\n" << "<property name=\"Emails\" type=\"oneLine\"/>\n" << "<property name=\"Note\" type=\"multiLine\"/>\n" << "</entry>\n" << "<entry name=\"User account\">\n" << "<property name=\"Host\" type=\"oneLine\"/>\n" << "<property name=\"Username\" type=\"oneLine\"/>\n" << "<property name=\"Password\" type=\"oneLine\"/>\n" << "<property name=\"Type\" type=\"oneLine\"/>\n" << "<property name=\"Note\" type=\"mutliLine\"/>\n" << "</entry>\n" << "<entry name=\"Credit card\">\n" << "<property name=\"Card type\" type=\"oneLine\"/>\n" << "<property name=\"Account #\" type=\"oneLine\"/>\n" << "<property name=\"Expire\" type=\"oneLine\"/>\n" << "<property name=\"PIN\" type=\"oneLine\"/>\n" << "<property name=\"Note\" type=\"mutliLine\"/>\n" << "</entry>\n" << "<entry name=\"WWW Link\">\n" << "<property name=\"Title\" type=\"oneLine\"/>\n" << "<property name=\"URL\" type=\"oneLine\"/>\n" << "<property name=\"Description\" type=\"multiLine\"/>\n" << "</entry>\n" << "</entries>\n" << "<notes>\n</notes>\n</iqnotes>\n"; xmlFile.close(); source = new QXmlInputSource(xmlFile); } else if (xmlRijndaelFile.exists()) { StartUpPasswdBase passwdD(this, 0, true); passwdD.adjustSize(); if (!passwdD.exec()) { return false; } preferences.setPasswd(passwdD.Passwd->text()); xmlRijndaelFile.open(IO_ReadOnly); uint fileSize = xmlRijndaelFile.size(); unsigned char *rijnData = (unsigned char *) malloc(fileSize), *rijnDataDecrypted = (unsigned char *) malloc(fileSize); Rijndael rijndael; xmlRijndaelFile.readBlock((char *)rijnData, fileSize); rijndael.init(Rijndael::CBC, Rijndael::Decrypt, preferences.passwd16Bin, Rijndael::Key16Bytes); rijndael.padDecrypt(rijnData, fileSize, rijnDataDecrypted); // yeah, stupid if (memcmp((void *)"<iqnotes>", (void *)rijnDataDecrypted, 9) != 0 && memcmp((void *)"<?xml version", (void *)rijnDataDecrypted, 13) != 0) { free(rijnData); free(rijnDataDecrypted); QMessageBox::critical(this, "Bad password", "Please,\ntype correct password."); return false; } source = new QXmlInputSource(); // source->setData(QString((char *)rijnDataDecrypted)); source->setData(QString::fromUtf8((char *)rijnDataDecrypted)); free(rijnData); free(rijnDataDecrypted); } else { source = new QXmlInputSource(xmlFile); } QXmlSimpleReader reader; reader.setContentHandler(&handler); reader.setErrorHandler(new NotesFileParserError); if(!reader.parse(*source)) qWarning(tr("parse error")); delete source; setCaption("IQNotes :: " + file); return true; }