Skip to content

Commit

Permalink
use SQLite3MultipleCiphers to start to be able to encrypt Ditto's dat…
Browse files Browse the repository at this point in the history
…abase (sabrogden#666)
  • Loading branch information
sabrogden authored Apr 30, 2024
1 parent ffba08b commit 1522219
Show file tree
Hide file tree
Showing 10 changed files with 100,367 additions and 40,216 deletions.
25 changes: 5 additions & 20 deletions CP_Main.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;Winmm.lib;Version.lib;$(Configuration)\EncryptDecrypt.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;Winmm.lib;Version.lib;Shlwapi.lib;$(Configuration)\EncryptDecrypt.lib;crypt32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>release\Ditto.exe</OutputFile>
<SuppressStartupBanner>true</SuppressStartupBanner>
<AdditionalLibraryDirectories>%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
Expand Down Expand Up @@ -368,7 +368,7 @@
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<AdditionalDependencies>ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;Winmm.lib;Pdh.lib;Version.lib;$(Configuration)\EncryptDecrypt.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;Winmm.lib;Pdh.lib;Version.lib;$(Configuration)\EncryptDecrypt.lib;Shlwapi.lib;crypt32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>debug\Ditto.exe</OutputFile>
<SuppressStartupBanner>true</SuppressStartupBanner>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand Down Expand Up @@ -2086,25 +2086,11 @@
<ClCompile Include="RichTextAggregator.cpp" />
<ClCompile Include="SaveAnimation.cpp" />
<ClCompile Include="SendKeys.cpp" />
<ClCompile Include="sqlite\shell.c">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">NotUsing</PrecompiledHeader>
<ClCompile Include="sqlite\sqlite3mc_amalgamation.c">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="sqlite\sqlite3.c">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="StdAfx.cpp">
<Optimization Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Disabled</Optimization>
Expand Down Expand Up @@ -2963,8 +2949,7 @@
<ClInclude Include="Slugify.h" />
<ClInclude Include="SnapWindow.h" />
<ClInclude Include="SpecialPasteOptions.h" />
<ClInclude Include="sqlite\sqlite3.h" />
<ClInclude Include="sqlite\sqlite3ext.h" />
<ClInclude Include="sqlite\sqlite3mc_amalgamation.h" />
<ClInclude Include="SymbolEdit.h" />
<ClInclude Include="ToolTipEx.h" />
<ClInclude Include="TrayWnd.h" />
Expand Down
18 changes: 6 additions & 12 deletions CP_Main.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
<ClCompile Include="sqlite\CppSQLite3.cpp">
<Filter>sqlite</Filter>
</ClCompile>
<ClCompile Include="sqlite\shell.c">
<Filter>sqlite</Filter>
</ClCompile>
<ClCompile Include="sqlite\sqlite3.c">
<Filter>sqlite</Filter>
</ClCompile>
<ClCompile Include="ClipboardSaveRestore.cpp">
<Filter>source</Filter>
</ClCompile>
Expand Down Expand Up @@ -472,17 +466,14 @@
</ClCompile>
<ClCompile Include="ImageFormatAggregator.cpp" />
<ClCompile Include="ImageHelper.cpp" />
<ClCompile Include="sqlite\sqlite3mc_amalgamation.c">
<Filter>sqlite</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="sqlite\CppSQLite3.h">
<Filter>sqlite</Filter>
</ClInclude>
<ClInclude Include="sqlite\sqlite3.h">
<Filter>sqlite</Filter>
</ClInclude>
<ClInclude Include="sqlite\sqlite3ext.h">
<Filter>sqlite</Filter>
</ClInclude>
<ClInclude Include="About.h">
<Filter>header</Filter>
</ClInclude>
Expand Down Expand Up @@ -981,6 +972,9 @@
</ClInclude>
<ClInclude Include="ImageFormatAggregator.h" />
<ClInclude Include="ImageHelper.h" />
<ClInclude Include="sqlite\sqlite3mc_amalgamation.h">
<Filter>sqlite</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="CP_Main.rc">
Expand Down
8 changes: 4 additions & 4 deletions EncryptDecrypt/sha2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ sha2_void sha256_end(unsigned char hval[], sha256_ctx ctx[1])
hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));
}

sha2_void sha256(unsigned char hval[], const unsigned char data[], unsigned long len)
sha2_void sha256Lib(unsigned char hval[], const unsigned char data[], unsigned long len)
{ sha256_ctx cx[1];

sha256_begin(cx); sha256_hash(data, len, cx); sha256_end(hval, cx);
Expand Down Expand Up @@ -616,7 +616,7 @@ sha2_void sha384_end(unsigned char hval[], sha384_ctx ctx[1])
sha_end(hval, ctx, SHA384_DIGEST_SIZE);
}

sha2_void sha384(unsigned char hval[], const unsigned char data[], unsigned long len)
sha2_void sha384Lib(unsigned char hval[], const unsigned char data[], unsigned long len)
{ sha384_ctx cx[1];

sha384_begin(cx); sha384_hash(data, len, cx); sha384_end(hval, cx);
Expand Down Expand Up @@ -647,7 +647,7 @@ sha2_void sha512_end(unsigned char hval[], sha512_ctx ctx[1])
sha_end(hval, ctx, SHA512_DIGEST_SIZE);
}

sha2_void sha512(unsigned char hval[], const unsigned char data[], unsigned long len)
sha2_void sha512Lib(unsigned char hval[], const unsigned char data[], unsigned long len)
{ sha512_ctx cx[1];

sha512_begin(cx); sha512_hash(data, len, cx); sha512_end(hval, cx);
Expand Down Expand Up @@ -702,7 +702,7 @@ sha2_void sha2_end(unsigned char hval[], sha2_ctx ctx[1])
}
}

sha2_int sha2(unsigned char hval[], unsigned long size,
sha2_int sha2Lib(unsigned char hval[], unsigned long size,
const unsigned char data[], unsigned long len)
{ sha2_ctx cx[1];

Expand Down
8 changes: 4 additions & 4 deletions EncryptDecrypt/sha2.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,22 +130,22 @@ sha2_void sha512_compile(sha512_ctx ctx[1]);
sha2_void sha256_begin(sha256_ctx ctx[1]);
sha2_void sha256_hash(const unsigned char data[], unsigned long len, sha256_ctx ctx[1]);
sha2_void sha256_end(unsigned char hval[], sha256_ctx ctx[1]);
sha2_void sha256(unsigned char hval[], const unsigned char data[], unsigned long len);
sha2_void sha256Lib(unsigned char hval[], const unsigned char data[], unsigned long len);

sha2_void sha384_begin(sha384_ctx ctx[1]);
#define sha384_hash sha512_hash
sha2_void sha384_end(unsigned char hval[], sha384_ctx ctx[1]);
sha2_void sha384(unsigned char hval[], const unsigned char data[], unsigned long len);
sha2_void sha384Lib(unsigned char hval[], const unsigned char data[], unsigned long len);

sha2_void sha512_begin(sha512_ctx ctx[1]);
sha2_void sha512_hash(const unsigned char data[], unsigned long len, sha512_ctx ctx[1]);
sha2_void sha512_end(unsigned char hval[], sha512_ctx ctx[1]);
sha2_void sha512(unsigned char hval[], const unsigned char data[], unsigned long len);
sha2_void sha512Lib(unsigned char hval[], const unsigned char data[], unsigned long len);

sha2_int sha2_begin(unsigned long size, sha2_ctx ctx[1]);
sha2_void sha2_hash(const unsigned char data[], unsigned long len, sha2_ctx ctx[1]);
sha2_void sha2_end(unsigned char hval[], sha2_ctx ctx[1]);
sha2_int sha2(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len);
sha2_int sha2Lib(unsigned char hval[], unsigned long size, const unsigned char data[], unsigned long len);

#if defined(__cplusplus)
}
Expand Down
41 changes: 40 additions & 1 deletion sqlite/CppSQLite3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,31 @@ void sqlite_regexp(sqlite3_context* context, int argc, sqlite3_value** values)
}
}

bool CppSQLite3DB::DBEncrypted()
{
bool encrypted = false;
CFile file;
CFileException ex;
if (file.Open(m_dbFile, CFile::modeRead | CFile::shareDenyNone, &ex))
{
if (file.GetLength() > 10)
{
//an unencrypted database will start with "SQLite format 3\000", if it's encrypted it will start with "Salted__"
CStringA b;
file.Read(b.GetBufferSetLength(6), 6);
b.ReleaseBuffer();

if (b != "SQLite")
{
encrypted = true;
}
}
file.Close();
}

return encrypted;
}

void CppSQLite3DB::open(const TCHAR* szFile)
{
#ifdef _UNICODE
Expand All @@ -790,6 +815,18 @@ void CppSQLite3DB::open(const TCHAR* szFile)
int nRet = sqlite3_open(szFile, &mpDB);
#endif

//sqlite3_exec(mpDB, "PRAGMA rekey=123456", 0, 0, 0);
//sqlite3_exec(mpDB, "PRAGMA key=123456", 0, 0, 0);

//int r = sqlite3_rekey(mpDB, "123456", 6);

//if (encrypted)
//{
// int rr = sqlite3_key(mpDB, "123456", 6);
//}

m_dbFile = szFile;

if (nRet != SQLITE_OK)
{
SQLITE3_ERRMSG(mpDB);
Expand Down Expand Up @@ -834,7 +871,9 @@ bool CppSQLite3DB::close()
bool bRet = true;
if (mpDB)
{
//sqlite3_shutdown();
int nClose = sqlite3_close(mpDB);

if(nClose != SQLITE_OK)
{
ASSERT(!"Error closing sqlite db");
Expand Down Expand Up @@ -892,7 +931,7 @@ int CppSQLite3DB::execDML(const TCHAR* szSQL)
{
nRet = sqlite3_changes(mpDB);
sqlite3_finalize(pVM);
}
}
else
{
nRet = sqlite3_finalize(pVM);
Expand Down
6 changes: 5 additions & 1 deletion sqlite/CppSQLite3.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
#ifndef _CppSQLite3_H_
#define _CppSQLite3_H_

#include "sqlite3.h"
//#include "sqlite3.h"
#include "sqlite3mc_amalgamation.h"
#include <cstdio>
#include <cstring>

Expand Down Expand Up @@ -205,6 +206,8 @@ class CppSQLite3DB

bool IsDatabaseOpen() { return mpDB != NULL; }

bool DBEncrypted();

private:

CppSQLite3DB(const CppSQLite3DB& db);
Expand All @@ -216,6 +219,7 @@ class CppSQLite3DB

sqlite3* mpDB;
int mnBusyTimeoutMs;
CString m_dbFile;
};

#endif
Loading

0 comments on commit 1522219

Please sign in to comment.