OpenSSL-based transparent page-level encryption method and system for SQLite database
By implementing a Crypto-VFS encryption proxy layer in SQLite's virtual file system layer, and utilizing the OpenSSL encryption API and three-layer HKDF key derivation, the high cost of version upgrades and maintenance, imperfect key management, and OpenSSL compatibility issues of the SQLite encryption scheme are resolved, achieving transparent encryption and high security.
Patent Information
- Application Number
- CN202611228637.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-13
- Publication Date
- 2026-11-24
AI Technical Summary
Existing SQLite encryption solutions require intrusive modifications to the database kernel source code, resulting in high costs for version upgrades and maintenance. They also have a simple key management layer that lacks atomic rotation capabilities and the encryption backend is incompatible with the OpenSSL standard ecosystem.
A Crypto-VFS encryption proxy layer is implemented in the SQLite virtual file system layer. The page data is encrypted before being written to disk and decrypted after being read from disk using the OpenSSL encryption API. It adopts three-layer HKDF key derivation and transaction-level atomic rotation, and supports multi-algorithm authentication encryption.
It achieves backward compatibility with any version of SQLite without modifying the SQLite source code, maintains the semantic integrity of queries, provides flexibility and high security for the encrypted backend, reduces the risk of key leakage, and optimizes the key rotation process.
Smart Images

Figure CN123158656A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of database security technology, and specifically relates to a transparent page-level encryption method and system for SQLite databases based on OpenSSL. Background Technology
[0002] SQLite, as the world's most widely deployed embedded relational database engine, is widely used in mobile terminals, IoT devices, desktop applications, and edge computing scenarios due to its core advantages of zero configuration, serverless operation, and single-file storage. However, SQLite does not natively provide database file encryption capabilities; database files are stored on disk in plaintext, meaning any user or process with file system access can directly read the entire database content. For applications storing user privacy information, business data, or key materials, this plaintext storage method poses a serious risk of data leakage, making the construction of a secure data storage solution on top of SQLite a pressing technical challenge.
[0003] Current technologies typically employ the following methods to solve SQLite data encryption problems. The first is the commercial extension SQLiteEncryptionExtension provided by SQLite, which injects encryption logic into hook functions reserved in the SQLite kernel's Pager layer, using a page-level encryption strategy and supporting AES series encryption algorithms. The second is the open-source solution SQLCipher, which deeply modifies the SQLite source code's Pager layer during compilation, embedding encryption logic into the database kernel. Combined with PBKDF2 key derivation and HMAC integrity verification, it enjoys high popularity in the open-source community. The third is an application-layer field-level encryption scheme, where developers encrypt sensitive fields one by one in the application before storing them in the database, without involving any changes to the SQLite kernel. In addition, there is a transparent encryption scheme based on the FUSE file system layer, which intercepts file I / O operations at the operating system level to encrypt the entire database file.
[0004] However, the existing methods generally suffer from a series of common drawbacks. First, they are highly invasive; both SEE and SQLCipher require modification of the SQLite kernel code or calls to non-public APIs, resulting in tight coupling with upstream SQLite versions. Each SQLite version upgrade necessitates re-merging patches or adapting to interface changes, leading to high long-term maintenance costs. Second, while application-layer encryption schemes do not intrude into the kernel, they compromise the integrity of SQL query semantics. Encrypted fields cannot directly participate in operations such as WHERE condition filtering, ORDER BY sorting, and JOIN joins, increasing application development complexity and maintenance burden. Third, most existing schemes have limited encryption backend options. SQLCipher uses LibTomCrypt or CommonCrypto as its encryption backend, which is incompatible with the widely adopted and continuously audited OpenSSL ecosystem, failing to fully utilize OpenSSL's multi-platform support and hardware-accelerated instruction set optimization capabilities. Finally, the key management mechanisms of existing schemes are relatively simple, lacking hierarchical key derivation structures and atomic key rotation capabilities, limiting applications in high-security scenarios. Therefore, a transparent page-level encryption method and system for SQLite databases based on OpenSSL is needed to address these issues. Summary of the Invention
[0005] The technical problem to be solved by this invention is to provide a transparent page-level encryption method and system for SQLite databases based on OpenSSL. It aims to overcome the shortcomings of existing SQLite encryption schemes, such as the need for intrusive modification of the database kernel source code leading to high version upgrade and maintenance costs, simple key management layer and lack of atomic rotation capability, and incompatibility between the encryption backend and the OpenSSL standard ecosystem. It features zero-intrusive transparent page encryption and decryption through VFS layer proxy, three-layer HKDF key derivation and transaction-level atomic rotation, and multi-algorithm authentication encryption based on the OpenSSL standard library.
[0006] To solve the above-mentioned technical problems, the technical solution adopted by the present invention is as follows: A transparent page-level encryption method for SQLite databases based on OpenSSL is proposed. An encryption proxy layer is implemented at the SQLite virtual file system layer. This proxy layer encapsulates the underlying operating system's native VFS, intercepting all page-level read / write operations on database files. It calls OpenSSL's encryption API to encrypt page data before writing it to disk and decrypt it after reading it from disk. The encryption proxy layer performs proxying by implementing the `sqlite3_vfs` and `sqlite3_file` interfaces, encapsulating the encryption logic in the `xRead` and `xWrite` callback functions. The encryption proxy layer is registered as a default or named VFS via `sqlite3_vfs_register()`, and is enabled by the user during `sqlite3_open_v2()`. The core data structures include a `CryptoVfs` structure that inherits from `sqlite3_vfs` and adds an underlying VFS pointer and global encryption configuration; a `CryptoFile` structure that inherits from `sqlite3_file` and adds an encryption context and page size cache; and a `CryptoContext` structure that stores the master key, page encryption key, IV generation key, and encryption algorithm identifier.
[0007] Preferably, except for the initialization logic of injecting the encrypted file handle in the xOpen method, the other VFS methods are directly delegated to the underlying VFS for execution.
[0008] Preferably, the write operation process is as follows: calculate the page number based on the offset, call the IV generation function to calculate the IV value of the page, use the OpenSSLEVP interface to perform AES-256-GCM encryption, write the IV and GCM authentication tag to the reserved space at the end of the page, and finally write it to the disk through the underlying VFS.
[0009] Preferably, the IV value is calculated using the following formula: ; Take the first 12 bytes as the page IV; where, This indicates the IV generation key. Indicates the page number. This represents the write operation counter.
[0010] Preferably, the AES-256-GCM encryption process sequentially calls EVP_EncryptInit_ex to initialize the encryption context, EVP_EncryptUpdate to encrypt the valid data of the page, and EVP_EncryptFinal_ex to obtain the GCM authentication tag.
[0011] Preferably, the read operation process is as follows: read the encrypted page from the disk, extract the IV and authentication tag from the reserved space, call the OpenSSLEVP decryption interface to decrypt and verify the integrity. If the verification fails, return the SQLITE_IOERR error code; if the verification passes, return the plaintext data to the Pager module.
[0012] Preferably, SQLite's reserved space mechanism is used to reserve a fixed length of space at the end of each page to store IV and GCM authentication tags, so that the total size of the encrypted page remains unchanged and the page addressing mechanism of SQLite is not violated.
[0013] Furthermore, the IV is 12 bytes and the GCM authentication tag is 16 bytes, occupying a total of 28 bytes of reserved space; the reserved space is configured through PRAGMAreserved_page_size.
[0014] Furthermore, the database header page, i.e., page 1, contains a 100-byte file format header, of which the first 16 bytes are a fixed magic string that keeps the plaintext unencrypted, and bytes 17 to (page size minus reserved space size) execute the standard encryption process; the reserved space additionally stores a 4-byte encryption scheme version number and a 32-byte PBKDF2 salt value.
[0015] Preferably, a three-layer key derivation architecture is adopted: the first layer is the user password, provided through the PRAGMAkey command or sqlite3_key() API; the second layer is the master key, derived from the user password using the PBKDF2-HMAC-SHA512 algorithm; the third layer is the working key, including the page encryption key and the IV generation key, both derived from the master key using different information strings through the HKDF-SHA256 algorithm.
[0016] Furthermore, the salt value in the master key derivation is a 32-byte random number generated by OpenSSL's RAND_bytes, and the number of iterations is configurable by default.
[0017] Preferably, key rotation is triggered by the PRAGMArekey command. The system starts a write transaction, decrypts the entire database page by page using the old key, re-encrypts it using the new key, updates the salt value and encryption scheme identifier in the reserved space, and finally commits the transaction. If it fails midway, it will automatically roll back to the old key state.
[0018] Preferably, the performance optimization strategy includes at least one of the following: OpenSSL automatically detects CPU AES-NI instruction set support at runtime and enables hardware-accelerated AES implementation; pre-allocates and caches EVP_CIPHER_CTX objects for each database connection to avoid repeatedly creating and destroying the context each time a page is encrypted or decrypted; the encryption buffer is aligned to 16-byte boundaries and standard page encryption (excluding the first page) is performed directly in-place on the buffer provided by SQLite; the initialization of the encryption context is delayed until the first actual I / O operation, and the page size and reserved space size are obtained from the database header and cached when the first page is read for the first time.
[0019] Preferably, the PRAGMA command interface provides encryption algorithm selection, supporting the switching of multiple algorithms such as AES-256-GCM, AES-256-CBC and ChaCha20-Poly1305 at runtime, allowing users to select encryption strategies according to security and performance requirements.
[0020] Preferably, the number of iterations of the PBKDF2-HMAC-SHA512 algorithm is dynamically determined based on the device computing resource index (CRI). Specifically, when the encrypted database is first created or opened, a set of AES-256-GCM baseline encryption operations are performed and the time consumed is measured. The CRI is calculated based on the ratio of the baseline time consumed to the baseline time consumed by the reference platform, and the number of iterations is determined accordingly. Determine using the following formula: ; in, Indicates the baseline iteration number. and These represent the lower and upper limits of the number of iterations, respectively, and CRI represents the device computing resource index.
[0021] Preferably, the Computational Resource Index (CRI) of the device is calculated using the following formula: ; in, Indicates that the current device is executing Total time taken for each AES-256-GCM encryption operation This represents the baseline time taken for the reference platform to perform the same number of AES-256-GCM encryption operations; a CRI greater than 1 indicates that the current device's encryption computing capability is stronger than that of the reference platform, and a CRI less than 1 indicates that the current device's encryption computing capability is weaker than that of the reference platform.
[0022] Preferably, a hierarchical caching management system is implemented for working keys based on page access popularity. Specifically, this involves maintaining a cache of length [length missing]. A sliding window to record recent The page number visited next; the number of times each page number appears within the window is counted, and if the number of occurrences exceeds the popularity threshold... Pages with certain characteristics are marked as hot pages, and the rest are marked as cold pages. Working keys are pre-derived for hot pages and stored in the key cache, while working keys for cold pages are derived on demand each time they are accessed.
[0023] Preferably, the key buffer is managed using an LRU (Least Recently Used) eviction policy, and the maximum buffer capacity is [missing information]. For each key entry, when the key for a new popular page needs to be added to the cache and the cache is full, the least recently accessed key entry is evicted; the popularity threshold... Based on the sliding window length and cache capacity Dynamically adjusted, with values ranging from 1 to 2. .
[0024] Furthermore, the aforementioned This is a 64-bit unsigned monotonically increasing counter maintained per database instance, initially set to 0 and stored in the CryptoContext structure. It atomically increments by 1 after each successful page write operation; it is reset when the database is closed. The current value is written to the header of the database file and persistently saved. When the database is reopened, it is read from the header. The value resumes the counting state; when Reaching the maximum value At that time, a key rotation operation is triggered and the key is reset. It is 0.
[0025] Furthermore, the information strings used when deriving the working key using HKDF-SHA256 are specifically as follows: the information string used for deriving the page encryption key is an ASCII string "sqlite-page-encryption-key-v1" ending with a null character; the information string used for deriving the IV generation key is an ASCII string "sqlite-iv-generation-key-v1" ending with a null character.
[0026] Preferably, when the ChaCha20-Poly1305 algorithm is selected, its nonce is 12 bytes, and the generation method is as follows: the IV-generated key is used as the key input for the ChaCha20 block function, and the page number and... The concatenated and padded 64-byte counter block is used as the plaintext input of the ChaCha20 block function. The ChaCha20 block function is executed once, and the first 12 bytes of the output are taken as the nonce of this page.
[0027] Furthermore, the length of the sliding window Based on the total number of database pages Sure: ,in Sets the default window length.
[0028] Preferably, the upper limit of the key buffer capacity Determined based on available system memory: ; in, This indicates the default cache size. This indicates the number of bytes of physical memory currently available in the system. This indicates the number of bytes of memory occupied by a single working key item. This indicates the number of bytes of memory occupied by a single EVP_CIPHER_CTX context.
[0029] Furthermore, during the AES-256-GCM encryption process, the algorithm identifier passed in when calling EVP_EncryptInit_ex is EVP_aes_256_gcm(), the key is a 256-bit page encryption key, and the IV is 12 bytes; after calling EVP_EncryptFinal_ex, a 16-byte GCM authentication tag is obtained through the EVP_CIPHER_CTX_ctrl interface.
[0030] Preferably, a transparent page-level encryption system for SQLite databases based on OpenSSL includes: The application layer interacts with the database through the standard SQLite API; The SQLite core engine layer includes the SQL parser, query optimizer, B-Tree module, and Pager module; The Crypto-VFS proxy layer implements a complete proxy for the sqlite3_vfs and sqlite3_file interfaces, encapsulates the encryption logic in the xRead and xWrite callback functions, calls the OpenSSL encryption API to perform encryption before writing page data to disk, and performs decryption after reading from disk; The underlying operating system VFS is responsible for the actual file I / O operations; The Pager module calls the xRead / xWrite methods of the Crypto-VFS proxy layer through the VFS interface, and the Crypto-VFS proxy layer interacts with the disk through the underlying VFS. The data flow within the SQLite core engine layer is as follows: SQL statements issued by the application layer are lexically and syntactically analyzed by the SQL parser to generate an abstract syntax tree. The query optimizer performs an equivalent transformation on the abstract syntax tree to generate an execution plan. The B-Tree module locates the page number of the target data based on the execution plan. The Pager module converts the page number into a file offset and then initiates a page read / write request to the Crypto-VFS proxy layer through the VFS interface.
[0031] Preferably, a computer device includes a memory and a processor, which are communicatively connected to each other. The memory stores computer instructions, and the processor executes the computer instructions to perform the aforementioned transparent page-level encryption method for SQLite databases based on OpenSSL.
[0032] Preferably, a computer-readable storage medium stores computer instructions that cause a computer to execute the described OpenSSL-based transparent page-level encryption method for SQLite databases.
[0033] The beneficial effects of this invention are as follows: 1. This invention implements a Crypto-VFS encrypted proxy architecture at the virtual file system layer of SQLite, intercepting all page-level read and write operations by wrapping the underlying operating system's native VFS. Encryption logic is injected entirely through SQLite's exposed VFS interface without modifying any SQLite source code, achieving backward compatibility with any version of SQLite and fundamentally eliminating the patch merging and maintenance costs during version upgrades. Simultaneously, this solution is completely transparent to upper-layer applications; applications can use the encrypted database without any code modifications, and all SQL operations remain consistent with the unencrypted version, maintaining the original query semantic integrity and application integration simplicity. This solves the technical defects of existing encryption schemes, such as high invasiveness leading to high upgrade and maintenance costs, and application-layer encryption destroying query semantics.
[0034] 2. This invention performs AES-256-GCM authentication encryption independently on each database page, ensuring data confidentiality while providing integrity verification capabilities based on GCM authentication tags. This effectively detects and defends against tampering attacks targeting database content. Simultaneously, it utilizes SQLite's own reserved space mechanism to embed the IV and GCM authentication tags in the reserved space at the end of each page. The total page size remains unchanged after encryption, without involving file format changes or page address remapping. This naturally aligns with SQLite's internal B-Tree index and Pager caching mechanism, overcoming the technical deficiency of existing CBC mode non-authentication encryption schemes in failing to detect data tampering.
[0035] 3. This invention achieves hierarchical key isolation management by designing a three-layer key derivation architecture: the user password is used to generate a master key via PBKDF2-HMAC-SHA512, and the master key is used to derive page encryption keys and IV-generated keys via HKDF-SHA256. This reduces the risk of exposing the entire database due to a single-layer key leak. An atomic key rotation mechanism, which decrypts the entire database page by page under transaction protection and re-encrypts it with a new key before submission, ensures that the database can be safely rolled back to the original key state during key replacement interruptions without data corruption. Simultaneously, an adaptive iteration number adjustment mechanism based on device computing resource index and a hierarchical key caching strategy based on page access popularity are introduced to automatically balance key derivation security strength and operational performance on different hardware platforms. This addresses the technical shortcomings of existing encryption schemes, such as simple key management layers and a lack of flexible key derivation and secure rotation capabilities. Attached Figure Description
[0036] Figure 1 This is a system architecture diagram according to an embodiment of the present invention; Figure 2 This is a flowchart of the page encryption process of the xWrite process according to an embodiment of the present invention; Figure 3 This is a flowchart of the page decryption process of the xRead process according to an embodiment of the present invention; Figure 4 This is a diagram illustrating the key derivation and management architecture of an embodiment of the present invention; Figure 5 This is a schematic diagram of the database page structure according to an embodiment of the present invention; Figure 6 This is a flowchart illustrating the key rotation process according to an embodiment of the present invention. Figure 7 This is a bar chart comparing page encryption / decryption latency under different access modes in embodiments of the present invention. Figure 8 The diagram shows the effect of the adaptive PBKDF2 iteration number mechanism in an embodiment of the present invention. Detailed Implementation
[0037] Example 1: like Figures 1-6As shown, a transparent page-level encryption method for SQLite databases based on OpenSSL is implemented. An encryption proxy layer is implemented at the SQLite virtual file system layer. This proxy layer encapsulates the underlying operating system's native VFS, intercepting all page-level read / write operations on the database files. It calls OpenSSL's encryption API to encrypt page data before writing it to disk and decrypt it after reading it from disk. The encryption proxy layer completes its proxying by implementing the sqlite3_vfs and sqlite3_file interfaces, encapsulating the encryption logic in the xRead and xWrite callback functions. The encryption proxy layer is registered as a default or named VFS via sqlite3_vfs_register(), and is enabled by the user during sqlite3_open_v2(). The core data structures include a CryptoVfs structure that inherits from sqlite3_vfs and adds an underlying VFS pointer and global encryption configuration; a CryptoFile structure that inherits from sqlite3_file and adds an encryption context and page size cache; and a CryptoContext structure that stores the master key, page encryption key, IV generation key, and encryption algorithm identifier.
[0038] Preferably, except for the initialization logic of injecting the encrypted file handle in the xOpen method, the other VFS methods are directly delegated to the underlying VFS for execution.
[0039] Preferably, the write operation process is as follows: calculate the page number based on the offset, call the IV generation function to calculate the IV value of the page, use the OpenSSLEVP interface to perform AES-256-GCM encryption, write the IV and GCM authentication tag to the reserved space at the end of the page, and finally write it to the disk through the underlying VFS.
[0040] Preferably, the IV value is calculated using the following formula: ; Take the first 12 bytes as the page IV; where, This indicates the IV generation key. Indicates the page number. This represents the write operation counter.
[0041] Preferably, the AES-256-GCM encryption process sequentially calls EVP_EncryptInit_ex to initialize the encryption context, EVP_EncryptUpdate to encrypt the valid data of the page, and EVP_EncryptFinal_ex to obtain the GCM authentication tag.
[0042] Preferably, the read operation process is as follows: read the encrypted page from the disk, extract the IV and authentication tag from the reserved space, call the OpenSSLEVP decryption interface to decrypt and verify the integrity. If the verification fails, return the SQLITE_IOERR error code; if the verification passes, return the plaintext data to the Pager module.
[0043] Preferably, SQLite's reserved space mechanism is used to reserve a fixed length of space at the end of each page to store IV and GCM authentication tags, so that the total size of the encrypted page remains unchanged and the page addressing mechanism of SQLite is not violated.
[0044] Furthermore, the IV is 12 bytes and the GCM authentication tag is 16 bytes, occupying a total of 28 bytes of reserved space; the reserved space is configured through PRAGMAreserved_page_size.
[0045] Furthermore, the database header page, i.e., page 1, contains a 100-byte file format header, of which the first 16 bytes are a fixed magic string that keeps the plaintext unencrypted, and bytes 17 to (page size minus reserved space size) execute the standard encryption process; the reserved space additionally stores a 4-byte encryption scheme version number and a 32-byte PBKDF2 salt value.
[0046] Preferably, a three-layer key derivation architecture is adopted: the first layer is the user password, provided through the PRAGMAkey command or sqlite3_key() API; the second layer is the master key, derived from the user password using the PBKDF2-HMAC-SHA512 algorithm; the third layer is the working key, including the page encryption key and the IV generation key, both derived from the master key using different information strings through the HKDF-SHA256 algorithm.
[0047] Furthermore, the salt value in the master key derivation is a 32-byte random number generated by OpenSSL's RAND_bytes, and the number of iterations is configurable by default.
[0048] Preferably, key rotation is triggered by the PRAGMArekey command. The system starts a write transaction, decrypts the entire database page by page using the old key, re-encrypts it using the new key, updates the salt value and encryption scheme identifier in the reserved space, and finally commits the transaction. If it fails midway, it will automatically roll back to the old key state.
[0049] Preferably, the performance optimization strategy includes at least one of the following: OpenSSL automatically detects CPU AES-NI instruction set support at runtime and enables hardware-accelerated AES implementation; pre-allocates and caches EVP_CIPHER_CTX objects for each database connection to avoid repeatedly creating and destroying the context each time a page is encrypted or decrypted; the encryption buffer is aligned to 16-byte boundaries and standard page encryption (excluding the first page) is performed directly in-place on the buffer provided by SQLite; the initialization of the encryption context is delayed until the first actual I / O operation, and the page size and reserved space size are obtained from the database header and cached when the first page is read for the first time.
[0050] Preferably, the PRAGMA command interface provides encryption algorithm selection, supporting the switching of multiple algorithms such as AES-256-GCM, AES-256-CBC and ChaCha20-Poly1305 at runtime, allowing users to select encryption strategies according to security and performance requirements.
[0051] Preferably, the number of iterations of the PBKDF2-HMAC-SHA512 algorithm is dynamically determined based on the device computing resource index (CRI). Specifically, when the encrypted database is first created or opened, a set of AES-256-GCM baseline encryption operations are performed and the time consumed is measured. The CRI is calculated based on the ratio of the baseline time consumed to the baseline time consumed by the reference platform, and the number of iterations is determined accordingly. Determine using the following formula: ; in, Indicates the baseline iteration number. and These represent the lower and upper limits of the number of iterations, respectively, and CRI represents the device computing resource index.
[0052] Preferably, the Computational Resource Index (CRI) of the device is calculated using the following formula: ; in, Indicates that the current device is executing Total time taken for each AES-256-GCM encryption operation This represents the baseline time taken for the reference platform to perform the same number of AES-256-GCM encryption operations; a CRI greater than 1 indicates that the current device's encryption computing capability is stronger than that of the reference platform, and a CRI less than 1 indicates that the current device's encryption computing capability is weaker than that of the reference platform.
[0053] Preferably, a hierarchical caching management system is implemented for working keys based on page access popularity. Specifically, this involves maintaining a cache of length [length missing]. A sliding window to record recent The page number visited next; the number of times each page number appears within the window is counted, and if the number of occurrences exceeds the popularity threshold... Pages with certain characteristics are marked as hot pages, and the rest are marked as cold pages. Working keys are pre-derived for hot pages and stored in the key cache, while working keys for cold pages are derived on demand each time they are accessed.
[0054] Preferably, the key buffer is managed using an LRU (Least Recently Used) eviction policy, and the maximum buffer capacity is [missing information]. For each key entry, when the key for a new popular page needs to be added to the cache and the cache is full, the least recently accessed key entry is evicted; the popularity threshold... Based on the sliding window length and cache capacity Dynamically adjusted, with values ranging from 1 to 2. .
[0055] Furthermore, the aforementioned This is a 64-bit unsigned monotonically increasing counter maintained per database instance, initially set to 0 and stored in the CryptoContext structure. It atomically increments by 1 after each successful page write operation; it is reset when the database is closed. The current value is written to the header of the database file and persistently saved. When the database is reopened, it is read from the header. The value resumes the counting state; when Reaching the maximum value At that time, a key rotation operation is triggered and the key is reset. It is 0.
[0056] Furthermore, the information strings used when deriving the working key using HKDF-SHA256 are specifically as follows: the information string used for deriving the page encryption key is an ASCII string "sqlite-page-encryption-key-v1" ending with a null character; the information string used for deriving the IV generation key is an ASCII string "sqlite-iv-generation-key-v1" ending with a null character.
[0057] Preferably, when the ChaCha20-Poly1305 algorithm is selected, its nonce is 12 bytes, and the generation method is as follows: the IV-generated key is used as the key input for the ChaCha20 block function, and the page number and... The concatenated and padded 64-byte counter block is used as the plaintext input of the ChaCha20 block function. The ChaCha20 block function is executed once, and the first 12 bytes of the output are taken as the nonce of this page.
[0058] Furthermore, the length of the sliding window Based on the total number of database pages Sure: ,in Sets the default window length.
[0059] Preferably, the upper limit of the key buffer capacity Determined based on available system memory: ; in, This indicates the default cache size. This indicates the number of bytes of physical memory currently available in the system. This indicates the number of bytes of memory occupied by a single working key item. This indicates the number of bytes of memory occupied by a single EVP_CIPHER_CTX context.
[0060] Furthermore, during the AES-256-GCM encryption process, the algorithm identifier passed in when calling EVP_EncryptInit_ex is EVP_aes_256_gcm(), the key is a 256-bit page encryption key, and the IV is 12 bytes; after calling EVP_EncryptFinal_ex, a 16-byte GCM authentication tag is obtained through the EVP_CIPHER_CTX_ctrl interface.
[0061] Preferably, a transparent page-level encryption system for SQLite databases based on OpenSSL includes: The application layer interacts with the database through the standard SQLite API; The SQLite core engine layer includes the SQL parser, query optimizer, B-Tree module, and Pager module; The Crypto-VFS proxy layer implements a complete proxy for the sqlite3_vfs and sqlite3_file interfaces, encapsulates the encryption logic in the xRead and xWrite callback functions, calls the OpenSSL encryption API to perform encryption before writing page data to disk, and performs decryption after reading from disk; The underlying operating system VFS is responsible for the actual file I / O operations; The Pager module calls the xRead / xWrite methods of the Crypto-VFS proxy layer through the VFS interface, and the Crypto-VFS proxy layer interacts with the disk through the underlying VFS. The data flow within the SQLite core engine layer is as follows: SQL statements issued by the application layer are lexically and syntactically analyzed by the SQL parser to generate an abstract syntax tree. The query optimizer performs an equivalent transformation on the abstract syntax tree to generate an execution plan. The B-Tree module locates the page number of the target data based on the execution plan. The Pager module converts the page number into a file offset and then initiates a page read / write request to the Crypto-VFS proxy layer through the VFS interface.
[0062] Preferably, a computer device includes a memory and a processor, which are communicatively connected to each other. The memory stores computer instructions, and the processor executes the computer instructions to perform the aforementioned transparent page-level encryption method for SQLite databases based on OpenSSL.
[0063] Preferably, a computer-readable storage medium stores computer instructions that cause a computer to execute the described OpenSSL-based transparent page-level encryption method for SQLite databases.
[0064] Example 2: This embodiment uses industrial IoT edge gateway data storage as an application scenario. The edge gateway device is deployed on the factory production line side and collects data from four types of sensors—temperature, vibration, pressure, and flow—from the PLC controller via the Modbus TCP protocol at a frequency of 1Hz. Each record contains seven fields: device ID, timestamp, values of the four types of sensors, and status flags. Approximately 86,400 records are generated daily. The gateway device uses SQLite 3.43.2 as its local embedded database, and the database file is stored in eMMC flash memory. The experimental platform is divided into two groups: the edge gateway embedded platform uses an ARM Cortex-A72 processor (1.8GHz, 4 cores, 4GB LPDDR4 memory), and the comparison server platform uses an Intel Xeon Gold 6326 processor (2.9GHz, 32 cores, 64GB DDR5 memory), both running the Linux kernel 5.15 operating system.
[0065] This method's parameter settings are as follows: It employs the AES-256-GCM authentication encryption algorithm. The page encryption key and IV generation key are obtained from the user password via PBKDF2-HMAC-SHA512 (adaptive iteration count). , , The derived master key is generated using HKDF-SHA256, with a salt value of a 32-byte random number; the database page size is set to 4096 bytes, and the reserved space size is set to 32 bytes (12-byte IV + 16-byte GCM authentication tag + 4-byte encryption scheme version number); the key buffer has a preset capacity. bar, sliding window length Second visit, popularity threshold CRI benchmark execution The benchmark time for each AES-256-GCM encryption operation was measured. The comparison methods used were SQLCipher 4.5.3, employing AES-256-CBC+HMAC-SHA512 mode, PBKDF2-HMAC-SHA512 iterations (256,000), and a page size of 4096 bytes; and the official SQLite SEE, employing AES-256-GCM mode and a page size of 4096 bytes. Both methods were run on identical hardware platforms and datasets, with WAL mode disabled and traditional rollback logs used. Example procedure: A test database containing approximately 500,000 records was first constructed using a dataset generation tool. Four configurations—the unencrypted baseline, this method, SQLCipher, and SEE—were run sequentially on three hardware platforms. Performance metrics were collected using Linux perfstat and SQLite's built-in PRAGMAdatabase_list and .timer commands. Each experiment was repeated 10 times, and the average value was taken. The data are shown in Tables 1 and 2 below.
[0066] Table 1. Performance data of this method under different platforms and access modes;
[0067] Table 2: Performance and security comparison of this method with existing technologies on the Cortex-A72 platform;
[0068] The experimental data above shows that the average page read / write latency of this method on the embedded platform Cortex-A72 is 111. and 156 Compared to the unencrypted baseline, this method achieves approximately 4-5 times higher encryption overhead while maintaining acceptable encryption performance on resource-constrained devices. Compared to SQLCipher 4.5.3, this method reduces read latency by 21.8% and write latency by 21.2%, primarily due to EVP context reuse eliminating context allocation overhead for each encryption / decryption operation, and the page-heat-based key caching strategy reducing the overhead of repeated key derivation for high-frequency pages. The method's advantage in key derivation time is even more pronounced: on Cortex-A72, the adaptive iteration count mechanism reduces the PBKDF2 iteration count to 16,000 (CRI=0.0625), stabilizing the key derivation time at 195ms, while SQLCipher's fixed 256,000 iterations take as long as 3.2s, resulting in a 16.4-fold improvement in database open response speed. Simultaneously, on the Xeon server platform, the adaptive mechanism automatically increases the iteration count to 602,000, further enhancing security by 2.4 times compared to the fixed scheme, achieving an automatic balance between key derivation performance and security strength.
[0069] In terms of functional completeness, this method supports both AES-256-GCM authenticated encryption and AES-256-CBC unauthenticated encryption modes, allowing users to flexibly choose according to their different security and performance requirements. In contrast, neither SQLCipher nor SEE supports runtime algorithm switching. The non-intrusive design of this method allows it to be deployed on all SQLite versions without modifying the SQLite kernel source code, completely eliminating the patch merging and maintenance burden associated with each SQLite version upgrade for SQLCipher. Furthermore, this method introduces a page access popularity-based key caching strategy, achieving a key cache hit rate of 97.2% and 88.6% in high-frequency access scenarios, effectively eliminating the overhead of duplicate key derivation for high-frequency pages—a mechanism absent in SQLCipher and SEE. Overall, this method achieves an average performance of 111 in authenticated encryption scenarios on embedded platforms. The page read latency enables transparent encryption of the entire database, and the key derivation time is consistently within 200ms. It also provides flexible features not found in existing solutions, such as algorithm runtime switching, zero-intrusive deployment, and adaptive security strength adjustment. It has significant advantages in terms of fast opening response of encrypted databases and cross-platform deployment adaptability.
[0070] Figure 7This study demonstrates the latency introduced by encryption operations under four typical page access modes on the Intel Core i7-13700 platform. It shows that with EVP context reuse enabled, the average page latency for AES-256-GCM encryption is 35-55μs, a reduction of approximately 44% compared to the 68-98μs latency without EVP reuse, proving the effectiveness of EVP context reuse optimization. Encryption latency mainly consists of the authentication encryption calculations for AES-GCM and the HMAC operations in IV generation. EVP context reuse eliminates the overhead of repeatedly allocating and initializing the encryption context for each page operation, keeping page encryption / decryption latency at a low level. While there is still an approximately 3-4 times increase in latency compared to the unencrypted baseline, this has a manageable impact on the overall database I / O performance.
[0071] Figure 8 The top subplot compares the key derivation time on three different hardware platforms with a fixed 256,000 iterations versus an adaptive number of iterations. Under the fixed scheme, Xeon Gold takes only 85ms, while Cortex-A72 takes a whopping 3.2s, resulting in a significant difference in user experience. The adaptive scheme dynamically adjusts the number of iterations using the device's computational resource index (CRI), stabilizing the key derivation time across all three platforms within the 182-198ms range. The bottom subplot shows the adaptively allocated number of iterations for each platform: Xeon Gold achieves 602,000 iterations, while Cortex-A72, although reduced to 16,000 iterations, still exceeds the NIST-recommended minimum threshold of 10,000 iterations, ensuring basic security strength even on resource-constrained platforms.
Claims
1. An OpenSSL-based SQLite database transparent page-level encryption method, characterized in that, An encryption proxy layer is implemented at the SQLite virtual file system layer. This layer encapsulates the underlying operating system's native VFS, intercepts all page-level read and write operations on database files, and calls OpenSSL's encryption API to encrypt page data before writing it to disk and decrypt it after reading it from disk. The encryption proxy layer completes its proxying by implementing the sqlite3_vfs and sqlite3_file interfaces, encapsulating the encryption logic in the xRead and xWrite callback functions. The encryption proxy layer is registered as a default or named VFS via sqlite3_vfs_register() and enabled by the user during sqlite3_open_v2(). The core data structures include the CryptoVfs structure, which inherits from sqlite3_vfs and adds an underlying VFS pointer and global encryption configuration; the CryptoFile structure, which inherits from sqlite3_file and adds an encryption context and page size cache; and the CryptoContext structure, which stores the master key, page encryption key, IV generation key, and encryption algorithm identifier.
2. The OpenSSL-based SQLite database transparent page-level encryption method according to claim 1, wherein, Except for the initialization logic of injecting the encrypted file handle in the xOpen method, the other VFS methods are directly delegated to the underlying VFS for execution.
3. The OpenSSL-based SQLite database transparent page-level encryption method according to claim 1, wherein, The write operation process is as follows: calculate the page number based on the offset, call the IV generation function to calculate the IV value of the page, use the OpenSSLEVP interface to perform AES-256-GCM encryption, write the IV and GCM authentication tag to the reserved space at the end of the page, and finally write it to the disk through the underlying VFS.
4. The OpenSSL-based SQLite database transparent page-level encryption method of claim 3, wherein, The IV value is calculated using the following formula: ; Take the first 12 bytes as page IV; wherein, denotes the IV generation key, denotes the page number, denotes the write operation counter.
5. The OpenSSL-based SQLite database transparent page-level encryption method according to claim 3, wherein, The AES-256-GCM encryption process sequentially calls EVP_EncryptInit_ex to initialize the encryption context, EVP_EncryptUpdate to encrypt the valid data on the page, and EVP_EncryptFinal_ex to obtain the GCM authentication tag.
6. The OpenSSL-based SQLite database transparent page-level encryption method of claim 1, wherein, The read operation process is as follows: read the encrypted page from the disk, extract the IV and authentication tag from the reserved space, call the OpenSSLEVP decryption interface to decrypt and verify the integrity. If the verification fails, return the SQLITE_IOERR error code; if the verification passes, return the plaintext data to the Pager module.
7. The OpenSSL-based SQLite database transparent page-level encryption method of claim 1, wherein, By utilizing SQLite's reserved space mechanism, a fixed length of space is reserved at the end of each page to store IV and GCM authentication tags, so that the total size of the encrypted page remains unchanged and the page addressing mechanism of SQLite is not violated.
8. The OpenSSL-based SQLite database transparent page-level encryption method of claim 1, wherein, A three-layer key derivation architecture is adopted: the first layer is the user password, which is provided through the PRAGMAkey command or the sqlite3_key() API; the second layer is the master key, which is derived from the user password using the PBKDF2-HMAC-SHA512 algorithm; the third layer is the working key, which includes the page encryption key and the IV generation key, both of which are derived from the master key using different information strings through the HKDF-SHA256 algorithm.
9. The OpenSSL-based SQLite database transparent page-level encryption method of claim 1, wherein, Key rotation is triggered by the PRAGMArekey command. The system starts a write transaction, decrypts the entire database page by page using the old key, re-encrypts it using the new key, updates the salt value and encryption scheme identifier in the reserved space, and finally commits the transaction. If it fails midway, it will automatically roll back to the old key state.
10. The OpenSSL-based SQLite database transparent page-level encryption method of claim 1, wherein, Performance optimization strategies include at least one of the following: OpenSSL automatically detects CPU AES-NI instruction set support at runtime and enables hardware-accelerated AES implementation; pre-allocates and caches EVP_CIPHER_CTX objects for each database connection to avoid repeatedly creating and destroying the context each time a page is encrypted or decrypted; the encryption buffer is aligned to 16-byte boundaries and standard page encryption (excluding the first page) is performed directly in-place on the buffer provided by SQLite; the initialization of the encryption context is delayed until the first actual I / O operation, and the page size and reserved space size are obtained from the database header and cached when the first page is read for the first time.
11. The OpenSSL-based SQLite database transparent page-level encryption method of claim 1, wherein, The PRAGMA command interface provides encryption algorithm selection, supporting the switching between multiple algorithms such as AES-256-GCM, AES-256-CBC, and ChaCha20-Poly1305 at runtime, allowing users to choose the encryption strategy according to their security and performance requirements.
12. The transparent page-level encryption method for SQLite database based on OpenSSL according to claim 9, characterized in that, The number of iterations of the PBKDF2-HMAC-SHA512 algorithm is dynamically determined based on the device's Computational Resource Index (CRI). Specifically, when the encrypted database is first created or opened, a set of AES-256-GCM baseline encryption operations are performed and the time consumed is measured. The CRI is calculated based on the ratio of the baseline time consumed to the baseline time consumed by the reference platform. The number of iterations is then determined. Determine using the following formula: ; in, Indicates the baseline iteration number. and These represent the lower and upper limits of the number of iterations, respectively, and CRI represents the device computing resource index.
13. The transparent page-level encryption method for SQLite database based on OpenSSL according to claim 12, characterized in that, The Computational Resource Index (CRI) of the equipment is calculated using the following formula: ; in, Indicates that the current device is executing Total time taken for each AES-256-GCM encryption operation This represents the baseline time taken for the reference platform to perform the same number of AES-256-GCM encryption operations; a CRI greater than 1 indicates that the current device's encryption computing capability is stronger than that of the reference platform, and a CRI less than 1 indicates that the current device's encryption computing capability is weaker than that of the reference platform.
14. The transparent page-level encryption method for SQLite database based on OpenSSL according to claim 1, characterized in that, Implement tiered caching management for working keys based on page access popularity. The specific process is as follows: Maintain a cache of length... A sliding window to record recent The page number visited next; the number of times each page number appears within the window is counted, and if the number of occurrences exceeds the popularity threshold... Pages with certain characteristics are marked as hot pages, and the rest are marked as cold pages. Working keys are pre-derived for hot pages and stored in the key cache, while working keys for cold pages are derived on demand each time they are accessed.
15. The transparent page-level encryption method for SQLite database based on OpenSSL according to claim 14, characterized in that, The key buffer is managed using an LRU (Least Recently Used) eviction policy, and its maximum capacity is [missing information]. For each key entry, when the key for a new popular page needs to be added to the cache and the cache is full, the least recently accessed key entry is evicted; the popularity threshold... Based on the sliding window length and cache capacity Dynamically adjusted, with values ranging from 1 to 2. .
16. The transparent page-level encryption method for SQLite database based on OpenSSL according to claim 11, characterized in that, When the ChaCha20-Poly1305 algorithm is selected, its nonce is 12 bytes, generated as follows: the IV-generated key is used as the key input for the ChaCha20 block function, and the page number and... The concatenated and padded 64-byte counter block is used as the plaintext input of the ChaCha20 block function. The ChaCha20 block function is executed once, and the first 12 bytes of the output are taken as the nonce of this page.
17. The transparent page-level encryption method for SQLite database based on OpenSSL according to claim 15, characterized in that, The maximum capacity of the key buffer Determined based on available system memory: ; in, This indicates the default cache size. This indicates the number of bytes of physical memory currently available in the system. This indicates the number of bytes of memory occupied by a single working key item. This indicates the number of bytes of memory occupied by a single EVP_CIPHER_CTX context.
18. A transparent page-level encryption system for SQLite databases based on OpenSSL, used to execute the transparent page-level encryption method for SQLite databases based on OpenSSL as described in any one of claims 1-17, characterized in that, include: The application layer interacts with the database through the standard SQLite API; The SQLite core engine layer includes the SQL parser, query optimizer, B-Tree module, and Pager module; The Crypto-VFS proxy layer implements a complete proxy for the sqlite3_vfs and sqlite3_file interfaces, encapsulates the encryption logic in the xRead and xWrite callback functions, calls the OpenSSL encryption API to perform encryption before writing page data to disk, and performs decryption after reading from disk; The underlying operating system VFS is responsible for the actual file I / O operations; The Pager module calls the xRead / xWrite methods of the Crypto-VFS proxy layer through the VFS interface, and the Crypto-VFS proxy layer interacts with the disk through the underlying VFS. The data flow within the SQLite core engine layer is as follows: SQL statements issued by the application layer are lexically and syntactically analyzed by the SQL parser to generate an abstract syntax tree. The query optimizer performs an equivalent transformation on the abstract syntax tree to generate an execution plan. The B-Tree module locates the page number of the target data based on the execution plan. The Pager module converts the page number into a file offset and then initiates a page read / write request to the Crypto-VFS proxy layer through the VFS interface.
19. A computer device, characterized in that, It includes a memory and a processor, which are interconnected and communicate with each other. The memory stores computer instructions, and the processor executes the computer instructions to perform the transparent page-level encryption method for SQLite database based on OpenSSL, as described in any one of claims 1 to 17.
20. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions for causing the computer to execute any one of the OpenSSL-based SQLite database transparent page-level encryption methods as described in any one of claims 1 to 17.
