SQLCipher with qt6 hangs on queries against encrypted database

Hello, I am using the community version of sqlcipher built with vcpkg and qt 6.5.0. I made my own sqlcipher driver based on the built-in qt sqlite driver, I changed almost nothing in the code, basically only the name of the driver class. The driver was connected to the project through the plugin and directly through driver registration (this does not affect the problem in any way). I create a database with a password, and when registering or authorizing the database during queries, my application freezes for 2-3-4 seconds, if you do not set a password for the database, then the queries go through quickly. My code is something like this:

QSqlDatabase currentUserDb = QSqlDatabase::addDatabase(“SQLITECIPHER”, user);
currentUserDb.open();
QSqlQuery query(currentUserDb);
query.exec(“pragma key = '” + password + “';”);// I also tried using sqlite3_key, same effect
QString str = “create table if not exists data(id integer primary key autoincrement, there are 5 more TEXT type names”;
query.exec(str)//this is already starting to hang
// etc. further queries also hang if the query to create a table is removed.

Hangups should be like this (I have an old PC, an amd a8-6600k processor)? Or incorrectly assembled the driver, or some other reason?

Update: hangs only in the application built via mingw in the release version, it does not hang in the debug version. Through msvc it does not hang in debug and release versions, although it used to hang in the release version too, but not in debug. I don’t know what I did to stop hanging in the release version via msvc, maybe changing the compiler to a newer version in the settings helped, but I’m not sure. This is some kind of mystic.

This is the result of key derivation. This occurs for the first operation that uses the database after it is opened. The key derivation is deliberately slow to provide protection against brute force and dictionary attacks. You can read more about it at the design page, and lean about optimization at the following two links

https://www.zetetic.net/sqlcipher/design/

The delay will be much more noticeable on older devices. Take a look at the performance optimization faq, and make sure you are reusing database connections to avoid multiple key derivations. Alternately, you can either use a RAW key to skip key derivation or adjust the number of iterations to speed things up but note that both these settings have security implications.

1 Like

It works fine for me now through the msvc compiler, it does not hang. After a complete rebuild of the application, it also works fine. I still don’t understand why it stopped.

After a while, it began to hang again, for some reason it started after I had more databases (5 and more) to open (1 for each user). But after changing the user, I close the previous database using the following methods:
QSqlDatabase::database(prevUser).close();
QSqlDatabase::removeDatabase(prevUser);
I do not understand how the number of databases can affect hangups if the previous databases are closed.

I reduced the number of iterations to 10000, so far everything is fine