diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000000..e2f2e2a0a8 --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1 @@ +We ask that you submit a contributor agreement in order for us to accept this request. More information about this agreement can be found [here](https://www.zetetic.net/contributions/). Pull requests are merged into the `prerelease` branch. Please let us know if you have any further questions. Thanks! diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000000..0044cb52c7 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,9 @@ +### Expected Behavior + +### Actual Behavior + +### Steps to Reproduce + +SQLCipher version: + +*Note:* If you are not posting a specific issue for the SQLCipher library, please consider posting your question to the SQLCipher [discuss site](https://discuss.zetetic.net/c/sqlcipher). Thanks! diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000000..51264e295e --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,6 @@ + + +Changes proposed in this pull request: +- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..f68644eb92 --- /dev/null +++ b/.gitignore @@ -0,0 +1,40 @@ +*.lo +*.o +*.mode1v3 +*.pbxuser +xcuserdata +xcuserdata/* +*.xcworkspace/* +*.xcworkspace +/Makefile +/.libs +/.target_source +/config.h +/config.log +/config.status +/keywordhash.h +/mkkeywordhash* +/opcodes.c +/opcodes.h +/parse.c +/parse.h +/parse.h.temp +/parse.out +/tsrc +/testfixture* +/lemon* +/libtool +/libsqlite3.la +/libtclsqlite3.la +/sqlite3 +/sqlite3.c +/sqlite3.h +/lempar.c +/parse.y +/shell.c +/sqlcipher +/sqlite3.pc +/sqlcipher.pc +/sqlite3ext.h +/libsqlcipher.la +/.DS_Store diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000000..357d62bd56 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,392 @@ +# SQLCipher Change Log +Notable changes to this project are documented in this file. + +## [unreleased] - (? 2025 - [unreleased changes]) + +## [4.9.0] - (May 2025 - [4.9.0 changes]) +- Updates baseline to upstream SQLite 3.46.2 +- Removes use of static mutex in `sqlcipher_extra_shutdown()` + +## [4.8.0] - (April 2025 - [4.8.0 changes]) +- Fixes regression in `PRAGMA cipher_migrate` where an error would be thrown when migrating a current-version database +- Adds selective locking in critical sections of the library for shared cache connections (Note: use of shared cache is still strongly discouraged) +- Standardizes initial private heap size to 48KB to ensure mlock under constrained limits +- Removes changes to windows working set sizes +- Improvements to logging of memory stats and other cleanup + +## [4.7.0] - (March 2025 - [4.7.0 changes]) +- Updates baseline to upstream SQLite 3.49.1, including complete upstream SQLite refactoring of build system to use autosetup +- Significantly refactors and optimizes library initialization and cleanup +- Allocates majority of requisite memory at startup to improve memory locking on constrained platforms (i.e. Android and Windows) and reduce fragmentation +- Expands `sqlcipher_provider` interface to include `init` and `shutdown` functions +- Adds support for `.recover` shell command on corrupt databases with a full plaintext first page +- Performs fast random overwrite of freed memory segments for improved security +- Adds basic obfuscation of context key material for improved security +- Generates keyspecs dynamically on demand instead of storing them +- Expands keyspec/raw key format to accept key, HMAC key, and salt +- Improves error handling in `sqlcipher_export()` and `PRAGMA cipher_migrate` +- Allows setting custom compile-time default cryptographic provider via the `SQLCIPHER_CRYPTO_CUSTOM` macro +- Removes support for end-of-life OpenSSL versions older than 3.0 +__BREAKING CHANGE__: `SELECT` statements (now also including schema independent queries like `SELECT 1`) cannot be executed on encrypt ed databases prior to setting the database key (behavior inherited from upstream SQLite) +- __BREAKING CHANGE__: Renames `configure` flag `--enable-tempstore=yes` to `--with-tempstore=yes` for alignment with SQLite (change required for upstream SQLite autosetup) +- __BREAKING CHANGE__: Renames default executable and library build outputs from `sqlcipher` and `libsqlcipher` to `sqlite3` and `libsqlite3` (for alignment with SQLite) +- __BREAKING CHANGE__: Removes `configure` flag `--with-crypto-lib` (replace with appropriate `-DSQLCIPHER_CRYPTO_*` CFLAG) +- __BREAKING CHANGE__: Requires defining `SQLITE_EXTRA_INIT=sqlcipher_extra_init` and `SQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown` at compile time for optimized library initialization and cleanup +- __BREAKING CHANGE__: Enforces thread safe mode (i.e. `SQLITE_THREADSAFE` of 1 or 2) and temporary storage (i.e. `SQLITE_TEMP_STORE` of 2 or 3) settings at compile time + +## [4.6.1] - (August 2024 - [4.6.1 changes]) +- Updates baseline to upstream SQLite 3.46.1 +- Significant refactor to merge `crypto.h`, `crypto.c`, and `crypto_impl.c` into a single `sqlcipher.c` source file for simplicity. +- Updates minimum working set size on windows to increase lockable pages +- Adds new `PRAGMA cipher_log_source` for filtering log output on higher verbosity levels +- Improves log output by including the log level and source prior to message +- Improves error logging in `PRAGMA cipher_migrate` +- Fixes issue where log level and target would be overwritten if set prior to initialization +- Corrects Podspec license element to use specific BSD 3 Clause +- Fixes default log output to console for macOS + +## [4.6.0] - (May 2024 - [4.6.0 changes]) +- Sets default log level to WARN +- Sends default log output to: logcat for Android; Console for iOS and macOS; and stderr for all other platforms +- General improvements to log level assignments, output, and sanitization +- Fixes Apple Privacy Manifest by removing empty NSPrivacyCollectedDataType from PrivacyInfo.xcprivacy +- Moves Swift support defines for podspec user_target_xcconfig so they only apply to the consuming project + +## [4.5.7] - (April 2024 - [4.5.7 changes]) +- Updates baseline to upstream SQLite 3.45.3 +- Adds "device" logging and profile target using os_log for Apple (and logcat on Android) +- Fixes issues compiling with SQLITE_OMIT_LOG +- fixes malformed man page caused by old merge conflict +- Updates podspec for current Xcode versions, improved Swift support, and Privacy Manifest + +## [4.5.6] - (January 2024 - [4.5.6 changes]) +- Updates baseline to upstream SQLite 3.44.2 +- Improve PRAGMA cipher_integrity check to report expected page size if invalid +- Implement PRAGMA page_size compatibility with PRAGMA cipher_page_size so both will operate properly on encrypted databases +- Updates LICENSE.md with SQLCipher license to avoid ambiguity and remove redundance + +## [4.5.5] - (August 2023 - [4.5.5 changes]) +- Updates baseline to upstream SQLite 3.42.0 +- Do not allow key to be changed on a connection after it has been successfully used for an encryption or decryption operation to prevent accidental database corruption +- Raise an error if a rekey operation is attempted on an unencrypted database +- Raise an error when a key or rekey operation is passed an empty key +- Minor improvements to constant time functions +- Miscellaneous code and comment cleanup + +## [4.5.4] - (April 2023 - [4.5.4 changes]) +- Updates baseline to upstream SQLite 3.41.2 +- Updates minimum Apple SDK versions in podspec for new Xcode compatibility +- Return runtime OpenSSL version from PRAGMA cipher_provider_version (instead of hardcoded value) +- Adds guard against zero block size and crash if cryptographic provider initialization fails +- When an ATTACH occurs creating a new encrypted database as the first operation after keying the main database, the new database will have the same salt value. + +## [4.5.3] - (December 2022 - [4.5.3 changes]) +- Updates baseline to upstream SQLite 3.39.4 + +## [4.5.2] - (August 2022 - [4.5.2 changes]) +- Updates source code baseline to upstream SQLite 3.39.2 +- Simplifies OpenSSL version conditional code +- Fixes issue where PRAGMA cipher_memory_security could report OFF when it was actually ON +- Fixes fix unfreed OpenSSL allocation when compiled against version 3 +- Fixes support for building against recent versions of BoringSSL + +## [4.5.1] - (March 2022 - [4.5.1 changes]) +- Updates source code baseline to upstream SQLite 3.37.2 +- Adds PRAGMA cipher_log and cipher_log_level features to allow logging of TRACE, DEBUG, INFO, WARN, and ERROR messages to stdout, stderr, file, or logcat +- Modifies PRAGMA cipher_profile to use sqlite3_trace_v2 and adds logcat target for Android +- Updates OpenSSL provider to use EVP_MAC API with version 3+ +- Adds new PRAGMA cipher_test_on, cipher_test_off, and cipher_test_rand (available when compiled with -DSQLCIPHER_TEST) to facilitate simulation of error conditions +- Fixes PRAGMA cipher_integrity_check to work properly with databases larger that 2GB +- Fixes missing munlock before free for context internal buffer (thanks to Fedor Indutny) + +## [4.5.0] - (October 2021 - [4.5.0 changes]) +- Updates baseline to upstream SQLite 3.36.0 +- Changes the enhanced memory security feature to be DISABLED by default; once enabled by PRAGMA cipher_memory_security = ON, it can't be turned off for the lifetime of the process +- Changes PRAGMA cipher_migrate to permanently enter an error state if a migration fails +- Fixes memory locking/unlocking issue with realloc implementation on hardened runtimes when memory security is enabled +- Fixes cipher_migrate to cleanup the temporary database if a migration fails +- Removes logging of non-string pointers when compiling with trace level logging + +## [4.4.3] - (February 2021 - [4.4.3 changes]) +- Updates baseline to ustream SQLite 3.34.1 +- Fixes sqlcipher_export handling of NULL parameters +- Removes randomization of rekey-delete tests to avoid false test failures +- Changes internal usage of sqlite_master to sqlite_schema +- Omits unusued profiling function under certain defines to avoid compiler warnings + +## [4.4.2] - (November 2020 - [4.4.2 changes]) +- Improve error handling to resolve potential corruption if an encryption operation failed while operating in WAL mode +- Changes to OpenSSL library cryptographic provider to reduce initialization complexity +- Adjust cipher_integrity_check to skip locking page to avoid a spurious error report for very large databases +- Miscellaneous code and comment cleanup + +## [4.4.1] - (October 2020 - [4.4.1 changes]) +- Updates baseline to upstream SQLite 3.33.0 +- Fixes double-free bug in cipher_default_plaintext_header_size +- Changes SQLCipher tests to use suite runner +- Improvement to cipher_integrity_check tests to minimize false negatives +- Deprecates PRAGMA cipher_store_pass + +## [4.4.0] - (May 2020 - [4.4.0 changes]) +- Updates baseline to upstream SQLite 3.31.0 +- Adjusts shell to report SQLCipher version alongside SQLite version +- Fixes various build warnings under several compilers +- Removes unused id and status functions from provider interface + +## [4.3.0] - (November 2019 - [4.3.0 changes]) +- Updates baseline to upstream SQLite 3.30.1 +- PRAGMA key now returns text result value "ok" after execution +- Adjusts backup API so that encrypted to encrypted backups are permitted +- Adds NSS crypto provider implementation +- Fixes OpenSSL provider compatibility with BoringSSL +- Separates memory related traces to reduce verbosity of logging +- Fixes output of PRAGMA cipher_integrity_check on big endian platforms +- Cryptograpic provider interface cleanup +- Rework of mutex allocation and management +- Resolves miscellaneous build warnings +- Force error state at database pager level if SQLCipher initialization fails + +## [4.2.0] - (May 2019 - [4.2.0 changes]) +- Adds PRAGMA cipher_integrity_check to perform independent verification of page HMACs +- Updates baseline to upstream SQLite 3.28.0 +- Improves PRAGMA cipher_migrate to handle keys containing non-terminating zero bytes + +## [4.1.0] - (March 2019 - [4.1.0 changes]) +- Defer reading salt from header until key derivation is triggered +- Clarify usage of sqlite3_rekey for plaintext databases in header +- Normalize attach behavior when key is not yet derived +- Adds PRAGMA cipher_settings to query current database codec settings +- Adds PRAGMA cipher_default_settings to query current default SQLCipher options +- PRAGMA cipher_hmac_pgno is now deprecated +- PRAGMA cipher_hmac_salt_mask is now deprecated +- PRAGMA fast_kdf_iter is now deprecated +- Improve sqlcipher_export routine and restore all database flags +- Clear codec data buffers if a crypographic provider operation fails +- Disable backup API for encrypted databases (this was previously documented as not-working and non-supported, but will now explicitly error out on initialization) +- Updates baseline to upstream SQLite 3.27.2 + +## [4.0.1] - (December 2018 - [4.0.1 changes]) +- Based on upstream SQLite 3.26.0 (addresses SQLite “Magellan” issue) +- Adds PRAGMA cipher_compatibility and cipher_default_compatibility which take automatcially configure appropriate compatibility settings for the specified SQLCipher major version number +- Filters attach statements with KEY parameters from readline history +- Fixes crash in command line shell with empty input (i.e. ^D) +- Fixes warnings when compiled with strict-prototypes + +## [4.0.0] - (November 2018 - [4.0.0 changes]) +### Changed +- Default page size for databases increased to 4096 bytes (up from 1024) * +- Default PBKDF2 iterations increased to 256,000 (up from 64,000) * +- Default KDF algorithm is now PBKDF2-HMAC-SHA512 (from PBKDF2-HMAC-SHA1) * +- Default HMAC algorithm is now HMAC-SHA512 (from HMAC-SHA1) * +- PRAGMA cipher is now disabled and no longer supported (after multi-year deprecation) * +- PRAGMA rekey_cipher is now disabled and no longer supported * +- PRAGMA rekey_kdf_iter is now disabled and no longer supported * +- By default all memory allocated internally by SQLite before the memory is wiped before it is freed +- PRAGMA cipher_memory_security: allows full memory wiping to be disabled for performance when the feature is not required +- PRAGMA cipher_kdf_algorithm, cipher_default_kdf_algorithm to control KDF algorithm selection between PBKDF2-HMAC-SHA1, PBKDF2-HMAC-SHA256 and PBKDF2-HMAC-SHA512 +- PRAGMA cipher_hmac_algorithm, cipher_default_hmac_algorithm to control HMAC algorithm selection between HMAC-SHA1, HMAC-SHA256 and PBKDF2-HMAC-SHA512 +- Based on upstream SQLite 3.25.2 +- When compiled with readline support, PRAGMA key and rekey lines will no longer be + saved to history +- Adds second optional parameter to sqlcipher_export to specify source database to + support bidirectional exports +- Fixes compatibility with LibreSSL 2.7.0+ +- Fixes compatibility with OpenSSL 1.1.x +- Simplified and improved performance for PRAGMA cipher_migrate when migrating older database versions +- Refactoring of SQLCipher tests into separate files by test type +- PRAGMA cipher_plaintext_header_size and cipher_default_plaintext_header_size: allocates a portion of the database header which will not be encrypted to allow identification as a SQLite database +- PRAGMA cipher_salt: retrieve or set the salt value for the database +- Adds Podspec for using tagged versions of SQLCipher +- Define SQLCIPHER_PROFILE_USE_FOPEN for WinXP support +- Improved error handling for cryptographic providers +- Improved memory handling for PRAGMA commands that return values +- Improved version reporting to assist with identification of distribution +- Major rewrite and simplification of internal codec and pager extension +- Fixes compilation with --disable-amalgamation +- Removes sqlcipher.xcodeproj build support + +## [3.4.2] - (December 2017 - [3.4.2 changes]) +### Added +- Added support for building with LibreSSL + +### Changed +- Merge upstream SQLite 3.20.1 +- Text strings for `SQLITE_ERROR` and `SQLITE_NOTADB` changed to match upstream SQLite +- Remove static modifier for codec password functions +- Page alignment for `mlock` +- Fix segfault in `sqlcipher_cipher_ctx_cmp` during rekey operation +- Fix `sqlcipher_export` and `cipher_migrate` when tracing API in use +- Validate codec page size when setting +- Guard OpenSSL initialization and cleanup routines +- Allow additional linker options to be passed via command line for Windows platforms + +## [3.4.1] - (December 2016 - [3.4.1 changes]) +### Added +- Added support for OpenSSL 1.1.0 + +### Changed +- Merged upstream SQLite 3.15.2 + +## [3.4.0] - (April 2016 - [3.4.0 changes]) +### Added +- Added `PRAGMA cipher_provider_version` + +### Changed +- Merged upstream SQLite 3.11.0 + +### Deprecated +- Deprecated `PRAGMA cipher` command + +## [3.3.1] - (July 2015 - [3.3.1 changes]) +### Changed +- Merge upstream SQLite 3.8.10.2 +- Fixed segfault when provided an invalid cipher name +- Check for codec context when performing `PRAGMA cipher_store_pass` +- Remove extraneous null check in `PRAGMA cipher_migrate` + +## [3.3.0] - (March 2015 - [3.3.0 changes]) +### Added +- Added FIPS API calls within the OpenSSL crypto provider +- `PRAGMA cipher_default_page_size` - support for attaching non-default page sizes + +### Changed +- Merged upstream SQLite 3.8.8.3 + +## [3.2.0] - (September 2014 - [3.2.0 changes]) +### Added +- Added `PRAGMA cipher_store_pass` + +### Changed +- Merged upstream SQLite 3.8.6 +- Renmed README to README.md + +## [3.1.0] - (April 2014 - [3.1.0 changes]) +### Added +- Added `PRAGMA cipher_profile` + +### Changed +- Merged upstream SQLite 3.8.4.3 + +## [3.0.1] - (December 2013 - [3.0.1 changes]) +### Added +- Added `PRAGMA cipher_add_random` to source external entropy + +### Changed +- Fix `PRAGMA cipher_migrate` to handle passphrases longer than 64 characters & raw keys +- Improvements to the libtomcrypt provider + +## [3.0.0] - (November 2013 - [3.0.0 changes]) +### Added +- Added `PRAGMA cipher_migrate` to migrate older database file formats + +### Changed +- Merged upstream SQLite 3.8.0.2 +- Remove usage of VirtualLock/Unlock on WinRT and Windows Phone +- Ignore HMAC read during Btree file copy +- Fix lib naming for pkg-config +- Use _v2 version of `sqlite3_key` and `sqlite3_rekey` +- Update xcodeproj file + +### Security +- Change KDF iteration length from 4,000 to 64,000 + +[unreleased]: https://github.com/sqlcipher/sqlcipher/tree/prerelease +[unreleased changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.8.0...prerelease +[4.9.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.9.0 +[4.9.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.8.0...v4.9.0 +[4.8.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.8.0 +[4.8.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.7.0...v4.8.0 +[4.7.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.7.0 +[4.7.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.6.1...v4.7.0 +[4.6.1]: https://github.com/sqlcipher/sqlcipher/tree/v4.6.1 +[4.6.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.6.0...v4.6.1 +[4.6.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.6.0 +[4.6.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.7...v4.6.0 +[4.5.7]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.7 +[4.5.7 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.6...v4.5.7 +[4.5.6]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.6 +[4.5.6 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.5...v4.5.6 +[4.5.5]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.5 +[4.5.5 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.4...v4.5.5 +[4.5.4]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.4 +[4.5.4 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.3...v4.5.4 +[4.5.3]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.3 +[4.5.3 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.2...v4.5.3 +[4.5.2]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.2 +[4.5.2 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.1...v4.5.2 +[4.5.1]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.1 +[4.5.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.5.0...v4.5.1 +[4.5.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.5.0 +[4.5.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.4.3...v4.5.0 +[4.4.3]: https://github.com/sqlcipher/sqlcipher/tree/v4.4.3 +[4.4.3 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.4.2...v4.4.3 +[4.4.2]: https://github.com/sqlcipher/sqlcipher/tree/v4.4.2 +[4.4.2 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.4.1...v4.4.2 +[4.4.1]: https://github.com/sqlcipher/sqlcipher/tree/v4.4.1 +[4.4.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.4.0...v4.4.1 +[4.4.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.4.0 +[4.4.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.3.0...v4.4.0 +[4.3.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.3.0 +[4.3.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.2.0...v4.3.0 +[4.2.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.2.0 +[4.2.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.1.0...v4.2.0 +[4.1.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.1.0 +[4.1.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.0.1...v4.1.0 +[4.0.1]: https://github.com/sqlcipher/sqlcipher/tree/v4.0.1 +[4.0.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v4.0.0...v4.0.1 +[4.0.0]: https://github.com/sqlcipher/sqlcipher/tree/v4.0.0 +[4.0.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.4.2...v4.0.0 +[3.4.2]: https://github.com/sqlcipher/sqlcipher/tree/v3.4.2 +[3.4.2 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.4.1...v3.4.2 +[3.4.1]: https://github.com/sqlcipher/sqlcipher/tree/v3.4.1 +[3.4.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.4.0...v3.4.1 +[3.4.0]: https://github.com/sqlcipher/sqlcipher/tree/v3.4.0 +[3.4.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.3.1...v3.4.0 +[3.3.1]: https://github.com/sqlcipher/sqlcipher/tree/v3.3.1 +[3.3.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.3.0...v3.3.1 +[3.3.0]: https://github.com/sqlcipher/sqlcipher/tree/v3.3.0 +[3.3.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.2.0...v3.3.0 +[3.2.0]: https://github.com/sqlcipher/sqlcipher/tree/v3.2.0 +[3.2.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.1.0...v3.2.0 +[3.1.0]: https://github.com/sqlcipher/sqlcipher/tree/v3.1.0 +[3.1.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.0.1...v3.1.0 +[3.0.1]: https://github.com/sqlcipher/sqlcipher/tree/v3.0.1 +[3.0.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v3.0.0...v3.0.1 +[3.0.0]: https://github.com/sqlcipher/sqlcipher/tree/v3.0.0 +[3.0.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.2.0...v3.0.0 +[2.2.0]: https://github.com/sqlcipher/sqlcipher/tree/v2.2.0 +[2.2.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.1.1...v2.2.0 +[2.1.1]: https://github.com/sqlcipher/sqlcipher/tree/v2.1.1 +[2.1.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.1.0...v2.1.1 +[2.1.0]: https://github.com/sqlcipher/sqlcipher/tree/v2.1.0 +[2.1.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.0.6...v2.1.0 +[2.0.6]: https://github.com/sqlcipher/sqlcipher/tree/v2.0.6 +[2.0.6 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.0.5...v2.0.6 +[2.0.5]: https://github.com/sqlcipher/sqlcipher/tree/v2.0.5 +[2.0.5 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.0.3...v2.0.5 +[2.0.3]: https://github.com/sqlcipher/sqlcipher/tree/v2.0.3 +[2.0.3 changes]: https://github.com/sqlcipher/sqlcipher/compare/v2.0.0...v2.0.3 +[2.0.0]: https://github.com/sqlcipher/sqlcipher/tree/v2.0.0 +[2.0.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.10...v2.0.0 +[1.1.10]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.10 +[1.1.10 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.9...v1.1.10 +[1.1.9]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.9 +[1.1.9 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.8...v1.1.9 +[1.1.8]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.8 +[1.1.8 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.7...v1.1.8 +[1.1.7]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.7 +[1.1.7 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.6...v1.1.7 +[1.1.6]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.6 +[1.1.6 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.5...v1.1.6 +[1.1.5]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.5 +[1.1.5 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.4...v1.1.5 +[1.1.4]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.4 +[1.1.4 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.3...v1.1.4 +[1.1.3]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.3 +[1.1.3 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.2...v1.1.3 +[1.1.2]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.2 +[1.1.2 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.1...v1.1.1 +[1.1.1]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.1 +[1.1.1 changes]: https://github.com/sqlcipher/sqlcipher/compare/v1.1.0...v1.1.1 +[1.1.0]: https://github.com/sqlcipher/sqlcipher/tree/v1.1.0 +[1.1.0 changes]: https://github.com/sqlcipher/sqlcipher/compare/617ed01...v1.1.0 diff --git a/LICENSE.md b/LICENSE.md index ebfc077603..3f71443161 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,91 +1,24 @@ -License Information -=================== - -SQLite Is Public Domain ------------------------ - -The SQLite source code, including all of the files in the directories -listed in the bullets below are -[Public Domain](https://sqlite.org/copyright.html). -The authors have submitted written affidavits releasing their work to -the public for any use. Every byte of the public-domain code can be -traced back to the original authors. The files of this repository -that are public domain include the following: - - * All of the primary SQLite source code files found in the - [src/ directory](https://sqlite.org/src/tree/src?type=tree&expand) - * All of the test cases and testing code in the - [test/ directory](https://sqlite.org/src/tree/test?type=tree&expand) - * All of the SQLite extension source code and test cases in the - [ext/ directory](https://sqlite.org/src/tree/ext?type=tree&expand) - * All code that ends up in the "sqlite3.c" and "sqlite3.h" build products - that actually implement the SQLite RDBMS. - * All of the code used to compile the - [command-line interface](https://sqlite.org/cli.html) - * All of the code used to build various utility programs such as - "sqldiff", "sqlite3_rsync", and "sqlite3_analyzer". - - -The public domain source files usually contain a header comment -similar to the following to make it clear that the software is -public domain. - -> ~~~ -The author disclaims copyright to this source code. In place of -a legal notice, here is a blessing: - - * May you do good and not evil. - * May you find forgiveness for yourself and forgive others. - * May you share freely, never taking more than you give. -~~~ - -Almost every file you find in this source repository will be -public domain. But there are a small number of exceptions: - -Non-Public-Domain Code Included With This Source Repository AS A Convenience ----------------------------------------------------------------------------- - -This repository contains a (relatively) small amount of non-public-domain -code used to help implement the configuration and build logic. In other -words, there are some non-public-domain files used to implement: - -> ~~~ -./configure && make -~~~ - -In all cases, the non-public-domain files included with this -repository have generous BSD-style licenses. So anyone is free to -use any of the code in this source repository for any purpose, though -attribution may be required to reuse or republish the configure and -build scripts. None of the non-public-domain code ever actually reaches -the build products, such as "sqlite3.c", however, so no attribution is -required to use SQLite itself. The non-public-domain code consists of -scripts used to help compile SQLite. The non-public-domain code is -technically not part of SQLite. The non-public-domain code is -included in this repository as a convenience to developers, so that those -who want to build SQLite do not need to go download a bunch of -third-party build scripts in order to compile SQLite. - -Non-public-domain code included in this respository includes: - - * The ["autosetup"](http://msteveb.github.io/autosetup/) configuration - system that is contained (mostly) the autosetup/ directory, but also - includes the "./configure" script at the top-level of this archive. - Autosetup has a separate BSD-style license. See the - [autosetup/LICENSE](http://msteveb.github.io/autosetup/license/) - for details. - - * There are BSD-style licenses on some of the configuration - software found in the legacy autoconf/ directory and its - subdirectories. - -The following unix shell command is can be run from the top-level -of this source repository in order to remove all non-public-domain -code: - -> ~~~ -rm -rf configure autosetup autoconf -~~~ - -If you unpack this source repository and then run the command above, what -is left will be 100% public domain. +Copyright (c) 2025, ZETETIC LLC +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the ZETETIC LLC nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000000..3f71443161 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,24 @@ +Copyright (c) 2025, ZETETIC LLC +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the ZETETIC LLC nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile.msc b/Makefile.msc index c1a8f88b6e..951d3e1536 100644 --- a/Makefile.msc +++ b/Makefile.msc @@ -1212,7 +1212,7 @@ LTLIBS = $(LTLIBS) rpcrt4.lib # set this for you. Otherwise, the linker will attempt # to deduce the binary type based on the object files. !IFDEF PLATFORM -LTLINKOPTS = /NOLOGO /MACHINE:$(PLATFORM) +LTLINKOPTS = $(LTLINKOPTS) /NOLOGO /MACHINE:$(PLATFORM) LTLIBOPTS = /NOLOGO /MACHINE:$(PLATFORM) !ELSEIF "$(VISUALSTUDIOVERSION)"=="12.0" || \ "$(VISUALSTUDIOVERSION)"=="14.0" || \ @@ -1220,7 +1220,7 @@ LTLIBOPTS = /NOLOGO /MACHINE:$(PLATFORM) LTLINKOPTS = /NOLOGO /MACHINE:x86 LTLIBOPTS = /NOLOGO /MACHINE:x86 !ELSE -LTLINKOPTS = /NOLOGO +LTLINKOPTS = $(LTLINKOPTS) /NOLOGO LTLIBOPTS = /NOLOGO !ENDIF @@ -1385,6 +1385,12 @@ LIBRESOBJS = # Core source code files, part 1. # SRC00 = \ + $(TOP)\src\sqlcipher.c \ + $(TOP)\src\crypto_cc.c \ + $(TOP)\src\crypto_libtomcrypt.c \ + $(TOP)\src\crypto_nss.c \ + $(TOP)\src\crypto_openssl.c \ + $(TOP)\src\sqlcipher.h \ $(TOP)\src\alter.c \ $(TOP)\src\analyze.c \ $(TOP)\src\attach.c \ diff --git a/README.md b/README.md index 689b52dabd..2fa46ecc64 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,156 @@ +## SQLCipher + +SQLCipher is a standalone fork of the [SQLite](https://www.sqlite.org/) database library that adds 256 bit AES encryption of database files and other security features like: + +- on-the-fly encryption +- tamper detection +- memory sanitization +- strong key derivation + +SQLCipher is based on SQLite and stable upstream release features are periodically integrated. While SQLCipher is maintained as a separate version of the source tree, the project minimizes alterations to core SQLite code whenever possible. + +SQLCipher is maintained by Zetetic, LLC, and additional information and documentation is available on the official [SQLCipher site](https://www.zetetic.net/sqlcipher/). + +## Features + +- Fast performance with as little as 5-15% overhead for encryption on many operations +- 100% of data in the database file is encrypted +- Good security practices (CBC mode, HMAC, key derivation) +- Zero-configuration and application level cryptography +- Support for multiple cryptographic providers + +## Compatibility + +SQLCipher maintains database format compatibility within the same major version number so an application on any platform can open databases created by any other application provided the major version of SQLCipher is the same between them. However, major version updates (e.g. from 3.x to 4.x) often include changes to default settings. This means that newer major versions of SQLCipher will not open databases created by older versions without using special settings. For example, SQLCipher 4 introduces many new performance and security enhancements. The new default algorithms, increased KDF iterations, and larger page size mean that SQLCipher 4 will not open databases created by SQLCipher 1.x, 2.x, or 3.x by default. Instead, an application would either need to migrate the older databases to use the new format or enable a special backwards-compatibility mode. The available options are described in SQLCipher's [upgrade documentation](https://discuss.zetetic.net/t/upgrading-to-sqlcipher-4/3283). + +SQLCipher is also compatible with standard SQLite databases. When a key is not provided, SQLCipher will behave just like the standard SQLite library. It is also possible to convert from a plaintext database (standard SQLite) to an encrypted SQLCipher database using [ATTACH and the sqlcipher_export() convenience function](https://discuss.zetetic.net/t/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher-and-avoid-file-is-encrypted-or-is-not-a-database-errors/868). + +## Contributions + +The SQLCipher team welcomes contributions to the core library. All contributions including pull requests and patches should be based on the `prerelease` branch, and must be accompanied by a [contributor agreement](https://www.zetetic.net/contributions/). We strongly encourage [discussion](https://discuss.zetetic.net/c/sqlcipher) of the proposed change prior to development and submission. + +## Compiling + +Building SQLCipher is similar to compiling a regular version of SQLite from source, with a few small exceptions. You must: + + 1. define `SQLITE_HAS_CODEC` + 2. define `SQLITE_TEMP_STORE=2` or `SQLITE_TEMP_STORE=3` (or use `configure`'s --with-tempstore=yes option) + 3. define `SQLITE_EXTRA_INIT=sqlcipher_extra_init` and `SQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown` + 4. define `SQLITE_THREADSAFE` to `1` or `2` (enabled automatically by `configure`) + 2. compile and link with a supported cryptographic provider (OpenSSL, LibTomCrypt, CommonCrypto/Security.framework, or NSS) + +The following examples demonstrate use of OpenSSL, which is a readily available provider on most Unix-like systems. Note that, in this example, `--with-tempstore=yes` is setting `SQLITE_TEMP_STORE=2` for the build, and `SQLITE_THREADSAFE` has a default value of `1`. + +``` +$ ./configure --with-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC -DSQLITE_EXTRA_INIT=sqlcipher_extra_init -DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown" \ + LDFLAGS="-lcrypto" +$ make +``` + +## Testing + +The full SQLite test suite will not complete successfully when using SQLCipher. In some cases encryption interferes with low-level tests that require access to database file data or features which are unsupported by SQLCipher. Those tests that are intended to support encryption are intended for non-SQLCipher implementations. In addition, because SQLite tests are not always isolated, if one test fails it can trigger a domino effect with other failures in later steps. + +As a result, the SQLCipher package includes it's own independent tests that exercise and verify the core functionality of the SQLCipher extensions. This test suite is intended to provide an abbreviated verification of SQLCipher's internal logic; it does not perform an exhaustive test of the SQLite database system as a whole or verify functionality on specific platforms. Because SQLCipher is based on stable upstream builds of SQLite, it is considered a basic assumption that the core SQLite library code is operating properly (the SQLite core is almost untouched in SQLCipher). Thus, the additional SQLCipher-specific test provide the requisite verification that the library is operating as expected with SQLCipher's security features enabled. + +To run SQLCipher specific tests, configure as described here and run the following to execute the tests and receive a report of the results: + +``` +$ ./configure --with-tempstore=yes --enable-fts5 CFLAGS="-DSQLITE_HAS_CODEC -DSQLITE_EXTRA_INIT=sqlcipher_extra_init -DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown -DSQLCIPHER_TEST" \ + LDFLAGS="-lcrypto" +$ make testfixture +$ ./testfixture test/sqlcipher.test +``` + +## Encrypting a database + +To specify an encryption passphrase for the database via the SQL interface you +use a PRAGMA. The passphrase you enter is passed through PBKDF2 key derivation to +obtain the encryption key for the database + + PRAGMA key = 'passphrase'; + +Alternately, you can specify an exact byte sequence using a blob literal. If you +use this method it is your responsibility to ensure that the data you provide is a +64 character hex string, which will be converted directly to 32 bytes (256 bits) of +key data without key derivation. + + PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + +To encrypt a database programmatically you can use the `sqlite3_key` function. +The data provided in `pKey` is converted to an encryption key according to the +same rules as `PRAGMA key`. + + int sqlite3_key(sqlite3 *db, const void *pKey, int nKey); + +`PRAGMA key` or `sqlite3_key` should be called as the first operation when a database is open. + +## Changing a database key + +To change the encryption passphrase for an existing database you may use the rekey PRAGMA +after you've supplied the correct database password; + + PRAGMA key = 'passphrase'; -- start with the existing database passphrase + PRAGMA rekey = 'new-passphrase'; -- rekey will reencrypt with the new passphrase + +The hex rekey pragma may be used to rekey to a specific binary value + + PRAGMA rekey = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + +This can be accomplished programmatically by using sqlite3_rekey; + + sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) + +## Support + +The primary source for complete documentation (design, API, platforms, usage) is the SQLCipher website: + +https://www.zetetic.net/sqlcipher/documentation + +The primary avenue for support and discussions is the SQLCipher discuss site: + +https://discuss.zetetic.net/c/sqlcipher + +Issues or support questions on using SQLCipher should be entered into the +GitHub Issue tracker: + +https://github.com/sqlcipher/sqlcipher/issues + +Please DO NOT post issues, support questions, or other problems to blog +posts about SQLCipher as we do not monitor them frequently. + +If you are using SQLCipher in your own software please let us know at +support@zetetic.net! + +## Community Edition Open Source License + +Copyright (c) 2025, ZETETIC LLC +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of the ZETETIC LLC nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +# Begin SQLite README.md +

SQLite Source Repository

This repository contains the complete source code for the diff --git a/SQLCipher.podspec.json b/SQLCipher.podspec.json new file mode 100644 index 0000000000..05c9daf8cb --- /dev/null +++ b/SQLCipher.podspec.json @@ -0,0 +1,100 @@ +{ + "authors": "Zetetic LLC", + "default_subspecs": "standard", + "description": "SQLCipher is an open source extension to SQLite that provides transparent 256-bit AES encryption of database files.", + "homepage": "https://www.zetetic.net/sqlcipher/", + "license": { + "type": "BSD-3-Clause", + "file": "LICENSE.txt" + }, + "name": "SQLCipher", + "platforms": { + "ios": "12.0", + "osx": "10.13", + "tvos": "12.0", + "watchos": "7.0" + }, + "prepare_command": "./configure && make sqlite3.c", + "requires_arc": false, + "source": { + "git": "https://github.com/sqlcipher/sqlcipher.git", + "tag": "v4.9.0" + }, + "summary": "Full Database Encryption for SQLite.", + "version": "4.9.0", + "subspecs": [ + { + "compiler_flags": [ + "-DNDEBUG", + "-DSQLITE_HAS_CODEC", + "-DSQLITE_TEMP_STORE=2", + "-DSQLITE_SOUNDEX", + "-DSQLITE_THREADSAFE", + "-DSQLITE_ENABLE_RTREE", + "-DSQLITE_ENABLE_STAT3", + "-DSQLITE_ENABLE_STAT4", + "-DSQLITE_ENABLE_COLUMN_METADATA", + "-DSQLITE_ENABLE_MEMORY_MANAGEMENT", + "-DSQLITE_ENABLE_LOAD_EXTENSION", + "-DSQLITE_ENABLE_FTS4", + "-DSQLITE_ENABLE_FTS4_UNICODE61", + "-DSQLITE_ENABLE_FTS3_PARENTHESIS", + "-DSQLITE_ENABLE_UNLOCK_NOTIFY", + "-DSQLITE_ENABLE_JSON1", + "-DSQLITE_ENABLE_FTS5", + "-DSQLCIPHER_CRYPTO_CC", + "-DHAVE_USLEEP=1", + "-DSQLITE_MAX_VARIABLE_NUMBER=99999", + "-DSQLITE_EXTRA_INIT=sqlcipher_extra_init", + "-DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown" + ], + "frameworks": [ + "Foundation", + "Security" + ], + "name": "common", + "source_files": "sqlite3.{h,c}", + "resource_bundles": {"SQLCipher": ["sqlcipher-resources/PrivacyInfo.xcprivacy"]}, + "xcconfig": { + "HEADER_SEARCH_PATHS": "$(PODS_ROOT)/SQLCipher", + "GCC_PREPROCESSOR_DEFINITIONS": "SQLITE_HAS_CODEC=1", + "OTHER_CFLAGS": "$(inherited) -DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2 -DSQLITE_SOUNDEX -DSQLITE_THREADSAFE -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_STAT3 -DSQLITE_ENABLE_STAT4 -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_LOAD_EXTENSION -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_UNLOCK_NOTIFY -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLCIPHER_CRYPTO_CC -DHAVE_USLEEP=1 -DSQLITE_MAX_VARIABLE_NUMBER=99999 -DSQLITE_EXTRA_INIT=sqlcipher_extra_init -DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown" + }, + "user_target_xcconfig": { + "GCC_PREPROCESSOR_DEFINITIONS": "_SQLITE3_H_=1 _FTS5_H=1 _SQLITE3RTREE_H_=1" + } + }, + { + "dependencies": { + "SQLCipher/common": [ + + ] + }, + "name": "standard" + }, + { + "compiler_flags": "", + "dependencies": { + "SQLCipher/common": [ + + ] + }, + "name": "fts", + "xcconfig": { + "OTHER_CFLAGS": "$(inherited)" + } + }, + { + "compiler_flags": "", + "dependencies": { + "SQLCipher/common": [ + + ] + }, + "name": "unlock_notify", + "xcconfig": { + "OTHER_CFLAGS": "$(inherited)" + } + } + ] +} diff --git a/macosx/Info-tvOS.plist b/macosx/Info-tvOS.plist new file mode 100644 index 0000000000..5234c89250 --- /dev/null +++ b/macosx/Info-tvOS.plist @@ -0,0 +1,30 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + FMWK + CFBundleShortVersionString + 3.3.1 + CFBundleSignature + ???? + CFBundleVersion + $(CURRENT_PROJECT_VERSION) + NSPrincipalClass + + UIRequiredDeviceCapabilities + + arm64 + + + diff --git a/macosx/Info.plist b/macosx/Info.plist new file mode 100644 index 0000000000..dd6cc70b48 --- /dev/null +++ b/macosx/Info.plist @@ -0,0 +1,26 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + FMWK + CFBundleShortVersionString + 3.3.1 + CFBundleSignature + ???? + CFBundleVersion + $(CURRENT_PROJECT_VERSION) + NSPrincipalClass + + + diff --git a/macosx/SQLCipher.h b/macosx/SQLCipher.h new file mode 100644 index 0000000000..1023799d9e --- /dev/null +++ b/macosx/SQLCipher.h @@ -0,0 +1,7 @@ + +@import Foundation; + +FOUNDATION_EXPORT double SQLCipherVersionNumber; +FOUNDATION_EXPORT const unsigned char SQLCipherVersionString[]; + +#import "sqlite3.h" diff --git a/main.mk b/main.mk index b40b9f9687..6d07489ff6 100644 --- a/main.mk +++ b/main.mk @@ -489,6 +489,35 @@ clean-sanity-check: rm -f $(MAKE_SANITY_CHECK) clean: clean-sanity-check +# BEGIN SQLCIPHER +SQLCIPHER_OBJ = \ + sqlcipher.o \ + crypto_openssl.o \ + crypto_libtomcrypt.o \ + crypto_nss.o \ + crypto_cc.o + +SQLCIPHER_SRC = \ + $(TOP)/src/sqlcipher.h \ + $(TOP)/src/sqlcipher.c \ + $(TOP)/src/crypto_libtomcrypt.c \ + $(TOP)/src/crypto_nss.c \ + $(TOP)/src/crypto_openssl.c \ + $(TOP)/src/crypto_cc.c + +sqlcipher.o: $(TOP)/src/sqlcipher.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) $(CFLAGS.libsqlite3) -c $(TOP)/src/sqlcipher.c +crypto_openssl.o: $(TOP)/src/crypto_openssl.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) $(CFLAGS.libsqlite3) -c $(TOP)/src/crypto_openssl.c +crypto_nss.o: $(TOP)/src/crypto_nss.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) $(CFLAGS.libsqlite3) -c $(TOP)/src/crypto_nss.c +crypto_libtomcrypt.o: $(TOP)/src/crypto_libtomcrypt.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) $(CFLAGS.libsqlite3) -c $(TOP)/src/crypto_libtomcrypt.c +crypto_cc.o: $(TOP)/src/crypto_cc.c $(DEPS_OBJ_COMMON) + $(T.cc.sqlite) $(CFLAGS.libsqlite3) -c $(TOP)/src/crypto_cc.c + +# END SQLCIPHER + # # Object files for the SQLite library (non-amalgamation). # @@ -516,7 +545,7 @@ LIBOBJS0 = alter.o analyze.o attach.o auth.o \ vdbe.o vdbeapi.o vdbeaux.o vdbeblob.o vdbemem.o vdbesort.o \ vdbetrace.o vdbevtab.o vtab.o \ wal.o walker.o where.o wherecode.o whereexpr.o \ - window.o + window.o $(SQLCIPHER_OBJ) LIBOBJS = $(LIBOBJS0) # @@ -534,7 +563,7 @@ $(LIBOBJ): $(MAKE_SANITY_CHECK) # # All of the source code files. # -SRC = \ +SRC = $(SQLCIPHER_SRC) \ $(TOP)/src/alter.c \ $(TOP)/src/analyze.c \ $(TOP)/src/attach.c \ diff --git a/sqlcipher-resources/PrivacyInfo.xcprivacy b/sqlcipher-resources/PrivacyInfo.xcprivacy new file mode 100644 index 0000000000..60b27a68ca --- /dev/null +++ b/sqlcipher-resources/PrivacyInfo.xcprivacy @@ -0,0 +1,32 @@ + + + + + NSPrivacyCollectedDataTypes + + NSPrivacyAccessedAPITypes + + + NSPrivacyAccessedAPIType + NSPrivacyAccessedAPICategoryDiskSpace + NSPrivacyAccessedAPITypeReasons + + E174.1 + + + + NSPrivacyAccessedAPIType + NSPrivacyAccessedAPICategoryFileTimestamp + NSPrivacyAccessedAPITypeReasons + + C617.1 + 3B52.1 + + + + NSPrivacyTrackingDomains + + NSPrivacyTracking + + + diff --git a/sqlcipher-resources/sqlcipher-1.1.8-testkey.db b/sqlcipher-resources/sqlcipher-1.1.8-testkey.db new file mode 100644 index 0000000000..1e069c5b87 Binary files /dev/null and b/sqlcipher-resources/sqlcipher-1.1.8-testkey.db differ diff --git a/sqlcipher-resources/sqlcipher-2.0-be-testkey.db b/sqlcipher-resources/sqlcipher-2.0-be-testkey.db new file mode 100644 index 0000000000..fcabab3cbc Binary files /dev/null and b/sqlcipher-resources/sqlcipher-2.0-be-testkey.db differ diff --git a/sqlcipher-resources/sqlcipher-2.0-beta-testkey.db b/sqlcipher-resources/sqlcipher-2.0-beta-testkey.db new file mode 100755 index 0000000000..394c91528f Binary files /dev/null and b/sqlcipher-resources/sqlcipher-2.0-beta-testkey.db differ diff --git a/sqlcipher-resources/sqlcipher-2.0-le-testkey.db b/sqlcipher-resources/sqlcipher-2.0-le-testkey.db new file mode 100644 index 0000000000..15a9723bc2 Binary files /dev/null and b/sqlcipher-resources/sqlcipher-2.0-le-testkey.db differ diff --git a/sqlcipher-resources/sqlcipher-3.0-testkey.db b/sqlcipher-resources/sqlcipher-3.0-testkey.db new file mode 100644 index 0000000000..6af4430c56 Binary files /dev/null and b/sqlcipher-resources/sqlcipher-3.0-testkey.db differ diff --git a/sqlcipher-resources/sqlcipher-4.0-testkey.db b/sqlcipher-resources/sqlcipher-4.0-testkey.db new file mode 100644 index 0000000000..b8db5378cb Binary files /dev/null and b/sqlcipher-resources/sqlcipher-4.0-testkey.db differ diff --git a/sqlcipher.1 b/sqlcipher.1 new file mode 100644 index 0000000000..c9a310057d --- /dev/null +++ b/sqlcipher.1 @@ -0,0 +1,160 @@ +.\" Hey, EMACS: -*- nroff -*- +.\" First parameter, NAME, should be all caps +.\" Second parameter, SECTION, should be 1-8, maybe w/ subsection +.\" other parameters are allowed: see man(7), man(1) +.TH SQLCIPHER 1 "Fri Aug 11 23:50:12 CET 2023" +.\" Please adjust this date whenever revising the manpage. +.\" +.\" Some roff macros, for reference: +.\" .nh disable hyphenation +.\" .hy enable hyphenation +.\" .ad l left justify +.\" .ad b justify to both left and right margins +.\" .nf disable filling +.\" .fi enable filling +.\" .br insert line break +.\" .sp insert n+1 empty lines +.\" for manpage-specific macros, see man(7) +.SH NAME +.B sqlcipher +\- A command line interface for SQLCipher version 3 + +.SH SYNOPSIS +.B sqlcipher +.RI [ options ] +.RI [ databasefile ] +.RI [ SQL ] + +.SH SUMMARY +.PP +.B sqlcipher +is a terminal-based front-end to the SQLCipher library that can evaluate +queries interactively and display the results in multiple formats. +.B sqlcipher +can also be used within shell scripts and other applications to provide +batch processing features. + +.SH DESCRIPTION +To start a +.B sqlcipher +interactive session, invoke the +.B sqlcipher +command and optionally provide the name of a database file. If the +database file does not exist, it will be created. If the database file +does exist, it will be opened. + +For example, to create a new database file named "mydata.db", create +a table named "memos" and insert a couple of records into that table: +.sp +$ +.B sqlcipher mydata.db +.br +SQLCipher version 3.43.0 2023-08-11 17:45:23 +.br +Enter ".help" for usage hints. +.br +sqlite> +.B create table memos(text, priority INTEGER); +.br +sqlite> +.B insert into memos values('deliver project description', 10); +.br +sqlite> +.B insert into memos values('lunch with Christine', 100); +.br +sqlite> +.B select * from memos; +.br +deliver project description|10 +.br +lunch with Christine|100 +.br +sqlite> +.sp + +If no database name is supplied, the ATTACH sql command can be used +to attach to existing or create new database files. ATTACH can also +be used to attach to multiple databases within the same interactive +session. This is useful for migrating data between databases, +possibly changing the schema along the way. + +Optionally, a SQL statement or set of SQL statements can be supplied as +a single argument. Multiple statements should be separated by +semi-colons. + +For example: +.sp +$ +.B sqlcipher -line mydata.db 'select * from memos where priority > 20;' +.br + text = lunch with Christine +.br +priority = 100 +.br +.sp + +.SS SQLITE META-COMMANDS +.PP +The interactive interpreter offers a set of meta-commands that can be +used to control the output format, examine the currently attached +database files, or perform administrative operations upon the +attached databases (such as rebuilding indices). Meta-commands are +always prefixed with a dot (.). + +A list of available meta-commands can be viewed at any time by issuing +the '.help' command. For example: +.sp +sqlite> +.B .help +.nf +.tr %. +... +.sp +.fi + +The available commands differ by version and build options, so they +are not listed here. Please refer to your local copy for all available +options. + + +.SH INIT FILE +.B sqlcipher +reads an initialization file to set the configuration of the +interactive environment. Throughout initialization, any previously +specified setting can be overridden. The sequence of initialization is +as follows: + +o The default configuration is established as follows: + +.sp +.nf +.cc | +mode = LIST +separator = "|" +main prompt = "sqlite> " +continue prompt = " ...> " +|cc . +.sp +.fi + +o If the file +.B ${XDG_CONFIG_HOME}/sqlcipher/sqliterc +or +.B ~/.sqliterc +exists, the first of those to be found is processed during startup. +It should generally only contain meta-commands. + +o If the -init option is present, the specified file is processed. + +o All other command line options are processed. + +.SH SEE ALSO +https://zetetic.net/sqlcipher + +.br +The sqlcipher-doc package. +.SH AUTHOR +This manual page was originally written by Andreas Rottmann +, for the Debian GNU/Linux system (but may be used +by others). It was subsequently revised by Bill Bumgarner , +Laszlo Boszormenyi , and the sqlcipher developers. diff --git a/src/attach.c b/src/attach.c index 399a6cb537..fce4898dc9 100644 --- a/src/attach.c +++ b/src/attach.c @@ -216,6 +216,52 @@ static void attachFunc( if( rc==SQLITE_OK && pNew->zDbSName==0 ){ rc = SQLITE_NOMEM_BKPT; } + +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + if( rc==SQLITE_OK ){ + extern int sqlcipherCodecAttach(sqlite3*, int, const void*, int); + extern void sqlcipherCodecGetKey(sqlite3*, int, void**, int*); + extern void sqlcipher_free(void*, sqlite3_uint64); + int nKey; + char *zKey; + int t = sqlite3_value_type(argv[2]); + switch( t ){ + case SQLITE_INTEGER: + case SQLITE_FLOAT: + zErrDyn = sqlite3DbStrDup(db, "Invalid key value"); + rc = SQLITE_ERROR; + break; + + case SQLITE_TEXT: + case SQLITE_BLOB: + nKey = sqlite3_value_bytes(argv[2]); + zKey = (char *)sqlite3_value_blob(argv[2]); + /* SQLCipher allows a special case to attach a plaintext database + * to an encrypted database by passing key as an empty string, eg. + * ATTACH DATABASE 'plain.db' AS plain KEY ''; + * In this case, do not attempt to attach a codec to the attached + * database */ + if(nKey && zKey) { + rc = sqlcipherCodecAttach(db, db->nDb-1, zKey, nKey); + } + break; + + case SQLITE_NULL: + /* No key specified. Use the key from URI filename, or if none, + ** use the key from the main database. */ + if( sqlite3CodecQueryParameters(db, zName, zPath)==0 ){ + sqlcipherCodecGetKey(db, 0, (void**)&zKey, &nKey); + if( nKey || sqlite3BtreeGetRequestedReserve(db->aDb[0].pBt)>0 ){ + rc = sqlcipherCodecAttach(db, db->nDb-1, zKey, nKey); + } + if(nKey) sqlcipher_free(zKey, nKey); + } + break; + } + } +#endif +/* END SQLCIPHER */ sqlite3_free_filename( zPath ); /* If the file was opened successfully, read the schema for the new database. diff --git a/src/backup.c b/src/backup.c index 22615d1499..cf3cb445e8 100644 --- a/src/backup.c +++ b/src/backup.c @@ -152,6 +152,29 @@ sqlite3_backup *sqlite3_backup_init( } #endif +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + { + extern int sqlcipher_find_db_index(sqlite3*, const char*); + extern void sqlcipherCodecGetKey(sqlite3*, int, void**, int*); + extern void sqlcipher_free(void*, sqlite3_uint64); + int srcNKey, destNKey; + void *zKey; + + sqlcipherCodecGetKey(pSrcDb, sqlcipher_find_db_index(pSrcDb, zSrcDb), &zKey, &srcNKey); + if(srcNKey) sqlcipher_free(zKey, srcNKey); + sqlcipherCodecGetKey(pDestDb, sqlcipher_find_db_index(pDestDb, zDestDb), &zKey, &destNKey); + if(destNKey) sqlcipher_free(zKey, destNKey); + + /* either both databases must be plaintext, or both must be encrypted */ + if((srcNKey == 0 && destNKey > 0) || (srcNKey > 0 && destNKey == 0)) { + sqlite3ErrorWithMsg(pDestDb, SQLITE_ERROR, "backup is not supported with encrypted databases"); + return NULL; + } + } +#endif +/* END SQLCIPHER */ + /* Lock the source database handle. The destination database ** handle is not locked in this routine, but it is locked in ** sqlite3_backup_step(). The user is required to ensure that no @@ -234,6 +257,16 @@ static int backupOnePage( int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest); const int nCopy = MIN(nSrcPgsz, nDestPgsz); const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz; +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + extern void *sqlcipherPagerGetCodec(Pager*); + /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is + ** guaranteed that the shared-mutex is held by this thread, handle + ** p->pSrc may not actually be the owner. */ + int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc); + int nDestReserve = sqlite3BtreeGetRequestedReserve(p->pDest); +#endif +/* END SQLCIPHER */ int rc = SQLITE_OK; i64 iOff; @@ -244,6 +277,28 @@ static int backupOnePage( assert( zSrcData ); assert( nSrcPgsz==nDestPgsz || sqlite3PagerIsMemdb(pDestPager)==0 ); +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + /* Backup is not possible if the page size of the destination is changing + ** and a codec is in use. + */ + if( nSrcPgsz!=nDestPgsz && sqlcipherPagerGetCodec(pDestPager)!=0 ){ + rc = SQLITE_READONLY; + } + + /* Backup is not possible if the number of bytes of reserve space differ + ** between source and destination. If there is a difference, try to + ** fix the destination to agree with the source. If that is not possible, + ** then the backup cannot proceed. + */ + if( nSrcReserve!=nDestReserve ){ + u32 newPgsz = nSrcPgsz; + rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve); + if( rc==SQLITE_OK && newPgsz!=(u32)nSrcPgsz ) rc = SQLITE_READONLY; + } +#endif +/* END SQLCIPHER */ + /* This loop runs once for each destination page spanned by the source ** page. For each iteration, variable iOff is set to the byte offset ** of the destination page. @@ -742,6 +797,12 @@ int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){ b.pDest = pTo; b.iNext = 1; +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + sqlite3PagerAlignReserve(sqlite3BtreePager(pTo), sqlite3BtreePager(pFrom)); +#endif +/* END SQLCIPHER */ + /* 0x7FFFFFFF is the hard limit for the number of pages in a database ** file. By passing this as the number of pages to copy to ** sqlite3_backup_step(), we can guarantee that the copy finishes diff --git a/src/crypto_cc.c b/src/crypto_cc.c new file mode 100644 index 0000000000..49be9503ad --- /dev/null +++ b/src/crypto_cc.c @@ -0,0 +1,209 @@ +/* +** SQLCipher +** http://sqlcipher.net +** +** Copyright (c) 2008 - 2013, ZETETIC LLC +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** * Neither the name of the ZETETIC LLC nor the +** names of its contributors may be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +** +*/ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC +#ifdef SQLCIPHER_CRYPTO_CC +#include "sqlcipher.h" +#include +#include +#include + +int sqlcipher_cc_setup(sqlcipher_provider *p); + +static int sqlcipher_cc_add_random(void *ctx, const void *buffer, int length) { + return SQLITE_OK; +} + +/* generate a defined number of random bytes */ +static int sqlcipher_cc_random (void *ctx, void *buffer, int length) { + return (SecRandomCopyBytes(kSecRandomDefault, length, (uint8_t *)buffer) == kCCSuccess) ? SQLITE_OK : SQLITE_ERROR; +} + +static const char* sqlcipher_cc_get_provider_name(void *ctx) { + return "commoncrypto"; +} + +static const char* sqlcipher_cc_get_provider_version(void *ctx) { +#if TARGET_OS_MAC + CFTypeRef version; + CFBundleRef bundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.security")); + if(bundle == NULL) { + return "unknown"; + } + version = CFBundleGetValueForInfoDictionaryKey(bundle, CFSTR("CFBundleShortVersionString")); + return CFStringGetCStringPtr(version, kCFStringEncodingUTF8); +#else + return "unknown"; +#endif +} + +static int sqlcipher_cc_hmac( + void *ctx, int algorithm, + const unsigned char *hmac_key, int key_sz, + const unsigned char *in, int in_sz, + const unsigned char *in2, int in2_sz, + unsigned char *out +) { + CCHmacContext hmac_context; + if(in == NULL) return SQLITE_ERROR; + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + CCHmacInit(&hmac_context, kCCHmacAlgSHA1, hmac_key, key_sz); + break; + case SQLCIPHER_HMAC_SHA256: + CCHmacInit(&hmac_context, kCCHmacAlgSHA256, hmac_key, key_sz); + break; + case SQLCIPHER_HMAC_SHA512: + CCHmacInit(&hmac_context, kCCHmacAlgSHA512, hmac_key, key_sz); + break; + default: + return SQLITE_ERROR; + } + CCHmacUpdate(&hmac_context, in, in_sz); + if(in2 != NULL) CCHmacUpdate(&hmac_context, in2, in2_sz); + CCHmacFinal(&hmac_context, out); + return SQLITE_OK; +} + +static int sqlcipher_cc_kdf( + void *ctx, int algorithm, + const unsigned char *pass, int pass_sz, + const unsigned char* salt, int salt_sz, + int workfactor, + int key_sz, unsigned char *key +) { + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + if(CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)pass, pass_sz, salt, salt_sz, kCCPRFHmacAlgSHA1, workfactor, key, key_sz) != kCCSuccess) return SQLITE_ERROR; + break; + case SQLCIPHER_HMAC_SHA256: + if(CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)pass, pass_sz, salt, salt_sz, kCCPRFHmacAlgSHA256, workfactor, key, key_sz) != kCCSuccess) return SQLITE_ERROR; + break; + case SQLCIPHER_HMAC_SHA512: + if(CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)pass, pass_sz, salt, salt_sz, kCCPRFHmacAlgSHA512, workfactor, key, key_sz) != kCCSuccess) return SQLITE_ERROR; + break; + default: + return SQLITE_ERROR; + } + return SQLITE_OK; +} + +static int sqlcipher_cc_cipher( + void *ctx, int mode, + const unsigned char *key, int key_sz, + const unsigned char *iv, + const unsigned char *in, int in_sz, + unsigned char *out +) { + CCCryptorRef cryptor; + size_t tmp_csz, csz; + CCOperation op = mode == CIPHER_ENCRYPT ? kCCEncrypt : kCCDecrypt; + + if(CCCryptorCreate(op, kCCAlgorithmAES128, 0, key, kCCKeySizeAES256, iv, &cryptor) != kCCSuccess) return SQLITE_ERROR; + if(CCCryptorUpdate(cryptor, in, in_sz, out, in_sz, &tmp_csz) != kCCSuccess) return SQLITE_ERROR; + csz = tmp_csz; + out += tmp_csz; + if(CCCryptorFinal(cryptor, out, in_sz - csz, &tmp_csz) != kCCSuccess) return SQLITE_ERROR; + csz += tmp_csz; + if(CCCryptorRelease(cryptor) != kCCSuccess) return SQLITE_ERROR; + assert(in_sz == csz); + + return SQLITE_OK; +} + +static const char* sqlcipher_cc_get_cipher(void *ctx) { + return "aes-256-cbc"; +} + +static int sqlcipher_cc_get_key_sz(void *ctx) { + return kCCKeySizeAES256; +} + +static int sqlcipher_cc_get_iv_sz(void *ctx) { + return kCCBlockSizeAES128; +} + +static int sqlcipher_cc_get_block_sz(void *ctx) { + return kCCBlockSizeAES128; +} + +static int sqlcipher_cc_get_hmac_sz(void *ctx, int algorithm) { + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + return CC_SHA1_DIGEST_LENGTH; + break; + case SQLCIPHER_HMAC_SHA256: + return CC_SHA256_DIGEST_LENGTH; + break; + case SQLCIPHER_HMAC_SHA512: + return CC_SHA512_DIGEST_LENGTH; + break; + default: + return 0; + } +} + +static int sqlcipher_cc_ctx_init(void **ctx) { + return SQLITE_OK; +} + +static int sqlcipher_cc_ctx_free(void **ctx) { + return SQLITE_OK; +} + +static int sqlcipher_cc_fips_status(void *ctx) { + return 0; +} + +int sqlcipher_cc_setup(sqlcipher_provider *p) { + p->init = NULL; + p->shutdown = NULL; + p->random = sqlcipher_cc_random; + p->get_provider_name = sqlcipher_cc_get_provider_name; + p->hmac = sqlcipher_cc_hmac; + p->kdf = sqlcipher_cc_kdf; + p->cipher = sqlcipher_cc_cipher; + p->get_cipher = sqlcipher_cc_get_cipher; + p->get_key_sz = sqlcipher_cc_get_key_sz; + p->get_iv_sz = sqlcipher_cc_get_iv_sz; + p->get_block_sz = sqlcipher_cc_get_block_sz; + p->get_hmac_sz = sqlcipher_cc_get_hmac_sz; + p->ctx_init = sqlcipher_cc_ctx_init; + p->ctx_free = sqlcipher_cc_ctx_free; + p->add_random = sqlcipher_cc_add_random; + p->fips_status = sqlcipher_cc_fips_status; + p->get_provider_version = sqlcipher_cc_get_provider_version; + return SQLITE_OK; +} + +#endif +#endif +/* END SQLCIPHER */ diff --git a/src/crypto_libtomcrypt.c b/src/crypto_libtomcrypt.c new file mode 100644 index 0000000000..9a9bb17b7e --- /dev/null +++ b/src/crypto_libtomcrypt.c @@ -0,0 +1,314 @@ +/* +** SQLCipher +** http://sqlcipher.net +** +** Copyright (c) 2008 - 2013, ZETETIC LLC +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** * Neither the name of the ZETETIC LLC nor the +** names of its contributors may be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +** +*/ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC +#ifdef SQLCIPHER_CRYPTO_LIBTOMCRYPT +#include "sqliteInt.h" +#include "sqlcipher.h" +#include + +#define FORTUNA_MAX_SZ 32 +static prng_state prng; +static volatile unsigned int ltc_init = 0; +static volatile unsigned int ltc_ref_count = 0; + +#define LTC_CIPHER "rijndael" + +static int sqlcipher_ltc_add_random(void *ctx, const void *buffer, int length) { + int rc = 0; + int data_to_read = length; + int block_sz = data_to_read < FORTUNA_MAX_SZ ? data_to_read : FORTUNA_MAX_SZ; + const unsigned char * data = (const unsigned char *)buffer; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_add_random: entering SQLCIPHER_MUTEX_PROVIDER_RAND"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_add_random: entered SQLCIPHER_MUTEX_PROVIDER_RAND"); + + while(data_to_read > 0){ + rc = fortuna_add_entropy(data, block_sz, &prng); + rc = rc != CRYPT_OK ? SQLITE_ERROR : SQLITE_OK; + if(rc != SQLITE_OK){ + break; + } + data_to_read -= block_sz; + data += block_sz; + block_sz = data_to_read < FORTUNA_MAX_SZ ? data_to_read : FORTUNA_MAX_SZ; + } + fortuna_ready(&prng); + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_add_random: leaving SQLCIPHER_MUTEX_PROVIDER_RAND"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_add_random: left SQLCIPHER_MUTEX_PROVIDER_RAND"); + + return rc; +} + +static int sqlcipher_ltc_activate(void *ctx) { + unsigned char random_buffer[FORTUNA_MAX_SZ]; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_activate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_activate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + + sqlcipher_memset(random_buffer, 0, FORTUNA_MAX_SZ); + if(ltc_init == 0) { + if(register_prng(&fortuna_desc) < 0) return SQLITE_ERROR; + if(register_cipher(&rijndael_desc) < 0) return SQLITE_ERROR; + if(register_hash(&sha512_desc) < 0) return SQLITE_ERROR; + if(register_hash(&sha256_desc) < 0) return SQLITE_ERROR; + if(register_hash(&sha1_desc) < 0) return SQLITE_ERROR; + if(fortuna_start(&prng) != CRYPT_OK) { + return SQLITE_ERROR; + } + + ltc_init = 1; + } + ltc_ref_count++; + +#ifndef SQLCIPHER_TEST + sqlite3_randomness(FORTUNA_MAX_SZ, random_buffer); +#endif + + if(sqlcipher_ltc_add_random(ctx, random_buffer, FORTUNA_MAX_SZ) != SQLITE_OK) { + return SQLITE_ERROR; + } + sqlcipher_memset(random_buffer, 0, FORTUNA_MAX_SZ); + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_activate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_activate: left SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + + return SQLITE_OK; +} + +static int sqlcipher_ltc_deactivate(void *ctx) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_deactivate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_deactivate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + + ltc_ref_count--; + if(ltc_ref_count == 0){ + fortuna_done(&prng); + sqlcipher_memset((void *)&prng, 0, sizeof(prng)); + } + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_deactivate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_deactivate: left SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + + return SQLITE_OK; +} + +static const char* sqlcipher_ltc_get_provider_name(void *ctx) { + return "libtomcrypt"; +} + +static const char* sqlcipher_ltc_get_provider_version(void *ctx) { + return SCRYPT; +} + +static int sqlcipher_ltc_random(void *ctx, void *buffer, int length) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_random: entering SQLCIPHER_MUTEX_PROVIDER_RAND"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_random: entered SQLCIPHER_MUTEX_PROVIDER_RAND"); + + fortuna_read(buffer, length, &prng); + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_random: leaving SQLCIPHER_MUTEX_PROVIDER_RAND"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_random: left SQLCIPHER_MUTEX_PROVIDER_RAND"); + + return SQLITE_OK; +} + +static int sqlcipher_ltc_hmac( + void *ctx, int algorithm, + const unsigned char *hmac_key, int key_sz, + const unsigned char *in, int in_sz, + const unsigned char *in2, int in2_sz, + unsigned char *out +) { + int rc, hash_idx; + hmac_state hmac; + unsigned long outlen; + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + hash_idx = find_hash("sha1"); + break; + case SQLCIPHER_HMAC_SHA256: + hash_idx = find_hash("sha256"); + break; + case SQLCIPHER_HMAC_SHA512: + hash_idx = find_hash("sha512"); + break; + default: + return SQLITE_ERROR; + } + + if(hash_idx < 0) return SQLITE_ERROR; + outlen = hash_descriptor[hash_idx].hashsize; + + if(in == NULL) return SQLITE_ERROR; + if((rc = hmac_init(&hmac, hash_idx, hmac_key, key_sz)) != CRYPT_OK) return SQLITE_ERROR; + if((rc = hmac_process(&hmac, in, in_sz)) != CRYPT_OK) return SQLITE_ERROR; + if(in2 != NULL && (rc = hmac_process(&hmac, in2, in2_sz)) != CRYPT_OK) return SQLITE_ERROR; + if((rc = hmac_done(&hmac, out, &outlen)) != CRYPT_OK) return SQLITE_ERROR; + return SQLITE_OK; +} + +static int sqlcipher_ltc_kdf( + void *ctx, int algorithm, + const unsigned char *pass, int pass_sz, + const unsigned char* salt, int salt_sz, + int workfactor, + int key_sz, unsigned char *key +) { + int rc, hash_idx; + unsigned long outlen = key_sz; + + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + hash_idx = find_hash("sha1"); + break; + case SQLCIPHER_HMAC_SHA256: + hash_idx = find_hash("sha256"); + break; + case SQLCIPHER_HMAC_SHA512: + hash_idx = find_hash("sha512"); + break; + default: + return SQLITE_ERROR; + } + if(hash_idx < 0) return SQLITE_ERROR; + + if((rc = pkcs_5_alg2(pass, pass_sz, salt, salt_sz, + workfactor, hash_idx, key, &outlen)) != CRYPT_OK) { + return SQLITE_ERROR; + } + return SQLITE_OK; +} + +static const char* sqlcipher_ltc_get_cipher(void *ctx) { + return "aes-256-cbc"; +} + +static int sqlcipher_ltc_cipher( + void *ctx, int mode, + const unsigned char *key, int key_sz, + const unsigned char *iv, + const unsigned char *in, int in_sz, + unsigned char *out +) { + int rc, cipher_idx; + symmetric_CBC cbc; + + if((cipher_idx = find_cipher(LTC_CIPHER)) == -1) return SQLITE_ERROR; + if((rc = cbc_start(cipher_idx, iv, key, key_sz, 0, &cbc)) != CRYPT_OK) return SQLITE_ERROR; + rc = mode == 1 ? cbc_encrypt(in, out, in_sz, &cbc) : cbc_decrypt(in, out, in_sz, &cbc); + if(rc != CRYPT_OK) return SQLITE_ERROR; + cbc_done(&cbc); + return SQLITE_OK; +} + +static int sqlcipher_ltc_get_key_sz(void *ctx) { + int cipher_idx = find_cipher(LTC_CIPHER); + return cipher_descriptor[cipher_idx].max_key_length; +} + +static int sqlcipher_ltc_get_iv_sz(void *ctx) { + int cipher_idx = find_cipher(LTC_CIPHER); + return cipher_descriptor[cipher_idx].block_length; +} + +static int sqlcipher_ltc_get_block_sz(void *ctx) { + int cipher_idx = find_cipher(LTC_CIPHER); + return cipher_descriptor[cipher_idx].block_length; +} + +static int sqlcipher_ltc_get_hmac_sz(void *ctx, int algorithm) { + int hash_idx; + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + hash_idx = find_hash("sha1"); + break; + case SQLCIPHER_HMAC_SHA256: + hash_idx = find_hash("sha256"); + break; + case SQLCIPHER_HMAC_SHA512: + hash_idx = find_hash("sha512"); + break; + default: + return 0; + } + + if(hash_idx < 0) return 0; + + return hash_descriptor[hash_idx].hashsize; +} + +static int sqlcipher_ltc_ctx_init(void **ctx) { + sqlcipher_ltc_activate(NULL); + return SQLITE_OK; +} + +static int sqlcipher_ltc_ctx_free(void **ctx) { + sqlcipher_ltc_deactivate(&ctx); + return SQLITE_OK; +} + +static int sqlcipher_ltc_fips_status(void *ctx) { + return 0; +} + +int sqlcipher_ltc_setup(sqlcipher_provider *p) { + p->init = NULL; + p->shutdown = NULL; + p->get_provider_name = sqlcipher_ltc_get_provider_name; + p->random = sqlcipher_ltc_random; + p->hmac = sqlcipher_ltc_hmac; + p->kdf = sqlcipher_ltc_kdf; + p->cipher = sqlcipher_ltc_cipher; + p->get_cipher = sqlcipher_ltc_get_cipher; + p->get_key_sz = sqlcipher_ltc_get_key_sz; + p->get_iv_sz = sqlcipher_ltc_get_iv_sz; + p->get_block_sz = sqlcipher_ltc_get_block_sz; + p->get_hmac_sz = sqlcipher_ltc_get_hmac_sz; + p->ctx_init = sqlcipher_ltc_ctx_init; + p->ctx_free = sqlcipher_ltc_ctx_free; + p->add_random = sqlcipher_ltc_add_random; + p->fips_status = sqlcipher_ltc_fips_status; + p->get_provider_version = sqlcipher_ltc_get_provider_version; + return SQLITE_OK; +} + +#endif +#endif +/* END SQLCIPHER */ diff --git a/src/crypto_nss.c b/src/crypto_nss.c new file mode 100644 index 0000000000..c0fd9dbdb0 --- /dev/null +++ b/src/crypto_nss.c @@ -0,0 +1,323 @@ +/* +** SQLCipher +** http://sqlcipher.net +** +** Copyright (c) 2008 - 2013, ZETETIC LLC +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** * Neither the name of the ZETETIC LLC nor the +** names of its contributors may be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +** +*/ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC +#ifdef SQLCIPHER_CRYPTO_NSS +#include "sqlcipher.h" +#include +#include +#include + +static NSSInitContext* nss_init_context = NULL; +static unsigned int nss_init_count = 0; + +int sqlcipher_nss_setup(sqlcipher_provider *p); + +static int sqlcipher_nss_activate(void *ctx) { + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_nss_activate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_nss_activate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + if (nss_init_context == NULL) { + nss_init_context = NSS_InitContext("", "", "", "", NULL, + NSS_INIT_READONLY | NSS_INIT_NOCERTDB | NSS_INIT_NOMODDB | + NSS_INIT_FORCEOPEN | NSS_INIT_OPTIMIZESPACE | NSS_INIT_NOROOTINIT); + } + nss_init_count++; + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_nss_activate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_nss_activate: left SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + return SQLITE_OK; +} + +static int sqlcipher_nss_deactivate(void *ctx) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_nss_activate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_nss_activate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + + nss_init_count--; + if (nss_init_count == 0 && nss_init_context != NULL) { + NSS_ShutdownContext(nss_init_context); + nss_init_context = NULL; + } + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_nss_activate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_nss_activate: left SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + return SQLITE_OK; +} + +static int sqlcipher_nss_add_random(void *ctx, const void *buffer, int length) { + return SQLITE_OK; +} + +/* generate a defined number of random bytes */ +static int sqlcipher_nss_random (void *ctx, void *buffer, int length) { + // PK11_GenerateRandom should be thread-safe. + return (PK11_GenerateRandom((unsigned char *)buffer, length) == SECSuccess) ? SQLITE_OK : SQLITE_ERROR; +} + +static const char* sqlcipher_nss_get_provider_name(void *ctx) { + return "nss"; +} + +static const char* sqlcipher_nss_get_provider_version(void *ctx) { + return NSS_GetVersion(); +} + +static const char* sqlcipher_nss_get_cipher(void *ctx) { + return "aes-256-cbc"; +} + +static int sqlcipher_nss_get_key_sz(void *ctx) { + return AES_256_KEY_LENGTH; +} + +static int sqlcipher_nss_get_iv_sz(void *ctx) { + return AES_BLOCK_SIZE; +} + +static int sqlcipher_nss_get_block_sz(void *ctx) { + return AES_BLOCK_SIZE; +} + +static int sqlcipher_nss_get_hmac_sz(void *ctx, int algorithm) { + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + return SHA1_LENGTH; + break; + case SQLCIPHER_HMAC_SHA256: + return SHA256_LENGTH; + break; + case SQLCIPHER_HMAC_SHA512: + return SHA512_LENGTH; + break; + default: + return 0; + } +} + +static int sqlcipher_nss_hmac( + void *ctx, int algorithm, + const unsigned char *hmac_key, int key_sz, + const unsigned char *in, int in_sz, + const unsigned char *in2, int in2_sz, + unsigned char *out +) { + int rc = SQLITE_OK; + unsigned int length; + unsigned int outLen; + PK11Context* context = NULL; + PK11SlotInfo * slot = NULL; + PK11SymKey* symKey = NULL; + if(in == NULL) goto error; + CK_MECHANISM_TYPE mech; + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + mech = CKM_SHA_1_HMAC; + break; + case SQLCIPHER_HMAC_SHA256: + mech = CKM_SHA256_HMAC; + break; + case SQLCIPHER_HMAC_SHA512: + mech = CKM_SHA512_HMAC; + break; + default: + goto error; + } + length = sqlcipher_nss_get_hmac_sz(ctx, algorithm); + slot = PK11_GetInternalSlot(); + if (slot == NULL) goto error; + SECItem keyItem; + keyItem.data = hmac_key; + keyItem.len = key_sz; + symKey = PK11_ImportSymKey(slot, mech, PK11_OriginUnwrap, + CKA_SIGN, &keyItem, NULL); + if (symKey == NULL) goto error; + SECItem noParams; + noParams.data = 0; + noParams.len = 0; + context = PK11_CreateContextBySymKey(mech, CKA_SIGN, symKey, &noParams); + if (context == NULL) goto error; + if (PK11_DigestBegin(context) != SECSuccess) goto error; + if (PK11_DigestOp(context, in, in_sz) != SECSuccess) goto error; + if (in2 != NULL) { + if (PK11_DigestOp(context, in2, in2_sz) != SECSuccess) goto error; + } + if (PK11_DigestFinal(context, out, &outLen, length) != SECSuccess) goto error; + + goto cleanup; + error: + rc = SQLITE_ERROR; + cleanup: + if (context) PK11_DestroyContext(context, PR_TRUE); + if (symKey) PK11_FreeSymKey(symKey); + if (slot) PK11_FreeSlot(slot); + return rc; +} + +static int sqlcipher_nss_kdf( + void *ctx, int algorithm, + const unsigned char *pass, int pass_sz, + const unsigned char* salt, int salt_sz, + int workfactor, + int key_sz, unsigned char *key +) { + int rc = SQLITE_OK; + PK11SlotInfo * slot = NULL; + SECAlgorithmID * algid = NULL; + PK11SymKey* symKey = NULL; + SECOidTag oidtag; + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + oidtag = SEC_OID_HMAC_SHA1; + break; + case SQLCIPHER_HMAC_SHA256: + oidtag = SEC_OID_HMAC_SHA256; + break; + case SQLCIPHER_HMAC_SHA512: + oidtag = SEC_OID_HMAC_SHA512; + break; + default: + goto error; + } + SECItem secSalt; + secSalt.data = salt; + secSalt.len = salt_sz; + // Always pass SEC_OID_HMAC_SHA1 (i.e. PBMAC1) as this parameter + // is unused for key generation. It is currently only used + // for PBKDF2 authentication or key (un)wrapping when specifying an + // encryption algorithm (PBES2). + algid = PK11_CreatePBEV2AlgorithmID(SEC_OID_PKCS5_PBKDF2, SEC_OID_HMAC_SHA1, + oidtag, key_sz, workfactor, &secSalt); + if (algid == NULL) goto error; + slot = PK11_GetInternalSlot(); + if (slot == NULL) goto error; + SECItem pwItem; + pwItem.data = (unsigned char *) pass; // PK11_PBEKeyGen doesn't modify the key. + pwItem.len = pass_sz; + symKey = PK11_PBEKeyGen(slot, algid, &pwItem, PR_FALSE, NULL); + if (symKey == NULL) goto error; + if (PK11_ExtractKeyValue(symKey) != SECSuccess) goto error; + // No need to free keyData as it is a buffer managed by symKey. + SECItem* keyData = PK11_GetKeyData(symKey); + if (keyData == NULL) goto error; + memcpy(key, keyData->data, key_sz); + + goto cleanup; + error: + rc = SQLITE_ERROR; + cleanup: + if (slot) PK11_FreeSlot(slot); + if (algid) SECOID_DestroyAlgorithmID(algid, PR_TRUE); + if (symKey) PK11_FreeSymKey(symKey); + return rc; +} + +static int sqlcipher_nss_cipher( + void *ctx, int mode, + const unsigned char *key, int key_sz, + const unsigned char *iv, + const unsigned char *in, + int in_sz, unsigned char *out +) { + int rc = SQLITE_OK; + PK11SlotInfo * slot = NULL; + PK11SymKey* symKey = NULL; + unsigned int outLen; + SECItem params; + params.data = iv; + params.len = sqlcipher_nss_get_iv_sz(ctx); + slot = PK11_GetInternalSlot(); + if (slot == NULL) goto error; + SECItem keyItem; + keyItem.data = key; + keyItem.len = key_sz; + symKey = PK11_ImportSymKey(slot, CKM_AES_CBC, PK11_OriginUnwrap, + CKA_ENCRYPT, &keyItem, NULL); + if (symKey == NULL) goto error; + SECStatus rv; + if (mode == CIPHER_ENCRYPT) { + rv = PK11_Encrypt(symKey, CKM_AES_CBC, ¶ms, out, &outLen, + in_sz + 16, in, in_sz); + } else { + rv = PK11_Decrypt(symKey, CKM_AES_CBC, ¶ms, out, &outLen, + in_sz + 16, in, in_sz); + } + if (rv != SECSuccess) goto error; + + goto cleanup; + error: + rc = SQLITE_ERROR; + cleanup: + if (slot) PK11_FreeSlot(slot); + if (symKey) PK11_FreeSymKey(symKey); + return rc; +} + +static int sqlcipher_nss_ctx_init(void **ctx) { + sqlcipher_nss_activate(NULL); + return SQLITE_OK; +} + +static int sqlcipher_nss_ctx_free(void **ctx) { + sqlcipher_nss_deactivate(NULL); + return SQLITE_OK; +} + +static int sqlcipher_nss_fips_status(void *ctx) { + return 0; +} + +int sqlcipher_nss_setup(sqlcipher_provider *p) { + p->init = NULL; + p->shutdown = NULL; + p->random = sqlcipher_nss_random; + p->get_provider_name = sqlcipher_nss_get_provider_name; + p->hmac = sqlcipher_nss_hmac; + p->kdf = sqlcipher_nss_kdf; + p->cipher = sqlcipher_nss_cipher; + p->get_cipher = sqlcipher_nss_get_cipher; + p->get_key_sz = sqlcipher_nss_get_key_sz; + p->get_iv_sz = sqlcipher_nss_get_iv_sz; + p->get_block_sz = sqlcipher_nss_get_block_sz; + p->get_hmac_sz = sqlcipher_nss_get_hmac_sz; + p->ctx_init = sqlcipher_nss_ctx_init; + p->ctx_free = sqlcipher_nss_ctx_free; + p->add_random = sqlcipher_nss_add_random; + p->fips_status = sqlcipher_nss_fips_status; + p->get_provider_version = sqlcipher_nss_get_provider_version; + return SQLITE_OK; +} + +#endif +#endif +/* END SQLCIPHER */ diff --git a/src/crypto_openssl.c b/src/crypto_openssl.c new file mode 100644 index 0000000000..828588b2d1 --- /dev/null +++ b/src/crypto_openssl.c @@ -0,0 +1,417 @@ +/* +** SQLCipher +** http://sqlcipher.net +** +** Copyright (c) 2008 - 2013, ZETETIC LLC +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** * Neither the name of the ZETETIC LLC nor the +** names of its contributors may be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +** +*/ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC +#ifdef SQLCIPHER_CRYPTO_OPENSSL +#include "sqliteInt.h" +#include "sqlcipher.h" +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ + +static unsigned int openssl_init_count = 0; + +static void sqlcipher_openssl_log_errors() { + unsigned long err = 0; + while((err = ERR_get_error()) != 0) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_log_errors: ERR_get_error() returned %lx: %s", err, ERR_error_string(err, NULL)); + } +} + +static int sqlcipher_openssl_add_random(void *ctx, const void *buffer, int length) { +#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_add_random: entering SQLCIPHER_MUTEX_PROVIDER_RAND"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_add_random: entered SQLCIPHER_MUTEX_PROVIDER_RAND"); +#endif + RAND_add(buffer, length, 0); +#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_add_random: leaving SQLCIPHER_MUTEX_PROVIDER_RAND"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_add_random: left SQLCIPHER_MUTEX_PROVIDER_RAND"); +#endif + return SQLITE_OK; +} + +#define OPENSSL_CIPHER EVP_aes_256_cbc() + +/* activate and initialize sqlcipher. Most importantly, this will automatically + intialize OpenSSL's EVP system if it hasn't already be externally. Note that + this function may be called multiple times as new codecs are intiialized. + Thus it performs some basic counting to ensure that only the last and final + sqlcipher_openssl_deactivate() will free the EVP structures. +*/ +static int sqlcipher_openssl_activate(void *ctx) { + /* initialize openssl and increment the internal init counter + but only if it hasn't been initalized outside of SQLCipher by this program + e.g. on startup */ + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_activate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_activate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + +#if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L) + ERR_load_crypto_strings(); +#endif + + openssl_init_count++; + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_activate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_activate: left SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + return SQLITE_OK; +} + +/* deactivate SQLCipher, most imporantly decremeting the activation count and + freeing the EVP structures on the final deactivation to ensure that + OpenSSL memory is cleaned up */ +static int sqlcipher_openssl_deactivate(void *ctx) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_deactivate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_deactivate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + + openssl_init_count--; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_deactivate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_deactivate: left SQLCIPHER_MUTEX_PROVIDER_ACTIVATE"); + return SQLITE_OK; +} + +static const char* sqlcipher_openssl_get_provider_name(void *ctx) { + return "openssl"; +} + +static const char* sqlcipher_openssl_get_provider_version(void *ctx) { +#if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L) + return OPENSSL_VERSION_TEXT; +#else + return OpenSSL_version(OPENSSL_VERSION); +#endif +} + +/* generate a defined number of random bytes */ +static int sqlcipher_openssl_random (void *ctx, void *buffer, int length) { + int rc = 0; + /* concurrent calls to RAND_bytes can cause a crash under some openssl versions when a + naive application doesn't use CRYPTO_set_locking_callback and + CRYPTO_THREADID_set_callback to ensure openssl thread safety. + This is simple workaround to prevent this common crash + but a more proper solution is that applications setup platform-appropriate + thread saftey in openssl externally */ +#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_random: entering SQLCIPHER_MUTEX_PROVIDER_RAND"); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_random: entered SQLCIPHER_MUTEX_PROVIDER_RAND"); +#endif + rc = RAND_bytes((unsigned char *)buffer, length); +#ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_random: leaving SQLCIPHER_MUTEX_PROVIDER_RAND"); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_openssl_random: left SQLCIPHER_MUTEX_PROVIDER_RAND"); +#endif + if(!rc) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_random: RAND_bytes() returned %d", rc); + sqlcipher_openssl_log_errors(); + return SQLITE_ERROR; + } + return SQLITE_OK; +} + +static int sqlcipher_openssl_hmac( + void *ctx, int algorithm, + const unsigned char *hmac_key, int key_sz, + const unsigned char *in, int in_sz, + const unsigned char *in2, int in2_sz, + unsigned char *out +) { + int rc = 0; + + size_t outlen; + EVP_MAC *mac = NULL; + EVP_MAC_CTX *hctx = NULL; + OSSL_PARAM sha1[] = { { "digest", OSSL_PARAM_UTF8_STRING, "sha1", 4, 0 }, OSSL_PARAM_END }; + OSSL_PARAM sha256[] = { { "digest", OSSL_PARAM_UTF8_STRING, "sha256", 6, 0 }, OSSL_PARAM_END }; + OSSL_PARAM sha512[] = { { "digest", OSSL_PARAM_UTF8_STRING, "sha512", 6, 0 }, OSSL_PARAM_END }; + + if(in == NULL) goto error; + + mac = EVP_MAC_fetch(NULL, "HMAC", NULL); + if(mac == NULL) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_fetch for HMAC failed"); + sqlcipher_openssl_log_errors(); + goto error; + } + + hctx = EVP_MAC_CTX_new(mac); + if(hctx == NULL) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_CTX_new() failed"); + sqlcipher_openssl_log_errors(); + goto error; + } + + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + if(!(rc = EVP_MAC_init(hctx, hmac_key, key_sz, sha1))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_init() with key size %d and sha1 returned %d", key_sz, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + break; + case SQLCIPHER_HMAC_SHA256: + if(!(rc = EVP_MAC_init(hctx, hmac_key, key_sz, sha256))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_init() with key size %d and sha256 returned %d", key_sz, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + break; + case SQLCIPHER_HMAC_SHA512: + if(!(rc = EVP_MAC_init(hctx, hmac_key, key_sz, sha512))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_init() with key size %d and sha512 returned %d", key_sz, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + break; + default: + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: invalid algorithm %d", algorithm); + goto error; + } + + if(!(rc = EVP_MAC_update(hctx, in, in_sz))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_update() on 1st input buffer of %d bytes using algorithm %d returned %d", in_sz, algorithm, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + if(in2 != NULL) { + if(!(rc = EVP_MAC_update(hctx, in2, in2_sz))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: EVP_MAC_update() on 2nd input buffer of %d bytes using algorithm %d returned %d", in_sz, algorithm, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + } + + if(!(rc = EVP_MAC_final(hctx, NULL, &outlen, 0))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: 1st EVP_MAC_final() for output length calculation using algorithm %d returned %d", algorithm, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + if(!(rc = EVP_MAC_final(hctx, out, &outlen, outlen))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_hmac: 2nd EVP_MAC_final() using algorithm %d returned %d", algorithm, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + rc = SQLITE_OK; + goto cleanup; + +error: + rc = SQLITE_ERROR; + +cleanup: + if(hctx) EVP_MAC_CTX_free(hctx); + if(mac) EVP_MAC_free(mac); + + return rc; +} + +static int sqlcipher_openssl_kdf( + void *ctx, int algorithm, + const unsigned char *pass, int pass_sz, + const unsigned char* salt, int salt_sz, + int workfactor, + int key_sz, unsigned char *key +) { + int rc = 0; + + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + if(!(rc = PKCS5_PBKDF2_HMAC((const char *)pass, pass_sz, salt, salt_sz, workfactor, EVP_sha1(), key_sz, key))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_kdf: PKCS5_PBKDF2_HMAC() for EVP_sha1() workfactor %d and key size %d returned %d", workfactor, key_sz, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + break; + case SQLCIPHER_HMAC_SHA256: + if(!(rc = PKCS5_PBKDF2_HMAC((const char *)pass, pass_sz, salt, salt_sz, workfactor, EVP_sha256(), key_sz, key))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_kdf: PKCS5_PBKDF2_HMAC() for EVP_sha256() workfactor %d and key size %d returned %d", workfactor, key_sz, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + break; + case SQLCIPHER_HMAC_SHA512: + if(!(rc = PKCS5_PBKDF2_HMAC((const char *)pass, pass_sz, salt, salt_sz, workfactor, EVP_sha512(), key_sz, key))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_kdf: PKCS5_PBKDF2_HMAC() for EVP_sha512() workfactor %d and key size %d returned %d", workfactor, key_sz, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + break; + default: + return SQLITE_ERROR; + } + + rc = SQLITE_OK; + goto cleanup; +error: + rc = SQLITE_ERROR; +cleanup: + return rc; +} + +static int sqlcipher_openssl_cipher( + void *ctx, int mode, + const unsigned char *key, int key_sz, + const unsigned char *iv, + const unsigned char *in, int in_sz, + unsigned char *out +) { + int tmp_csz, csz, rc = 0; + EVP_CIPHER_CTX* ectx = EVP_CIPHER_CTX_new(); + if(ectx == NULL) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_cipher: EVP_CIPHER_CTX_new failed"); + sqlcipher_openssl_log_errors(); + goto error; + } + + if(!(rc = EVP_CipherInit_ex(ectx, OPENSSL_CIPHER, NULL, NULL, NULL, mode))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_cipher: EVP_CipherInit_ex for mode %d returned %d", mode, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + if(!(rc = EVP_CIPHER_CTX_set_padding(ectx, 0))) { /* no padding */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_cipher: EVP_CIPHER_CTX_set_padding 0 returned %d", rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + if(!(rc = EVP_CipherInit_ex(ectx, NULL, NULL, key, iv, mode))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_cipher: EVP_CipherInit_ex for mode %d returned %d", mode, rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + if(!(rc = EVP_CipherUpdate(ectx, out, &tmp_csz, in, in_sz))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_cipher: EVP_CipherUpdate returned %d", rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + csz = tmp_csz; + out += tmp_csz; + if(!(rc = EVP_CipherFinal_ex(ectx, out, &tmp_csz))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "sqlcipher_openssl_cipher: EVP_CipherFinal_ex returned %d", rc); + sqlcipher_openssl_log_errors(); + goto error; + } + + csz += tmp_csz; + assert(in_sz == csz); + + rc = SQLITE_OK; + goto cleanup; +error: + rc = SQLITE_ERROR; +cleanup: + if(ectx) EVP_CIPHER_CTX_free(ectx); + return rc; +} + +static const char* sqlcipher_openssl_get_cipher(void *ctx) { + return OBJ_nid2sn(EVP_CIPHER_nid(OPENSSL_CIPHER)); +} + +static int sqlcipher_openssl_get_key_sz(void *ctx) { + return EVP_CIPHER_key_length(OPENSSL_CIPHER); +} + +static int sqlcipher_openssl_get_iv_sz(void *ctx) { + return EVP_CIPHER_iv_length(OPENSSL_CIPHER); +} + +static int sqlcipher_openssl_get_block_sz(void *ctx) { + return EVP_CIPHER_block_size(OPENSSL_CIPHER); +} + +static int sqlcipher_openssl_get_hmac_sz(void *ctx, int algorithm) { + switch(algorithm) { + case SQLCIPHER_HMAC_SHA1: + return EVP_MD_size(EVP_sha1()); + break; + case SQLCIPHER_HMAC_SHA256: + return EVP_MD_size(EVP_sha256()); + break; + case SQLCIPHER_HMAC_SHA512: + return EVP_MD_size(EVP_sha512()); + break; + default: + return 0; + } +} + +static int sqlcipher_openssl_ctx_init(void **ctx) { + return sqlcipher_openssl_activate(*ctx); +} + +static int sqlcipher_openssl_ctx_free(void **ctx) { + return sqlcipher_openssl_deactivate(NULL); +} + +static int sqlcipher_openssl_fips_status(void *ctx) { + return 0; +} + +int sqlcipher_openssl_setup(sqlcipher_provider *p) { + p->init = NULL; + p->shutdown = NULL; + p->get_provider_name = sqlcipher_openssl_get_provider_name; + p->random = sqlcipher_openssl_random; + p->hmac = sqlcipher_openssl_hmac; + p->kdf = sqlcipher_openssl_kdf; + p->cipher = sqlcipher_openssl_cipher; + p->get_cipher = sqlcipher_openssl_get_cipher; + p->get_key_sz = sqlcipher_openssl_get_key_sz; + p->get_iv_sz = sqlcipher_openssl_get_iv_sz; + p->get_block_sz = sqlcipher_openssl_get_block_sz; + p->get_hmac_sz = sqlcipher_openssl_get_hmac_sz; + p->ctx_init = sqlcipher_openssl_ctx_init; + p->ctx_free = sqlcipher_openssl_ctx_free; + p->add_random = sqlcipher_openssl_add_random; + p->fips_status = sqlcipher_openssl_fips_status; + p->get_provider_version = sqlcipher_openssl_get_provider_version; + return SQLITE_OK; +} + +#endif +#endif +/* END SQLCIPHER */ diff --git a/src/ctime.c b/src/ctime.c index 9f358bd27f..f85184108c 100644 --- a/src/ctime.c +++ b/src/ctime.c @@ -382,6 +382,11 @@ static const char * const sqlite3azCompileOpt[] = { #ifdef SQLITE_FTS5_NO_WITHOUT_ROWID "FTS5_NO_WITHOUT_ROWID", #endif +/* BEGIN SQLCIPHER */ +#if SQLITE_HAS_CODEC + "HAS_CODEC", +#endif +/* END SQLCIPHER */ #if HAVE_ISNAN || SQLITE_HAVE_ISNAN "HAVE_ISNAN", #endif diff --git a/src/global.c b/src/global.c index b4864a446c..65ed82d31d 100644 --- a/src/global.c +++ b/src/global.c @@ -163,9 +163,18 @@ const unsigned char sqlite3CtypeMap[256] = { ** EVIDENCE-OF: R-43642-56306 By default, URI handling is globally ** disabled. The default value may be changed by compiling with the ** SQLITE_USE_URI symbol defined. +** +** URI filenames are enabled by default if SQLITE_HAS_CODEC is +** enabled. */ #ifndef SQLITE_USE_URI -# define SQLITE_USE_URI 0 +/* BEGIN SQLCIPHER */ +# ifdef SQLITE_HAS_CODEC +# define SQLITE_USE_URI 1 +# else +# define SQLITE_USE_URI 0 +# endif +/* END SQLCIPHER */ #endif /* EVIDENCE-OF: R-38720-18127 The default setting is determined by the diff --git a/src/main.c b/src/main.c index 3f8790d414..3e55afc2d5 100644 --- a/src/main.c +++ b/src/main.c @@ -3212,6 +3212,43 @@ static const char *uriParameter(const char *zFilename, const char *zParam){ return 0; } +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) +/* +** Process URI filename query parameters relevant to the SQLite Encryption +** Extension. Return true if any of the relevant query parameters are +** seen and return false if not. +*/ +int sqlite3CodecQueryParameters( + sqlite3 *db, /* Database connection */ + const char *zDb, /* Which schema is being created/attached */ + const char *zUri /* URI filename */ +){ + const char *zKey; + if( zUri==0 ){ + return 0; + }else if( (zKey = uriParameter(zUri, "hexkey"))!=0 && zKey[0] ){ + u8 iByte; + int i; + char zDecoded[40]; + for(i=0, iByte=0; ixCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; } +# define CODEC2(P,D,N,X,E,O) \ + if( P->xCodec==0 ){ O=(char*)D; }else \ + if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; } +#else +# define CODEC1(P,D,N,X,E) /* NO-OP */ +# define CODEC2(P,D,N,X,E,O) O=(char*)D +#endif +/* END SQLCIPHER */ + /* ** The maximum allowed sector size. 64KiB. If the xSectorsize() method ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead. @@ -694,6 +710,14 @@ struct Pager { #endif void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */ int (*xGet)(Pager*,Pgno,DbPage**,int); /* Routine to fetch a patch */ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */ + void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */ + void (*xCodecFree)(void*); /* Destructor for the codec */ + void *pCodec; /* First argument to xCodec... methods */ +#endif +/* END SQLCIPHER */ char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */ PCache *pPCache; /* Pointer to page cache object */ #ifndef SQLITE_OMIT_WAL @@ -804,6 +828,11 @@ int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){ assert( pPager->fd!=0 ); if( pPager->fd->pMethods==0 ) return 0; /* Case (1) */ if( sqlite3PCacheIsDirty(pPager->pPCache) ) return 0; /* Failed (3) */ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + if( pPager->xCodec!=0 ) return 0; +#endif +/* END SQLCIPHER */ #ifndef SQLITE_OMIT_WAL if( pPager->pWal ){ u32 iRead = 0; @@ -1041,7 +1070,13 @@ static void setGetterMethod(Pager *pPager){ if( pPager->errCode ){ pPager->xGet = getPageError; #if SQLITE_MAX_MMAP_SIZE>0 - }else if( USEFETCH(pPager) ){ + }else if( USEFETCH(pPager) +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + && pPager->xCodec==0 +#endif +/* END SQLCIPHER */ + ){ pPager->xGet = getPageMMap; #endif /* SQLITE_MAX_MMAP_SIZE>0 */ }else{ @@ -2233,6 +2268,39 @@ static u32 pager_cksum(Pager *pPager, const u8 *aData){ return cksum; } +/* +** Report the current page size and number of reserved bytes back +** to the codec. +*/ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC +static void pagerReportSize(Pager *pPager){ + if( pPager->xCodecSizeChng ){ + pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize, + (int)pPager->nReserve); + } +} +#else +# define pagerReportSize(X) /* No-op if we do not support a codec */ +#endif +/* END SQLCIPHER */ + +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC +/* +** Make sure the number of reserved bits is the same in the destination +** pager as it is in the source. This comes up when a VACUUM changes the +** number of reserved bits to the "optimal" amount. +*/ +void sqlite3PagerAlignReserve(Pager *pDest, Pager *pSrc){ + if( pDest->nReserve!=pSrc->nReserve ){ + pDest->nReserve = pSrc->nReserve; + pagerReportSize(pDest); + } +} +#endif +/* END SQLCIPHER */ + /* ** Read a single page from either the journal file (if isMainJrnl==1) or ** from the sub-journal (if isMainJrnl==0) and playback that page. @@ -2284,6 +2352,13 @@ static int pager_playback_one_page( char *aData; /* Temporary storage for the page */ sqlite3_file *jfd; /* The file descriptor for the journal file */ int isSynced; /* True if journal page is synced */ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + /* The jrnlEnc flag is true if Journal pages should be passed through + ** the codec. It is false for pure in-memory journals. */ + const int jrnlEnc = (isMainJrnl || pPager->subjInMemory==0); +#endif +/* END SQLCIPHER */ assert( (isMainJrnl&~1)==0 ); /* isMainJrnl is 0 or 1 */ assert( (isSavepnt&~1)==0 ); /* isSavepnt is 0 or 1 */ @@ -2346,6 +2421,7 @@ static int pager_playback_one_page( */ if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){ pPager->nReserve = ((u8*)aData)[20]; + pagerReportSize(pPager); } /* If the pager is in CACHEMOD state, then there must be a copy of this @@ -2413,12 +2489,30 @@ static int pager_playback_one_page( ** is if the data was just read from an in-memory sub-journal. In that ** case it must be encrypted here before it is copied into the database ** file. */ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + if( !jrnlEnc ){ + CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT, aData); + rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst); + CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT); + }else +#endif +/* END SQLCIPHER */ rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst); if( pgno>pPager->dbFileSize ){ pPager->dbFileSize = pgno; } if( pPager->pBackup ){ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + if( jrnlEnc ){ + CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM_BKPT); + sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData); + CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM_BKPT,aData); + }else +#endif +/* END SQLCIPHER */ sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData); } }else if( !isMainJrnl && pPg==0 ){ @@ -2469,6 +2563,13 @@ static int pager_playback_one_page( if( pgno==1 ){ memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers)); } + + /* Decode the page just read from disk */ +/* BEGIN SQLCIPHER */ +#if SQLITE_HAS_CODEC + if( jrnlEnc ){ CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM_BKPT); } +#endif +/* END SQLCIPHER */ sqlite3PcacheRelease(pPg); } return rc; @@ -3047,6 +3148,8 @@ static int readDbPage(PgHdr *pPg){ memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers)); } } + CODEC1(pPager, pPg->pData, pPg->pgno, 3, rc = SQLITE_NOMEM_BKPT); + PAGER_INCR(sqlite3_pager_readdb_count); PAGER_INCR(pPager->nRead); IOTRACE(("PGIN %p %d\n", pPager, pPg->pgno)); @@ -3790,6 +3893,7 @@ int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){ if( nReserve<0 ) nReserve = pPager->nReserve; assert( nReserve>=0 && nReserve<1000 ); pPager->nReserve = (i16)nReserve; + pagerReportSize(pPager); pagerFixMaplimit(pPager); } return rc; @@ -4193,6 +4297,13 @@ int sqlite3PagerClose(Pager *pPager, sqlite3 *db){ sqlite3OsClose(pPager->fd); sqlite3PageFree(pTmp); sqlite3PcacheClose(pPager->pPCache); + +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec); +#endif +/* END SQLCIPHER */ + assert( !pPager->aSavepoint && !pPager->pInJournal ); assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) ); @@ -4443,7 +4554,8 @@ static int pager_write_pagelist(Pager *pPager, PgHdr *pList){ assert( (pList->flags&PGHDR_NEED_SYNC)==0 ); if( pList->pgno==1 ) pager_write_changecounter(pList); - pData = pList->pData; + /* Encode the database */ + CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM_BKPT, pData); /* Write out the page data. */ rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset); @@ -4532,6 +4644,14 @@ static int subjournalPage(PgHdr *pPg){ void *pData = pPg->pData; i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize); char *pData2; + +/* BEGIN SQLCIPHER */ +#if SQLITE_HAS_CODEC + if( !pPager->subjInMemory ){ + CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2); + }else +#endif +/* END SQLCIPHER */ pData2 = pData; PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno)); rc = write32bits(pPager->sjfd, offset, pPg->pgno); @@ -5624,6 +5744,11 @@ static int getPageMMap( ); assert( USEFETCH(pPager) ); +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + assert( pPager->xCodec==0 ); +#endif +/* END SQLCIPHER */ /* Optimization note: Adding the "pgno<=1" term before "pgno==0" here ** allows the compiler optimizer to reuse the results of the "pgno>1" @@ -5968,7 +6093,7 @@ static SQLITE_NOINLINE int pagerAddPageToRollbackJournal(PgHdr *pPg){ assert( pPg->pgno!=PAGER_SJ_PGNO(pPager) ); assert( pPager->journalHdr<=pPager->journalOff ); - pData2 = pPg->pData; + CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM_BKPT, pData2); cksum = pager_cksum(pPager, (u8*)pData2); /* Even if an IO or diskfull error occurs while journalling the @@ -6333,7 +6458,7 @@ static int pager_incr_changecounter(Pager *pPager, int isDirectMode){ if( DIRECT_MODE ){ const void *zBuf; assert( pPager->dbFileSize>0 ); - zBuf = pPgHdr->pData; + CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM_BKPT, zBuf); if( rc==SQLITE_OK ){ rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0); pPager->aStat[PAGER_STAT_WRITE]++; @@ -7096,6 +7221,49 @@ const char *sqlite3PagerJournalname(Pager *pPager){ return pPager->zJournal; } +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC +/* +** Set (or overwrite) the codec for this pager. If there is +** already a codec context (pCodec) attached, it WILL NOT be freed. +** The caller is responsible for freeing the old codec context prior +** setting a new one. +*/ +void sqlcipherPagerSetCodec( + Pager *pPager, + void *(*xCodec)(void*,void*,Pgno,int), + void (*xCodecSizeChng)(void*,int,int), + void (*xCodecFree)(void*), + void *pCodec +){ + pager_reset(pPager); + pPager->xCodec = pPager->memDb ? 0 : xCodec; + pPager->xCodecSizeChng = xCodecSizeChng; + pPager->xCodecFree = xCodecFree; + pPager->pCodec = pCodec; + setGetterMethod(pPager); + pagerReportSize(pPager); +} +/* Retrieve the codec for this pager */ +void *sqlcipherPagerGetCodec(Pager *pPager){ + return pPager->pCodec; +} + +/* +** This function is called by the wal module when writing page content +** into the log file. +** +** This function returns a pointer to a buffer containing the encrypted +** page content. If a malloc fails, this function may return NULL. +*/ +void *sqlcipherPagerCodec(PgHdr *pPg){ + void *aData = 0; + CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData); + return aData; +} +#endif /* SQLITE_HAS_CODEC */ +/* END SQLCIPHER */ + #ifndef SQLITE_OMIT_AUTOVACUUM /* ** Move the page pPg to location pgno in the file. @@ -7793,3 +7961,24 @@ int sqlite3PagerWalSystemErrno(Pager *pPager){ #endif #endif /* SQLITE_OMIT_DISKIO */ + +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + +int sqlite3pager_is_sj_pgno(Pager *pPager, Pgno pgno) { + return (PAGER_SJ_PGNO(pPager) == pgno) ? 1 : 0; +} + +void sqlite3pager_error(Pager *pPager, int error) { + pPager->errCode = error; + pPager->eState = PAGER_ERROR; + setGetterMethod(pPager); +} + +void sqlite3pager_reset(Pager *pPager){ + pager_reset(pPager); +} + +#endif +/* END SQLCIPHER */ + diff --git a/src/pager.h b/src/pager.h index 9b2cfc0bcf..b30775a15f 100644 --- a/src/pager.h +++ b/src/pager.h @@ -145,6 +145,11 @@ int sqlite3PagerReadFileheader(Pager*, int, unsigned char*); /* Functions used to configure a Pager object. */ void sqlite3PagerSetBusyHandler(Pager*, int(*)(void *), void *); int sqlite3PagerSetPagesize(Pager*, u32*, int); +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC +void sqlite3PagerAlignReserve(Pager*,Pager*); +#endif +/* END SQLCIPHER */ Pgno sqlite3PagerMaxPageCount(Pager*, Pgno); void sqlite3PagerSetCachesize(Pager*, int); int sqlite3PagerSetSpillsize(Pager*, int); @@ -241,6 +246,12 @@ void sqlite3PagerTruncateImage(Pager*,Pgno); void sqlite3PagerRekey(DbPage*, Pgno, u16); +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL) +void *sqlcipherPagerCodec(DbPage *); +#endif +/* END SQLCIPHER */ + /* Functions to support testing and debugging. */ #if !defined(NDEBUG) || defined(SQLITE_TEST) Pgno sqlite3PagerPagenumber(DbPage*); diff --git a/src/pragma.c b/src/pragma.c index 291ccf7af1..ce1726cd3e 100644 --- a/src/pragma.c +++ b/src/pragma.c @@ -424,6 +424,11 @@ void sqlite3Pragma( Db *pDb; /* The specific database being pragmaed */ Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */ const PragmaName *pPragma; /* The pragma */ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + extern int sqlcipher_codec_pragma(sqlite3*, int, Parse *, const char *, const char *); +#endif +/* END SQLCIPHER */ if( v==0 ) return; sqlite3VdbeRunOnlyOnce(v); @@ -491,8 +496,18 @@ void sqlite3Pragma( } pParse->nErr++; pParse->rc = rc; + + goto pragma_out; + } + +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + if(sqlcipher_codec_pragma(db, iDb, pParse, zLeft, zRight)) { + /* sqlcipher_codec_pragma executes internal */ goto pragma_out; } +#endif +/* END SQLCIPHER */ /* Locate the pragma in the lookup table */ pPragma = pragmaLocate(zLeft); @@ -2731,6 +2746,55 @@ void sqlite3Pragma( } #endif +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + /* Pragma iArg + ** ---------- ------ + ** key 0 + ** rekey 1 + ** hexkey 2 + ** hexrekey 3 + ** textkey 4 + ** textrekey 5 + */ + case PragTyp_KEY: { + if( zRight ){ + char zBuf[40]; + const char *zKey = zRight; + int n; + if( pPragma->iArg==2 || pPragma->iArg==3 ){ + u8 iByte; + int i; + for(i=0, iByte=0; iiArg<4 ? sqlite3Strlen30(zRight) : -1; + } + if( (pPragma->iArg & 1)==0 ){ + rc = sqlite3_key_v2(db, zDb, zKey, n); + }else{ + rc = sqlite3_rekey_v2(db, zDb, zKey, n); + } + if( rc==SQLITE_OK && n!=0 ){ + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "ok", SQLITE_STATIC); + returnSingleText(v, "ok"); + } else { + sqlite3ErrorMsg(pParse, "An error occurred with PRAGMA key or rekey. " + "PRAGMA key requires a key of one or more characters. " + "PRAGMA rekey can only be run on an existing encrypted database. " + "Use sqlcipher_export() and ATTACH to convert encrypted/plaintext databases."); + goto pragma_out; + } + } + break; + } +#endif +/* END SQLCIPHER */ #if defined(SQLITE_ENABLE_CEROD) case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){ if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){ diff --git a/src/pragma.h b/src/pragma.h index 56a469b9fa..b52679e6f7 100644 --- a/src/pragma.h +++ b/src/pragma.h @@ -5,6 +5,7 @@ */ /* The various pragma types */ +#define PragTyp_KEY 255 #define PragTyp_ACTIVATE_EXTENSIONS 0 #define PragTyp_ANALYSIS_LIMIT 1 #define PragTyp_HEADER_VALUE 2 @@ -341,6 +342,20 @@ static const PragmaName aPragmaName[] = { /* ePragFlg: */ PragFlg_Result0, /* ColNames: */ 0, 0, /* iArg: */ 0 }, +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) + {/* zName: */ "hexkey", + /* ePragTyp: */ PragTyp_KEY, + /* ePragFlg: */ 0, + /* ColNames: */ 0, 0, + /* iArg: */ 2 }, + {/* zName: */ "hexrekey", + /* ePragTyp: */ PragTyp_KEY, + /* ePragFlg: */ 0, + /* ColNames: */ 0, 0, + /* iArg: */ 3 }, +#endif +/* END SQLCIPHER */ #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) #if !defined(SQLITE_OMIT_CHECK) {/* zName: */ "ignore_check_constraints", @@ -393,6 +408,15 @@ static const PragmaName aPragmaName[] = { /* ColNames: */ 0, 0, /* iArg: */ 0 }, #endif +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) + {/* zName: */ "key", + /* ePragTyp: */ PragTyp_KEY, + /* ePragFlg: */ 0, + /* ColNames: */ 0, 0, + /* iArg: */ 0 }, +#endif +/* END SQLCIPHER */ #if !defined(SQLITE_OMIT_FLAG_PRAGMAS) {/* zName: */ "legacy_alter_table", /* ePragTyp: */ PragTyp_FLAG, @@ -500,6 +524,17 @@ static const PragmaName aPragmaName[] = { /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, /* ColNames: */ 0, 0, /* iArg: */ SQLITE_RecTriggers }, +#endif +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) + {/* zName: */ "rekey", + /* ePragTyp: */ PragTyp_KEY, + /* ePragFlg: */ 0, + /* ColNames: */ 0, 0, + /* iArg: */ 1 }, +#endif +/* END SQLCIPHER */ +#if !defined(SQLITE_OMIT_FLAG_PRAGMAS) {/* zName: */ "reverse_unordered_selects", /* ePragTyp: */ PragTyp_FLAG, /* ePragFlg: */ PragFlg_Result0|PragFlg_NoColumns1, @@ -589,6 +624,20 @@ static const PragmaName aPragmaName[] = { /* ColNames: */ 0, 0, /* iArg: */ 0 }, #endif +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) + {/* zName: */ "textkey", + /* ePragTyp: */ PragTyp_KEY, + /* ePragFlg: */ 0, + /* ColNames: */ 0, 0, + /* iArg: */ 4 }, + {/* zName: */ "textrekey", + /* ePragTyp: */ PragTyp_KEY, + /* ePragFlg: */ 0, + /* ColNames: */ 0, 0, + /* iArg: */ 5 }, +#endif +/* END SQLCIPHER */ {/* zName: */ "threads", /* ePragTyp: */ PragTyp_THREADS, /* ePragFlg: */ PragFlg_Result0, diff --git a/src/shell.c.in b/src/shell.c.in index fcc9316b00..860052831a 100644 --- a/src/shell.c.in +++ b/src/shell.c.in @@ -987,6 +987,19 @@ static char *one_input_line(FILE *in, char *zPrior, int isContinuation){ if( seenInterrupt==0 ) break; zResult = shell_readline(""); } +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + /* Simplistic filtering of input lines to prevent PRAGKA key and + PRAGMA rekey statements from being stored in readline history. + Note that this will only prevent single line statements, but that + will be sufficient for common cases. */ + if(zResult && *zResult && ( + sqlite3_strlike("%pragma%key%=%", zResult, 0)==0 || + sqlite3_strlike("%attach%database%as%key%", zResult, 0)==0 + ) + ) return zResult; +#endif +/* END SQLCIPHER */ if( zResult && *zResult ) shell_add_history(zResult); #endif } @@ -11889,6 +11902,16 @@ static int do_meta_command(char *zLine, ShellState *p){ char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit"; sqlite3_fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/, sqlite3_libversion(), sqlite3_sourceid()); +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + { + extern char* sqlcipher_version(); + char *sqlcipher_ver = sqlcipher_version(); + sqlite3_fprintf(p->out, "SQLCipher %s\n", sqlcipher_ver); + sqlite3_free(sqlcipher_ver); + } +#endif +/* END SQLCIPHER */ #if SQLITE_HAVE_ZLIB sqlite3_fprintf(p->out, "zlib version %s\n", zlibVersion()); #endif @@ -13177,8 +13200,19 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ }else if( cli_strcmp(z,"-bail")==0 ){ /* No-op. The bail_on_error flag should already be set. */ }else if( cli_strcmp(z,"-version")==0 ){ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + extern char* sqlcipher_version(); + char *sqlcipher_ver = sqlcipher_version(); + sqlite3_fprintf(stdout, "%s %s (%d-bit)", + sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*)); + sqlite3_fprintf(stdout, " (SQLCipher %s)\n", sqlcipher_ver); + sqlite3_free(sqlcipher_ver); +#else sqlite3_fprintf(stdout, "%s %s (%d-bit)\n", sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*)); +#endif +/* END SQLCIPHER */ return 0; }else if( cli_strcmp(z,"-interactive")==0 ){ /* Need to check for interactive override here to so that it can @@ -13309,10 +13343,25 @@ int SQLITE_CDECL wmain(int argc, wchar_t **wargv){ char *zHome; char *zHistory; int nHistory; +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + { + extern char* sqlcipher_version(); + char *sqlcipher_ver = sqlcipher_version(); + sqlite3_fprintf(stdout, + "SQLite version %s %.19s" /*extra-version-info*/ + " (SQLCipher %s)\n" /*sqlcipher version info*/ + "Enter \".help\" for usage hints.\n", + sqlite3_libversion(), sqlite3_sourceid(), sqlcipher_ver); + sqlite3_free(sqlcipher_ver); + } +#else sqlite3_fprintf(stdout, "SQLite version %s %.19s\n" /*extra-version-info*/ "Enter \".help\" for usage hints.\n", sqlite3_libversion(), sqlite3_sourceid()); +#endif +/* END SQLCIPHER */ if( warnInmemoryDb ){ sputz(stdout, "Connected to a "); printBold("transient in-memory database"); diff --git a/src/sqlcipher.c b/src/sqlcipher.c new file mode 100644 index 0000000000..bd346e5b37 --- /dev/null +++ b/src/sqlcipher.c @@ -0,0 +1,3825 @@ +/* +** SQLCipher +** http://zetetic.net +** +** Copyright (c) 2008-2024, ZETETIC LLC +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** * Neither the name of the ZETETIC LLC nor the +** names of its contributors may be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +** +*/ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + +#include "sqliteInt.h" +#include "btreeInt.h" +#include "pager.h" +#include "vdbeInt.h" + +#if !defined(SQLCIPHER_OMIT_LOG_DEVICE) +#if defined(__ANDROID__) +#include +#elif defined(__APPLE__) +#include +#include +#endif +#endif + +#include + +#if defined(_WIN32) || defined(SQLITE_OS_WINRT) +#include /* amalgamator: dontcache */ +#else +#include /* amalgamator: dontcache */ +#endif + +#ifndef OMIT_MEMLOCK +#if defined(__unix__) || defined(__APPLE__) || defined(_AIX) +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ +#include /* amalgamator: dontcache */ +#endif +#endif + +#include +#include "sqlcipher.h" + +#if !defined(SQLITE_EXTRA_INIT) || !defined(SQLITE_EXTRA_SHUTDOWN) +#error "SQLCipher must be compiled with -DSQLITE_EXTRA_INIT=sqlcipher_extra_init -DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown" +#endif + +#if !defined(SQLITE_THREADSAFE) || !(SQLITE_THREADSAFE == 1 || SQLITE_THREADSAFE == 2) +#error "SQLCipher must be compiled with -DSQLITE_THREADSAFE=<1 or 2>" +#endif + +#if !defined(SQLITE_TEMP_STORE) || SQLITE_TEMP_STORE == 0 || SQLITE_TEMP_STORE == 1 +#error "SQLCipher must be compiled with -DSQLITE_TEMP_STORE=<2 or 3>" +#endif + +/* extensions defined in pager.c */ +void *sqlcipherPagerGetCodec(Pager*); +void sqlcipherPagerSetCodec(Pager*, void *(*)(void*,void*,Pgno,int), void (*)(void*,int,int), void (*)(void*), void *); +int sqlite3pager_is_sj_pgno(Pager*, Pgno); +void sqlite3pager_error(Pager*, int); +void sqlite3pager_reset(Pager *pPager); +/* end extensions defined in pager.c */ + +#if !defined (SQLCIPHER_CRYPTO_CC) \ + && !defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT) \ + && !defined (SQLCIPHER_CRYPTO_NSS) \ + && !defined (SQLCIPHER_CRYPTO_OPENSSL) \ + && !defined (SQLCIPHER_CRYPTO_CUSTOM) +#define SQLCIPHER_CRYPTO_OPENSSL +#endif + +#define FILE_HEADER_SZ 16 + +#define CIPHER_XSTR(s) CIPHER_STR(s) +#define CIPHER_STR(s) #s + +#ifndef CIPHER_VERSION_NUMBER +#define CIPHER_VERSION_NUMBER 4.9.0 +#endif + +#ifndef CIPHER_VERSION_BUILD +#define CIPHER_VERSION_BUILD community +#endif + +#define CIPHER_DECRYPT 0 +#define CIPHER_ENCRYPT 1 + +#define CIPHER_READ_CTX 0 +#define CIPHER_WRITE_CTX 1 +#define CIPHER_READWRITE_CTX 2 + +#ifndef PBKDF2_ITER +#define PBKDF2_ITER 256000 +#endif + +#define SQLCIPHER_FLAG_GET(FLAG,BIT) ((FLAG & BIT) != 0) +#define SQLCIPHER_FLAG_SET(FLAG,BIT) FLAG |= BIT +#define SQLCIPHER_FLAG_UNSET(FLAG,BIT) FLAG &= ~BIT + +/* possible flags for codec_ctx->flags */ +#define CIPHER_FLAG_HMAC (1 << 0) +#define CIPHER_FLAG_LE_PGNO (1 << 1) +#define CIPHER_FLAG_BE_PGNO (1 << 2) +#define CIPHER_FLAG_KEY_USED (1 << 3) +#define CIPHER_FLAG_HAS_KDF_SALT (1 << 4) + + +#ifndef DEFAULT_CIPHER_FLAGS +#define DEFAULT_CIPHER_FLAGS CIPHER_FLAG_HMAC | CIPHER_FLAG_LE_PGNO +#endif + + +/* by default, sqlcipher will use a reduced number of iterations to generate + the HMAC key / or transform a raw cipher key + */ +#ifndef FAST_PBKDF2_ITER +#define FAST_PBKDF2_ITER 2 +#endif + +/* this if a fixed random array that will be xor'd with the database salt to ensure that the + salt passed to the HMAC key derivation function is not the same as that used to derive + the encryption key. This can be overridden at compile time but it will make the resulting + binary incompatible with the default builds when using HMAC. A future version of SQLcipher + will likely allow this to be defined at runtime via pragma */ +#ifndef HMAC_SALT_MASK +#define HMAC_SALT_MASK 0x3a +#endif + +#ifndef CIPHER_MAX_IV_SZ +#define CIPHER_MAX_IV_SZ 16 +#endif + +#ifndef CIPHER_MAX_KEY_SZ +#define CIPHER_MAX_KEY_SZ 64 +#endif + + +/* the default implementation of SQLCipher uses a cipher_ctx + to keep track of read / write state separately. The following + struct and associated functions are defined here */ +typedef struct { + int derive_key; + int pass_sz; + unsigned char *key; + unsigned char *hmac_key; + unsigned char *pass; +} cipher_ctx; + + +typedef struct { + int store_pass; + int kdf_iter; + int fast_kdf_iter; + int kdf_salt_sz; + int key_sz; + int iv_sz; + int block_sz; + int page_sz; + int reserve_sz; + int hmac_sz; + int plaintext_header_sz; + int hmac_algorithm; + int kdf_algorithm; + int error; + unsigned int flags; + unsigned char *kdf_salt; + unsigned char *hmac_kdf_salt; + unsigned char *buffer; + Btree *pBt; + cipher_ctx *read_ctx; + cipher_ctx *write_ctx; + sqlcipher_provider *provider; + void *provider_ctx; +} codec_ctx ; + +typedef struct private_block private_block; +struct private_block { + private_block *next; + u32 size; + u32 is_used; +}; + +/* implementation of simple, fast PSRNG function using xoshiro256++ (XOR/shift/rotate) + * https://prng.di.unimi.it/ under the public domain via https://prng.di.unimi.it/xoshiro256plusplus.c + * xoshiro is NEVER used for any cryptographic functions as CSPRNG. It is solely used for + * generating random data for testing, debugging, and forensic purposes (overwriting memory segments) */ +static volatile uint64_t xoshiro_s[4]; + +static inline uint64_t xoshiro_rotl(const uint64_t x, int k) { + return (x << k) | (x >> (64 - k)); +} + +uint64_t xoshiro_next(void) { + volatile uint64_t result = xoshiro_rotl(xoshiro_s[0] + xoshiro_s[3], 23) + xoshiro_s[0]; + volatile uint64_t t = xoshiro_s[1] << 17; + + xoshiro_s[2] ^= xoshiro_s[0]; + xoshiro_s[3] ^= xoshiro_s[1]; + xoshiro_s[1] ^= xoshiro_s[2]; + xoshiro_s[0] ^= xoshiro_s[3]; + + xoshiro_s[2] ^= t; + + xoshiro_s[3] = xoshiro_rotl(xoshiro_s[3], 45); + + return result; +} + +static void xoshiro_randomness(unsigned char *ptr, int sz) { + volatile uint64_t val; + volatile int to_copy; + while (sz > 0) { + val = xoshiro_next(); + to_copy = (sz >= sizeof(val)) ? sizeof(val) : sz; + memcpy(ptr, (void *) &val, to_copy); + ptr += to_copy; + sz -= to_copy; + } +} + +#ifdef SQLCIPHER_TEST +/* possible flags for simulating specific test conditions */ +#define TEST_FAIL_ENCRYPT 0x01 +#define TEST_FAIL_DECRYPT 0x02 +#define TEST_FAIL_MIGRATE 0x04 + +static volatile unsigned int cipher_test_flags = 0; +static volatile int cipher_test_rand = 0; + +static int sqlcipher_get_test_fail() { + int x; + + /* if cipher_test_rand is not set to a non-zero value always fail (return true) */ + if (cipher_test_rand == 0) return 1; + + xoshiro_randomness((unsigned char *) &x, sizeof(x)); + return ((x % cipher_test_rand) == 0); +} +#endif + +static volatile unsigned int default_flags = DEFAULT_CIPHER_FLAGS; +static volatile unsigned char hmac_salt_mask = HMAC_SALT_MASK; +static volatile int default_kdf_iter = PBKDF2_ITER; +static volatile int default_page_size = 4096; +static volatile int default_plaintext_header_size = 0; +static volatile int default_hmac_algorithm = SQLCIPHER_HMAC_SHA512; +static volatile int default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA512; +static volatile int sqlcipher_mem_security_on = 0; +static volatile int sqlcipher_mem_executed = 0; +static volatile int sqlcipher_mem_initialized = 0; +static volatile sqlite3_mem_methods default_mem_methods; +static sqlcipher_provider *default_provider = NULL; + +static sqlite3_mutex* sqlcipher_static_mutex[SQLCIPHER_MUTEX_COUNT]; +static FILE* sqlcipher_log_file = NULL; +static volatile int sqlcipher_log_device = 0; +static volatile unsigned int sqlcipher_log_level = SQLCIPHER_LOG_NONE; +static volatile unsigned int sqlcipher_log_source = SQLCIPHER_LOG_ANY; +static volatile int sqlcipher_log_set = 0; + +static size_t sqlcipher_shield_mask_sz = 32; +static u8* sqlcipher_shield_mask = NULL; + +/* Establish the default size of the private heap. This can be overriden + * at compile time by setting -DSQLCIPHER_PRIVATE_HEAP_SIZE_DEFAULT=X */ +#ifndef SQLCIPHER_PRIVATE_HEAP_SIZE_DEFAULT +/* On android, the maximim amount of memory that can be memlocked in 64k. This also + * seems to be a popular ulimit on linux distributions, containsers, etc. Therefore + * the default heap size is chosen as 48K, which is either 4 (with 4k page size) + * or 1 (with 16k page size) page less than the max. We choose to allocate slightly + * less than the max just in case the app has locked some other page(s). This + * initial allocation should be enough to support at least 10 concurrent + * sqlcipher-enabled database connections at the same time without requiring any + * overflow allocations */ +#define SQLCIPHER_PRIVATE_HEAP_SIZE_DEFAULT 49152 +#endif +/* if default allocation fails, we'll reduce the size by this amount + * and try again. This is also the minimium of the private heap. The minimum + * size will be 4 4K pages or 1 16K page (possible with latest android)*/ +#define SQLCIPHER_PRIVATE_HEAP_SIZE_STEP 16384 + +static volatile size_t private_heap_sz = SQLCIPHER_PRIVATE_HEAP_SIZE_DEFAULT; +static u8* private_heap = NULL; +static volatile size_t private_heap_used = 0; /* bytes currently used on private heap */ +static volatile size_t private_heap_hwm = 0; /* larged number of bytes used on the private heap at one time */ +static volatile size_t private_heap_alloc = 0; /* total bytes allocated on private heap over time */ +static volatile u32 private_heap_allocs = 0; /* total number of allocations on private heap over time */ +static volatile size_t private_heap_overflow = 0; /* total bytes overflowing private heap over time */ +static volatile u32 private_heap_overflows = 0; /* number of overlow allocations over time */ + +/* to prevent excessive fragmentation blocks will + * only be split if there are at least this many + * bytes available after the split. This should allow for at + * least two addtional small allocations */ +#define SQLCIPHER_PRIVATE_HEAP_MIN_SPLIT_SIZE 32 + +/* requested sizes will be rounded up to the nearest 8 bytes for alignment */ +#define SQLCIPHER_PRIVATE_HEAP_ALIGNMENT 8 +#define SQLCIPHER_PRIVATE_HEAP_ROUNDUP(x) ((x % SQLCIPHER_PRIVATE_HEAP_ALIGNMENT) ? \ + ((x / SQLCIPHER_PRIVATE_HEAP_ALIGNMENT) + 1) * SQLCIPHER_PRIVATE_HEAP_ALIGNMENT : x) + +static volatile int sqlcipher_init = 0; +static volatile int sqlcipher_shutdown = 0; +static volatile int sqlcipher_cleanup = 0; +static int sqlcipher_init_error = SQLITE_ERROR; + +static void sqlcipher_internal_free(void *, sqlite_uint64); +static void *sqlcipher_internal_malloc(sqlite_uint64); + + +/* +** Simple shared routines for converting hex char strings to binary data + */ +static int cipher_hex2int(char c) { + return (c>='0' && c<='9') ? (c)-'0' : + (c>='A' && c<='F') ? (c)-'A'+10 : + (c>='a' && c<='f') ? (c)-'a'+10 : 0; +} + +static void cipher_hex2bin(const unsigned char *hex, int sz, unsigned char *out){ + int i; + for(i = 0; i < sz; i += 2){ + out[i/2] = (cipher_hex2int(hex[i])<<4) | cipher_hex2int(hex[i+1]); + } +} + +static void cipher_bin2hex(const unsigned char* in, int sz, char *out) { + int i; + for(i=0; i < sz; i++) { + sqlite3_snprintf(3, out + (i*2), "%02x ", in[i]); + } +} + +static int cipher_isHex(const unsigned char *hex, int sz){ + int i; + for(i = 0; i < sz; i++) { + unsigned char c = hex[i]; + if ((c < '0' || c > '9') && + (c < 'A' || c > 'F') && + (c < 'a' || c > 'f')) { + return 0; + } + } + return 1; +} + +sqlite3_mutex* sqlcipher_mutex(int mutex) { + if(mutex < 0 || mutex >= SQLCIPHER_MUTEX_COUNT) return NULL; + return sqlcipher_static_mutex[mutex]; +} + +static void sqlcipher_atexit(void) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: calling sqlcipher_extra_shutdown()", __func__); + sqlcipher_extra_shutdown(); +} + +static void sqlcipher_fini(void) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: calling sqlcipher_extra_shutdown()", __func__); + sqlcipher_extra_shutdown(); +} + +#if defined(_WIN32) +#ifndef SQLCIPHER_OMIT_DLLMAIN +BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { + switch (fdwReason) { + case DLL_PROCESS_DETACH: + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: calling sqlcipher_extra_shutdown()", __func__); + sqlcipher_extra_shutdown(); + break; + default: + break; + } + return TRUE; +} +#endif +#elif defined(__APPLE__) +static void (*const sqlcipher_fini_func)(void) __attribute__((used, section("__DATA,__mod_term_func"))) = sqlcipher_fini; +#else +static void (*const sqlcipher_fini_func)(void) __attribute__((used, section(".fini_array"))) = sqlcipher_fini; +#endif + +static void sqlcipher_exportFunc(sqlite3_context*, int, sqlite3_value**); + +static int sqlcipher_export_init(sqlite3* db, const char** errmsg, const struct sqlite3_api_routines* api) { + sqlite3_create_function_v2(db, "sqlcipher_export", -1, SQLITE_TEXT, 0, sqlcipher_exportFunc, 0, 0, 0); + return SQLITE_OK; +} + +/* The extra_init function is called by sqlite3_init automaticay by virtue of + * being defined with SQLITE_EXTRA_INIT. This function sets up + * static mutexes used internally by SQLCipher and initializes + * the internal private heap */ +int sqlcipher_extra_init(const char* arg) { + int rc = SQLITE_OK, i=0; + void* provider_ctx = NULL; + + sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); + + if(sqlcipher_init) { + /* if this init routine already completed successfully return immediately */ + sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); + return SQLITE_OK; + } + + /* only register cleanup handlers once per process */ + if(!sqlcipher_cleanup) { + atexit(sqlcipher_atexit); + sqlcipher_cleanup = 1; + } + +#ifndef SQLCIPHER_OMIT_DEFAULT_LOGGING + /* when sqlcipher is first activated, set a default log target and level of WARN if the + logging settings have not yet been initialized. Use the "device log" for + android (logcat) or apple (console). Use stderr on all other platforms. */ + if(!sqlcipher_log_set) { + + /* set log level if it is different than the uninitalized default value of NONE */ + if(sqlcipher_log_level == SQLCIPHER_LOG_NONE) { + sqlcipher_log_level = SQLCIPHER_LOG_WARN; + } + + /* set the default file or device if neither is already set */ + if(sqlcipher_log_device == 0 && sqlcipher_log_file == NULL) { +#if defined(__ANDROID__) || defined(__APPLE__) + sqlcipher_log_device = 1; +#else + sqlcipher_log_file = stderr; +#endif + } + sqlcipher_log_set = 1; + } +#endif + + /* allocate static mutexe, and return error if any fail to allocate */ + for(i = 0; i < SQLCIPHER_MUTEX_COUNT; i++) { + if(sqlcipher_static_mutex[i] == NULL) { + if((sqlcipher_static_mutex[i] = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST)) == NULL) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to allocate static mutex %d", __func__, i); + rc = SQLITE_NOMEM; + goto error; + } + } + } + + /* initialize the private heap for use in internal SQLCipher memory allocations */ + if(private_heap == NULL) { + while(private_heap_sz >= SQLCIPHER_PRIVATE_HEAP_SIZE_STEP) { + /* attempt to allocate the private heap. If allocation fails, reduce the size and try again */ + if((private_heap = sqlcipher_internal_malloc(private_heap_sz))) { + xoshiro_randomness(private_heap, private_heap_sz); + /* initialize the head block of the linked list at the start of the heap */ + private_block *head = (private_block *) private_heap; + head->is_used = 0; + head->size = private_heap_sz - sizeof(private_block); + head->next = NULL; + break; + } + + /* allocation failed, reduce the requested size of the heap */ + private_heap_sz -= SQLCIPHER_PRIVATE_HEAP_SIZE_STEP; + } + } + if(!private_heap) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to allocate private heap", __func__); + rc = SQLITE_NOMEM; + goto error; + } + + /* check to see if there is a provider registered at this point + if there no provider registered at this point, register the + default provider */ + if(sqlcipher_get_provider() == NULL) { + sqlcipher_provider *p = sqlcipher_malloc(sizeof(sqlcipher_provider)); +#if defined (SQLCIPHER_CRYPTO_CC) + extern int sqlcipher_cc_setup(sqlcipher_provider *p); + sqlcipher_cc_setup(p); +#elif defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT) + extern int sqlcipher_ltc_setup(sqlcipher_provider *p); + sqlcipher_ltc_setup(p); +#elif defined (SQLCIPHER_CRYPTO_NSS) + extern int sqlcipher_nss_setup(sqlcipher_provider *p); + sqlcipher_nss_setup(p); +#elif defined (SQLCIPHER_CRYPTO_OPENSSL) + extern int sqlcipher_openssl_setup(sqlcipher_provider *p); + sqlcipher_openssl_setup(p); +#elif defined (SQLCIPHER_CRYPTO_OSSL3) + extern int sqlcipher_ossl3_setup(sqlcipher_provider *p); + sqlcipher_ossl3_setup(p); +#elif defined (SQLCIPHER_CRYPTO_CUSTOM) + extern int SQLCIPHER_CRYPTO_CUSTOM(sqlcipher_provider *p); + SQLCIPHER_CRYPTO_CUSTOM(p); +#else +#error "NO DEFAULT SQLCIPHER CRYPTO PROVIDER DEFINED" +#endif + if((rc = sqlcipher_register_provider(p)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "%s: failed to register provider %p %d", __func__, p, rc); + goto error; + } + } + + /* required random data */ + if((rc = default_provider->ctx_init(&provider_ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to initilize provider context %d", __func__, rc); + goto error; + } + + if((rc = default_provider->random(provider_ctx, (void *)xoshiro_s, sizeof(xoshiro_s))) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to generate xoshiro seed %d", __func__, rc); + goto error; + } + + if(!sqlcipher_shield_mask) { + if(!(sqlcipher_shield_mask = sqlcipher_internal_malloc(sqlcipher_shield_mask_sz))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to allocate shield mask", __func__); + goto error; + } + if((rc = default_provider->random(provider_ctx, sqlcipher_shield_mask, sqlcipher_shield_mask_sz)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to generate requisite random mask data %d", __func__, rc); + goto error; + } + } + + default_provider->ctx_free(provider_ctx); + + sqlcipher_init = 1; + sqlcipher_shutdown = 0; + + /* leave the master mutex so we can proceed with auto extension registration */ + sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); + + /* finally, extension registration occurs outside of the mutex because it is + * uses SQLITE_MUTEX_STATIC_MASTER itself */ + sqlite3_auto_extension((void (*)(void))sqlcipher_export_init); + + return SQLITE_OK; + +error: + /* if an error occurs during initialization, tear down everything that was setup */ + if(private_heap) { + sqlcipher_internal_free(private_heap, private_heap_sz); + private_heap = NULL; + } + if(sqlcipher_shield_mask) { + sqlcipher_internal_free(sqlcipher_shield_mask, sqlcipher_shield_mask_sz); + sqlcipher_shield_mask = NULL; + } + for(i = 0; i < SQLCIPHER_MUTEX_COUNT; i++) { + if(sqlcipher_static_mutex[i]) { + sqlite3_mutex_free(sqlcipher_static_mutex[i]); + sqlcipher_static_mutex[i] = NULL; + } + } + + /* post cleanup return the error code back up to sqlite3_init() */ + sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER)); + sqlcipher_init_error = rc; + return rc; +} + +/* The extra_shutdown function is called by sqlite3_shutdown() + * because it is defined with SQLITE_EXTRA_SHUTDOWN. In addition it will + * be called via atexit(), finalizer, and DllMain. The function will + * cleanup resources allocated by SQLCipher including mutexes, + * the private heap, and default provider. */ +void sqlcipher_extra_shutdown(void) { + int i = 0; + sqlcipher_provider *provider = NULL; + + /* if sqlcipher hasn't been initialized or the shutdown already completed exit early */ + if(!sqlcipher_init || sqlcipher_shutdown) { + goto cleanup; + } + + if(sqlcipher_shield_mask) { + sqlcipher_internal_free(sqlcipher_shield_mask, sqlcipher_shield_mask_sz); + sqlcipher_shield_mask = NULL; + } + + /* free the provider list. start at the default provider and move through the list + * freeing each one. If a provider has a shutdown function, call it before freeing. + * finally NULL out the default_provider */ + provider = default_provider; + while(provider) { + sqlcipher_provider *next = provider->next; + if(provider->shutdown) { + provider->shutdown(); + } + sqlcipher_free(provider, sizeof(sqlcipher_provider)); + provider = next; + } + default_provider = NULL; + + /* free private heap. If SQLCipher is compiled in test mode, it will deliberately + not free the heap (leaking it) if the heap is not empty. This will allow tooling + to detect memory issues like unfreed private heap memory */ + if(private_heap) { +#ifdef SQLCIPHER_TEST + size_t used = 0; + private_block *block = NULL; + block = (private_block *) private_heap; + while (block != NULL) { + if(block->is_used) { + used+= block->size; + i++; + } + block = block->next; + } + if(used > 0) { + /* don't free the heap so that sqlite treats this as unfreed memory */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, + "%s: SQLCipher private heap unfreed memory %u bytes in %d allocations", __func__, used, i); + } else { + sqlcipher_internal_free(private_heap, private_heap_sz); + private_heap = NULL; + } +#else + sqlcipher_internal_free(private_heap, private_heap_sz); + private_heap = NULL; +#endif + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, + "%s: SQLCipher private heap stats: size=%u, hwm=%u, alloc=%u, allocs=%u, overflow=%u, overflows=%u", __func__, + private_heap_sz, private_heap_hwm, private_heap_alloc, private_heap_allocs, private_heap_overflow, private_heap_overflows + ); + } + + /* free all of sqlcipher's static mutexes */ + for(i = 0; i < SQLCIPHER_MUTEX_COUNT; i++) { + if(sqlcipher_static_mutex[i]) { + sqlite3_mutex_free(sqlcipher_static_mutex[i]); + sqlcipher_static_mutex[i] = NULL; + } + } + +cleanup: + sqlcipher_init = 0; + sqlcipher_init_error = SQLITE_ERROR; + sqlcipher_shutdown = 1; +} + +static void sqlcipher_shield(unsigned char *in, int sz) { + int i = 0; + for(i = 0; i < sz; i++) { + in[i] ^= sqlcipher_shield_mask[i % sqlcipher_shield_mask_sz]; + } +} + +/* constant time memset using volitile to avoid having the memset + optimized out by the compiler. + Note: As suggested by Joachim Schipper (joachim.schipper@fox-it.com) +*/ +void* sqlcipher_memset(void *v, unsigned char value, sqlite_uint64 len) { + volatile sqlite_uint64 i = 0; + volatile unsigned char *a = v; + + if (v == NULL) return v; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_memset: setting %p[0-%u]=%d)", a, len, value); + for(i = 0; i < len; i++) { + a[i] = value; + } + + return v; +} + +/* constant time memory check tests every position of a memory segement + matches a single value (i.e. the memory is all zeros) + returns 0 if match, 1 of no match */ +int sqlcipher_ismemset(const void *v, unsigned char value, sqlite_uint64 len) { + const volatile unsigned char *a = v; + volatile sqlite_uint64 i = 0, result = 0; + + for(i = 0; i < len; i++) { + result |= a[i] ^ value; + } + + return (result != 0); +} + +/* constant time memory comparison routine. + returns 0 if match, 1 if no match */ +int sqlcipher_memcmp(const void *v0, const void *v1, int len) { + const volatile unsigned char *a0 = v0, *a1 = v1; + volatile int i = 0, result = 0; + + for(i = 0; i < len; i++) { + result |= a0[i] ^ a1[i]; + } + + return (result != 0); +} + +static void sqlcipher_mlock(void *ptr, sqlite_uint64 sz) { +#ifndef OMIT_MEMLOCK +#if defined(__unix__) || defined(__APPLE__) + int rc; + unsigned long pagesize = sysconf(_SC_PAGESIZE); + unsigned long offset = (unsigned long) ptr % pagesize; + + if(ptr == NULL || sz == 0) return; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: calling mlock(%p,%lu); _SC_PAGESIZE=%lu", ptr - offset, sz + offset, pagesize); + rc = mlock(ptr - offset, sz + offset); + if(rc!=0) { + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: mlock() returned %d errno=%d", rc, errno); + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: mlock(%p,%lu) returned %d errno=%d", ptr - offset, sz + offset, rc, errno); + } +#elif defined(_WIN32) +#if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_PC_APP)) + int rc; + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: calling VirtualLock(%p,%d)", ptr, sz); + rc = VirtualLock(ptr, sz); + if(rc==0) { + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: VirtualLock() returned %d LastError=%d", rc, GetLastError()); + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: VirtualLock(%p,%d) returned %d LastError=%d", ptr, sz, rc, GetLastError()); + } +#endif +#endif +#endif +} + +static void sqlcipher_munlock(void *ptr, sqlite_uint64 sz) { +#ifndef OMIT_MEMLOCK +#if defined(__unix__) || defined(__APPLE__) + int rc; + unsigned long pagesize = sysconf(_SC_PAGESIZE); + unsigned long offset = (unsigned long) ptr % pagesize; + + if(ptr == NULL || sz == 0) return; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_munlock: calling munlock(%p,%lu)", ptr - offset, sz + offset); + rc = munlock(ptr - offset, sz + offset); + if(rc!=0) { + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "sqlcipher_munlock: munlock(%p,%lu) returned %d errno=%d", ptr - offset, sz + offset, rc, errno); + } +#elif defined(_WIN32) +#if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_PC_APP)) + int rc; + + if(ptr == NULL || sz == 0) return; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_munlock: calling VirtualUnlock(%p,%d)", ptr, sz); + rc = VirtualUnlock(ptr, sz); + + /* because memory allocations may be made from the same individual page, it is possible for VirtualUnlock to be called + * multiple times for the same page. Subsequent calls will return an error, but this can be safely ignored (i.e. because + * the previous call for that page unlocked the memory already). Log an info level event only in that case. */ + if(!rc) { + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "sqlcipher_munlock: VirtualUnlock(%p,%d) returned %d LastError=%d", ptr, sz, rc, GetLastError()); + } +#endif +#endif +#endif +} + +/** sqlcipher wraps the default memory subsystem so it can optionally provide the + * memory security feature which will lock and sanitize ALL memory used by + * the sqlite library internally. Memory security feature is disabled by default + * but but the wrapper is used regardless, it just forwards to the default + * memory management implementation when disabled + */ +static int sqlcipher_mem_init(void *pAppData) { + return default_mem_methods.xInit(pAppData); +} +static void sqlcipher_mem_shutdown(void *pAppData) { + default_mem_methods.xShutdown(pAppData); +} +static void *sqlcipher_mem_malloc(int n) { + void *ptr = default_mem_methods.xMalloc(n); + if(!sqlcipher_mem_executed) sqlcipher_mem_executed = 1; + if(sqlcipher_mem_security_on) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_mem_malloc: calling sqlcipher_mlock(%p,%d)", ptr, n); + sqlcipher_mlock(ptr, n); + } + return ptr; +} +static int sqlcipher_mem_size(void *p) { + return default_mem_methods.xSize(p); +} +static void sqlcipher_mem_free(void *p) { + int sz; + if(!sqlcipher_mem_executed) sqlcipher_mem_executed = 1; + if(sqlcipher_mem_security_on) { + sz = sqlcipher_mem_size(p); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "%s: calling xoshiro_randomness(%p,%d) and sqlcipher_munlock(%p, %d)", __func__, p, sz, p, sz); + xoshiro_randomness(p, sz); + sqlcipher_munlock(p, sz); + } + default_mem_methods.xFree(p); +} +static void *sqlcipher_mem_realloc(void *p, int n) { + void *new = NULL; + int orig_sz = 0; + if(sqlcipher_mem_security_on) { + orig_sz = sqlcipher_mem_size(p); + if (n==0) { + sqlcipher_mem_free(p); + return NULL; + } else if (!p) { + return sqlcipher_mem_malloc(n); + } else if(n <= orig_sz) { + return p; + } else { + new = sqlcipher_mem_malloc(n); + if(new) { + memcpy(new, p, orig_sz); + sqlcipher_mem_free(p); + } + return new; + } + } else { + return default_mem_methods.xRealloc(p, n); + } +} + +static int sqlcipher_mem_roundup(int n) { + return default_mem_methods.xRoundup(n); +} + +static sqlite3_mem_methods sqlcipher_mem_methods = { + sqlcipher_mem_malloc, + sqlcipher_mem_free, + sqlcipher_mem_realloc, + sqlcipher_mem_size, + sqlcipher_mem_roundup, + sqlcipher_mem_init, + sqlcipher_mem_shutdown, + 0 +}; + +void sqlcipher_init_memmethods() { + if(sqlcipher_mem_initialized) return; + if(sqlite3_config(SQLITE_CONFIG_GETMALLOC, &default_mem_methods) != SQLITE_OK || + sqlite3_config(SQLITE_CONFIG_MALLOC, &sqlcipher_mem_methods) != SQLITE_OK) { + sqlcipher_mem_security_on = sqlcipher_mem_executed = sqlcipher_mem_initialized = 0; + } else { + sqlcipher_mem_initialized = 1; + } +} + +/** + * Free and wipe memory. Uses SQLites internal sqlite3_free so that memory + * can be countend and memory leak detection works in the test suite. + * If ptr is not null memory will be freed. + * If sz is greater than zero, the memory will be overwritten with zero before it is freed + * If sz is > 0, and not compiled with OMIT_MEMLOCK, system will attempt to unlock the + * memory segment so it can be paged + */ +static void sqlcipher_internal_free(void *ptr, sqlite_uint64 sz) { + xoshiro_randomness(ptr, sz); + sqlcipher_munlock(ptr, sz); + sqlite3_free(ptr); +} + +/** + * allocate memory. Uses sqlite's internall malloc wrapper so memory can be + * reference counted and leak detection works. Unless compiled with OMIT_MEMLOCK + * attempts to lock the memory pages so sensitive information won't be swapped + */ +static void* sqlcipher_internal_malloc(sqlite_uint64 sz) { + void *ptr; + ptr = sqlite3_malloc(sz); + sqlcipher_memset(ptr, 0, sz); + sqlcipher_mlock(ptr, sz); + return ptr; +} + +void *sqlcipher_malloc(sqlite3_uint64 size) { + void *alloc = NULL; + private_block *block = NULL, *split = NULL; + + if(size < 1) return NULL; + + size = SQLCIPHER_PRIVATE_HEAP_ROUNDUP(size); + + block = (private_block *) private_heap; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering SQLCIPHER_MUTEX_MEM", __func__); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_MEM)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered SQLCIPHER_MUTEX_MEM", __func__); + + /* iterate through the blocks in the heap to find one which is big enough to hold + the requested allocation. Stop when one is found. */ + while(block != NULL && alloc == NULL) { + if(!block->is_used && block->size >= size) { + /* mark the block as in use and set the return pointer to the start + of the block free space */ + block->is_used = 1; + alloc = ((u8*)block) + sizeof(private_block); + sqlcipher_memset(alloc, 0, size); + + /* if there is at least the minimim amount of required space left after allocation, + split off a new free block and insert it after the in-use block */ + if(block->size >= size + sizeof(private_block) + SQLCIPHER_PRIVATE_HEAP_MIN_SPLIT_SIZE) { + split = (private_block*) (((u8*) block) + size + sizeof(private_block)); + split->is_used = 0; + split->size = block->size - size - sizeof(private_block); + + /* insert inbetween current block and next */ + split->next = block->next; + block->next = split; + + /* only set the size of the current block to the requested amount + if the block was split. otherwise, size will be the full amount + of the block, which will actually be larger than the requested amount */ + block->size = size; + } + } + block = block->next; + } + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving SQLCIPHER_MUTEX_MEM", __func__); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_MEM)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left SQLCIPHER_MUTEX_MEM", __func__); + + /* If we were unable to locate a free block large enough to service the request, the fallback + behavior will simply attempt to allocate additional memory using malloc. */ + if(alloc == NULL) { + private_heap_overflow += size; + private_heap_overflows++; + alloc = sqlcipher_internal_malloc(size); + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "%s: unable to allocate %u bytes on private heap, allocated %p using sqlcipher_internal_malloc fallback", __func__, size, alloc); + } else { + private_heap_used += size; + if(private_heap_used > private_heap_hwm) { + /* if the current bytes allocated on the private heap are greater than the high water mark, set the HWM to the new amount */ + private_heap_hwm = private_heap_used; + } + private_heap_alloc += size; + private_heap_allocs++; + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "%s allocated %u bytes on private heap at %p", __func__, size, alloc); + } + + return alloc; +} + +void sqlcipher_free(void *mem, sqlite3_uint64 sz) { + private_block *block = NULL, *prev = NULL; + void *alloc = NULL; + u32 block_size = 0; + block = (private_block *) private_heap; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering SQLCIPHER_MUTEX_MEM", __func__); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_MEM)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered SQLCIPHER_MUTEX_MEM", __func__); + + /* search the heap for the block that contains this address */ + while(block != NULL) { + alloc = ((u8*)block)+sizeof(private_block); + /* if the memory address to be freed corresponds to this block's + allocation, mark it as unused. If they don't match, move + on to the next block */ + if(mem == alloc) { + block->is_used = 0; + block_size = block->size; /* retain the acual size of the block in use for stats adjustment */ + xoshiro_randomness(alloc, block->size); + + /* check whether the previous block is free, if so merge*/ + if(prev && !prev->is_used) { + prev->size = prev->size + sizeof(private_block) + block->size; + prev->next = block->next; + block = prev; + } + + /* check to see whether the next block is free, if so merge */ + if(block->next && !block->next->is_used) { + block->size = block->size + sizeof(private_block) + block->next->size; + block->next = block->next->next; + } + + /* once the block has been identified, marked free, and optionally + consolidated with it's neighbors, exit the loop, but leave + the block pointer intact so we know we found it in the heap */ + break; + } + + prev = block; + block = block->next; + } + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving SQLCIPHER_MUTEX_MEM", __func__); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_MEM)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left SQLCIPHER_MUTEX_MEM", __func__); + + /* If the memory address couldn't be found in the private heap + then it was allocated by the fallback mechanism and should + be deallocated with free() */ + if(!block) { + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "%s: unable to find %p with %u bytes on private heap, calling sqlcipher_internal_free fallback", __func__, mem, sz); + sqlcipher_internal_free(mem, sz); + } else { + private_heap_used -= block_size; + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "%s freed %u bytes (%u total) on private heap at %p", __func__, sz, block_size, mem); + } +} + +int sqlcipher_register_provider(sqlcipher_provider *p) { + int preexisting = 0, rc = SQLITE_OK; + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering SQLCIPHER_MUTEX_PROVIDER", __func__); + sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered SQLCIPHER_MUTEX_PROVIDER", __func__); + + if(!p || default_provider == p) { + goto cleanup; + } + + if(!default_provider) { + /* initial provider registration, NULL out previous pointer*/ + p->next = NULL; + } else { + /* one or more previous provider has already been registered, search through + * the list to see if the new provider has already been registered and handle + * appropriately */ + sqlcipher_provider *previous = default_provider; + sqlcipher_provider *current = default_provider->next; + while(current) { + if(current == p) { + /* this is a duplicate provider registration, and the provider in question + * already exists on the list. In that case, pop it out so it can be moved up to default. + * note that if we found an existing match we should avoid re-initializing the provider */ + previous->next = current->next; + preexisting = 1; + break; + } + previous = current; + current = current->next; + } + /* the current default_provider gets tacked on the list */ + p->next = default_provider; + } + + /* the new provider is elevated to default. if the provider was not preexisting and it has an initializer, call it */ + if(!preexisting && p->init) { + rc = p->init(); + } + + if(rc == SQLITE_OK) { + default_provider = p; + } +cleanup: + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving SQLCIPHER_MUTEX_PROVIDER", __func__); + sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER)); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left SQLCIPHER_MUTEX_PROVIDER", __func__); + + return rc; +} + +/* return a pointer to the currently registered provider. This will + allow an application to fetch the current registered provider and + make minor changes to it */ +sqlcipher_provider* sqlcipher_get_provider() { + return default_provider; +} + +char* sqlcipher_version(void) { +#ifdef CIPHER_VERSION_QUALIFIER + char *version = sqlite3_mprintf("%s %s %s", CIPHER_XSTR(CIPHER_VERSION_NUMBER), CIPHER_XSTR(CIPHER_VERSION_QUALIFIER), CIPHER_XSTR(CIPHER_VERSION_BUILD)); +#else + char *version = sqlite3_mprintf("%s %s", CIPHER_XSTR(CIPHER_VERSION_NUMBER), CIPHER_XSTR(CIPHER_VERSION_BUILD)); +#endif + return version; +} + +/** + * Initialize new cipher_ctx struct. This function will allocate memory + * for the cipher context and for the key + * + * returns SQLITE_OK if initialization was successful + * returns SQLITE_NOMEM if an error occured allocating memory + */ +static int sqlcipher_cipher_ctx_init(codec_ctx *ctx, cipher_ctx **iCtx) { + cipher_ctx *c_ctx; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_cipher_ctx_init: allocating context"); + *iCtx = (cipher_ctx *) sqlcipher_malloc(sizeof(cipher_ctx)); + c_ctx = *iCtx; + if(c_ctx == NULL) return SQLITE_NOMEM; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_cipher_ctx_init: allocating key"); + c_ctx->key = (unsigned char *) sqlcipher_malloc(ctx->key_sz); + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_cipher_ctx_init: allocating hmac_key"); + c_ctx->hmac_key = (unsigned char *) sqlcipher_malloc(ctx->key_sz); + + if(!c_ctx->key || !c_ctx->hmac_key) return SQLITE_NOMEM; + + return SQLITE_OK; +} + +/** + * Free and wipe memory associated with a cipher_ctx + */ +static void sqlcipher_cipher_ctx_free(codec_ctx* ctx, cipher_ctx **iCtx) { + cipher_ctx *c_ctx = *iCtx; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "cipher_ctx_free: iCtx=%p", iCtx); + if(c_ctx->key) sqlcipher_free(c_ctx->key, ctx->key_sz); + if(c_ctx->hmac_key) sqlcipher_free(c_ctx->hmac_key, ctx->key_sz); + if(c_ctx->pass) sqlcipher_free(c_ctx->pass, c_ctx->pass_sz); + sqlcipher_free(c_ctx, sizeof(cipher_ctx)); +} + +static int sqlcipher_codec_ctx_reserve_setup(codec_ctx *ctx) { + int base_reserve = ctx->iv_sz; /* base reserve size will be IV only */ + int reserve = base_reserve; + + ctx->hmac_sz = ctx->provider->get_hmac_sz(ctx->provider_ctx, ctx->hmac_algorithm); + + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC)) + reserve += ctx->hmac_sz; /* if reserve will include hmac, update that size */ + + /* calculate the amount of reserve needed in even increments of the cipher block size */ + if(ctx->block_sz > 0) { + reserve = ((reserve % ctx->block_sz) == 0) ? reserve : + ((reserve / ctx->block_sz) + 1) * ctx->block_sz; + } + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_reserve_setup: base_reserve=%d block_sz=%d md_size=%d reserve=%d", + base_reserve, ctx->block_sz, ctx->hmac_sz, reserve); + + ctx->reserve_sz = reserve; + + return SQLITE_OK; +} + +/** + * Compare one cipher_ctx to another. + * + * returns 0 if all the parameters (except the derived key data) are the same + * returns 1 otherwise + */ +static int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) { + int are_equal = ( + c1->pass_sz == c2->pass_sz + && ( + c1->pass == c2->pass + || !sqlcipher_memcmp((const unsigned char*)c1->pass, + (const unsigned char*)c2->pass, + c1->pass_sz) + )); + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_cmp: c1=%p c2=%p sqlcipher_memcmp(c1->pass, c2_pass)=%d are_equal=%d", + c1, c2, + (c1->pass == NULL || c2->pass == NULL) ? + -1 : + sqlcipher_memcmp( + (const unsigned char*)c1->pass, + (const unsigned char*)c2->pass, + c1->pass_sz + ), + are_equal + ); + + return !are_equal; /* return 0 if they are the same, 1 otherwise */ +} + +/** + * Copy one cipher_ctx to another. For instance, assuming that read_ctx is a + * fully initialized context, you could copy it to write_ctx and all yet data + * and pass information across + * + * returns SQLITE_OK if initialization was successful + * returns SQLITE_NOMEM if an error occured allocating memory + */ +static int sqlcipher_cipher_ctx_copy(codec_ctx *ctx, cipher_ctx *target, cipher_ctx *source) { + void *key = target->key; + void *hmac_key = target->hmac_key; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_copy: target=%p, source=%p", target, source); + if(target->pass) sqlcipher_free(target->pass, target->pass_sz); + memcpy(target, source, sizeof(cipher_ctx)); + + target->key = key; /* restore pointer to previously allocated key data */ + memcpy(target->key, source->key, ctx->key_sz); + + target->hmac_key = hmac_key; /* restore pointer to previously allocated hmac key data */ + memcpy(target->hmac_key, source->hmac_key, ctx->key_sz); + + if(source->pass && source->pass_sz) { + target->pass = sqlcipher_malloc(source->pass_sz); + if(target->pass == NULL) return SQLITE_NOMEM; + memcpy(target->pass, source->pass, source->pass_sz); + } + return SQLITE_OK; +} + +/** + * Get the keyspec for the cipher_ctx + * + * returns SQLITE_OK if assignment was successfull + * returns SQLITE_NOMEM if an error occured allocating memory + */ +static int sqlcipher_cipher_ctx_get_keyspec(codec_ctx *ctx, cipher_ctx *c_ctx, char **keyspec_ptr, int *keyspec_sz) { + int sz = 0; + char *keyspec = NULL, *out = NULL; + + if(keyspec_ptr == NULL) return SQLITE_NOMEM; + + /* establish the size for a hex-formated key specification, containing the + * raw encryption key, optional hmac key, and the salt used to generate it. + * The format will be either: + * x'hex(key)...hex(hmac_key)...hex(salt)' + * or + * x'hex(key)...hex(salt)' + *. The contents are SQLite BLOB formatted, so oversize by 3 bytes for the leading + * x' and trailing ' characters required by the spec*/ + if(ctx->flags & CIPHER_FLAG_HMAC) { /* if HMAC is enabled, encode key, hmac key, and salt */ + sz = ((ctx->key_sz + ctx->key_sz + ctx->kdf_salt_sz) * 2) + 3; + } else { /* otherwise encode key and salt */ + sz = ((ctx->key_sz + ctx->kdf_salt_sz) * 2) + 3; + } + + *keyspec_ptr = sqlcipher_malloc(sz); + keyspec = *keyspec_ptr; + + if(keyspec == NULL) return SQLITE_NOMEM; + + keyspec[0] = 'x'; + keyspec[1] = '\''; + + out = keyspec + 2; + + /* start with the encryption key */ + sqlcipher_shield(c_ctx->key, ctx->key_sz); + cipher_bin2hex(c_ctx->key, ctx->key_sz, out); + sqlcipher_shield(c_ctx->key, ctx->key_sz); + out += ctx->key_sz * 2; + + if(ctx->flags & CIPHER_FLAG_HMAC) { + /* add the hmac key after the encryption key if HMAC is in use*/ + sqlcipher_shield(c_ctx->hmac_key, ctx->key_sz); + cipher_bin2hex(c_ctx->hmac_key, ctx->key_sz, out); + sqlcipher_shield(c_ctx->hmac_key, ctx->key_sz); + out += ctx->key_sz * 2; + } + + /* finally encode the salt last */ + cipher_bin2hex(ctx->kdf_salt, ctx->kdf_salt_sz, out); + + keyspec[sz - 1] = '\''; + *keyspec_sz = sz; + + return SQLITE_OK; +} + +static void sqlcipher_set_derive_key(codec_ctx *ctx, int derive) { + if(ctx->read_ctx != NULL) ctx->read_ctx->derive_key = derive; + if(ctx->write_ctx != NULL) ctx->write_ctx->derive_key = derive; +} + +/** + * Set the passphrase for the cipher_ctx + * + * returns SQLITE_OK if assignment was successfull + * returns SQLITE_NOMEM if an error occured allocating memory + */ +static int sqlcipher_cipher_ctx_set_pass(cipher_ctx *ctx, const void *zKey, int nKey) { + /* free, zero existing pointers and size */ + if(ctx->pass) sqlcipher_free(ctx->pass, ctx->pass_sz); + ctx->pass = NULL; + ctx->pass_sz = 0; + + if(zKey && nKey) { /* if new password is provided, copy it */ + ctx->pass_sz = nKey; + ctx->pass = sqlcipher_malloc(nKey); + if(ctx->pass == NULL) return SQLITE_NOMEM; + memcpy(ctx->pass, zKey, nKey); + } + return SQLITE_OK; +} + +static int sqlcipher_codec_ctx_set_pass(codec_ctx *ctx, const void *zKey, int nKey, int for_ctx) { + cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; + int rc; + + if((rc = sqlcipher_cipher_ctx_set_pass(c_ctx, zKey, nKey)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_pass: error %d from sqlcipher_cipher_ctx_set_pass", rc); + return rc; + } + + c_ctx->derive_key = 1; + + if(for_ctx == 2) { + if((rc = sqlcipher_cipher_ctx_copy(ctx, for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_pass: error %d from sqlcipher_cipher_ctx_copy", rc); + return rc; + } + } + + return SQLITE_OK; +} + +static int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *ctx, int kdf_iter) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + ctx->kdf_iter = kdf_iter; + sqlcipher_set_derive_key(ctx, 1); + return SQLITE_OK; +} + +static int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *ctx, int fast_kdf_iter) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + + ctx->fast_kdf_iter = fast_kdf_iter; + sqlcipher_set_derive_key(ctx, 1); + return SQLITE_OK; +} + +/* set the global default flag for HMAC */ +static void sqlcipher_set_default_use_hmac(int use) { + if(use) SQLCIPHER_FLAG_SET(default_flags, CIPHER_FLAG_HMAC); + else SQLCIPHER_FLAG_UNSET(default_flags,CIPHER_FLAG_HMAC); +} + +/* set the codec flag for whether this individual database should be using hmac */ +static int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + + if(use) { + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HMAC); + } else { + SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_HMAC); + } + + return sqlcipher_codec_ctx_reserve_setup(ctx); +} + +/* the length of plaintext header size must be: + * 1. greater than or equal to zero + * 2. a multiple of the cipher block size + * 3. less than or equal to the non-reserve size of the first database page + * + * Note: it is possible to leave the entire first page in plaintext. This is discouraged since it will + * likely leak some small amount of schema data, but it's required to support use of the recovery VFS. + * see comment in sqlcipher_page_cipher for more details. + */ +static int sqlcipher_codec_ctx_set_plaintext_header_size(codec_ctx *ctx, int size) { + if(size >= 0 && ctx->block_sz > 0 && (size % ctx->block_sz) == 0 && size <= (ctx->page_sz - ctx->reserve_sz)) { + ctx->plaintext_header_sz = size; + return SQLITE_OK; + } + ctx->plaintext_header_sz = -1; + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: attempt to set invalid plantext_header_size %d", __func__, size); + return SQLITE_ERROR; +} + +static int sqlcipher_codec_ctx_set_hmac_algorithm(codec_ctx *ctx, int algorithm) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + + ctx->hmac_algorithm = algorithm; + return sqlcipher_codec_ctx_reserve_setup(ctx); +} + +static int sqlcipher_codec_ctx_set_kdf_algorithm(codec_ctx *ctx, int algorithm) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + + ctx->kdf_algorithm = algorithm; + return SQLITE_OK; +} + +static void sqlcipher_codec_ctx_set_error(codec_ctx *ctx, int error) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_error %d", error); + sqlite3pager_error(sqlite3BtreePager(ctx->pBt), error); + ctx->pBt->pBt->db->errCode = error; + ctx->error = error; +} + +static int sqlcipher_codec_ctx_init_kdf_salt(codec_ctx *ctx) { + sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(ctx->pBt)); + + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) { + return SQLITE_OK; /* don't reload salt when not needed */ + } + + /* read salt from header, if present, otherwise generate a new random salt */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init_kdf_salt: obtaining salt"); + if(fd == NULL || fd->pMethods == 0 || sqlite3OsRead(fd, ctx->kdf_salt, ctx->kdf_salt_sz, 0) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init_kdf_salt: unable to read salt from file header, generating random"); + if(ctx->provider->random(ctx->provider_ctx, ctx->kdf_salt, ctx->kdf_salt_sz) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init_kdf_salt: error retrieving random bytes from provider"); + return SQLITE_ERROR; + } + } + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT); + return SQLITE_OK; +} + +static int sqlcipher_codec_ctx_set_kdf_salt(codec_ctx *ctx, unsigned char *salt, int size) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + + if(size >= ctx->kdf_salt_sz) { + memcpy(ctx->kdf_salt, salt, ctx->kdf_salt_sz); + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT); + return SQLITE_OK; + } + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_kdf_salt: attempt to set salt of incorrect size %d", size); + return SQLITE_ERROR; +} + +static int sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx, void** salt) { + int rc = SQLITE_OK; + if(!SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) { + if((rc = sqlcipher_codec_ctx_init_kdf_salt(ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_get_kdf_salt: error %d from sqlcipher_codec_ctx_init_kdf_salt", rc); + } + } + *salt = ctx->kdf_salt; + + return rc; +} + +static int sqlcipher_codec_ctx_set_pagesize(codec_ctx *ctx, int size) { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK; + + if(!((size != 0) && ((size & (size - 1)) == 0)) || size < 512 || size > 65536) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "cipher_page_size not a power of 2 and between 512 and 65536 inclusive"); + return SQLITE_ERROR; + } + /* attempt to free the existing page buffer */ + if(ctx->buffer) sqlcipher_free(ctx->buffer,ctx->page_sz); + ctx->page_sz = size; + + /* pre-allocate a page buffer of PageSize bytes. This will + be used as a persistent buffer for encryption and decryption + operations to avoid overhead of multiple memory allocations*/ + ctx->buffer = sqlcipher_malloc(size); + if(ctx->buffer == NULL) return SQLITE_NOMEM; + + return SQLITE_OK; +} + +static int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, const void *zKey, int nKey) { + int rc; + codec_ctx *ctx; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_codec_ctx_init: allocating context"); + + *iCtx = sqlcipher_malloc(sizeof(codec_ctx)); + ctx = *iCtx; + + if(ctx == NULL) return SQLITE_NOMEM; + + ctx->pBt = pDb->pBt; /* assign pointer to database btree structure */ + + /* allocate space for salt data. Then read the first 16 bytes + directly off the database file. This is the salt for the + key derivation function. If we get a short read allocate + a new random salt value */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_codec_ctx_init: allocating kdf_salt"); + ctx->kdf_salt_sz = FILE_HEADER_SZ; + ctx->kdf_salt = sqlcipher_malloc(ctx->kdf_salt_sz); + if(ctx->kdf_salt == NULL) return SQLITE_NOMEM; + + /* allocate space for separate hmac salt data. We want the + HMAC derivation salt to be different than the encryption + key derivation salt */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_codec_ctx_init: allocating hmac_kdf_salt"); + ctx->hmac_kdf_salt = sqlcipher_malloc(ctx->kdf_salt_sz); + if(ctx->hmac_kdf_salt == NULL) return SQLITE_NOMEM; + + /* setup default flags */ + ctx->flags = default_flags; + + /* the context will use the current default crypto provider */ + ctx->provider = default_provider; + + if((rc = ctx->provider->ctx_init(&ctx->provider_ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d returned from ctx_init", rc); + return rc; + } + + ctx->key_sz = ctx->provider->get_key_sz(ctx->provider_ctx); + ctx->iv_sz = ctx->provider->get_iv_sz(ctx->provider_ctx); + ctx->block_sz = ctx->provider->get_block_sz(ctx->provider_ctx); + + /* + Always overwrite page size and set to the default because the first page of the database + in encrypted and thus sqlite can't effectively determine the pagesize. this causes an issue in + cases where bytes 16 & 17 of the page header are a power of 2 as reported by John Lehman + */ + if((rc = sqlcipher_codec_ctx_set_pagesize(ctx, default_page_size)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d returned from sqlcipher_codec_ctx_set_pagesize with %d", rc, default_page_size); + return rc; + } + + /* establish settings for the KDF iterations and fast (HMAC) KDF iterations */ + if((rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, default_kdf_iter)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting default_kdf_iter %d", rc, default_kdf_iter); + return rc; + } + + if((rc = sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, FAST_PBKDF2_ITER)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting fast_kdf_iter to %d", rc, FAST_PBKDF2_ITER); + return rc; + } + + /* set the default HMAC and KDF algorithms which will determine the reserve size */ + if((rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, default_hmac_algorithm)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting sqlcipher_codec_ctx_set_hmac_algorithm with %d", rc, default_hmac_algorithm); + return rc; + } + + /* Note that use_hmac is a special case that requires recalculation of page size + so we call set_use_hmac to perform setup */ + if((rc = sqlcipher_codec_ctx_set_use_hmac(ctx, SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC))) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting use_hmac %d", rc, SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC)); + return rc; + } + + if((rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, default_kdf_algorithm)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting sqlcipher_codec_ctx_set_kdf_algorithm with %d", rc, default_kdf_algorithm); + return rc; + } + + /* setup the default plaintext header size */ + if((rc = sqlcipher_codec_ctx_set_plaintext_header_size(ctx, default_plaintext_header_size)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting sqlcipher_codec_ctx_set_plaintext_header_size with %d", rc, default_plaintext_header_size); + return rc; + } + + /* initialize the read and write sub-contexts. this must happen after key_sz is established */ + if((rc = sqlcipher_cipher_ctx_init(ctx, &ctx->read_ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d initializing read_ctx", rc); + return rc; + } + + if((rc = sqlcipher_cipher_ctx_init(ctx, &ctx->write_ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d initializing write_ctx", rc); + return rc; + } + + /* set the key material on one of the sub cipher contexts and sync them up */ + if((rc = sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, 0)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting pass key", rc); + return rc; + } + + if((rc = sqlcipher_cipher_ctx_copy(ctx, ctx->write_ctx, ctx->read_ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d copying write_ctx to read_ctx", rc); + return rc; + } + + return SQLITE_OK; +} + +/** + * Free and wipe memory associated with a cipher_ctx, including the allocated + * read_ctx and write_ctx. + */ +static void sqlcipher_codec_ctx_free(codec_ctx **iCtx) { + codec_ctx *ctx = *iCtx; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "codec_ctx_free: iCtx=%p", iCtx); + if(ctx->kdf_salt) sqlcipher_free(ctx->kdf_salt, ctx->kdf_salt_sz); + if(ctx->hmac_kdf_salt) sqlcipher_free(ctx->hmac_kdf_salt, ctx->kdf_salt_sz); + if(ctx->buffer) sqlcipher_free(ctx->buffer, ctx->page_sz); + if(ctx->provider) ctx->provider->ctx_free(&ctx->provider_ctx); + + sqlcipher_cipher_ctx_free(ctx, &ctx->read_ctx); + sqlcipher_cipher_ctx_free(ctx, &ctx->write_ctx); + sqlcipher_free(ctx, sizeof(codec_ctx)); +} + +/** convert a 32bit unsigned integer to little endian byte ordering */ +static void sqlcipher_put4byte_le(unsigned char *p, u32 v) { + p[0] = (u8)v; + p[1] = (u8)(v>>8); + p[2] = (u8)(v>>16); + p[3] = (u8)(v>>24); +} + +static int sqlcipher_page_hmac(codec_ctx *ctx, cipher_ctx *c_ctx, Pgno pgno, unsigned char *in, int in_sz, unsigned char *out) { + unsigned char pgno_raw[sizeof(pgno)]; + int rc; + /* we may convert page number to consistent representation before calculating MAC for + compatibility across big-endian and little-endian platforms. + + Note: The public release of sqlcipher 2.0.0 to 2.0.6 had a bug where the bytes of pgno + were used directly in the MAC. SQLCipher convert's to little endian by default to preserve + backwards compatibility on the most popular platforms, but can optionally be configured + to use either big endian or native byte ordering via pragma. */ + + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_LE_PGNO)) { /* compute hmac using little endian pgno*/ + sqlcipher_put4byte_le(pgno_raw, pgno); + } else if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_BE_PGNO)) { /* compute hmac using big endian pgno */ + sqlite3Put4byte(pgno_raw, pgno); /* sqlite3Put4byte converts 32bit uint to big endian */ + } else { /* use native byte ordering */ + memcpy(pgno_raw, &pgno, sizeof(pgno)); + } + + /* include the encrypted page data, initialization vector, and page number in HMAC. This will + prevent both tampering with the ciphertext, manipulation of the IV, or resequencing otherwise + valid pages out of order in a database */ + sqlcipher_shield(c_ctx->hmac_key, ctx->key_sz); + rc = ctx->provider->hmac( + ctx->provider_ctx, ctx->hmac_algorithm, c_ctx->hmac_key, + ctx->key_sz, in, + in_sz, (unsigned char*) &pgno_raw, + sizeof(pgno), out); + sqlcipher_shield(c_ctx->hmac_key, ctx->key_sz); + + return rc; +} + +/* + * ctx - codec context + * pgno - page number in database + * size - size in bytes of input and output buffers + * mode - 1 to encrypt, 0 to decrypt + * in - pointer to input bytes + * out - pouter to output bytes + */ +static int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int page_sz, unsigned char *in, unsigned char *out) { + cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; + unsigned char *iv_in, *iv_out, *hmac_in, *hmac_out, *out_start; + int size, rc; + + /* calculate some required positions into various buffers */ + size = page_sz - ctx->reserve_sz; /* adjust size to useable size and memset reserve at end of page */ + iv_out = out + size; + iv_in = in + size; + + /* if the full amount of the first page (excluding reserve size), e.g. 4016 bytes for a 4096 byte page size with HMAC_SHA512, + * is used as a plaintext header, then the entire first page will be completely plaintext, and this function should just return early. + * This should almost never occur during normal usage, so we will log at WARN level, but it is required in the special case that + * a user wants to attempt recovery on an encrypted database. In that case, the database header must be completely plaintext so that + * the recovery VFS can be used with it's special 1st page logic */ + if(pgno == 1 && size == 0) { + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "%s: skipping encryption/decryption for fully plaintext header", __func__); + return SQLITE_OK; + } + + /* hmac will be written immediately after the initialization vector. the remainder of the page reserve will contain + random bytes. note, these pointers are only valid when using hmac */ + hmac_in = in + size + ctx->iv_sz; + hmac_out = out + size + ctx->iv_sz; + out_start = out; /* note the original position of the output buffer pointer, as out will be rewritten during encryption */ + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: pgno=%d, mode=%d, size=%d", __func__, pgno, mode, size); + CODEC_HEXDUMP("sqlcipher_page_cipher: input page data", in, page_sz); + + /* the key size should never be zero. If it is, error out. */ + if(ctx->key_sz == 0) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: error possible context corruption, key_sz is zero for pgno=%d", __func__, pgno); + goto error; + } + + if(mode == CIPHER_ENCRYPT) { + /* start at front of the reserve block, write random data to the end */ + if(ctx->provider->random(ctx->provider_ctx, iv_out, ctx->reserve_sz) != SQLITE_OK) goto error; + } else { /* CIPHER_DECRYPT */ + memcpy(iv_out, iv_in, ctx->iv_sz); /* copy the iv from the input to output buffer */ + } + + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == CIPHER_DECRYPT)) { + if(sqlcipher_page_hmac(ctx, c_ctx, pgno, in, size + ctx->iv_sz, hmac_out) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: hmac operation on decrypt failed for pgno=%d", __func__, pgno); + goto error; + } + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: comparing hmac on in=%p out=%p hmac_sz=%d", __func__, hmac_in, hmac_out, ctx->hmac_sz); + if(sqlcipher_memcmp(hmac_in, hmac_out, ctx->hmac_sz) != 0) { /* the hmac check failed */ + if(sqlite3BtreeGetAutoVacuum(ctx->pBt) != BTREE_AUTOVACUUM_NONE && sqlcipher_ismemset(in, 0, page_sz) == 0) { + /* first check if the entire contents of the page is zeros. If so, this page + resulted from a short read (i.e. sqlite attempted to pull a page after the end of the file. these + short read failures must be ignored for autovaccum mode to work so wipe the output buffer + and return SQLITE_OK to skip the decryption step. */ + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "%s: zeroed page (short read) for pgno %d with autovacuum enabled", __func__, pgno); + sqlcipher_memset(out, 0, page_sz); + return SQLITE_OK; + } else { + /* if the page memory is not all zeros, it means the there was data and a hmac on the page. + since the check failed, the page was either tampered with or corrupted. wipe the output buffer, + and return SQLITE_ERROR to the caller */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: hmac check failed for pgno=%d", __func__, pgno); + goto error; + } + } + } + + sqlcipher_shield(c_ctx->key, ctx->key_sz); + rc = ctx->provider->cipher(ctx->provider_ctx, mode, c_ctx->key, ctx->key_sz, iv_out, in, size, out); + sqlcipher_shield(c_ctx->key, ctx->key_sz); + + if(rc != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: cipher operation mode=%d failed for pgno=%d", __func__, mode, pgno); + goto error; + }; + + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == CIPHER_ENCRYPT)) { + if(sqlcipher_page_hmac(ctx, c_ctx, pgno, out_start, size + ctx->iv_sz, hmac_out) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: hmac operation on encrypt failed for pgno=%d", __func__, pgno); + goto error; + }; + } + + CODEC_HEXDUMP("sqlcipher_page_cipher: output page data", out_start, page_sz); + + return SQLITE_OK; +error: + sqlcipher_memset(out, 0, page_sz); + return SQLITE_ERROR; +} + +/** + * Derive an encryption key for a cipher contex key based on the raw password. + * + * If the raw key data is formated as x'hex' and there are exactly enough hex chars to fill + * the key (i.e 64 hex chars for a 256 bit key) then the key data will be used directly. + + * Else, if the raw key data is formated as x'hex' and there are exactly enough hex chars to fill + * the key and the salt (i.e 92 hex chars for a 256 bit key and 16 byte salt) then it will be unpacked + * as the key followed by the salt. + * + * Otherwise, a key data will be derived using PBKDF2 + * + * returns SQLITE_OK if initialization was successful + * returns SQLITE_ERROR if the key could't be derived (for instance if pass is NULL or pass_sz is 0) + */ +static int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) { + int rc, raw_key_sz = 0, raw_salt_sz = 0, blob_format = 0, derive_hmac_key = 1; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: ctx->kdf_salt_sz=%d ctx->kdf_iter=%d ctx->fast_kdf_iter=%d ctx->key_sz=%d", + __func__, ctx->kdf_salt_sz, ctx->kdf_iter, ctx->fast_kdf_iter, ctx->key_sz); + + /* if key material is present on the context for derivation */ + if(!c_ctx->pass || !c_ctx->pass_sz) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_key_derive: key material is not present on the context for key derivation"); + return SQLITE_ERROR; + } + + /* if necessary, initialize the salt from the header or random source */ + if(!SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) { + if((rc = sqlcipher_codec_ctx_init_kdf_salt(ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: error %d from sqlcipher_codec_ctx_init_kdf_salt", __func__, rc); + goto error; + } + } + + /* raw hey hex encoded is 2x long */ + raw_key_sz = ctx->key_sz * 2; + raw_salt_sz = ctx->kdf_salt_sz *2; + + /* raw key must be BLOB formatted: + * 1. greater than or equal to 5 characters long + * 2. starting with x' + * 3. ending with ' + * 4. length of contents between the x' and ' must be a power of 2 + * 5. contents must be hex */ + blob_format = + c_ctx->pass_sz >= 5 + && sqlite3StrNICmp((const char *)c_ctx->pass ,"x'", 2) == 0 + && c_ctx->pass[c_ctx->pass_sz - 1] == '\'' + && (c_ctx->pass_sz - 3) % 2 == 0 + && cipher_isHex(c_ctx->pass + 2, c_ctx->pass_sz - 3); + + if(blob_format && c_ctx->pass_sz == raw_key_sz + 3) { + /* option 1 - raw key consisting of only the encryption key */ + const unsigned char *z = c_ctx->pass + 2; /* adjust lead offset of x' */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: using raw key only", __func__); + cipher_hex2bin(z, raw_key_sz, c_ctx->key); + } else if(blob_format && c_ctx->pass_sz == raw_key_sz + raw_salt_sz + 3) { + /* option 2 - raw key consisting of the encryption key and salt */ + const unsigned char *z = c_ctx->pass + 2; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: using raw key and salt", __func__); + cipher_hex2bin(z, raw_key_sz, c_ctx->key); + cipher_hex2bin(z + raw_key_sz, raw_salt_sz, ctx->kdf_salt); + } else if(blob_format && c_ctx->pass_sz == raw_key_sz + raw_key_sz + raw_salt_sz + 3) { + /* option 3 - raw key consisting of the encryption key, then hmac key, then salt */ + const unsigned char *z = c_ctx->pass + 2; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: using raw key, hmac key, and salt", __func__); + cipher_hex2bin(z, raw_key_sz, c_ctx->key); + cipher_hex2bin(z + raw_key_sz, raw_key_sz, c_ctx->hmac_key); + cipher_hex2bin(z + raw_key_sz + raw_key_sz, raw_salt_sz, ctx->kdf_salt); + derive_hmac_key = 0; + } else { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: deriving key using PBKDF2 with %d iterations", __func__, ctx->kdf_iter); + if((rc = ctx->provider->kdf(ctx->provider_ctx, ctx->kdf_algorithm, c_ctx->pass, c_ctx->pass_sz, + ctx->kdf_salt, ctx->kdf_salt_sz, ctx->kdf_iter, + ctx->key_sz, c_ctx->key)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: error %d occurred from provider kdf generating encryption key", __func__, rc); + goto error; + } + } + + /* if this context is setup to use hmac checks, and we didn't already get an hmac + * key inbound via keyspec / raw key, then generate a seperate + * key for HMAC. In this case, we use the output of the previous KDF as the input to + * this KDF run. This ensures a distinct but predictable HMAC key. */ + if(ctx->flags & CIPHER_FLAG_HMAC && derive_hmac_key) { + int i; + + /* start by copying the kdf key into the hmac salt slot + then XOR it with the fixed hmac salt defined at compile time + this ensures that the salt passed in to derive the hmac key, while + easy to derive and publically known, is not the same as the salt used + to generate the encryption key */ + memcpy(ctx->hmac_kdf_salt, ctx->kdf_salt, ctx->kdf_salt_sz); + for(i = 0; i < ctx->kdf_salt_sz; i++) { + ctx->hmac_kdf_salt[i] ^= hmac_salt_mask; + } + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: deriving hmac key from encryption key using PBKDF2 with %d iterations", + __func__, ctx->fast_kdf_iter); + + if((rc = ctx->provider->kdf(ctx->provider_ctx, ctx->kdf_algorithm, c_ctx->key, ctx->key_sz, + ctx->hmac_kdf_salt, ctx->kdf_salt_sz, ctx->fast_kdf_iter, + ctx->key_sz, c_ctx->hmac_key)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: error occurred from provider kdf generating HMAC key", __func__, rc); + goto error; + } + } + + sqlcipher_shield(c_ctx->key, ctx->key_sz); + sqlcipher_shield(c_ctx->hmac_key, ctx->key_sz); + c_ctx->derive_key = 0; + return SQLITE_OK; + +error: + /* if an error occurred, overwrite any derived key material */ + xoshiro_randomness(c_ctx->key, ctx->key_sz); + xoshiro_randomness(c_ctx->hmac_key, ctx->key_sz); + return SQLITE_ERROR; +} + +static int sqlcipher_codec_key_derive(codec_ctx *ctx) { + /* derive key on first use if necessary */ + if(ctx->read_ctx->derive_key) { + if(sqlcipher_cipher_ctx_key_derive(ctx, ctx->read_ctx) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_key_derive: error occurred deriving read_ctx key"); + return SQLITE_ERROR; + } + } + + if(ctx->write_ctx->derive_key) { + if(sqlcipher_cipher_ctx_cmp(ctx->write_ctx, ctx->read_ctx) == 0) { + /* the relevant parameters are the same, just copy read key */ + if(sqlcipher_cipher_ctx_copy(ctx, ctx->write_ctx, ctx->read_ctx) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_key_derive: error occurred copying read_ctx to write_ctx"); + return SQLITE_ERROR; + } + } else { + if(sqlcipher_cipher_ctx_key_derive(ctx, ctx->write_ctx) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_key_derive: error occurred deriving write_ctx key"); + return SQLITE_ERROR; + } + } + } + + /* wipe and free passphrase after key derivation */ + if(ctx->store_pass != 1) { + sqlcipher_cipher_ctx_set_pass(ctx->read_ctx, NULL, 0); + sqlcipher_cipher_ctx_set_pass(ctx->write_ctx, NULL, 0); + } + + return SQLITE_OK; +} + +static int sqlcipher_codec_key_copy(codec_ctx *ctx, int source) { + if(source == CIPHER_READ_CTX) { + return sqlcipher_cipher_ctx_copy(ctx, ctx->write_ctx, ctx->read_ctx); + } else { + return sqlcipher_cipher_ctx_copy(ctx, ctx->read_ctx, ctx->write_ctx); + } +} + +static int sqlcipher_check_connection(const char *filename, char *key, int key_sz, char *sql, int *user_version, char** journal_mode) { + int rc; + sqlite3 *db = NULL; + sqlite3_stmt *statement = NULL; + char *query_journal_mode = "PRAGMA journal_mode;"; + char *query_user_version = "PRAGMA user_version;"; + + rc = sqlite3_open(filename, &db); + if(rc != SQLITE_OK) goto cleanup; + + rc = sqlite3_key(db, key, key_sz); + if(rc != SQLITE_OK) goto cleanup; + + rc = sqlite3_exec(db, sql, NULL, NULL, NULL); + if(rc != SQLITE_OK) goto cleanup; + + /* start by querying the user version. + this will fail if the key is incorrect */ + rc = sqlite3_prepare(db, query_user_version, -1, &statement, NULL); + if(rc != SQLITE_OK) goto cleanup; + + rc = sqlite3_step(statement); + if(rc == SQLITE_ROW) { + *user_version = sqlite3_column_int(statement, 0); + } else { + goto cleanup; + } + sqlite3_finalize(statement); + + rc = sqlite3_prepare(db, query_journal_mode, -1, &statement, NULL); + if(rc != SQLITE_OK) goto cleanup; + + rc = sqlite3_step(statement); + if(rc == SQLITE_ROW) { + *journal_mode = sqlite3_mprintf("%s", sqlite3_column_text(statement, 0)); + } else { + goto cleanup; + } + rc = SQLITE_OK; + /* cleanup will finalize open statement */ + +cleanup: + if(statement) sqlite3_finalize(statement); + if(db) sqlite3_close(db); + return rc; +} + +static int sqlcipher_codec_ctx_integrity_check(codec_ctx *ctx, Parse *pParse, char *column) { + Pgno page = 1; + int rc = 0; + char *result; + unsigned char *hmac_out = NULL; + sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(ctx->pBt)); + i64 file_sz; + + Vdbe *v = sqlite3GetVdbe(pParse); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, column, SQLITE_STATIC); + + if(fd == NULL || fd->pMethods == 0) { + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "database file is undefined", P4_TRANSIENT); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + goto cleanup; + } + + if(!(ctx->flags & CIPHER_FLAG_HMAC)) { + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "HMAC is not enabled, unable to integrity check", P4_TRANSIENT); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + goto cleanup; + } + + if((rc = sqlcipher_codec_key_derive(ctx)) != SQLITE_OK) { + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "unable to derive keys", P4_TRANSIENT); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + goto cleanup; + } + + sqlite3OsFileSize(fd, &file_sz); + hmac_out = sqlcipher_malloc(ctx->hmac_sz); + + for(page = 1; page <= file_sz / ctx->page_sz; page++) { + i64 offset = (page - 1) * ctx->page_sz; + int payload_sz = ctx->page_sz - ctx->reserve_sz + ctx->iv_sz; + int read_sz = ctx->page_sz; + + /* skip integrity check on PAGER_SJ_PGNO since it will have no valid content */ + if(sqlite3pager_is_sj_pgno(sqlite3BtreePager(ctx->pBt), page)) continue; + + if(page==1) { + int page1_offset = ctx->plaintext_header_sz ? ctx->plaintext_header_sz : FILE_HEADER_SZ; + read_sz = read_sz - page1_offset; + payload_sz = payload_sz - page1_offset; + offset += page1_offset; + } + + sqlcipher_memset(ctx->buffer, 0, ctx->page_sz); + sqlcipher_memset(hmac_out, 0, ctx->hmac_sz); + if(sqlite3OsRead(fd, ctx->buffer, read_sz, offset) != SQLITE_OK) { + result = sqlite3_mprintf("error reading %d bytes from file page %d at offset %d", read_sz, page, offset); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + } else if(sqlcipher_page_hmac(ctx, ctx->read_ctx, page, ctx->buffer, payload_sz, hmac_out) != SQLITE_OK) { + result = sqlite3_mprintf("HMAC operation failed for page %d", page); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + } else if(sqlcipher_memcmp(ctx->buffer + payload_sz, hmac_out, ctx->hmac_sz) != 0) { + result = sqlite3_mprintf("HMAC verification failed for page %d", page); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + } + } + + if(file_sz % ctx->page_sz != 0) { + result = sqlite3_mprintf("page %d has an invalid size of %lld bytes (expected %d bytes)", page, file_sz - ((file_sz / ctx->page_sz) * ctx->page_sz), ctx->page_sz); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); + } + +cleanup: + if(hmac_out != NULL) sqlcipher_free(hmac_out, ctx->hmac_sz); + return SQLITE_OK; +} + +static int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) { + int i, pass_sz, keyspec_sz, nRes, user_version, rc, rc_cleanup, oflags; + Db *pDb = 0; + sqlite3 *db = ctx->pBt->db; + const char *db_filename = sqlite3_db_filename(db, "main"); + char *set_user_version = NULL, *pass = NULL, *attach_command = NULL, *migrated_db_filename = NULL, *keyspec = NULL, *temp = NULL, *journal_mode = NULL, *set_journal_mode = NULL, *pragma_compat = NULL; + Btree *pDest = NULL, *pSrc = NULL; + sqlite3_file *srcfile, *destfile; +#if defined(_WIN32) || defined(SQLITE_OS_WINRT) + LPWSTR w_db_filename = NULL, w_migrated_db_filename = NULL; + int w_db_filename_sz = 0, w_migrated_db_filename_sz = 0; +#endif + pass_sz = keyspec_sz = rc = user_version = 0; + + if(!db_filename || sqlite3Strlen30(db_filename) < 1) + goto cleanup; /* exit immediately if this is an in memory database */ + + /* pull the provided password / key material off the current codec context */ + pass_sz = ctx->read_ctx->pass_sz; + pass = sqlcipher_malloc(pass_sz+1); + memset(pass, 0, pass_sz+1); + memcpy(pass, ctx->read_ctx->pass, pass_sz); + + /* Version 4 - current, no upgrade required, so exit immediately */ + rc = sqlcipher_check_connection(db_filename, pass, pass_sz, "", &user_version, &journal_mode); + if(rc == SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: no upgrade required - exiting"); + goto cleanup; + } + + for(i = 3; i > 0; i--) { + pragma_compat = sqlite3_mprintf("PRAGMA cipher_compatibility = %d;", i); + rc = sqlcipher_check_connection(db_filename, pass, pass_sz, pragma_compat, &user_version, &journal_mode); + if(rc == SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: version %d format found", i); + goto migrate; + } + if(pragma_compat) sqlcipher_free(pragma_compat, sqlite3Strlen30(pragma_compat)); + pragma_compat = NULL; + } + + /* if we exit the loop normally we failed to determine the version, this is an error */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: unable to determine format version for upgrade: this may indicate custom settings were used "); + goto handle_error; + +migrate: + + temp = sqlite3_mprintf("%s-migrated", db_filename); + /* overallocate migrated_db_filename, because sqlite3OsOpen will read past the null terminator + * to determine whether the filename was URI formatted */ + migrated_db_filename = sqlcipher_malloc(sqlite3Strlen30(temp)+2); + memcpy(migrated_db_filename, temp, sqlite3Strlen30(temp)); + sqlcipher_free(temp, sqlite3Strlen30(temp)); + + attach_command = sqlite3_mprintf("ATTACH DATABASE '%s' as migrate;", migrated_db_filename, pass); + set_user_version = sqlite3_mprintf("PRAGMA migrate.user_version = %d;", user_version); + + rc = sqlite3_exec(db, pragma_compat, NULL, NULL, NULL); + if(rc != SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: set compatibility mode failed, error code %d", rc); + goto handle_error; + } + + /* force journal mode to DELETE, we will set it back later if different */ + rc = sqlite3_exec(db, "PRAGMA journal_mode = delete;", NULL, NULL, NULL); + if(rc != SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: force journal mode DELETE failed, error code %d", rc); + goto handle_error; + } + + rc = sqlite3_exec(db, attach_command, NULL, NULL, NULL); + if(rc != SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: attach failed, error code %d", rc); + goto handle_error; + } + + rc = sqlite3_key_v2(db, "migrate", pass, pass_sz); + if(rc != SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: keying attached database failed, error code %d", rc); + goto handle_error; + } + + rc = sqlite3_exec(db, "SELECT sqlcipher_export('migrate');", NULL, NULL, NULL); + if(rc != SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: sqlcipher_export failed, error code %d", rc); + goto handle_error; + } + +#ifdef SQLCIPHER_TEST + if((cipher_test_flags & TEST_FAIL_MIGRATE) > 0) { + rc = SQLITE_ERROR; + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: simulated migrate failure, error code %d", rc); + goto handle_error; + } +#endif + + rc = sqlite3_exec(db, set_user_version, NULL, NULL, NULL); + if(rc != SQLITE_OK){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: set user version failed, error code %d", rc); + goto handle_error; + } + + if( !db->autoCommit ){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: cannot migrate from within a transaction"); + goto handle_error; + } + if( db->nVdbeActive>1 ){ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: cannot migrate - SQL statements in progress"); + goto handle_error; + } + + pDest = db->aDb[0].pBt; + pDb = &(db->aDb[db->nDb-1]); + pSrc = pDb->pBt; + + nRes = sqlite3BtreeGetRequestedReserve(pSrc); + /* unset the BTS_PAGESIZE_FIXED flag to avoid SQLITE_READONLY */ + pDest->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED; + rc = sqlite3BtreeSetPageSize(pDest, default_page_size, nRes, 0); + if(rc != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to set btree page size to %d res %d rc %d", default_page_size, nRes, rc); + goto handle_error; + } + + sqlcipherCodecGetKey(db, db->nDb - 1, (void**)&keyspec, &keyspec_sz); + SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_KEY_USED); + sqlcipherCodecAttach(db, 0, keyspec, keyspec_sz); + + srcfile = sqlite3PagerFile(sqlite3BtreePager(pSrc)); + destfile = sqlite3PagerFile(sqlite3BtreePager(pDest)); + + sqlite3OsClose(srcfile); + sqlite3OsClose(destfile); + +#if defined(_WIN32) || defined(SQLITE_OS_WINRT) + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: performing windows MoveFileExA"); + + w_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) db_filename, -1, NULL, 0); + w_db_filename = sqlcipher_malloc(w_db_filename_sz * sizeof(wchar_t)); + w_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) db_filename, -1, (const LPWSTR) w_db_filename, w_db_filename_sz); + + w_migrated_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) migrated_db_filename, -1, NULL, 0); + w_migrated_db_filename = sqlcipher_malloc(w_migrated_db_filename_sz * sizeof(wchar_t)); + w_migrated_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) migrated_db_filename, -1, (const LPWSTR) w_migrated_db_filename, w_migrated_db_filename_sz); + + if(!MoveFileExW(w_migrated_db_filename, w_db_filename, MOVEFILE_REPLACE_EXISTING)) { + rc = SQLITE_ERROR; + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: error occurred while renaming migration files %d", rc); + goto handle_error; + } +#else + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: performing POSIX rename"); + if ((rc = rename(migrated_db_filename, db_filename)) != 0) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: error occurred while renaming migration files %s to %s: %d", migrated_db_filename, db_filename, rc); + goto handle_error; + } +#endif + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: renamed migration database %s to main database %s: %d", migrated_db_filename, db_filename, rc); + + rc = sqlite3OsOpen(db->pVfs, migrated_db_filename, srcfile, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB, &oflags); + if(rc != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to reopen migration database %s: %d", migrated_db_filename, rc); + goto handle_error; + } + + rc = sqlite3OsOpen(db->pVfs, db_filename, destfile, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB, &oflags); + if(rc != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to reopen main database %s: %d", db_filename, rc); + goto handle_error; + } + + sqlite3pager_reset(sqlite3BtreePager(pDest)); + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: reset pager"); + +handle_error: + rc_cleanup = sqlite3_exec(db, "DETACH DATABASE migrate;", NULL, NULL, NULL); + if(rc_cleanup != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: DETACH DATABASE migrate failed: %d", rc_cleanup); + /* only overwrite the rc in the cleanup stage if it is currently not an error. This will prevent overwriting a previous error that occured earlier in migration */ + if(rc == SQLITE_OK) { + rc = rc_cleanup; + } + } + + sqlite3ResetAllSchemasOfConnection(db); + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: reset all schemas"); + + if(journal_mode) { + set_journal_mode = sqlite3_mprintf("PRAGMA journal_mode = %s;", journal_mode); + rc_cleanup = sqlite3_exec(db, set_journal_mode, NULL, NULL, NULL); + if(rc_cleanup != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to re-set journal mode via %s: %d", set_journal_mode, rc_cleanup); + if(rc == SQLITE_OK) { + rc = rc_cleanup; + } + } + } + + if(migrated_db_filename) { + int del_rc = sqlite3OsDelete(db->pVfs, migrated_db_filename, 0); + if(del_rc != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to delete migration database %s: %d", migrated_db_filename, del_rc); + } + } + + if(rc != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: an error occurred attempting to migrate the database - last error %d", rc); + sqlite3pager_reset(sqlite3BtreePager(ctx->pBt)); + ctx->error = rc; /* set flag for deferred error */ + } + +cleanup: + if(pass) sqlcipher_free(pass, pass_sz); + if(keyspec) sqlcipher_free(keyspec, keyspec_sz); + if(attach_command) sqlcipher_free(attach_command, sqlite3Strlen30(attach_command)); + if(migrated_db_filename) sqlcipher_free(migrated_db_filename, sqlite3Strlen30(migrated_db_filename)); + if(set_user_version) sqlcipher_free(set_user_version, sqlite3Strlen30(set_user_version)); + if(set_journal_mode) sqlcipher_free(set_journal_mode, sqlite3Strlen30(set_journal_mode)); + if(journal_mode) sqlcipher_free(journal_mode, sqlite3Strlen30(journal_mode)); + if(pragma_compat) sqlcipher_free(pragma_compat, sqlite3Strlen30(pragma_compat)); +#if defined(_WIN32) || defined(SQLITE_OS_WINRT) + if(w_db_filename) sqlcipher_free(w_db_filename, w_db_filename_sz); + if(w_migrated_db_filename) sqlcipher_free(w_migrated_db_filename, w_migrated_db_filename_sz); +#endif + + return rc; +} + +static int sqlcipher_codec_add_random(codec_ctx *ctx, const char *zRight, int random_sz){ + const char *suffix = &zRight[random_sz-1]; + int n = random_sz - 3; /* adjust for leading x' and tailing ' */ + if (n > 0 && + sqlite3StrNICmp((const char *)zRight ,"x'", 2) == 0 && + sqlite3StrNICmp(suffix, "'", 1) == 0 && + n % 2 == 0) { + int rc = 0; + int buffer_sz = n / 2; + unsigned char *random; + const unsigned char *z = (const unsigned char *)zRight + 2; /* adjust lead offset of x' */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_add_random: using raw random blob from hex"); + random = sqlcipher_malloc(buffer_sz); + memset(random, 0, buffer_sz); + cipher_hex2bin(z, n, random); + rc = ctx->provider->add_random(ctx->provider_ctx, random, buffer_sz); + sqlcipher_free(random, buffer_sz); + return rc; + } + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_add_random: attemt to add random with invalid format"); + return SQLITE_ERROR; +} + +#if !defined(SQLITE_OMIT_TRACE) + +#define SQLCIPHER_PROFILE_FMT "Elapsed time:%.3f ms - %s\n" +#define SQLCIPHER_PROFILE_FMT_OSLOG "Elapsed time:%{public}.3f ms - %{public}s\n" + +static int sqlcipher_profile_callback(unsigned int trace, void *file, void *stmt, void *run_time){ + FILE *f = (FILE*) file; + double elapsed = (*((sqlite3_uint64*)run_time))/1000000.0; + if(f == NULL) { +#if !defined(SQLCIPHER_OMIT_LOG_DEVICE) +#if defined(__ANDROID__) + __android_log_print(ANDROID_LOG_DEBUG, "sqlcipher", SQLCIPHER_PROFILE_FMT, elapsed, sqlite3_sql((sqlite3_stmt*)stmt)); +#elif defined(__APPLE__) + os_log(OS_LOG_DEFAULT, SQLCIPHER_PROFILE_FMT_OSLOG, elapsed, sqlite3_sql((sqlite3_stmt*)stmt)); +#endif +#endif + } else { + fprintf(f, SQLCIPHER_PROFILE_FMT, elapsed, sqlite3_sql((sqlite3_stmt*)stmt)); + } + return SQLITE_OK; +} +#endif + +static int sqlcipher_cipher_profile(sqlite3 *db, const char *destination){ +#if defined(SQLITE_OMIT_TRACE) + return SQLITE_ERROR; +#else + FILE *f = NULL; + if(sqlite3_stricmp(destination, "off") == 0){ + sqlite3_trace_v2(db, 0, NULL, NULL); /* disable tracing */ + } else { + if(sqlite3_stricmp(destination, "stdout") == 0){ + f = stdout; + }else if(sqlite3_stricmp(destination, "stderr") == 0){ + f = stderr; + }else if(sqlite3_stricmp(destination, "logcat") == 0 || sqlite3_stricmp(destination, "device") == 0){ + f = NULL; /* file pointer will be NULL indicating the device target (i.e. logcat or oslog). We will accept logcat for backwards compatibility */ + }else{ +#if !defined(SQLCIPHER_PROFILE_USE_FOPEN) && (defined(_WIN32) && (__STDC_VERSION__ > 199901L) || defined(SQLITE_OS_WINRT)) + if(fopen_s(&f, destination, "a") != 0) return SQLITE_ERROR; +#else + if((f = fopen(destination, "a")) == 0) return SQLITE_ERROR; +#endif + } + sqlite3_trace_v2(db, SQLITE_TRACE_PROFILE, sqlcipher_profile_callback, f); + } + return SQLITE_OK; +#endif +} + +static char *sqlcipher_get_log_level_str(unsigned int level) { + switch(level) { + case SQLCIPHER_LOG_ERROR: + return "ERROR"; + case SQLCIPHER_LOG_WARN: + return "WARN"; + case SQLCIPHER_LOG_INFO: + return "INFO"; + case SQLCIPHER_LOG_DEBUG: + return "DEBUG"; + case SQLCIPHER_LOG_TRACE: + return "TRACE"; + case SQLCIPHER_LOG_ANY: + return "ANY"; + } + return "NONE"; +} + +static char *sqlcipher_get_log_source_str(unsigned int source) { + switch(source) { + case SQLCIPHER_LOG_NONE: + return "NONE"; + case SQLCIPHER_LOG_CORE: + return "CORE"; + case SQLCIPHER_LOG_MEMORY: + return "MEMORY"; + case SQLCIPHER_LOG_MUTEX: + return "MUTEX"; + case SQLCIPHER_LOG_PROVIDER: + return "PROVIDER"; + } + return "ANY"; +} + +static char *sqlcipher_get_log_sources_str(unsigned int source) { + if(source == SQLCIPHER_LOG_NONE) { + return sqlite3_mprintf("%s", "NONE"); + } else if (source == SQLCIPHER_LOG_ANY) { + return sqlite3_mprintf("%s", "ANY"); + } else { + char *sources = NULL; + unsigned int flag; + for(flag = SQLCIPHER_LOG_CORE; flag != 0; flag = flag << 1) { + if(SQLCIPHER_FLAG_GET(source, flag)) { + char *src = sqlcipher_get_log_source_str(flag); + if(sources) { + char *tmp = sqlite3_mprintf("%s %s", sources, src); + sqlite3_free(sources); + sources = tmp; + } else { + sources = sqlite3_mprintf("%s", src); + } + } + } + return sources; + } +} + +#ifndef SQLCIPHER_OMIT_LOG +/* constants from https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-crt/misc/gettimeofday.c */ +#define FILETIME_1970 116444736000000000ull /* seconds between 1/1/1601 and 1/1/1970 */ +#define HECTONANOSEC_PER_SEC 10000000ull +#define MAX_LOG_LEN 8192 +void sqlcipher_log(unsigned int level, unsigned int source, const char *message, ...) { + va_list params; + va_start(params, message); + char formatted[MAX_LOG_LEN]; + size_t len = 0; + +#ifdef CODEC_DEBUG +#if defined(SQLCIPHER_OMIT_LOG_DEVICE) || (!defined(__ANDROID__) && !defined(__APPLE__)) + vfprintf(stderr, message, params); + fprintf(stderr, "\n"); + goto end; +#else +#if defined(__ANDROID__) + __android_log_vprint(ANDROID_LOG_DEBUG, "sqlcipher", message, params); + goto end; +#elif defined(__APPLE__) + sqlite3_vsnprintf(MAX_LOG_LEN, formatted, message, params); + os_log(OS_LOG_DEFAULT, "%{public}s", formatted); + goto end; +#endif +#endif +#endif + if( + level > sqlcipher_log_level /* log level is higher, e.g. level filter is at ERROR but this message is DEBUG */ + || !SQLCIPHER_FLAG_GET(sqlcipher_log_source, source) /* source filter doesn't match this message source */ + || (sqlcipher_log_device == 0 && sqlcipher_log_file == NULL) /* no configured log target */ + ) { + /* skip logging this message */ + goto end; + } + + sqlite3_snprintf(MAX_LOG_LEN, formatted, "%s %s ", sqlcipher_get_log_level_str(level), sqlcipher_get_log_source_str(source)); + len = strlen(formatted); + sqlite3_vsnprintf(MAX_LOG_LEN - (int) len, formatted + (int) len, message, params); + +#if !defined(SQLCIPHER_OMIT_LOG_DEVICE) + if(sqlcipher_log_device) { +#if defined(__ANDROID__) + __android_log_write(ANDROID_LOG_DEBUG, "sqlcipher", formatted); + goto end; +#elif defined(__APPLE__) + os_log(OS_LOG_DEFAULT, "%{public}s", formatted); + goto end; +#endif + } +#endif + + if(sqlcipher_log_file != NULL){ + char buffer[24]; + struct tm tt; + int ms; + time_t sec; +#ifdef _WIN32 + SYSTEMTIME st; + FILETIME ft; + GetSystemTime(&st); + SystemTimeToFileTime(&st, &ft); + sec = (time_t) ((*((sqlite_int64*)&ft) - FILETIME_1970) / HECTONANOSEC_PER_SEC); + ms = st.wMilliseconds; + localtime_s(&tt, &sec); +#else + struct timeval tv; + gettimeofday(&tv, NULL); + sec = tv.tv_sec; + ms = tv.tv_usec/1000.0; + localtime_r(&sec, &tt); +#endif + if(strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tt)) { + fprintf((FILE*)sqlcipher_log_file, "%s.%03d: %s\n", buffer, ms, formatted); + goto end; + } + } + +end: + va_end(params); +} +#endif + +static int sqlcipher_set_log(const char *destination){ +#ifdef SQLCIPHER_OMIT_LOG + return SQLITE_ERROR; +#else + /* close open trace file if it is not stdout or stderr, then + reset trace settings */ + if(sqlcipher_log_file != NULL && sqlcipher_log_file != stdout && sqlcipher_log_file != stderr) { + fclose((FILE*)sqlcipher_log_file); + } + sqlcipher_log_file = NULL; + sqlcipher_log_device = 0; + + if(sqlite3_stricmp(destination, "logcat") == 0 || sqlite3_stricmp(destination, "device") == 0){ + /* use the appropriate device log. accept logcat for backwards compatibility */ + sqlcipher_log_device = 1; + } else if(sqlite3_stricmp(destination, "stdout") == 0){ + sqlcipher_log_file = stdout; + }else if(sqlite3_stricmp(destination, "stderr") == 0){ + sqlcipher_log_file = stderr; + }else if(sqlite3_stricmp(destination, "off") != 0){ +#if !defined(SQLCIPHER_PROFILE_USE_FOPEN) && (defined(_WIN32) && (__STDC_VERSION__ > 199901L) || defined(SQLITE_OS_WINRT)) + if(fopen_s(&sqlcipher_log_file, destination, "a") != 0) return SQLITE_ERROR; +#else + if((sqlcipher_log_file = fopen(destination, "a")) == 0) return SQLITE_ERROR; +#endif + } + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "sqlcipher_set_log: set log to %s", destination); + return SQLITE_OK; +#endif +} + +static void sqlcipher_vdbe_return_string(Parse *pParse, const char *zLabel, const char *value, int value_type){ + Vdbe *v = sqlite3GetVdbe(pParse); + sqlite3VdbeSetNumCols(v, 1); + sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC); + sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, value, value_type); + sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1); +} + +static int codec_set_btree_to_codec_pagesize(sqlite3 *db, Db *pDb, codec_ctx *ctx) { + int rc; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize() size=%d reserve=%d", ctx->page_sz, ctx->reserve_sz); + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "codec_set_btree_to_codec_pagesize: entering database mutex %p", db->mutex); + sqlite3_mutex_enter(db->mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "codec_set_btree_to_codec_pagesize: entered database mutex %p", db->mutex); + db->nextPagesize = ctx->page_sz; + + /* before forcing the page size we need to unset the BTS_PAGESIZE_FIXED flag, else + sqliteBtreeSetPageSize will block the change */ + pDb->pBt->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED; + rc = sqlite3BtreeSetPageSize(pDb->pBt, ctx->page_sz, ctx->reserve_sz, 0); + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize returned %d", rc); + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "codec_set_btree_to_codec_pagesize: leaving database mutex %p", db->mutex); + sqlite3_mutex_leave(db->mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "codec_set_btree_to_codec_pagesize: left database mutex %p", db->mutex); + + return rc; +} + +static int codec_set_pass_key(sqlite3* db, int nDb, const void *zKey, int nKey, int for_ctx) { + struct Db *pDb = &db->aDb[nDb]; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "codec_set_pass_key: db=%p nDb=%d for_ctx=%d", db, nDb, for_ctx); + if(pDb->pBt) { + codec_ctx *ctx = (codec_ctx*) sqlcipherPagerGetCodec(sqlite3BtreePager(pDb->pBt)); + + if(ctx) { + return sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, for_ctx); + } else { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "codec_set_pass_key: error ocurred fetching codec from pager on db %d", nDb); + return SQLITE_ERROR; + } + } + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "codec_set_pass_key: no btree present on db %d", nDb); + return SQLITE_ERROR; +} + +int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const char *zRight) { + struct Db *pDb = &db->aDb[iDb]; + codec_ctx *ctx = NULL; + int rc; + + if(pDb->pBt) { + ctx = (codec_ctx*) sqlcipherPagerGetCodec(sqlite3BtreePager(pDb->pBt)); + } + + if(sqlite3_stricmp(zLeft, "key") !=0 && sqlite3_stricmp(zLeft, "rekey") != 0) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_pragma: db=%p iDb=%d pParse=%p zLeft=%s zRight=%s ctx=%p", db, iDb, pParse, zLeft, zRight, ctx); + } + +#ifdef SQLCIPHER_TEST + if( sqlite3_stricmp(zLeft,"cipher_test_on")==0 ){ + if( zRight ) { + if(sqlite3_stricmp(zRight, "fail_encrypt")==0) { + SQLCIPHER_FLAG_SET(cipher_test_flags,TEST_FAIL_ENCRYPT); + } else + if(sqlite3_stricmp(zRight, "fail_decrypt")==0) { + SQLCIPHER_FLAG_SET(cipher_test_flags,TEST_FAIL_DECRYPT); + } else + if(sqlite3_stricmp(zRight, "fail_migrate")==0) { + SQLCIPHER_FLAG_SET(cipher_test_flags,TEST_FAIL_MIGRATE); + } + } + } else + if( sqlite3_stricmp(zLeft,"cipher_test_off")==0 ){ + if( zRight ) { + if(sqlite3_stricmp(zRight, "fail_encrypt")==0) { + SQLCIPHER_FLAG_UNSET(cipher_test_flags,TEST_FAIL_ENCRYPT); + } else + if(sqlite3_stricmp(zRight, "fail_decrypt")==0) { + SQLCIPHER_FLAG_UNSET(cipher_test_flags,TEST_FAIL_DECRYPT); + } else + if(sqlite3_stricmp(zRight, "fail_migrate")==0) { + SQLCIPHER_FLAG_UNSET(cipher_test_flags,TEST_FAIL_MIGRATE); + } + } + } else + if( sqlite3_stricmp(zLeft,"cipher_test")==0 ){ + char *flags = sqlite3_mprintf("%u", cipher_test_flags); + sqlcipher_vdbe_return_string(pParse, "cipher_test", flags, P4_DYNAMIC); + }else + if( sqlite3_stricmp(zLeft,"cipher_test_rand")==0 ){ + if( zRight ) { + int rand = atoi(zRight); + cipher_test_rand = rand; + } else { + char *rand = sqlite3_mprintf("%d", cipher_test_rand); + sqlcipher_vdbe_return_string(pParse, "cipher_test_rand", rand, P4_DYNAMIC); + } + } else +#endif + if( sqlite3_stricmp(zLeft, "cipher_fips_status")== 0 && !zRight ){ + if(ctx) { + char *fips_mode_status = sqlite3_mprintf("%d", ctx->provider->fips_status(ctx->provider_ctx)); + sqlcipher_vdbe_return_string(pParse, "cipher_fips_status", fips_mode_status, P4_DYNAMIC); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_store_pass")==0 && zRight ) { + if(ctx) { + char *deprecation = "PRAGMA cipher_store_pass is deprecated, please remove from use"; + ctx->store_pass = sqlite3GetBoolean(zRight, 1); + sqlcipher_vdbe_return_string(pParse, "cipher_store_pass", deprecation, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, deprecation); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_store_pass")==0 && !zRight ) { + if(ctx){ + char *store_pass_value = sqlite3_mprintf("%d", ctx->store_pass); + sqlcipher_vdbe_return_string(pParse, "cipher_store_pass", store_pass_value, P4_DYNAMIC); + } + } + if( sqlite3_stricmp(zLeft, "cipher_profile")== 0 && zRight ){ + char *profile_status = sqlite3_mprintf("%d", sqlcipher_cipher_profile(db, zRight)); + sqlcipher_vdbe_return_string(pParse, "cipher_profile", profile_status, P4_DYNAMIC); + } else + if( sqlite3_stricmp(zLeft, "cipher_add_random")==0 && zRight ){ + if(ctx) { + char *add_random_status = sqlite3_mprintf("%d", sqlcipher_codec_add_random(ctx, zRight, sqlite3Strlen30(zRight))); + sqlcipher_vdbe_return_string(pParse, "cipher_add_random", add_random_status, P4_DYNAMIC); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_migrate")==0 && !zRight ){ + if(ctx){ + int status = sqlcipher_codec_ctx_migrate(ctx); + char *migrate_status = sqlite3_mprintf("%d", status); + sqlcipher_vdbe_return_string(pParse, "cipher_migrate", migrate_status, P4_DYNAMIC); + if(status != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_pragma: error occurred during cipher_migrate: %d", status); + } + } + } else + if( sqlite3_stricmp(zLeft, "cipher_provider")==0 && !zRight ){ + if(ctx) { + sqlcipher_vdbe_return_string(pParse, "cipher_provider", + ctx->provider->get_provider_name(ctx->provider_ctx), P4_TRANSIENT); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_provider_version")==0 && !zRight){ + if(ctx) { + sqlcipher_vdbe_return_string(pParse, "cipher_provider_version", + ctx->provider->get_provider_version(ctx->provider_ctx), P4_TRANSIENT); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_version")==0 && !zRight ){ + sqlcipher_vdbe_return_string(pParse, "cipher_version", sqlcipher_version(), P4_DYNAMIC); + }else + if( sqlite3_stricmp(zLeft, "cipher")==0 ){ + if(ctx) { + if( zRight ) { + const char* message = "PRAGMA cipher is no longer supported."; + sqlcipher_vdbe_return_string(pParse, "cipher", message, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, message); + }else { + sqlcipher_vdbe_return_string(pParse, "cipher", + ctx->provider->get_cipher(ctx->provider_ctx), P4_TRANSIENT); + } + } + }else + if( sqlite3_stricmp(zLeft, "rekey_cipher")==0 && zRight ){ + const char* message = "PRAGMA rekey_cipher is no longer supported."; + sqlcipher_vdbe_return_string(pParse, "rekey_cipher", message, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, message); + }else + if( sqlite3_stricmp(zLeft,"cipher_default_kdf_iter")==0 ){ + if( zRight ) { + default_kdf_iter = atoi(zRight); /* change default KDF iterations */ + } else { + char *kdf_iter = sqlite3_mprintf("%d", default_kdf_iter); + sqlcipher_vdbe_return_string(pParse, "cipher_default_kdf_iter", kdf_iter, P4_DYNAMIC); + } + }else + if( sqlite3_stricmp(zLeft, "kdf_iter")==0 ){ + if(ctx) { + if( zRight ) { + sqlcipher_codec_ctx_set_kdf_iter(ctx, atoi(zRight)); /* change of RW PBKDF2 iteration */ + } else { + char *kdf_iter = sqlite3_mprintf("%d", ctx->kdf_iter); + sqlcipher_vdbe_return_string(pParse, "kdf_iter", kdf_iter, P4_DYNAMIC); + } + } + }else + if( sqlite3_stricmp(zLeft, "fast_kdf_iter")==0){ + if(ctx) { + if( zRight ) { + char *deprecation = "PRAGMA fast_kdf_iter is deprecated, please remove from use"; + sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, atoi(zRight)); /* change of RW PBKDF2 iteration */ + sqlcipher_vdbe_return_string(pParse, "fast_kdf_iter", deprecation, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, deprecation); + } else { + char *fast_kdf_iter = sqlite3_mprintf("%d", ctx->fast_kdf_iter); + sqlcipher_vdbe_return_string(pParse, "fast_kdf_iter", fast_kdf_iter, P4_DYNAMIC); + } + } + }else + if( sqlite3_stricmp(zLeft, "rekey_kdf_iter")==0 && zRight ){ + const char* message = "PRAGMA rekey_kdf_iter is no longer supported."; + sqlcipher_vdbe_return_string(pParse, "rekey_kdf_iter", message, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, message); + }else + if( sqlite3_stricmp(zLeft,"page_size")==0 || sqlite3_stricmp(zLeft,"cipher_page_size")==0 ){ + /* PRAGMA cipher_page_size will alter the size of the database pages while ensuring that the + required reserve space is allocated at the end of each page. This will also override the + standard SQLite PRAGMA page_size behavior if a codec context is attached to the database handle. + If PRAGMA page_size is invoked but a codec context is not attached (i.e. dealing with a standard + unencrypted database) then return early and allow the standard PRAGMA page_size logic to apply. */ + if(ctx) { + if( zRight ) { + int size = atoi(zRight); + rc = sqlcipher_codec_ctx_set_pagesize(ctx, size); + if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); + rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx); + if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); + } else { + char * page_size = sqlite3_mprintf("%d", ctx->page_sz); + sqlcipher_vdbe_return_string(pParse, "cipher_page_size", page_size, P4_DYNAMIC); + } + } else { + return 0; /* return early so that the PragTyp_PAGE_SIZE case logic in pragma.c will take effect */ + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_page_size")==0 ){ + if( zRight ) { + default_page_size = atoi(zRight); + } else { + char *page_size = sqlite3_mprintf("%d", default_page_size); + sqlcipher_vdbe_return_string(pParse, "cipher_default_page_size", page_size, P4_DYNAMIC); + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_use_hmac")==0 ){ + if( zRight ) { + sqlcipher_set_default_use_hmac(sqlite3GetBoolean(zRight,1)); + } else { + char *default_use_hmac = sqlite3_mprintf("%d", SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC)); + sqlcipher_vdbe_return_string(pParse, "cipher_default_use_hmac", default_use_hmac, P4_DYNAMIC); + } + }else + if( sqlite3_stricmp(zLeft,"cipher_use_hmac")==0 ){ + if(ctx) { + if( zRight ) { + rc = sqlcipher_codec_ctx_set_use_hmac(ctx, sqlite3GetBoolean(zRight,1)); + if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); + /* since the use of hmac has changed, the page size may also change */ + rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx); + if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); + } else { + char *hmac_flag = sqlite3_mprintf("%d", SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC)); + sqlcipher_vdbe_return_string(pParse, "cipher_use_hmac", hmac_flag, P4_DYNAMIC); + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_hmac_pgno")==0 ){ + if(ctx) { + if(zRight) { + char *deprecation = "PRAGMA cipher_hmac_pgno is deprecated, please remove from use"; + /* clear both pgno endian flags */ + if(sqlite3_stricmp(zRight, "le") == 0) { + SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_BE_PGNO); + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_LE_PGNO); + } else if(sqlite3_stricmp(zRight, "be") == 0) { + SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_LE_PGNO); + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_BE_PGNO); + } else if(sqlite3_stricmp(zRight, "native") == 0) { + SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_LE_PGNO); + SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_BE_PGNO); + } + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", deprecation, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, deprecation); + + } else { + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_LE_PGNO)) { + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", "le", P4_TRANSIENT); + } else if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_BE_PGNO)) { + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", "be", P4_TRANSIENT); + } else { + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", "native", P4_TRANSIENT); + } + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_hmac_salt_mask")==0 ){ + if(ctx) { + if(zRight) { + char *deprecation = "PRAGMA cipher_hmac_salt_mask is deprecated, please remove from use"; + if (sqlite3StrNICmp(zRight ,"x'", 2) == 0 && sqlite3Strlen30(zRight) == 5) { + unsigned char mask = 0; + const unsigned char *hex = (const unsigned char *)zRight+2; + cipher_hex2bin(hex,2,&mask); + hmac_salt_mask = mask; + } + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_salt_mask", deprecation, P4_TRANSIENT); + sqlite3_log(SQLITE_WARNING, deprecation); + } else { + char *mask = sqlite3_mprintf("%02x", hmac_salt_mask); + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_salt_mask", mask, P4_DYNAMIC); + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_plaintext_header_size")==0 ){ + if(ctx) { + if( zRight ) { + int size = atoi(zRight); + /* deliberately ignore result code, if size is invalid it will be set to -1 + and trip the error later in the codec */ + sqlcipher_codec_ctx_set_plaintext_header_size(ctx, size); + } else { + char *size = sqlite3_mprintf("%d", ctx->plaintext_header_sz); + sqlcipher_vdbe_return_string(pParse, "cipher_plaintext_header_size", size, P4_DYNAMIC); + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_plaintext_header_size")==0 ){ + if( zRight ) { + default_plaintext_header_size = atoi(zRight); + } else { + char *size = sqlite3_mprintf("%d", default_plaintext_header_size); + sqlcipher_vdbe_return_string(pParse, "cipher_default_plaintext_header_size", size, P4_DYNAMIC); + } + }else + if( sqlite3_stricmp(zLeft,"cipher_salt")==0 ){ + if(ctx) { + if(zRight) { + if (sqlite3StrNICmp(zRight ,"x'", 2) == 0 && sqlite3Strlen30(zRight) == (FILE_HEADER_SZ*2)+3) { + unsigned char *salt = (unsigned char*) sqlite3_malloc(FILE_HEADER_SZ); + const unsigned char *hex = (const unsigned char *)zRight+2; + cipher_hex2bin(hex,FILE_HEADER_SZ*2,salt); + sqlcipher_codec_ctx_set_kdf_salt(ctx, salt, FILE_HEADER_SZ); + sqlite3_free(salt); + } + } else { + void *salt; + char *hexsalt = (char*) sqlite3_malloc((FILE_HEADER_SZ*2)+1); + if((rc = sqlcipher_codec_ctx_get_kdf_salt(ctx, &salt)) == SQLITE_OK) { + cipher_bin2hex(salt, FILE_HEADER_SZ, hexsalt); + sqlcipher_vdbe_return_string(pParse, "cipher_salt", hexsalt, P4_DYNAMIC); + } else { + sqlite3_free(hexsalt); + sqlcipher_codec_ctx_set_error(ctx, rc); + } + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_hmac_algorithm")==0 ){ + if(ctx) { + if(zRight) { + rc = SQLITE_ERROR; + if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA1_LABEL) == 0) { + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA1); + } else if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA256_LABEL) == 0) { + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA256); + } else if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA512_LABEL) == 0) { + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA512); + } + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + } else { + int algorithm = ctx->hmac_algorithm; + if(ctx->hmac_algorithm == SQLCIPHER_HMAC_SHA1) { + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA1_LABEL, P4_TRANSIENT); + } else if(algorithm == SQLCIPHER_HMAC_SHA256) { + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA256_LABEL, P4_TRANSIENT); + } else if(algorithm == SQLCIPHER_HMAC_SHA512) { + sqlcipher_vdbe_return_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA512_LABEL, P4_TRANSIENT); + } + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_hmac_algorithm")==0 ){ + if(zRight) { + rc = SQLITE_OK; + if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA1_LABEL) == 0) { + default_hmac_algorithm = SQLCIPHER_HMAC_SHA1; + } else if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA256_LABEL) == 0) { + default_hmac_algorithm = SQLCIPHER_HMAC_SHA256; + } else if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA512_LABEL) == 0) { + default_hmac_algorithm = SQLCIPHER_HMAC_SHA512; + } + } else { + if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA1) { + sqlcipher_vdbe_return_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA1_LABEL, P4_TRANSIENT); + } else if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA256) { + sqlcipher_vdbe_return_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA256_LABEL, P4_TRANSIENT); + } else if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA512) { + sqlcipher_vdbe_return_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA512_LABEL, P4_TRANSIENT); + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_kdf_algorithm")==0 ){ + if(ctx) { + if(zRight) { + rc = SQLITE_ERROR; + if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL) == 0) { + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA1); + } else if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL) == 0) { + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA256); + } else if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL) == 0) { + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA512); + } + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + } else { + if(ctx->kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) { + sqlcipher_vdbe_return_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL, P4_TRANSIENT); + } else if(ctx->kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) { + sqlcipher_vdbe_return_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL, P4_TRANSIENT); + } else if(ctx->kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) { + sqlcipher_vdbe_return_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL, P4_TRANSIENT); + } + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_kdf_algorithm")==0 ){ + if(zRight) { + rc = SQLITE_OK; + if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL) == 0) { + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA1; + } else if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL) == 0) { + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA256; + } else if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL) == 0) { + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA512; + } + } else { + if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) { + sqlcipher_vdbe_return_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL, P4_TRANSIENT); + } else if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) { + sqlcipher_vdbe_return_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL, P4_TRANSIENT); + } else if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) { + sqlcipher_vdbe_return_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL, P4_TRANSIENT); + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_compatibility")==0 ){ + if(ctx) { + if(zRight) { + int version = atoi(zRight); + + switch(version) { + case 1: + rc = sqlcipher_codec_ctx_set_pagesize(ctx, 1024); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, 4000); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_use_hmac(ctx, 0); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + break; + + case 2: + rc = sqlcipher_codec_ctx_set_pagesize(ctx, 1024); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, 4000); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_use_hmac(ctx, 1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + break; + + case 3: + rc = sqlcipher_codec_ctx_set_pagesize(ctx, 1024); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, 64000); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_use_hmac(ctx, 1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + break; + + default: + rc = sqlcipher_codec_ctx_set_pagesize(ctx, 4096); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA512); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA512); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, 256000); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + rc = sqlcipher_codec_ctx_set_use_hmac(ctx, 1); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + break; + } + + rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx); + if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_compatibility")==0 ){ + if(zRight) { + int version = atoi(zRight); + switch(version) { + case 1: + default_page_size = 1024; + default_hmac_algorithm = SQLCIPHER_HMAC_SHA1; + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA1; + default_kdf_iter = 4000; + sqlcipher_set_default_use_hmac(0); + break; + + case 2: + default_page_size = 1024; + default_hmac_algorithm = SQLCIPHER_HMAC_SHA1; + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA1; + default_kdf_iter = 4000; + sqlcipher_set_default_use_hmac(1); + break; + + case 3: + default_page_size = 1024; + default_hmac_algorithm = SQLCIPHER_HMAC_SHA1; + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA1; + default_kdf_iter = 64000; + sqlcipher_set_default_use_hmac(1); + break; + + default: + default_page_size = 4096; + default_hmac_algorithm = SQLCIPHER_HMAC_SHA512; + default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA512; + default_kdf_iter = 256000; + sqlcipher_set_default_use_hmac(1); + break; + } + } + }else + if( sqlite3_stricmp(zLeft,"cipher_memory_security")==0 ){ + if( zRight ) { + if(sqlite3GetBoolean(zRight,1)) { + /* memory security can only be enabled, not disabled */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_set_mem_security: on"); + sqlcipher_mem_security_on = 1; + } + } else { + /* only report that memory security is enabled if pragma cipher_memory_security is ON and + SQLCipher's allocator/deallocator was run at least one time */ + int state = sqlcipher_mem_security_on && sqlcipher_mem_executed; + char *on = sqlite3_mprintf("%d", state); + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, + "sqlcipher_get_mem_security: sqlcipher_mem_security_on = %d, sqlcipher_mem_executed = %d", + sqlcipher_mem_security_on, sqlcipher_mem_executed); + sqlcipher_vdbe_return_string(pParse, "cipher_memory_security", on, P4_DYNAMIC); + } + }else + if( sqlite3_stricmp(zLeft,"cipher_settings")==0 ){ + if(ctx) { + int algorithm; + char *pragma; + + pragma = sqlite3_mprintf("PRAGMA kdf_iter = %d;", ctx->kdf_iter); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_page_size = %d;", ctx->page_sz); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_use_hmac = %d;", SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC)); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_plaintext_header_size = %d;", ctx->plaintext_header_sz); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + algorithm = ctx->hmac_algorithm; + pragma = NULL; + if(algorithm == SQLCIPHER_HMAC_SHA1) { + pragma = sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA1_LABEL); + } else if(algorithm == SQLCIPHER_HMAC_SHA256) { + pragma = sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA256_LABEL); + } else if(algorithm == SQLCIPHER_HMAC_SHA512) { + pragma = sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA512_LABEL); + } + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + algorithm = ctx->kdf_algorithm; + pragma = NULL; + if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) { + pragma = sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL); + } else if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) { + pragma = sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL); + } else if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) { + pragma = sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL); + } + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + } + }else + if( sqlite3_stricmp(zLeft,"cipher_default_settings")==0 ){ + char *pragma; + + pragma = sqlite3_mprintf("PRAGMA cipher_default_kdf_iter = %d;", default_kdf_iter); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_default_page_size = %d;", default_page_size); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_default_use_hmac = %d;", SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC)); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = sqlite3_mprintf("PRAGMA cipher_default_plaintext_header_size = %d;", default_plaintext_header_size); + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = NULL; + if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA1) { + pragma = sqlite3_mprintf("PRAGMA cipher_default_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA1_LABEL); + } else if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA256) { + pragma = sqlite3_mprintf("PRAGMA cipher_default_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA256_LABEL); + } else if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA512) { + pragma = sqlite3_mprintf("PRAGMA cipher_default_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA512_LABEL); + } + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + + pragma = NULL; + if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) { + pragma = sqlite3_mprintf("PRAGMA cipher_default_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL); + } else if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) { + pragma = sqlite3_mprintf("PRAGMA cipher_default_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL); + } else if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) { + pragma = sqlite3_mprintf("PRAGMA cipher_default_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL); + } + sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC); + }else + if( sqlite3_stricmp(zLeft,"cipher_integrity_check")==0 ){ + if(ctx) { + sqlcipher_codec_ctx_integrity_check(ctx, pParse, "cipher_integrity_check"); + } + } else + if( sqlite3_stricmp(zLeft, "cipher_log_level")==0 ){ + if(zRight) { + sqlcipher_log_level = SQLCIPHER_LOG_NONE; + if(sqlite3_stricmp(zRight, "ERROR")==0) sqlcipher_log_level = SQLCIPHER_LOG_ERROR; + else if(sqlite3_stricmp(zRight, "WARN" )==0) sqlcipher_log_level = SQLCIPHER_LOG_WARN; + else if(sqlite3_stricmp(zRight, "INFO" )==0) sqlcipher_log_level = SQLCIPHER_LOG_INFO; + else if(sqlite3_stricmp(zRight, "DEBUG")==0) sqlcipher_log_level = SQLCIPHER_LOG_DEBUG; + else if(sqlite3_stricmp(zRight, "TRACE")==0) sqlcipher_log_level = SQLCIPHER_LOG_TRACE; + } + sqlcipher_vdbe_return_string(pParse, "cipher_log_level", sqlcipher_get_log_level_str(sqlcipher_log_level), P4_TRANSIENT); + } else + if( sqlite3_stricmp(zLeft, "cipher_log_source")==0 ){ + if(zRight) { + if(sqlite3_stricmp(zRight, "NONE" )==0) sqlcipher_log_source = SQLCIPHER_LOG_NONE; + else if(sqlite3_stricmp(zRight, "ANY" )==0) sqlcipher_log_source = SQLCIPHER_LOG_ANY; + else { + if(sqlite3_stricmp(zRight, "CORE" )==0) SQLCIPHER_FLAG_SET(sqlcipher_log_source, SQLCIPHER_LOG_CORE); + else if(sqlite3_stricmp(zRight, "MEMORY" )==0) SQLCIPHER_FLAG_SET(sqlcipher_log_source, SQLCIPHER_LOG_MEMORY); + else if(sqlite3_stricmp(zRight, "MUTEX" )==0) SQLCIPHER_FLAG_SET(sqlcipher_log_source, SQLCIPHER_LOG_MUTEX); + else if(sqlite3_stricmp(zRight, "PROVIDER")==0) SQLCIPHER_FLAG_SET(sqlcipher_log_source, SQLCIPHER_LOG_PROVIDER); + } + } + sqlcipher_vdbe_return_string(pParse, "cipher_log_source", sqlcipher_get_log_sources_str(sqlcipher_log_source), P4_DYNAMIC); + } else + if( sqlite3_stricmp(zLeft, "cipher_log")== 0 && zRight ){ + char *status = sqlite3_mprintf("%d", sqlcipher_set_log(zRight)); + sqlcipher_vdbe_return_string(pParse, "cipher_log", status, P4_DYNAMIC); + }else { + return 0; + } + return 1; +} + +/* these constants are used internally within SQLite's pager.c to differentiate between + operations on the main database or journal pages. This is important in the context + of a rekey operations, where the journal must be written using the original key + material (to allow a transactional rollback), while the new database pages are being + written with the new key material*/ +#define CODEC_READ_OP 3 +#define CODEC_WRITE_OP 6 +#define CODEC_JOURNAL_OP 7 + +/* + * sqlite3Codec can be called in multiple modes. + * encrypt mode - expected to return a pointer to the + * encrypted data without altering pData. + * decrypt mode - expected to return a pointer to pData, with + * the data decrypted in the input buffer + */ +static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) { + codec_ctx *ctx = (codec_ctx *) iCtx; + int offset = 0, rc = 0; + unsigned char *pData = (unsigned char *) data; + int cctx = CIPHER_READ_CTX; + void *out = NULL; + sqlite3_mutex *mutex = ctx->pBt->sharable ? sqlcipher_mutex(SQLCIPHER_MUTEX_SHAREDCACHE) : NULL; + + /* in shared cache mode, this needs to be mutexed to prevent a separate database handle from + * nuking the context on the shared Btree */ + if(mutex) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering mutex %p", __func__, mutex); + sqlite3_mutex_enter(mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered mutex %p", __func__, mutex); + } + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3Codec: pgno=%d, mode=%d, ctx->page_sz=%d", pgno, mode, ctx->page_sz); + + if(ctx->error != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: identified deferred error condition: %d", __func__, rc); + sqlcipher_codec_ctx_set_error(ctx, ctx->error); + goto cleanup; + } + + /* call to derive keys if not present yet */ + if((rc = sqlcipher_codec_key_derive(ctx)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error occurred during key derivation: %d", rc); + sqlcipher_codec_ctx_set_error(ctx, rc); + goto cleanup; + } + + /* if the plaintext_header_size is negative that means an invalid size was set via + PRAGMA. We can't set the error state on the pager at that point because the pager + may not be open yet. However, this is a fatal error state, so abort the codec */ + if(ctx->plaintext_header_sz < 0) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error invalid ctx->plaintext_header_sz: %d", ctx->plaintext_header_sz); + sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); + goto cleanup; + } + + if(pgno == 1) /* adjust starting pointers in data page for header offset on first page*/ + offset = ctx->plaintext_header_sz ? ctx->plaintext_header_sz : FILE_HEADER_SZ; + + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3Codec: switch mode=%d offset=%d", mode, offset); + switch(mode) { + case CODEC_READ_OP: /* decrypt */ + if(pgno == 1) /* copy initial part of file header or SQLite magic to buffer */ + memcpy(ctx->buffer, ctx->plaintext_header_sz ? pData : (void *) SQLITE_FILE_HEADER, offset); + + rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_DECRYPT, ctx->page_sz - offset, pData + offset, (unsigned char*)ctx->buffer + offset); +#ifdef SQLCIPHER_TEST + if((cipher_test_flags & TEST_FAIL_DECRYPT) > 0 && sqlcipher_get_test_fail()) { + rc = SQLITE_ERROR; + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlite3Codec: simulating decryption failure for pgno=%d, mode=%d, ctx->page_sz=%d", pgno, mode, ctx->page_sz); + } +#endif + if(rc != SQLITE_OK) { + /* failure to decrypt a page is considered a permanent error and will render the pager unusable + * in order to prevent inconsistent data being loaded into page cache. The only exception here is when a database is being "recovered", + * which we consider to be the case if the plaintext header size is set to the full non-reserved size of a page. If that is the case we consider + * this to be operating in recovery mode, and will log the error but not permanently put the codec into an error state */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error decrypting page %d data: %d", pgno, rc); + sqlcipher_memset((unsigned char*) ctx->buffer+offset, 0, ctx->page_sz-offset); + if(ctx->plaintext_header_sz == ctx->page_sz - ctx->reserve_sz) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: plaintext header size of %d indicates recovery mode, suppressing permanent error", __func__, ctx->plaintext_header_sz); + } else { + sqlcipher_codec_ctx_set_error(ctx, rc); + } + } else { + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_KEY_USED); + } + memcpy(pData, ctx->buffer, ctx->page_sz); /* copy buffer data back to pData and return */ + out = pData; + goto cleanup; + break; + + case CODEC_WRITE_OP: /* encrypt database page, operate on write context and fall through to case 7, so the write context is used*/ + cctx = CIPHER_WRITE_CTX; + + case CODEC_JOURNAL_OP: /* encrypt journal page, operate on read context use to get the original page data from the database */ + if(pgno == 1) { /* copy initial part of file header or salt to buffer */ + void *kdf_salt = NULL; + /* retrieve the kdf salt */ + if((rc = sqlcipher_codec_ctx_get_kdf_salt(ctx, &kdf_salt)) != SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error retrieving salt: %d", rc); + sqlcipher_codec_ctx_set_error(ctx, rc); + goto cleanup; + } + memcpy(ctx->buffer, ctx->plaintext_header_sz ? pData : kdf_salt, offset); + } + rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_ENCRYPT, ctx->page_sz - offset, pData + offset, (unsigned char*)ctx->buffer + offset); +#ifdef SQLCIPHER_TEST + if((cipher_test_flags & TEST_FAIL_ENCRYPT) > 0 && sqlcipher_get_test_fail()) { + rc = SQLITE_ERROR; + sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlite3Codec: simulating encryption failure for pgno=%d, mode=%d, ctx->page_sz=%d", pgno, mode, ctx->page_sz); + } +#endif + if(rc != SQLITE_OK) { + /* failure to encrypt a page is considered a permanent error and will render the pager unusable + in order to prevent corrupted pages from being written to the main databased when using WAL */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error encrypting page %d data: %d", pgno, rc); + sqlcipher_memset((unsigned char*)ctx->buffer+offset, 0, ctx->page_sz-offset); + sqlcipher_codec_ctx_set_error(ctx, rc); + goto cleanup; + } + SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_KEY_USED); + out = ctx->buffer; /* return persistent buffer data, pData remains intact */ + goto cleanup; + break; + + default: + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error unsupported codec mode %d", mode); + sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); /* unsupported mode, set error */ + out = pData; + goto cleanup; + break; + } + +cleanup: + if(mutex) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving mutex %p", __func__, mutex); + sqlite3_mutex_leave(mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left mutex %p", __func__, mutex); + } + return out; +} + +/* This callback will be invoked when a database connection is closed. It is basically a light wrapper + * ariund sqlciher_codec_ctx_free that locks the shared cache mutex if necessary */ +static void sqlite3FreeCodecArg(void *pCodecArg) { + codec_ctx *ctx = (codec_ctx *) pCodecArg; + sqlite3_mutex *mutex = ctx->pBt->sharable ? sqlcipher_mutex(SQLCIPHER_MUTEX_SHAREDCACHE) : NULL; + + if(pCodecArg == NULL) return; + + /* in shared cache mode, this needs to be mutexed to prevent a codec context from being deallocated when + * it is in use by the codec due to cross-database handle access to the shared Btree */ + if(mutex) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering mutex %p", __func__, mutex); + sqlite3_mutex_enter(mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered mutex %p", __func__, mutex); + } + + sqlcipher_codec_ctx_free(&ctx); /* wipe and free allocated memory for the context */ + + if(mutex) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving mutex %p", __func__, mutex); + sqlite3_mutex_leave(mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left mutex %p", __func__, mutex); + } +} + +int sqlcipherCodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) { + struct Db *pDb = NULL; + sqlite3_file *fd = NULL; + codec_ctx *ctx = NULL; + Pager *pPager = NULL; + int rc = SQLITE_OK; + sqlite3_mutex *extra_mutex = NULL; + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: db=%p, nDb=%d", __func__, db, nDb); + + if(!sqlcipher_init) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: sqlcipher not initialized %d", __func__, sqlcipher_init_error); + return sqlcipher_init_error; + } + + /* error pKey is not null and nKey is > 0 */ + if(!(nKey && zKey)) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: no key", __func__); + return SQLITE_MISUSE; + } + + if(!(db && (pDb = &db->aDb[nDb]))) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: invalid database", __func__); + return SQLITE_MISUSE; + } + + /* After this point, early returns for API misuse are complete, lock on a mutex and ensure it is cleaned + * up later. If shared cache is enabled then enter a specially defined "global" recursive mutex specifically + * for isolating shared cache connections, otherwise use the built-in databse mutex */ + extra_mutex = pDb->pBt->sharable ? sqlcipher_mutex(SQLCIPHER_MUTEX_SHAREDCACHE) : NULL; + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering database mutex %p", __func__, db->mutex); + sqlite3_mutex_enter(db->mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered database mutex %p", __func__, db->mutex); + + if(extra_mutex) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entering mutex %p", __func__, extra_mutex); + sqlite3_mutex_enter(extra_mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: entered mutex %p", __func__, extra_mutex); + } + + pPager = sqlite3BtreePager(pDb->pBt); + ctx = (codec_ctx*) sqlcipherPagerGetCodec(pPager); + + if(ctx != NULL) { + /* There is already a codec attached to this database */ + if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) { + /* The key was derived and used successfully, so return early */ + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "%s: disregarding attempt to set key on an previously keyed database connection handle", __func__); + goto cleanup; +#ifndef SQLITE_DEBUG + } else if (pDb->pBt->sharable) { + /* This Btree is participating in shared cache. It would be usafe to reset and reattach a new codec, so return early. + * + * When compiled with SQLITE_DEBUG, all database connections have shared cached enabled. This behavior of disallowing reset + * of the codec on a shared cache connection will break several tests that depend on the the ability to reset the codec, + * like migration tests, repeat-keying tests, etc. Asa result we will disable shared cache handling when compiled with + * SQLIE_DEBUG enabled.*/ + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "%s: disregarding attempt to set key on an shared cache handle", __func__); + goto cleanup; +#endif + } else { + /* To preseve legacy functionality where an incorrect key could be replaced by a correct key without closing the database, + * if the key has not been used, and shared cache is not enabled, reset the codec on this pager entirely. + * This will call sqlcipher_codec_ctx_free directly instead of through sqlite3FreeCodecArg because this function already + * holds the shared cache mutex if it is necessary, and that avoids requiring a more expensive recursive mutex */ + sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "%s: resetting existing codec on pager", __func__); + sqlcipher_codec_ctx_free(&ctx); + sqlcipherPagerSetCodec(pPager, NULL, NULL, NULL, NULL); + ctx = NULL; + } + } + + /* check if the sqlite3_file is open, and if not force handle to NULL */ + if((fd = sqlite3PagerFile(pPager))->pMethods == 0) fd = NULL; + + /* point the internal codec argument against the contet to be prepared */ + rc = sqlcipher_codec_ctx_init(&ctx, pDb, pPager, zKey, nKey); + + if(rc != SQLITE_OK) { + /* initialization failed, do not attach potentially corrupted context */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: context initialization failed, forcing error state with rc=%d", __func__, rc); + /* force an error at the pager level, such that even the upstream caller ignores the return code + the pager will be in an error state and will process no further operations */ + sqlite3pager_error(pPager, rc); + pDb->pBt->pBt->db->errCode = rc; + goto cleanup; + } + + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: calling sqlcipherPagerSetCodec()", __func__); + sqlcipherPagerSetCodec(pPager, sqlite3Codec, NULL, sqlite3FreeCodecArg, (void *) ctx); + + codec_set_btree_to_codec_pagesize(db, pDb, ctx); + + /* force secure delete. This has the benefit of wiping internal data when deleted + and also ensures that all pages are written to disk (i.e. not skipped by + sqlite3PagerDontWrite optimizations) */ + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: calling sqlite3BtreeSecureDelete()", __func__); + sqlite3BtreeSecureDelete(pDb->pBt, 1); + + /* if fd is null, then this is an in-memory database and + we dont' want to overwrite the AutoVacuum settings + if not null, then set to the default */ + if(fd != NULL) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: calling sqlite3BtreeSetAutoVacuum()", __func__); + sqlite3BtreeSetAutoVacuum(pDb->pBt, SQLITE_DEFAULT_AUTOVACUUM); + } + +cleanup: + + if(extra_mutex) { + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving mutex %p", __func__, extra_mutex); + sqlite3_mutex_leave(extra_mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left mutex %p", __func__, extra_mutex); + } + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: leaving database mutex %p", __func__, db->mutex); + sqlite3_mutex_leave(db->mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "%s: left database mutex %p", __func__, db->mutex); + + return rc; +} + +int sqlcipher_find_db_index(sqlite3 *db, const char *zDb) { + int db_index; + if(zDb == NULL){ + return 0; + } + for(db_index = 0; db_index < db->nDb; db_index++) { + struct Db *pDb = &db->aDb[db_index]; + if(strcmp(pDb->zDbSName, zDb) == 0) { + return db_index; + } + } + return 0; +} + +void sqlite3_activate_see(const char* in) { + /* do nothing, security enhancements are always active */ +} + +int sqlite3_key(sqlite3 *db, const void *pKey, int nKey) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: db=%p", __func__, db); + return sqlite3_key_v2(db, "main", pKey, nKey); +} + +int sqlite3_key_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) { + int db_index = sqlcipher_find_db_index(db, zDb); + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "%s: db=%p zDb=%s db_index=%d", __func__, db, zDb, db_index); + return sqlcipherCodecAttach(db, db_index, pKey, nKey); +} + +int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey: db=%p", db); + return sqlite3_rekey_v2(db, "main", pKey, nKey); +} + +/* sqlite3_rekey_v2 +** Given a database, this will reencrypt the database using a new key. +** There is only one possible modes of operation - to encrypt a database +** that is already encrpyted. If the database is not already encrypted +** this should do nothing +** The proposed logic for this function follows: +** 1. Determine if the database is already encryptped +** 2. If there is NOT already a key present do nothing +** 3. If there is a key present, re-encrypt the database with the new key +*/ +int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: db=%p zDb=%s", db, zDb); + + if(!sqlcipher_init) { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: sqlcipher not initialized %d",__func__, sqlcipher_init_error); + return sqlcipher_init_error; + } + + if(db && pKey && nKey) { + int db_index = sqlcipher_find_db_index(db, zDb); + struct Db *pDb = &db->aDb[db_index]; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: database zDb=%p db_index:%d", zDb, db_index); + if(pDb->pBt) { + codec_ctx *ctx; + int rc, page_count; + Pgno pgno; + PgHdr *page; + Pager *pPager = sqlite3BtreePager(pDb->pBt); + + ctx = (codec_ctx*) sqlcipherPagerGetCodec(pPager); + + if(ctx == NULL) { + /* there was no codec attached to this database, so this should do nothing! */ + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: no codec attached to db %s: rekey can't be used on an unencrypted database", zDb); + return SQLITE_MISUSE; + } + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlite3_rekey_v2: entering database mutex %p", db->mutex); + sqlite3_mutex_enter(db->mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlite3_rekey_v2: entered database mutex %p", db->mutex); + + codec_set_pass_key(db, db_index, pKey, nKey, CIPHER_WRITE_CTX); + + /* do stuff here to rewrite the database + ** 1. Create a transaction on the database + ** 2. Iterate through each page, reading it and then writing it. + ** 3. If that goes ok then commit and put ctx->rekey into ctx->key + ** note: don't deallocate rekey since it may be used in a subsequent iteration + */ + rc = sqlite3BtreeBeginTrans(pDb->pBt, 1, 0); /* begin write transaction */ + sqlite3PagerPagecount(pPager, &page_count); + for(pgno = 1; rc == SQLITE_OK && pgno <= (unsigned int)page_count; pgno++) { /* pgno's start at 1 see pager.c:pagerAcquire */ + if(!sqlite3pager_is_sj_pgno(pPager, pgno)) { /* skip this page (see pager.c:pagerAcquire for reasoning) */ + rc = sqlite3PagerGet(pPager, pgno, &page, 0); + if(rc == SQLITE_OK) { /* write page see pager_incr_changecounter for example */ + rc = sqlite3PagerWrite(page); + if(rc == SQLITE_OK) { + sqlite3PagerUnref(page); + } else { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: error %d occurred writing page %d", rc, pgno); + } + } else { + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: error %d occurred reading page %d", rc, pgno); + } + } + } + + /* if commit was successful commit and copy the rekey data to current key, else rollback to release locks */ + if(rc == SQLITE_OK) { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: committing"); + rc = sqlite3BtreeCommit(pDb->pBt); + sqlcipher_codec_key_copy(ctx, CIPHER_WRITE_CTX); + } else { + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: rollback"); + sqlite3BtreeRollback(pDb->pBt, SQLITE_ABORT_ROLLBACK, 0); + } + + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlite3_rekey_v2: leaving database mutex %p", db->mutex); + sqlite3_mutex_leave(db->mutex); + sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlite3_rekey_v2: left database mutex %p", db->mutex); + } + return SQLITE_OK; + } + sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: no key provided for db %s: rekey can't be used to decrypt an encrypted database", zDb); + return SQLITE_ERROR; +} + +/* + * Retrieves the current key attached to the database if there is a codec attached to it. + * The key will be passed back using internally allocated memory and must be freed using + * sqlcipher_free to avoid memory leaks. If no key is present, zKey will be set to NULL + * and nKey to 0. + * + * If the encryption key has not yet been derived or the key material is stored, it will + * be passed back directly. Otherwise, a "keyspec" consisting of the raw key and salt + * will be used instead. */ +void sqlcipherCodecGetKey(sqlite3* db, int nDb, void **zKey, int *nKey) { + struct Db *pDb = &db->aDb[nDb]; + sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipherCodecGetKey:db=%p, nDb=%d", db, nDb); + if( pDb->pBt ) { + codec_ctx *ctx = (codec_ctx*) sqlcipherPagerGetCodec(sqlite3BtreePager(pDb->pBt)); + + if(ctx) { + /* if the key has not been derived yet, or the key is stored (vi PRAGMA cipher_store_pass) + * then return the key material. Other wise pass back the keyspec */ + if(ctx->read_ctx->derive_key || ctx->store_pass == 1) { + *zKey = sqlcipher_malloc(ctx->read_ctx->pass_sz); + *nKey = ctx->read_ctx->pass_sz; + memcpy(*zKey, ctx->read_ctx->pass, ctx->read_ctx->pass_sz); + } else { + sqlcipher_cipher_ctx_get_keyspec(ctx, ctx->read_ctx, (char**) zKey, nKey); + } + } else { + *zKey = NULL; + *nKey = 0; + } + } +} + +/* + * Implementation of an "export" function that allows a caller + * to duplicate the main database to an attached database. This is intended + * as a conveneince for users who need to: + * + * 1. migrate from an non-encrypted database to an encrypted database + * 2. move from an encrypted database to a non-encrypted database + * 3. convert beween the various flavors of encrypted databases. + * + * This implementation is based heavily on the procedure and code used + * in vacuum.c, but is exposed as a function that allows export to any + * named attached database. + */ + +/* +** Finalize a prepared statement. If there was an error, store the +** text of the error message in *pzErrMsg. Return the result code. +** +** Based on vacuumFinalize from vacuum.c +*/ +static int sqlcipher_finalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){ + int rc; + rc = sqlite3VdbeFinalize((Vdbe*)pStmt); + if( rc ){ + sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); + } + return rc; +} + +/* +** Execute zSql on database db. Return an error code. +** +** Based on execSql from vacuum.c +*/ +static int sqlcipher_execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ + sqlite3_stmt *pStmt; + VVA_ONLY( int rc; ) + if( !zSql ){ + return SQLITE_NOMEM; + } + if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){ + sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); + return sqlite3_errcode(db); + } + VVA_ONLY( rc = ) sqlite3_step(pStmt); + assert( rc!=SQLITE_ROW ); + return sqlcipher_finalize(db, pStmt, pzErrMsg); +} + +/* +** Execute zSql on database db. The statement returns exactly +** one column. Execute this as SQL on the same database. +** +** Based on execExecSql from vacuum.c +*/ +static int sqlcipher_execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ + sqlite3_stmt *pStmt; + int rc; + + rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); + if( rc!=SQLITE_OK ) return rc; + + while( SQLITE_ROW==sqlite3_step(pStmt) ){ + rc = sqlcipher_execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0)); + if( rc!=SQLITE_OK ){ + sqlcipher_finalize(db, pStmt, pzErrMsg); + return rc; + } + } + + return sqlcipher_finalize(db, pStmt, pzErrMsg); +} + +/* + * copy database and schema from the main database to an attached database + * + * Based on sqlite3RunVacuum from vacuum.c +*/ +static void sqlcipher_exportFunc(sqlite3_context *context, int argc, sqlite3_value **argv) { + sqlite3 *db = sqlite3_context_db_handle(context); + const char* targetDb, *sourceDb; + int targetDb_idx = 0; + u64 saved_flags = db->flags; /* Saved value of the db->flags */ + u32 saved_mDbFlags = db->mDbFlags; /* Saved value of the db->mDbFlags */ + int saved_nChange = db->nChange; /* Saved value of db->nChange */ + int saved_nTotalChange = db->nTotalChange; /* Saved value of db->nTotalChange */ + u8 saved_mTrace = db->mTrace; /* Saved value of db->mTrace */ + int rc = SQLITE_OK; /* Return code from service routines */ + char *zSql = NULL; /* SQL statements */ + char *pzErrMsg = NULL; + + if(argc != 1 && argc != 2) { + rc = SQLITE_ERROR; + pzErrMsg = sqlite3_mprintf("invalid number of arguments (%d) passed to sqlcipher_export", argc); + goto end_of_export; + } + + if(sqlite3_value_type(argv[0]) == SQLITE_NULL) { + rc = SQLITE_ERROR; + pzErrMsg = sqlite3_mprintf("target database can't be NULL"); + goto end_of_export; + } + + targetDb = (const char*) sqlite3_value_text(argv[0]); + sourceDb = "main"; + + if(argc == 2) { + if(sqlite3_value_type(argv[1]) == SQLITE_NULL) { + rc = SQLITE_ERROR; + pzErrMsg = sqlite3_mprintf("target database can't be NULL"); + goto end_of_export; + } + sourceDb = (char *) sqlite3_value_text(argv[1]); + } + + + /* if the name of the target is not main, but the index returned is zero + there is a mismatch and we should not proceed */ + targetDb_idx = sqlcipher_find_db_index(db, targetDb); + if(targetDb_idx == 0 && targetDb != NULL && sqlite3_stricmp("main", targetDb) != 0) { + rc = SQLITE_ERROR; + pzErrMsg = sqlite3_mprintf("unknown database %s", targetDb); + goto end_of_export; + } + db->init.iDb = targetDb_idx; + + db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks; + db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum; + db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_Defensive | SQLITE_CountRows); + db->mTrace = 0; + + /* Query the schema of the main database. Create a mirror schema + ** in the temporary database. + */ + zSql = sqlite3_mprintf( + "SELECT sql " + " FROM %s.sqlite_schema WHERE type='table' AND name!='sqlite_sequence'" + " AND rootpage>0" + , sourceDb); + rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); + if( rc!=SQLITE_OK ) goto end_of_export; + sqlite3_free(zSql); + + zSql = sqlite3_mprintf( + "SELECT sql " + " FROM %s.sqlite_schema WHERE sql LIKE 'CREATE INDEX %%' " + , sourceDb); + rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); + if( rc!=SQLITE_OK ) goto end_of_export; + sqlite3_free(zSql); + + zSql = sqlite3_mprintf( + "SELECT sql " + " FROM %s.sqlite_schema WHERE sql LIKE 'CREATE UNIQUE INDEX %%'" + , sourceDb); + rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); + if( rc!=SQLITE_OK ) goto end_of_export; + sqlite3_free(zSql); + + /* Loop through the tables in the main database. For each, do + ** an "INSERT INTO rekey_db.xxx SELECT * FROM main.xxx;" to copy + ** the contents to the temporary database. + */ + zSql = sqlite3_mprintf( + "SELECT 'INSERT INTO %s.' || quote(name) " + "|| ' SELECT * FROM %s.' || quote(name) || ';'" + "FROM %s.sqlite_schema " + "WHERE type = 'table' AND name!='sqlite_sequence' " + " AND rootpage>0" + , targetDb, sourceDb, sourceDb); + rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); + if( rc!=SQLITE_OK ) goto end_of_export; + sqlite3_free(zSql); + + /* Copy over the contents of the sequence table + */ + zSql = sqlite3_mprintf( + "SELECT 'INSERT INTO %s.' || quote(name) " + "|| ' SELECT * FROM %s.' || quote(name) || ';' " + "FROM %s.sqlite_schema WHERE name=='sqlite_sequence';" + , targetDb, sourceDb, targetDb); + rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql); + if( rc!=SQLITE_OK ) goto end_of_export; + sqlite3_free(zSql); + + /* Copy the triggers, views, and virtual tables from the main database + ** over to the temporary database. None of these objects has any + ** associated storage, so all we have to do is copy their entries + ** from the SQLITE_MASTER table. + */ + zSql = sqlite3_mprintf( + "INSERT INTO %s.sqlite_schema " + " SELECT type, name, tbl_name, rootpage, sql" + " FROM %s.sqlite_schema" + " WHERE type='view' OR type='trigger'" + " OR (type='table' AND rootpage=0)" + , targetDb, sourceDb); + rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execSql(db, &pzErrMsg, zSql); + if( rc!=SQLITE_OK ) goto end_of_export; + sqlite3_free(zSql); + + zSql = NULL; +end_of_export: + db->init.iDb = 0; + db->flags = saved_flags; + db->mDbFlags = saved_mDbFlags; + db->nChange = saved_nChange; + db->nTotalChange = saved_nTotalChange; + db->mTrace = saved_mTrace; + + if(zSql) sqlite3_free(zSql); + + if(rc) { + if(pzErrMsg != NULL) { + sqlite3_result_error(context, pzErrMsg, -1); + sqlite3DbFree(db, pzErrMsg); + } else { + sqlite3_result_error(context, sqlite3ErrStr(rc), -1); + } + } +} +#endif +/* END SQLCIPHER */ diff --git a/src/sqlcipher.h b/src/sqlcipher.h new file mode 100644 index 0000000000..40416cb531 --- /dev/null +++ b/src/sqlcipher.h @@ -0,0 +1,161 @@ +/* +** SQLCipher +** sqlcipher.h developed by Stephen Lombardo (Zetetic LLC) +** sjlombardo at zetetic dot net +** http://zetetic.net +** +** Copyright (c) 2008, ZETETIC LLC +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** * Neither the name of the ZETETIC LLC nor the +** names of its contributors may be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +** +*/ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC +#ifndef SQLCIPHER_H +#define SQLCIPHER_H + +#include "sqlite3.h" + +#define SQLCIPHER_HMAC_SHA1 0 +#define SQLCIPHER_HMAC_SHA1_LABEL "HMAC_SHA1" +#define SQLCIPHER_HMAC_SHA256 1 +#define SQLCIPHER_HMAC_SHA256_LABEL "HMAC_SHA256" +#define SQLCIPHER_HMAC_SHA512 2 +#define SQLCIPHER_HMAC_SHA512_LABEL "HMAC_SHA512" + + +#define SQLCIPHER_PBKDF2_HMAC_SHA1 0 +#define SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL "PBKDF2_HMAC_SHA1" +#define SQLCIPHER_PBKDF2_HMAC_SHA256 1 +#define SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL "PBKDF2_HMAC_SHA256" +#define SQLCIPHER_PBKDF2_HMAC_SHA512 2 +#define SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL "PBKDF2_HMAC_SHA512" + +typedef struct sqlcipher_provider sqlcipher_provider; +struct sqlcipher_provider { + int (*init)(void); + void (*shutdown)(void); + const char* (*get_provider_name)(void *ctx); + int (*add_random)(void *ctx, const void *buffer, int length); + int (*random)(void *ctx, void *buffer, int length); + int (*hmac)(void *ctx, int algorithm, + const unsigned char *hmac_key, int key_sz, + const unsigned char *in, int in_sz, + const unsigned char *in2, int in2_sz, + unsigned char *out); + int (*kdf)(void *ctx, int algorithm, + const unsigned char *pass, int pass_sz, + const unsigned char* salt, int salt_sz, + int workfactor, + int key_sz, unsigned char *key); + int (*cipher)(void *ctx, int mode, + const unsigned char *key, int key_sz, + const unsigned char *iv, + const unsigned char *in, int in_sz, + unsigned char *out); + const char* (*get_cipher)(void *ctx); + int (*get_key_sz)(void *ctx); + int (*get_iv_sz)(void *ctx); + int (*get_block_sz)(void *ctx); + int (*get_hmac_sz)(void *ctx, int algorithm); + int (*ctx_init)(void **ctx); + int (*ctx_free)(void **ctx); + int (*fips_status)(void *ctx); + const char* (*get_provider_version)(void *ctx); + sqlcipher_provider *next; +}; + +/* public interfaces called externally */ +int sqlcipher_extra_init(const char*); +void sqlcipher_extra_shutdown(void); +void sqlcipher_init_memmethods(void); +int sqlcipher_codec_pragma(sqlite3*, int, Parse*, const char *, const char*); +int sqlcipherCodecAttach(sqlite3*, int, const void *, int); +void sqlcipherCodecGetKey(sqlite3*, int, void**, int*); +int sqlcipher_find_db_index(sqlite3 *, const char *); + +/* utility functions */ +void* sqlcipher_memset(void *, unsigned char, sqlite_uint64); +int sqlcipher_ismemset(const void *, unsigned char, sqlite_uint64); +int sqlcipher_memcmp(const void *, const void *, int); +void* sqlcipher_malloc(sqlite_uint64); +void sqlcipher_free(void *, sqlite_uint64); +char* sqlcipher_version(void); + +/* provider interfaces */ +int sqlcipher_register_provider(sqlcipher_provider *); +sqlcipher_provider* sqlcipher_get_provider(void); + +#define SQLCIPHER_MUTEX_PROVIDER 0 +#define SQLCIPHER_MUTEX_PROVIDER_ACTIVATE 1 +#define SQLCIPHER_MUTEX_PROVIDER_RAND 2 +#define SQLCIPHER_MUTEX_RESERVED1 3 +#define SQLCIPHER_MUTEX_RESERVED2 4 +#define SQLCIPHER_MUTEX_RESERVED3 5 +#define SQLCIPHER_MUTEX_MEM 6 +#define SQLCIPHER_MUTEX_SHAREDCACHE 7 +#define SQLCIPHER_MUTEX_COUNT 8 + +sqlite3_mutex* sqlcipher_mutex(int); + +#define SQLCIPHER_LOG_NONE 0 +#define SQLCIPHER_LOG_ANY 0xffffffff + +#define SQLCIPHER_LOG_ERROR (1<<0) +#define SQLCIPHER_LOG_WARN (1<<1) +#define SQLCIPHER_LOG_INFO (1<<2) +#define SQLCIPHER_LOG_DEBUG (1<<3) +#define SQLCIPHER_LOG_TRACE (1<<4) + +#define SQLCIPHER_LOG_CORE (1<<0) +#define SQLCIPHER_LOG_MEMORY (1<<1) +#define SQLCIPHER_LOG_MUTEX (1<<2) +#define SQLCIPHER_LOG_PROVIDER (1<<3) + +#ifdef SQLCIPHER_OMIT_LOG +#define sqlcipher_log(level, source, message, ...) +#else +void sqlcipher_log(unsigned int level, unsigned int source, const char *message, ...); +#endif + +#ifdef CODEC_DEBUG_PAGEDATA +#define CODEC_HEXDUMP(DESC,BUFFER,LEN) \ + { \ + int __pctr; \ + printf(DESC); \ + for(__pctr=0; __pctr < LEN; __pctr++) { \ + if(__pctr % 16 == 0) printf("\n%05x: ",__pctr); \ + printf("%02x ",((unsigned char*) BUFFER)[__pctr]); \ + } \ + printf("\n"); \ + fflush(stdout); \ + } +#else +#define CODEC_HEXDUMP(DESC,BUFFER,LEN) +#endif + +#endif +#endif +/* END SQLCIPHER */ + diff --git a/src/sqlite.h.in b/src/sqlite.h.in index a9eb5fed72..ba25b95b11 100644 --- a/src/sqlite.h.in +++ b/src/sqlite.h.in @@ -6541,6 +6541,66 @@ int sqlite3_collation_needed16( void(*)(void*,sqlite3*,int eTextRep,const void*) ); +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC +/* +** Specify the key for an encrypted database. This routine should be +** called right after sqlite3_open(). +** +** The code to implement this API is not available in the public release +** of SQLite. +*/ +int sqlite3_key( + sqlite3 *db, /* Database to be rekeyed */ + const void *pKey, int nKey /* The key */ +); +int sqlite3_key_v2( + sqlite3 *db, /* Database to be rekeyed */ + const char *zDbName, /* Name of the database */ + const void *pKey, int nKey /* The key */ +); + +/* +** Change the key on an open database. If the current database is not +** encrypted, this routine will encrypt it. If pNew==0 or nNew==0, the +** database is decrypted. +** +** The code to implement this API is not available in the public release +** of SQLite. +*/ +/* SQLCipher usage note: + + If the current database is plaintext SQLCipher will NOT encrypt it. + If the current database is encrypted and pNew==0 or nNew==0, SQLCipher + will NOT decrypt it. + + This routine will ONLY work on an already encrypted database in order + to change the key. + + Conversion from plaintext-to-encrypted or encrypted-to-plaintext should + use an ATTACHed database and the sqlcipher_export() convenience function + as per the SQLCipher Documentation. +*/ +int sqlite3_rekey( + sqlite3 *db, /* Database to be rekeyed */ + const void *pKey, int nKey /* The new key */ +); +int sqlite3_rekey_v2( + sqlite3 *db, /* Database to be rekeyed */ + const char *zDbName, /* Name of the database */ + const void *pKey, int nKey /* The new key */ +); + +/* +** Specify the activation key for a SEE database. Unless +** activated, none of the SEE routines will work. +*/ +void sqlite3_activate_see( + const char *zPassPhrase /* Activation phrase */ +); +#endif +/* END SQLCIPHER */ + #ifdef SQLITE_ENABLE_CEROD /* ** Specify the activation key for a CEROD database. Unless diff --git a/src/sqliteInt.h b/src/sqliteInt.h index fd4ed52988..35c7137ab8 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -4914,7 +4914,13 @@ void sqlite3EndTable(Parse*,Token*,Token*,u32,Select*); void sqlite3AddReturning(Parse*,ExprList*); int sqlite3ParseUri(const char*,const char*,unsigned int*, sqlite3_vfs**,char**,char **); -#define sqlite3CodecQueryParameters(A,B,C) 0 +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + int sqlite3CodecQueryParameters(sqlite3*,const char*,const char*); +#else +# define sqlite3CodecQueryParameters(A,B,C) 0 +#endif +/* END SQLCIPHER */ Btree *sqlite3DbNameToBtree(sqlite3*,const char*); #ifdef SQLITE_UNTESTABLE diff --git a/src/tclsqlite.c b/src/tclsqlite.c index 824e8c4d3c..e95bf69b60 100644 --- a/src/tclsqlite.c +++ b/src/tclsqlite.c @@ -3199,10 +3199,26 @@ static int SQLITE_TCLAPI DbObjCmd( ** Change the encryption key on the currently open database. */ case DB_REKEY: { +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) + int nKey; + void *pKey; +#endif +/* END SQLCIPHER */ if( objc!=3 ){ Tcl_WrongNumArgs(interp, 2, objv, "KEY"); return TCL_ERROR; } +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) + pKey = Tcl_GetByteArrayFromObj(objv[2], &nKey); + rc = sqlite3_rekey(pDb->db, pKey, nKey); + if( rc ){ + Tcl_AppendResult(interp, sqlite3_errstr(rc), (char*)0); + rc = TCL_ERROR; + } +#endif +/* END SQLCIPHER */ break; } @@ -3772,6 +3788,11 @@ static int sqliteCmdUsage( "HANDLE ?FILENAME? ?-vfs VFSNAME? ?-readonly BOOLEAN? ?-create BOOLEAN?" " ?-nofollow BOOLEAN?" " ?-nomutex BOOLEAN? ?-fullmutex BOOLEAN? ?-uri BOOLEAN?" +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) + " ?-key CODECKEY?" +#endif +/* END SQLCIPHER */ ); return TCL_ERROR; } @@ -3807,6 +3828,12 @@ static int SQLITE_TCLAPI DbMain( int flags; int bTranslateFileName = 1; Tcl_DString translatedFilename; +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) + void *pKey = 0; + int nKey = 0; +#endif +/* END SQLCIPHER */ int rc; /* In normal use, each TCL interpreter runs in a single thread. So @@ -3833,7 +3860,13 @@ static int SQLITE_TCLAPI DbMain( return TCL_OK; } if( strcmp(zArg,"-has-codec")==0 ){ +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) + Tcl_AppendResult(interp,"1",(char*)0); +#else Tcl_AppendResult(interp,"0",(char*)0); +#endif +/* END SQLCIPHER */ return TCL_OK; } if( zArg[0]=='-' ) return sqliteCmdUsage(interp, objv); @@ -3848,7 +3881,11 @@ static int SQLITE_TCLAPI DbMain( if( i==objc-1 ) return sqliteCmdUsage(interp, objv); i++; if( strcmp(zArg,"-key")==0 ){ - /* no-op */ +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) + pKey = Tcl_GetByteArrayFromObj(objv[i], &nKey); +#endif +/* END SQLCIPHER */ }else if( strcmp(zArg, "-vfs")==0 ){ zVfs = Tcl_GetString(objv[i]); }else if( strcmp(zArg, "-readonly")==0 ){ @@ -3932,6 +3969,13 @@ static int SQLITE_TCLAPI DbMain( }else{ zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc)); } +/* BEGIN SQLCIPHER */ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) + if( p->db ){ + sqlite3_key(p->db, pKey, nKey); + } +#endif +/* END SQLCIPHER */ if( p->db==0 ){ Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE); Tcl_Free((char*)p); diff --git a/src/test1.c b/src/test1.c index c204335dcc..438f7ea835 100644 --- a/src/test1.c +++ b/src/test1.c @@ -659,6 +659,20 @@ static int SQLITE_TCLAPI test_key( int argc, /* Number of arguments */ char **argv /* Text of each argument */ ){ +#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_CODEC_FROM_TCL) + sqlite3 *db; + const char *zKey; + int nKey; + if( argc!=3 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], + " FILENAME\"", 0); + return TCL_ERROR; + } + if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; + zKey = argv[2]; + nKey = strlen(zKey); + sqlite3_key(db, zKey, nKey); +#endif return TCL_OK; } @@ -673,6 +687,20 @@ static int SQLITE_TCLAPI test_rekey( int argc, /* Number of arguments */ char **argv /* Text of each argument */ ){ +#ifdef SQLITE_HAS_CODEC + sqlite3 *db; + const char *zKey; + int nKey; + if( argc!=3 ){ + Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0], + " FILENAME\"", 0); + return TCL_ERROR; + } + if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR; + zKey = argv[2]; + nKey = strlen(zKey); + sqlite3_rekey(db, zKey, nKey); +#endif return TCL_OK; } diff --git a/src/test_config.c b/src/test_config.c index c8ce2ab88a..2d4b4c98ab 100644 --- a/src/test_config.c +++ b/src/test_config.c @@ -257,7 +257,13 @@ static void set_options(Tcl_Interp *interp){ Tcl_SetVar2(interp, "sqlite_options", "json1", "0", TCL_GLOBAL_ONLY); #endif +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + Tcl_SetVar2(interp, "sqlite_options", "has_codec", "1", TCL_GLOBAL_ONLY); +#else Tcl_SetVar2(interp, "sqlite_options", "has_codec", "0", TCL_GLOBAL_ONLY); +#endif +/* END SQLCIPHER */ #ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS Tcl_SetVar2(interp, "sqlite_options", "like_match_blobs", "0", TCL_GLOBAL_ONLY); diff --git a/src/test_thread.c b/src/test_thread.c index 7c06d110ac..8dcf4ab50c 100644 --- a/src/test_thread.c +++ b/src/test_thread.c @@ -283,6 +283,24 @@ static int SQLITE_TCLAPI sqlthread_open( zFilename = Tcl_GetString(objv[2]); sqlite3_open(zFilename, &db); +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + if( db && objc>=4 ){ + const char *zKey; + int nKey; + int rc; + zKey = Tcl_GetStringFromObj(objv[3], &nKey); + rc = sqlite3_key(db, zKey, nKey); + if( rc!=SQLITE_OK ){ + char *zErrMsg = sqlite3_mprintf("error %d: %s", rc, sqlite3_errmsg(db)); + sqlite3_close(db); + Tcl_AppendResult(interp, zErrMsg, (char*)0); + sqlite3_free(zErrMsg); + return TCL_ERROR; + } + } +#endif +/* END SQLCIPHER */ Md5_Register(db, 0, 0); sqlite3_busy_handler(db, xBusy, 0); diff --git a/src/util.c b/src/util.c index ecce460e01..0044288994 100644 --- a/src/util.c +++ b/src/util.c @@ -1491,7 +1491,8 @@ u8 sqlite3HexToInt(int h){ return (u8)(h & 0xf); } -#if !defined(SQLITE_OMIT_BLOB_LITERAL) +/* BEGIN SQLCIPHER */ +#if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC) /* ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary ** value. Return a pointer to its binary value. Space to hold the @@ -1512,7 +1513,8 @@ void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){ } return zBlob; } -#endif /* !SQLITE_OMIT_BLOB_LITERAL */ +#endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */ +/* END SQLCIPHER */ /* ** Log an error that is an API call on a connection pointer that should diff --git a/src/vacuum.c b/src/vacuum.c index e203f68c65..f6b81d198b 100644 --- a/src/vacuum.c +++ b/src/vacuum.c @@ -246,6 +246,21 @@ SQLITE_NOINLINE int sqlite3RunVacuum( } nRes = sqlite3BtreeGetRequestedReserve(pMain); + /* A VACUUM cannot change the pagesize of an encrypted database. */ +/* BEGIN SQLCIPHER */ +#ifdef SQLITE_HAS_CODEC + if( db->nextPagesize ){ + extern void sqlcipherCodecGetKey(sqlite3*, int, void**, int*); + extern void sqlcipher_free(void*, sqlite3_uint64); + int nKey; + char *zKey; + sqlcipherCodecGetKey(db, iDb, (void**)&zKey, &nKey); + if( nKey ) db->nextPagesize = 0; + if(nKey) sqlcipher_free(zKey, nKey); + } +#endif +/* END SQLCIPHER */ + sqlite3BtreeSetCacheSize(pTemp, db->aDb[iDb].pSchema->cache_size); sqlite3BtreeSetSpillSize(pTemp, sqlite3BtreeSetSpillSize(pMain,0)); sqlite3BtreeSetPagerFlags(pTemp, pgflags|PAGER_CACHESPILL); diff --git a/src/wal.c b/src/wal.c index 42ce3cb97b..964444fa64 100644 --- a/src/wal.c +++ b/src/wal.c @@ -3925,7 +3925,11 @@ static int walWriteOneFrame( int rc; /* Result code from subfunctions */ void *pData; /* Data actually written */ u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */ +#if defined(SQLITE_HAS_CODEC) + if( (pData = sqlcipherPagerCodec(pPage))==0 ) return SQLITE_NOMEM_BKPT; +#else pData = pPage->pData; +#endif walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame); rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset); if( rc ) return rc; @@ -4110,7 +4114,11 @@ static int walFrames( if( pWal->iReCksum==0 || iWriteiReCksum ){ pWal->iReCksum = iWrite; } +#if defined(SQLITE_HAS_CODEC) + if( (pData = sqlcipherPagerCodec(p))==0 ) return SQLITE_NOMEM; +#else pData = p->pData; +#endif rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff); if( rc ) return rc; p->flags &= ~PGHDR_WAL_APPEND; diff --git a/test/sqlcipher-backup.test b/test/sqlcipher-backup.test new file mode 100644 index 0000000000..92058e07bd --- /dev/null +++ b/test/sqlcipher-backup.test @@ -0,0 +1,157 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +# backup from plaintext to plaintext +# is allowed +do_test sqlcipher-backup-plain-plain { + sqlite_orig db test.db + set rc {} + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1, randstr(16384,16384)); + } + + set md5a [execsql {SELECT md5sum(a,b) FROM t1}] + sqlite_orig db2 backup.db + sqlite3_backup B db2 main db main + lappend rc [B step -1] + lappend rc [B finish] + + db close + db2 close + + sqlite_orig db backup.db + + set md5b [execsql {SELECT md5sum(a,b) FROM t1}] + + lappend rc [ execsql { + PRAGMA integrity_check; + } ] + + lappend rc [string equal $md5a $md5b] +} {SQLITE_DONE SQLITE_OK ok 1} +db close +file delete -force test.db +file delete -force backup.db + +# backup from encrypted to encrypted +# is allowed +do_test sqlcipher-backup-encrypted-encrypted { + sqlite_orig db test.db + set rc {} + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1, randstr(16384,16384)); + } + set md5a [execsql {SELECT md5sum(a,b) FROM t1}] + + sqlite_orig db2 backup.db + execsql { PRAGMA key = 'testkey' } db2; + + sqlite3_backup B db2 main db main + lappend rc [B step -1] + lappend rc [B finish] + + db close + db2 close + + sqlite_orig db backup.db + execsql { PRAGMA key = 'testkey' }; + + set md5b [execsql {SELECT md5sum(a,b) FROM t1}] + + lappend rc [ execsql { + PRAGMA integrity_check; + PRAGMA cipher_integrity_check; + } ] + + lappend rc [string equal $md5a $md5b] + +} {SQLITE_DONE SQLITE_OK ok 1} +db close +file delete -force test.db +file delete -force backup.db + +# backup from plaintext to encrypted +# is blocked +do_test sqlcipher-backup-plain-encrypted { + sqlite_orig db test.db + set rc {} + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1, randstr(16384,16384)); + } + + sqlite_orig db2 backup.db + execsql { PRAGMA key = 'testkey' } db2; + + lappend rc [catch {sqlite3_backup B db2 main db main}] + lappend rc [sqlite3_errcode db2] + lappend rc [sqlite3_errmsg db2] +} {1 SQLITE_ERROR {backup is not supported with encrypted databases}} +db close +db2 close +file delete -force test.db +file delete -force backup.db + +# backup from encrypted to plaintext +# is blocked +do_test sqlcipher-backup-encrypted-plain { + sqlite_orig db test.db + set rc {} + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1, randstr(16384,16384)); + } + + sqlite_orig db2 backup.db + + lappend rc [catch {sqlite3_backup B db2 main db main}] + lappend rc [sqlite3_errcode db2] + lappend rc [sqlite3_errmsg db2] +} {1 SQLITE_ERROR {backup is not supported with encrypted databases}} +db close +db2 close +file delete -force test.db +file delete -force backup.db + +finish_test diff --git a/test/sqlcipher-codecerror.test b/test/sqlcipher-codecerror.test new file mode 100644 index 0000000000..12cafbd79a --- /dev/null +++ b/test/sqlcipher-codecerror.test @@ -0,0 +1,171 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +proc codec-test-setup {} { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a INTEGER PRIMARY KEY,b); + BEGIN; + } + + for {set i 1} {$i<=10000} {incr i} { + execsql "INSERT INTO t1(a,b) VALUES($i,'value $i');" + } + + execsql { + COMMIT; + } + + db close +} + + +do_test codec-error-journal-delete { + codec-test-setup + + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_test_on = fail_encrypt; + UPDATE t1 SET b = 'fail' WHERE a = 5000; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA cipher_test_off = fail_encrypt; + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + PRAGMA integrity_check; + SELECT b FROM t1 where a = 5000; + } + +} {ok ok {value 5000}} +db close +file delete -force test.db + +do_test codec-error-journal-wal { + codec-test-setup + + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_test_on = fail_encrypt; + UPDATE t1 SET b = 'fail' WHERE a = 5000; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA cipher_test_off = fail_encrypt; + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + PRAGMA integrity_check; + SELECT b FROM t1 where a = 5000; + } + +} {ok ok {value 5000}} +db close +file delete -force test.db + +do_test codec-error-journal-wal-transaction { + codec-test-setup + + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + BEGIN; + UPDATE t1 SET b = 'success' WHERE a = 1; + PRAGMA cipher_test_on = fail_encrypt; + UPDATE t1 SET b = 'fail' WHERE a = 5000; + COMMIT; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA cipher_test_off = fail_encrypt; + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + PRAGMA integrity_check; + SELECT b FROM t1 where a = 1; + SELECT b FROM t1 where a = 5000; + } + +} {ok ok {value 1} {value 5000}} +db close +file delete -force test.db + +do_test codec-error-journal-wal-read { + codec-test-setup + + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema; + PRAGMA cipher_test_on = fail_decrypt; + UPDATE t1 SET b = 'fail' WHERE a = 5000; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA cipher_test_off = fail_decrypt; + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + PRAGMA integrity_check; + SELECT b FROM t1 where a = 5000; + } + +} {ok ok {value 5000}} +db close +file delete -force test.db + +finish_test diff --git a/test/sqlcipher-compatibility.test b/test/sqlcipher-compatibility.test new file mode 100644 index 0000000000..69630fbf7d --- /dev/null +++ b/test/sqlcipher-compatibility.test @@ -0,0 +1,1507 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +set old_pending_byte [sqlite3_test_control_pending_byte 0x40000000] + +# create an unencrypted database, attach a new encrypted volume +# copy data between, verify the encypted database is good afterwards +do_test unencrypted-attach { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" + } + + execsql { + COMMIT; + ATTACH DATABASE 'test2.db' AS db2 KEY 'testkey'; + CREATE TABLE db2.t1(a,b); + INSERT INTO db2.t1 SELECT * FROM t1; + DETACH DATABASE db2; + } + + sqlite_orig db2 test2.db + execsql { + PRAGMA key='testkey'; + SELECT count(*) FROM t1; + } db2 +} {ok 1000} +db2 close +file delete -force test.db +file delete -force test2.db + +# create an unencrypted database, attach a new encrypted volume +# using a raw key copy data between, verify the encypted +# database is good afterwards +do_test unencrypted-attach-raw-key { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" + } + + execsql { + COMMIT; + ATTACH DATABASE 'test2.db' AS db2 KEY "x'10483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'"; + CREATE TABLE db2.t1(a,b); + INSERT INTO db2.t1 SELECT * FROM t1; + DETACH DATABASE db2; + } + + sqlite_orig db2 test2.db + execsql { + PRAGMA key="x'10483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'"; + SELECT count(*) FROM t1; + } db2 +} {ok 1000} +db2 close +file delete -force test.db +file delete -force test2.db + +# open a 4.0 database +do_test compat-open-4.0-database { + sqlite_orig db $sampleDir/sqlcipher-4.0-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} +db close + +# create an encrypted database, attach an default-key encrypted volume +# copy data between, verify the second database +do_test encrypted-attach-default-key { + sqlite_orig db test.db + + execsql { + PRAGMA key='testkey'; + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" + } + + execsql { + COMMIT; + ATTACH DATABASE 'test2.db' AS test; + CREATE TABLE test.t1(a,b); + INSERT INTO test.t1 SELECT * FROM t1; + DETACH DATABASE test; + } + + sqlite_orig db2 test2.db + + execsql { + PRAGMA key='testkey'; + SELECT count(*) FROM t1; + } db2 +} {ok 1000} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# create an encrypted database, attach an unencrypted volume +# copy data between, verify the unencypted database is good afterwards +do_test encrypted-attach-unencrypted { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + } + + sqlite_orig db2 test2.db + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + BEGIN; + } db2 + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" db2 + } + + execsql { + COMMIT; + ATTACH DATABASE 'test.db' AS test KEY ''; + INSERT INTO test.t1 SELECT * FROM t1; + DETACH DATABASE test; + } db2 + + execsql { + SELECT count(*) FROM t1; + } +} {1000} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# create an unencrypted database, attach an encrypted database +# then copy the data to it via sqlcipher_export and verify results +do_test unencrypted-to-encrypted-export { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" + } + + execsql { + COMMIT; + ATTACH DATABASE 'test2.db' AS test2 KEY 'testkey2'; + SELECT sqlcipher_export('test2'); + DETACH DATABASE test2; + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + SELECT count(*) FROM t1; + } + + execsql { + SELECT count(*) FROM t1; + } +} {1000} +db close +file delete -force test.db +file delete -force test2.db + +do_test unencrypted-corrupt-to-encrypted-export { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES (1,2); + + PRAGMA writable_schema = ON; + + UPDATE sqlite_schema SET sql = 'CREATE TABLE IF NOT EXISTS t1(a,b)' + WHERE tbl_name = 't1'; + + PRAGMA writable_schema = OFF; + INSERT INTO t1 VALUES (3,4); + + SELECT * FROM t1; + + ATTACH DATABASE 'test2.db' AS test2 KEY 'testkey2'; + + SELECT sqlcipher_export('test2'); + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + SELECT count(*) FROM sqlite_schema; + SELECT count(*) FROM t1; + } +} {ok 1 2} +db close +file delete -force test.db +file delete -force test2.db + + +# create an encrypted database, attach an unencrypted database +# with data in it, then import the data back into the encrypted DB +# and verify +do_test unencrypted-to-encrypted-import { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" + } + + execsql { + COMMIT; + } + db close + + sqlite_orig db test2.db + + execsql { + PRAGMA key = 'testkey2'; + ATTACH DATABASE 'test.db' AS test KEY ''; + SELECT sqlcipher_export('main', 'test'); + DETACH DATABASE test; + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + SELECT count(*) FROM t1; + } +} {ok 1000} +db close +file delete -force test.db +file delete -force test2.db + +# create an unencrypted database, attach an unencrypted volume +# copy data between, verify the unencypted database is good afterwards +do_test unencrypted-attach-unencrypted { + sqlite_orig db test.db + + execsql { + CREATE TABLE t1(a,b); + } + + sqlite_orig db2 test2.db + execsql { + CREATE TABLE t1(a,b); + BEGIN; + } db2 + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,$r);" db2 + } + + execsql { + COMMIT; + ATTACH DATABASE 'test.db' AS test; + INSERT INTO test.t1 SELECT * FROM t1; + DETACH DATABASE test; + } db2 + + execsql { + SELECT count(*) FROM t1; + } +} {1000} +db close +db2 close +file delete -force test.db +file delete -force test2.db + + +# open a 1.1.8 database using the new code, HMAC disabled +do_test open-1.1.8-database { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db test.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_use_hmac = off; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok 75709 1 1 one one 1 2 one two 1 2} +db close +file delete -force test.db + +# open a 1.1.8 database without hmac, then copy the data +do_test attach-and-copy-1.1.8 { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db test.db + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_use_hmac = OFF; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + ATTACH DATABASE 'test-new.db' AS db2 KEY 'testkey-hmac'; + CREATE TABLE db2.t1(a,b); + INSERT INTO db2.t1 SELECT * FROM main.t1; + DETACH DATABASE db2; + } + db close + + sqlite_orig db test-new.db + execsql { + PRAGMA key = 'testkey-hmac'; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok 75709 1 1 one one 1 2 one two 1 2} +db close +file delete -force test.db +file delete -force test-new.db + +# open a standard database, then attach a new +# database with completely different options. +# copy data between them, and verify that the +# new database can be opened with the proper data +do_test attached-database-pragmas { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + ATTACH DATABASE 'test2.db' AS db2 KEY 'testkey2'; + PRAGMA db2.cipher_page_size = 8192; + PRAGMA db2.kdf_iter = 1000; + PRAGMA db2.cipher_use_hmac = OFF; + CREATE TABLE db2.t1(a,b); + INSERT INTO db2.t1 SELECT * FROM main.t1; + DETACH DATABASE db2; + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + PRAGMA cipher_page_size = 8192; + PRAGMA kdf_iter = 1000; + PRAGMA cipher_use_hmac = OFF; + SELECT count(*) FROM t1; + } +} {ok 1000} +db close +file delete -force test.db +file delete -force test2.db + +# use the sqlcipher_export function +# on a non-existent database. Verify +# the error gets through. +do_test export-error { + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + SELECT sqlcipher_export('nodb'); + } +} {1 {unknown database nodb}} +db close +file delete -force test.db + +# verify sqlcipher_export with NULL parameters +do_test export-nulls { + sqlite_orig db test.db + + catchsql { + SELECT sqlcipher_export(NULL); + } + +} {1 {target database can't be NULL}} +db close +file delete -force test.db + +do_test export-nulls { + sqlite_orig db test.db + + catchsql { + SELECT sqlcipher_export('main', NULL); + } + +} {1 {target database can't be NULL}} +db close +file delete -force test.db + + +# use the sqlcipher_export function + +# use the sqlcipher_export function +# to copy a complicated database. +# tests autoincrement fields, +# indexes, views, and triggers, +# tables and virtual tables +do_test export-database { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b, c); + CREATE UNIQUE INDEX b_idx ON t1(b); + CREATE INDEX c_idx ON t1(c); + + CREATE TABLE t2(b,c); + CREATE TRIGGER t2_after_insert AFTER INSERT ON t2 + BEGIN + INSERT INTO t1(b,c) VALUES (new.b, new.c); + END; + + CREATE VIEW v1 AS + SELECT c FROM t1; + + CREATE VIRTUAL TABLE fts USING fts5(a,b); + + BEGIN; + -- start with one known value + INSERT INTO t2 VALUES(1000000,'value 1000000'); + } + + for {set i 1} {$i<=999} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t2 VALUES($i,'value $r');" + } + + execsql { + INSERT INTO fts SELECT b,c FROM t1; + COMMIT; + + ATTACH DATABASE 'test2.db' AS db2 KEY 'testkey2'; + PRAGMA db2.cipher_page_size = 8192; + + SELECT sqlcipher_export('db2'); + + DETACH DATABASE db2; + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + PRAGMA cipher_page_size = 8192; + SELECT count(*) FROM t1; + SELECT count(*) FROM v1; + SELECT count(*) FROM sqlite_sequence; + SELECT seq FROM sqlite_sequence WHERE name = 't1'; + INSERT INTO t2 VALUES(10001, 'value 938383'); + SELECT count(*) FROM t1; -- verify the trigger worked + SELECT seq FROM sqlite_sequence WHERE name = 't1'; -- verify that autoincrement worked + SELECT a FROM fts WHERE b MATCH '1000000'; + } +} {ok 1000 1000 1 1000 1001 1001 1000000} +db close +file delete -force test.db +file delete -force test2.db + +# use the sqlcipher_export function +# to copy a complicated attached database to the main database +do_test export-attached-database { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a INTEGER PRIMARY KEY AUTOINCREMENT, b, c); + CREATE UNIQUE INDEX b_idx ON t1(b); + CREATE INDEX c_idx ON t1(c); + + CREATE TABLE t2(b,c); + CREATE TRIGGER t2_after_insert AFTER INSERT ON t2 + BEGIN + INSERT INTO t1(b,c) VALUES (new.b, new.c); + END; + + CREATE VIEW v1 AS + SELECT c FROM t1; + + CREATE VIRTUAL TABLE fts USING fts5(a,b); + + BEGIN; + -- start with one known value + INSERT INTO t2 VALUES(1000000,'value 1000000'); + } + + for {set i 1} {$i<=999} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t2 VALUES($i,'value $r');" + } + + execsql { + INSERT INTO fts SELECT b,c FROM t1; + COMMIT; + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + + CREATE TABLE t3(a INTEGER PRIMARY KEY AUTOINCREMENT, b, c); + CREATE UNIQUE INDEX d_idx ON t3(b); + INSERT INTO t3(b,c) VALUES ('one', 'two'); + + ATTACH DATABASE 'test.db' AS db KEY 'testkey'; + + SELECT sqlcipher_export('main', 'db'); + + DETACH DATABASE db; + INSERT INTO t3(b,c) VALUES ('three', 'four'); + } + db close + + sqlite_orig db test2.db + execsql { + PRAGMA key = 'testkey2'; + SELECT count(*) FROM t1; + SELECT count(*) FROM v1; + SELECT count(*) FROM sqlite_sequence; + SELECT seq FROM sqlite_sequence WHERE name = 't1'; + INSERT INTO t2 VALUES(10001, 'value 938383'); + SELECT count(*) FROM t1; -- verify the trigger worked + SELECT seq FROM sqlite_sequence WHERE name = 't1'; -- verify that autoincrement worked + SELECT a FROM fts WHERE b MATCH '1000000'; + SELECT count(*) FROM t3; + } +} {ok 1000 1000 2 1000 1001 1001 1000000 2} +db close +file delete -force test.db +file delete -force test2.db + + +# open the database then insert a bunch of data. +# then delete it and run a manual vacuum +# verify that the file has become smaller +# but can still be opened with the proper +# key. also test vacuum into functionality introduced +# in sqlite 3.27.1 +do_test vacuum { + sqlite_orig db test.db + set rc {} + + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=10000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + lappend rc [execsql { + COMMIT; + SELECT count(*) FROM t1; + }] + + # grab current size of file + set sz [file size test.db] + + execsql { + DELETE FROM t1 WHERE rowid > 5000; + VACUUM into 'test-vacuum.db'; + VACUUM; + } + db close + + # grab separate vacuum file size + set sz2 [file size test-vacuum.db] + + # grab test.db file size, post vacuum + set sz3 [file size test.db] + + # verify that the new size is + # smaller than the old size + if {$sz > $sz2} { lappend rc true } + if {$sz > $sz3} { lappend rc true } + + sqlite_orig db test-vacuum.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + }] + db close + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + }] + +} {10000 true true {ok 5000} {ok 5000}} +db close +file delete -force test.db +file delete -force test-vacuum.db + +# open a 1.1.8 database (no HMAC, 4K iter), then +# try to open another 1.1.8 database. The +# attached database should have the same hmac +# setting as the original +do_test default-hmac-kdf-attach { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db test.db + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db sqlcipher-1.1.8-testkey.db + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_use_hmac = OFF; + PRAGMA cipher_default_kdf_iter = 4000; + PRAGMA cipher_default_page_size = 1024; + PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA1; + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2 KEY 'testkey'; + SELECT count(*) from db2.t1; + PRAGMA cipher_default_use_hmac = ON; + PRAGMA cipher_default_kdf_iter = 256000; + PRAGMA cipher_default_page_size = 4096; + PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA512; + } +} {ok 75709 75709} +db close +file delete -force test.db +file delete -force sqlcipher-1.1.8-testkey.db + +# open a 2.0 database (with HMAC), then +# try to a 1.1.8 database. this should +# fail because the hmac setting for the +# attached database is not compatible +do_test attach-1.1.8-database-from-2.0-fails { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db sqlcipher-1.1.8-testkey.db + sqlite_orig db test.db + catchsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2 KEY 'testkey'; + } +} {1 {file is not a database}} +db close +file delete -force test.db +file delete -force sqlcipher-1.1.8-testkey.db + +# open a 2.0 database (with HMAC, 4k iter), then +# set the default hmac setting to OFF. +# try to a 1.1.8 database. this should +# succeed now that hmac is off by default +# before the attach +do_test change-default-hmac-kdf-attach { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db sqlcipher-1.1.8-testkey.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + db close + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + PRAGMA cipher_default_use_hmac = OFF; + PRAGMA cipher_default_kdf_iter = 4000; + PRAGMA cipher_default_page_size = 1024; + PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA1; + ATTACH 'sqlcipher-1.1.8-testkey.db' AS db2 KEY 'testkey'; + SELECT count(*) from db2.t1; + PRAGMA cipher_default_use_hmac = ON; + PRAGMA cipher_default_kdf_iter = 256000; + PRAGMA cipher_default_page_size = 4096; + PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA512; + } +} {ok 1 75709} +db close +file delete -force test.db +file delete -force sqlcipher-1.1.8-testkey.db + + +# create a new database, insert some data +# and delete some data with +# auto_vacuum on +do_test auto-vacuum-full { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test123'; + PRAGMA auto_vacuum = FULL; + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<10000} {incr i} { + set r [expr {int(rand()*32767)}] + set r1 [expr {int(rand()*32767)}] + execsql "INSERT INTO t1 VALUES($r,$r1);" + } + set r [expr {int(rand()*32767)}] + execsql "DELETE FROM t1 WHERE a < $r;" + + execsql { + COMMIT; + PRAGMA integrity_check; + PRAGMA freelist_count; + SELECT (count(*) > 0) FROM t1; + } +} {ok 0 1} +db close +file delete -force test.db + +# create a new database, insert some data +# and delete some data with +# auto_vacuum incremental +do_test auto-vacuum-incremental { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test123'; + PRAGMA auto_vacuum = INCREMENTAL; + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<10000} {incr i} { + set r [expr {int(rand()*32767)}] + set r1 [expr {int(rand()*32767)}] + execsql "INSERT INTO t1 VALUES($r,$r1);" + } + set r [expr {int(rand()*32767)}] + execsql "DELETE FROM t1 WHERE a < $r;" + + execsql { + COMMIT; + PRAGMA incremental_vacuum; + PRAGMA freelist_count; + PRAGMA integrity_check; + SELECT (count(*) > 0) FROM t1; + } +} {0 ok 1} +db close +file delete -force test.db + + +# create a database with many hundred tables such that the schema +# will overflow the first several pages of the database. verify the schema +# is intact on open. +do_test multipage-schema { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + BEGIN EXCLUSIVE; + } db + + for {set i 1} {$i<=300} {incr i} { + execsql "CREATE TABLE tab$i (a TEXT, b TEXT, c TEXT, d TEXT, e TEXT, f TEXT, g TEXT, h TEXT, i TEXT, j TEXT, k, TEXT, l, m TEXT, n TEXT, o TEXT, p TEXT);" db + } + + execsql { + COMMIT; + } db + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema where type = 'table'; + } db + +} {ok 300} +db close +file delete -force test.db + +# create a database with many hundred tables such that the schema +# will overflow the first several pages of the database. this time, enable +# autovacuum on the database, which will cause sqlite to do some "short reads" +# after the end of the main database file. verify that there are no HMAC errors +# resulting from the short reads, and that the schema is intact when +# the database is reopened +do_test multipage-schema-autovacuum-shortread { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA auto_vacuum = FULL; + BEGIN EXCLUSIVE; + } db + + for {set i 1} {$i<=300} {incr i} { + execsql "CREATE TABLE tab$i (a TEXT, b TEXT, c TEXT, d TEXT, e TEXT, f TEXT, g TEXT, h TEXT, i TEXT, j TEXT, k, TEXT, l, m TEXT, n TEXT, o TEXT, p TEXT);" db + } + + execsql { + COMMIT; + } db + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema where type = 'table'; + } db + +} {ok 300} +db close +file delete -force test.db + +# same as multi-page-schema-autovacuum-shortread, except +# using write ahead log mode +do_test multipage-schema-autovacuum-shortread-wal { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA auto_vacuum = FULL; + PRAGMA journal_mode = WAL; + BEGIN EXCLUSIVE; + } db + + for {set i 1} {$i<=300} {incr i} { + execsql "CREATE TABLE tab$i (a TEXT, b TEXT, c TEXT, d TEXT, e TEXT, f TEXT, g TEXT, h TEXT, i TEXT, j TEXT, k, TEXT, l, m TEXT, n TEXT, o TEXT, p TEXT);" db + } + + execsql { + COMMIT; + } db + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema where type = 'table'; + } db +} {ok 300} +db close +file delete -force test.db + +# open a 3.0 database with little endian hmac page numbers (default) +# verify it can be opened +do_test open-3.0-le-database { + sqlite_orig db $sampleDir/sqlcipher-3.0-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_page_size = 1024; + PRAGMA kdf_iter = 64000; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok 78536 1 1 one one 1 2 one two} +db close + +# open a 2.0 database with little endian hmac page numbers (default) +# verify it can be opened +do_test open-2.0-le-database { + sqlite_orig db $sampleDir/sqlcipher-2.0-le-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok 78536 1 1 one one 1 2 one two} +db close + +# open a 2.0 database with big-endian hmac page numbers +# verify it can be opened +do_test open-2.0-be-database { + sqlite_orig db $sampleDir/sqlcipher-2.0-be-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_hmac_pgno = be; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok {PRAGMA cipher_hmac_pgno is deprecated, please remove from use} 78536 1 1 one one 1 2 one two} +db close + +# open a 2.0 database with big-endian hmac page numbers +# attach a new database with little endian page numbers (default) +# copy schema between the two, and verify the latter +# can be opened +do_test be-to-le-migration { + sqlite_orig db $sampleDir/sqlcipher-2.0-be-testkey.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_hmac_pgno = be; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + ATTACH DATABASE 'test.db' AS db2 KEY 'testkey'; + CREATE TABLE db2.t1(a,b); + INSERT INTO db2.t1 SELECT * FROM main.t1; + DETACH DATABASE db2; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok 78536 1 1 one one 1 2 one two} +db close +file delete -force test.db + + + +# open a 2.0 beta database with 4000 round hmac kdf and 0x00 +# hmac salt mask +# verify it can be opened +do_test open-2.0-beta-database { + sqlite_orig db $sampleDir/sqlcipher-2.0-beta-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA kdf_iter = 4000; + PRAGMA fast_kdf_iter = 4000; + PRAGMA cipher_hmac_salt_mask = "x'00'"; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + SELECT count(*) FROM t1; + SELECT distinct * FROM t1; + } +} {ok {PRAGMA fast_kdf_iter is deprecated, please remove from use} {PRAGMA cipher_hmac_salt_mask is deprecated, please remove from use} 38768 test-0-0 test-0-1 test-1-0 test-1-1} +db close + +# open a 2.0 beta database +# attach a new standard database +# copy schema between the two, and verify the latter +# can be opened +do_test 2.0-beta-to-2.0-migration { + sqlite_orig db $sampleDir/sqlcipher-2.0-beta-testkey.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_hmac_salt_mask = "x'00'"; + PRAGMA kdf_iter = 4000; + PRAGMA fast_kdf_iter = 4000; + PRAGMA cipher_page_size = 1024; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + SELECT count(*) FROM sqlite_schema; + + PRAGMA cipher_hmac_salt_mask = "x'3a'"; + ATTACH DATABASE 'test.db' AS db2 KEY 'testkey'; + + CREATE TABLE db2.t1(a,b); + INSERT INTO db2.t1 SELECT * FROM main.t1; + DETACH DATABASE db2; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT distinct * FROM t1; + } +} {ok test-0-0 test-0-1 test-1-0 test-1-1} +db close +file delete -force test.db + +do_test migrate-1.1.8-database-to-current-format { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db test.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_migrate; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema; + } +} {ok 1} +db close +file delete -force test.db + +do_test migrate-2-0-le-database-to-current-format { + file copy -force $sampleDir/sqlcipher-2.0-le-testkey.db test.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_migrate; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema; + } +} {ok 1} +db close +file delete -force test.db + +do_test migrate-3-0-database-to-current-format { + file copy -force $sampleDir/sqlcipher-3.0-testkey.db test.db + sqlite_orig db test.db + set rc {} + + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_migrate; + SELECT count(*) FROM sqlite_schema; + }] + db close + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema; + PRAGMA journal_mode; + }] +} {{ok 0 1} {ok 1 delete}} +db close +file delete -force test.db + +do_test migrate-wal-database-to-current { + file copy -force $sampleDir/sqlcipher-3.0-testkey.db test.db + sqlite_orig db test.db + set rc {} + + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_page_size = 1024; PRAGMA kdf_iter = 64000; PRAGMA cipher_hmac_algorithm = HMAC_SHA1; PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + PRAGMA journal_mode = wal; + }] + db close + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_migrate; + PRAGMA journal_mode; + }] + db close + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema; + PRAGMA journal_mode; + }] +} {{ok wal} {ok 0 wal} {ok 1 wal}} +db close +file delete -force test.db + +# test original database is left untouched after +# a failed migration e.g. due to low disk space +do_test migrate-failure { + file copy -force $sampleDir/sqlcipher-3.0-testkey.db test.db + sqlite_orig db test.db + + set rc {} + + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_test_on = fail_migrate; + PRAGMA cipher_migrate; + }] + db close + + lappend rc [file exists test.db-migrated] + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_test_off = fail_migrate; + PRAGMA cipher_compatibility = 3; + SELECT count(*) FROM sqlite_schema; + }] +} {{ok 1} 0 {ok 1}} +db close +file delete -force test.db + +# if a migration failes the database should be in a permanent error state +do_test migrate-failure-not-readable { + file copy -force $sampleDir/sqlcipher-3.0-testkey.db test.db + sqlite_orig db test.db + + set rc {} + lappend rc [execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_test_on = fail_migrate; + PRAGMA cipher_migrate; + }] + + lappend rc [catchsql { + SELECT count(*) FROM sqlite_schema; + }] + db close + + sqlite_orig db test.db + lappend rc [execsql { + PRAGMA cipher_test_off = fail_migrate; + PRAGMA cipher_test; + }] +} {{ok 1} {1 {out of memory}} 0} +db close +file delete -force test.db + +# if cipher_migrate is called on a current-version databse +# is should do nothing and just report OK +do_test migrate-current-format-noop { + file copy -force $sampleDir/sqlcipher-4.0-testkey.db test.db + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_migrate; + SELECT count(*) FROM sqlite_schema; + } +} {ok 0 1} +db close +file delete -force test.db + +do_test key-database-by-name { + sqlite_orig db test.db + execsql { + attach database 'new.db' as new; + pragma new.key = 'foo'; + create table new.t1(a,b); + insert into new.t1(a,b) values('foo', 'bar'); + detach database new; + } + db close + + sqlite_orig db new.db + execsql { + pragma key = 'foo'; + select * from t1; + } +} {ok foo bar} +db close +file delete -force test.db +file delete -force new.db + +do_test key-multiple-databases-with-different-keys-using-pragma { + sqlite_orig db test.db + execsql { + pragma key = 'foobar'; + create table t1(a,b); + insert into t1(a,b) values('baz','qux'); + attach database 'new.db' as new; + pragma new.key = 'foo'; + create table new.t1(a,b); + insert into new.t1(a,b) values('foo', 'bar'); + detach database new; + } + db close + + sqlite_orig db new.db + execsql { + pragma key = 'foo'; + attach database 'test.db' as test key 'foobar'; + select * from t1; + select * from test.t1; + } +} {ok foo bar baz qux} +db close +file delete -force test.db +file delete -force new.db + + +# Requires SQLCipher to be built with -DSQLCIPHER_TEST +if_built_with_libtomcrypt verify-random-data-alters-file-content { + file delete -force test.db + file delete -force test2.db + file delete -force test3.db + set rc {} + + sqlite_orig db test.db + execsql { + PRAGMA key="x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + create table t1(a,b); + } + db close + sqlite_orig db test2.db + execsql { + PRAGMA key="x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + create table t1(a,b); + } + db close + sqlite_orig db test3.db + execsql { + PRAGMA key="x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + PRAGMA cipher_add_random = "x'deadbaad'"; + create table t1(a,b); + } + db close + lappend rc [cmpFilesChunked test.db test2.db] + lappend rc [cmpFilesChunked test2.db test3.db] +} {0 1} +file delete -force test.db +file delete -force test2.db +file delete -force test3.db + +do_test can-migrate-with-keys-longer-than-64-characters { + sqlite_orig db test.db + execsql { + PRAGMA key = "012345678901234567890123456789012345678901234567890123456789012345"; + PRAGMA cipher_page_size = 1024; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + PRAGMA user_version = 5; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = "012345678901234567890123456789012345678901234567890123456789012345"; + PRAGMA cipher_migrate; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = "012345678901234567890123456789012345678901234567890123456789012345"; + PRAGMA user_version; + } +} {ok 5} +db close +file delete -force test.db + +do_test can-migrate-with-raw-hex-key { + sqlite_orig db test.db + execsql { + PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + PRAGMA cipher_page_size = 1024; + PRAGMA kdf_iter = 4000; + PRAGMA cipher_use_hmac = off; + PRAGMA user_version = 5; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + PRAGMA cipher_migrate; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'"; + PRAGMA user_version; + } + +} {ok 5} +db close +file delete -force test.db + +do_test attach_database_with_non_default_page_size { + sqlite_orig db test2.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_page_size = 8192; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) values('one for the money', 'two for the show'); + INSERT INTO t1(a,b) values('three to get ready', 'now, go cat, go'); + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_page_size = 8192; + PRAGMA key = 'test'; + ATTACH DATABASE 'test2.db' as test2 KEY 'test'; + SELECT count(*) FROM test2.t1; + PRAGMA cipher_default_page_size = 4096; + } +} {ok 2} +db close +file delete -force test.db test2.db + +do_test verify-cipher-export-with-trace-configured { + sqlite_orig db plain.db + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES(1,2); + } + set TRACE_OUT {} + db trace trace_proc + execsql { + ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'encrypted'; + SELECT sqlcipher_export('encrypted'); + DETACH DATABASE encrypted; + } + set TRACE_OUT +} {{ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'encrypted';} {SELECT sqlcipher_export('encrypted');} {DETACH DATABASE encrypted;}} +set TRACE_OUT {} +db close +file delete -force plain.db +file delete -force encrypted.db + +# open a 1.1.8 database using cipher_compatibility +do_test compat-open-1.1.8-database { + sqlite_orig db $sampleDir/sqlcipher-1.1.8-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 1; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 75709} +db close + +# open a 2.0 database using cipher_compatibility +do_test compat-open-2.0-database { + sqlite_orig db $sampleDir/sqlcipher-2.0-le-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 2; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} +db close + +# open a 3.0 database using cipher_compatibility +do_test compat-open-3.0-database { + sqlite_orig db $sampleDir/sqlcipher-3.0-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 3; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} +db close + +# open a 4.0 database using cipher_compatibility +do_test compat-open-4.0-database { + sqlite_orig db $sampleDir/sqlcipher-4.0-testkey.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 4; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} +db close + +# open a 1.1.8 database using cipher_default_compatibility +do_test default-compat-open-1.1.8-database { + sqlite_orig db $sampleDir/sqlcipher-1.1.8-testkey.db + execsql { + PRAGMA cipher_default_compatibility = 1; + PRAGMA key = 'testkey'; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 75709} +db close + +# open a 2.0 database using cipher_default_compatibility +do_test default-compat-open-2.0-database { + sqlite_orig db $sampleDir/sqlcipher-2.0-le-testkey.db + execsql { + PRAGMA cipher_default_compatibility = 2; + PRAGMA key = 'testkey'; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} + +# open a 3.0 database using cipher_default_compatibility +do_test default-compat-open-3.0-database { + sqlite_orig db $sampleDir/sqlcipher-3.0-testkey.db + execsql { + PRAGMA cipher_default_compatibility = 3; + PRAGMA key = 'testkey'; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} + +# re-open a 4.0 database using cipher_default_compatibility +do_test default-compat-open-4.0-database { + sqlite_orig db $sampleDir/sqlcipher-4.0-testkey.db + execsql { + PRAGMA cipher_default_compatibility = 4; + PRAGMA key = 'testkey'; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok ok 78536} + +# create a database using a full keyspec consising of +# 64 characters for the encryption key, 64 for the hmac key +# and 32 for the salt +do_test test-full-keyspec { + sqlite_orig db test.db + execsql { + PRAGMA key = "x'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'"; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = "x'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'"; + SELECT count(*) FROM t1; + PRAGMA cipher_salt; + } + +} {ok 1 00000000000000000000000000000000} +db close +file delete -force test.db + +sqlite3_test_control_pending_byte $old_pending_byte + +finish_test diff --git a/test/sqlcipher-core.test b/test/sqlcipher-core.test new file mode 100644 index 0000000000..ee544a6345 --- /dev/null +++ b/test/sqlcipher-core.test @@ -0,0 +1,1007 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +# The database is initially empty. +# set an hex key create some basic data +# create table and insert operations should work +# close database, open it again with the same +# hex key. verify that the table is readable +# and the data just inserted is visible +setup test.db "\"x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'\"" +do_test will-open-with-correct-raw-key { + sqlite_orig db test.db + execsql { + PRAGMA key = "x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'"; + SELECT name FROM sqlite_schema WHERE type='table'; + SELECT * from t1; + } +} {ok t1 test1 test2} +db close +file delete -force test.db + +# set an encryption key (non-hex) and create some basic data +# create table and insert operations should work +# close database, open it again with the same +# key. verify that the table is readable +# and the data just inserted is visible +setup test.db "'testkey'" +do_test will-open-with-correct-derived-key { + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT name FROM sqlite_schema WHERE type='table'; + SELECT * from t1; + } +} {ok t1 test1 test2} +db close +file delete -force test.db + +# set an encryption key (non-hex) and create +# temp tables, verify you can read from +# sqlite_temp_master +setup test.db "'testkey'" +do_test test-temp-master { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + CREATE TEMPORARY TABLE temp_t1(a,b); + INSERT INTO temp_t1(a,b) VALUES ('test1', 'test2'); + SELECT name FROM sqlite_temp_master WHERE type='table'; + SELECT * from temp_t1; + } +} {ok temp_t1 test1 test2} +db close +file delete -force test.db + +# verify that a when a standard database is encrypted the first +# 16 bytes are not "SQLite format 3\0" +do_test test-sqlcipher-header-overwrite { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + CREATE TABLE t1(a,b); + } + db close + set header [hexio_read test.db 0 16] + string equal $header "53514C69746520666F726D6174203300" +} {0} +file delete -force test.db + +# open the database and try to read from it without +# providing a passphrase. verify that the +# an error is returned from the library +setup test.db "'testkey'" +do_test wont-open-without-key { + sqlite_orig db test.db + catchsql { + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {1 {file is not a database}} +db close +file delete -force test.db + +# open the database and try to set an invalid +# passphrase. verify that an error is returned +# and that data couldn't be read +setup test.db "'testkey'" +do_test wont-open-with-invalid-derived-key { + sqlite_orig db test.db + catchsql { + PRAGMA key = 'testkey2'; + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {1 {file is not a database}} +db close +file delete -force test.db + +# open the database and try to set an invalid +# hex key. verify that an error is returned +# and that data couldn't be read +setup test.db "'testkey'" +do_test wont-open-with-invalid-raw-key { + sqlite_orig db test.db + catchsql { + PRAGMA key = "x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836480'"; + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {1 {file is not a database}} +db close +file delete -force test.db + +# test a large number of inserts in a transaction to a memory database +do_test memory-database { + sqlite_orig db :memory: + execsql { + PRAGMA key = 'testkey3'; + BEGIN; + CREATE TABLE t2(a,b); + } + for {set i 1} {$i<=25000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t2 VALUES($i,$r);" + } + execsql { + COMMIT; + SELECT count(*) FROM t2; + DELETE FROM t2; + SELECT count(*) FROM t2; + } +} {25000 0} +db close + +# test a large number of inserts in a transaction for multiple pages +do_test multi-page-database { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t2(a,b); + BEGIN; + } + for {set i 1} {$i<=25000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t2 VALUES($i,$r);" + } + execsql { + COMMIT; + SELECT count(*) FROM t2; + } +} {25000} +db close +file delete -force test.db + +# attach an encrypted database +# without specifying key, verify it fails +# even if the source passwords are the same +# because the kdf salts are different +setup test.db "'testkey'" +do_test attach-database-with-default-key { + sqlite_orig db2 test2.db + set rc {} + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_add_random = "x'deadbaad'"; + CREATE TABLE t2(a,b); + INSERT INTO t2 VALUES ('test1', 'test2'); + } db2 + + lappend rc [catchsql { + ATTACH 'test.db' AS db; + } db2] + + lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]] + +} {{1 {file is not a database}} 0} +db2 close +file delete -force test.db +file delete -force test2.db + +# attach an empty encrypted database +# without specifying key, verify the database has the same +# salt and as the original +setup test.db "'testkey'" +do_test attach-empty-database-with-default-key { + sqlite_orig db test.db + set rc {} + + execsql { + PRAGMA key='testkey'; + INSERT INTO t1(a,b) values (1,2); + ATTACH DATABASE 'test2.db' AS test; + CREATE TABLE test.t1(a,b); + INSERT INTO test.t1 SELECT * FROM t1; + DETACH DATABASE test; + } + + sqlite_orig db2 test2.db + + lappend rc [execsql { + PRAGMA key='testkey'; + SELECT count(*) FROM t1; + } db2] + lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]] +} {{ok 2} 1} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# attach an empty encrypted database as the first operation on a keyed database. Verify +# that the new database has the same salt as the original. +# +# HISTORICAL NOTE: The original behavior of SQLCipher under these conditions +# was that the databases would have different salts but the same keys. This was because +# derivation of the key spec would not have occurred yet. However, upstream check-in +# https://sqlite.org/src/info/a02da71f3a80dd8e changed this behavior by +# forcing a read of the main database schema during the attach operation. +# This causes the main database to be opened and the key derivation logic to fire which +# reads the salt. Thus the current behavior of this test should now be identical +# to the previous attach-empty-database-with-default-key. + +setup test.db "'testkey'" +do_test attach-empty-database-with-default-key-first-op { + sqlite_orig db test.db + set rc {} + + execsql { + PRAGMA key='testkey'; + ATTACH DATABASE 'test2.db' AS test; + CREATE TABLE test.t1(a,b); + INSERT INTO test.t1 SELECT * FROM t1; + DETACH DATABASE test; + } + + sqlite_orig db2 test2.db + + lappend rc [execsql { + PRAGMA key='testkey'; + SELECT count(*) FROM t1; + } db2] + + lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]] +} {{ok 1} 1} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# attach an empty encrypted database +# on a keyed database when PRAGMA cipher_store_pass = 1 +# and verify different salts +setup test.db "'testkey'" +do_test attach-empty-database-with-cipher-store-pass { + sqlite_orig db test.db + set rc {} + + execsql { + PRAGMA key='testkey'; + PRAGMA cipher_store_pass = 1; + INSERT INTO t1(a,b) VALUES (1,2); + ATTACH DATABASE 'test2.db' AS test; + CREATE TABLE test.t1(a,b); + INSERT INTO test.t1 SELECT * FROM t1; + DETACH DATABASE test; + } + + sqlite_orig db2 test2.db + + lappend rc [execsql { + PRAGMA key='testkey'; + SELECT count(*) FROM t1; + } db2] + lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]] +} {{ok 2} 0} +db close +db2 close +file delete -force test.db +file delete -force test2.db + +# attach an encrypted database +# without specifying key, verify it attaches +# correctly when PRAGMA cipher_store_pass = 1 +# is set +do_test attach-database-with-default-key-using-cipher-store-pass { + sqlite_orig db1 test.db + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES('foo', 'bar'); + } db1 + db1 close + + sqlite_orig db2 test2.db + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t2(a,b); + INSERT INTO t2 VALUES ('test1', 'test2'); + } db2 + db2 close + + sqlite_orig db1 test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_store_pass = 1; + ATTACH DATABASE 'test2.db' as db2; + SELECT sqlcipher_export('db2'); + DETACH DATABASE db2; + } db1 + db1 close + + sqlite_orig db2 test2.db + execsql { + PRAGMA key = 'testkey'; + SELECT * FROM t1; + } db2 + +} {ok foo bar} +db2 close +file delete -force test.db +file delete -force test2.db + +# attach an encrypted database +# where both database have the same +# key explicitly and verify they have different +# salt values +setup test.db "'testkey'" +do_test attach-database-with-same-key { + sqlite_orig db2 test2.db + + set rc {} + + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t2(a,b); + INSERT INTO t2 VALUES ('test1', 'test2'); + } db2 + + lappend rc [execsql { + SELECT count(*) FROM t2; + ATTACH 'test.db' AS db KEY 'testkey'; + SELECT count(*) FROM db.t1; + } db2] + + lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]] +} {{1 1} 0} +db2 close +file delete -force test.db +file delete -force test2.db + +# attach an encrypted database +# where databases have different keys +setup test.db "'testkey'" +do_test attach-database-with-different-keys { + sqlite_orig db2 test2.db + + execsql { + PRAGMA key = 'testkey2'; + CREATE TABLE t2(a,b); + INSERT INTO t2 VALUES ('test1', 'test2'); + } db2 + + execsql { + ATTACH 'test.db' AS db KEY 'testkey'; + SELECT count(*) FROM db.t1; + SELECT count(*) FROM t2; + } db2 + +} {1 1} +db2 close +file delete -force test.db +file delete -force test2.db + +# test locking across multiple handles +setup test.db "'testkey'" +do_test locking-across-multiple-handles-start { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + BEGIN EXCLUSIVE; + INSERT INTO t1 VALUES(1,2); + } + + sqlite_orig dba test.db + catchsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + } dba + + } {1 {database is locked}} + +do_test locking-accross-multiple-handles-finish { + execsql { + COMMIT; + } + + execsql { + SELECT count(*) FROM t1; + } dba +} {2} +db close +dba close +file delete -force test.db + +# alter schema +setup test.db "'testkey'" +do_test alter-schema { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + ALTER TABLE t1 ADD COLUMN c; + INSERT INTO t1 VALUES (1,2,3); + INSERT INTO t1 VALUES (1,2,4); + CREATE TABLE t1a (a); + INSERT INTO t1a VALUES ('teststring'); + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1 WHERE a IS NOT NULL; + SELECT count(*) FROM t1 WHERE c IS NOT NULL; + SELECT * FROM t1a; + } + +} {ok 3 2 teststring} +db close +file delete -force test.db + +# test alterations of KDF iterations and ciphers +# rekey then add +setup test.db "'testkey'" +do_test verify-errors-for-rekey-kdf-and-cipher-changes { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA rekey_kdf_iter = 1000; + PRAGMA rekey_cipher = 'aes-256-ecb'; + } +} {ok {PRAGMA rekey_kdf_iter is no longer supported.} {PRAGMA rekey_cipher is no longer supported.}} +db close +file delete -force test.db + + +setup test.db "'testkey'" +do_test verify-errors-for-cipher-change { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher = 'aes-256-ecb'; + } +} {ok {PRAGMA cipher is no longer supported.}} +db close +file delete -force test.db + + +# 1. create a database with a custom page size, +# 2. create table and insert operations should work +# 3. close database, open it again with the same +# key and page size +# 4. verify that the table is readable +# and the data just inserted is visible +do_test custom-pagesize-pragma-cipher-page-size { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_page_size = 8192; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_page_size = 8192; + SELECT count(*) FROM t1; + } + +} {ok 1000} +db close +file delete -force test.db + +# run the same logic as previous test but use +# pragma page_size instead +do_test custom-pagesize-pragma-pagesize { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA page_size = 8192; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA page_size = 8192; + SELECT count(*) FROM t1; + } + +} {ok 1000} +db close +file delete -force test.db + +# open the database with the default page size +## and verfiy that it is not readable +do_test custom-pagesize-must-match { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_page_size = 8192; + CREATE table t1(a,b); + } + + db close + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {1 {file is not a database}} +db close +file delete -force test.db + + +# 1. create a database with WAL journal mode +# 2. create table and insert operations should work +# 3. close database, open it again +# 4. verify that the table is present, readable, and that +# the journal mode is WAL +do_test journal-mode-wal { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA journal_mode = WAL; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + PRAGMA journal_mode; + } + +} {ok 1000 wal} +db close +file delete -force test.db + +# open a database and try to use an invalid +# passphrase. verify that an error is returned +# and that data couldn't be read. without closing the databsae +# set the correct key and verify it is working. +setup test.db "'testkey'" +do_test multiple-key-calls-safe-wrong-key-first { + sqlite_orig db test.db + set rc {} + + lappend rc [catchsql { + PRAGMA key = 'testkey2'; + SELECT count(*) FROM sqlite_schema; + }] + + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM sqlite_schema; + }] +} {{1 {file is not a database}} {ok 1}} +db close +file delete -force test.db + +# open a databse and use the valid key. Then +# use pragma key to try to set an invalid key +# without closing the database. It should not do anything + +setup test.db "'testkey'" +do_test multiple-key-calls-safe { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cache_size = 0; + SELECT name FROM sqlite_schema WHERE type='table'; + PRAGMA key = 'wrong key'; + SELECT name FROM sqlite_schema WHERE type='table'; + PRAGMA key = 'testkey'; + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {ok t1 ok t1 ok t1} + +db close +file delete -force test.db + +# open a databse and use the valid key. Then +# use pragma cipher_compatability to adjust settings that +# would normally trigger key derivation again. +# the new settings should be ignored + +setup test.db "'testkey'" +do_test setting-changes-after-key-calls-safe { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cache_size = 0; + SELECT name FROM sqlite_schema WHERE type='table'; + INSERT INTO t1(a,b) VALUES (2,zeroblob(8192)); + PRAGMA cipher_compatibility=3; + INSERT INTO t1(a,b) VALUES (3,zeroblob(8192)); + SELECT name FROM sqlite_schema WHERE type='table'; + SELECT count(*) FROM t1; + } +} {ok t1 t1 3} +db close +file delete -force test.db + +# 1. create a database with a custom hmac kdf iteration count, +# 2. create table and insert operations should work +# 3. close database, open it again with the same +# key and hmac kdf iteration count +# 4. verify that the table is readable +# and the data just inserted is visible +do_test custom-hmac-kdf-iter { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA kdf_iter = 10; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA kdf_iter = 10; + SELECT count(*) FROM t1; + } + +} {ok 1000} +db close + +# open the database with the default hmac +# kdf iteration count +# to verify that it is not readable +do_test custom-hmac-kdf-iter-must-match { + sqlite_orig db test.db + catchsql { + PRAGMA key = 'testkey'; + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {1 {file is not a database}} +db close +file delete -force test.db + +# open the database and turn on auto_vacuum +# then insert a bunch of data, delete it +# and verify that the file has become smaller +# but can still be opened with the proper +# key +do_test auto-vacuum { + sqlite_orig db test.db + set rc {} + + execsql { + PRAGMA key = 'testkey'; + PRAGMA auto_vacuum=FULL; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=10000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + lappend rc [execsql { + COMMIT; + SELECT count(*) FROM t1; + }] + + # grab current size of file + set sz [file size test.db] + + # delete some records, and verify + # autovacuum removes them + execsql { + DELETE FROM t1 WHERE rowid > 5000; + } + + db close + + # grab new file size, post + # autovacuum + set sz2 [file size test.db] + + # verify that the new size is + # smaller than the old size + if {$sz > $sz2} { lappend rc true } + + sqlite_orig db test.db + + lappend rc [execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + }] + +} {10000 true {ok 5000}} +db close +file delete -force test.db + +# test kdf_iter and other pragmas +# before a key is set. Verify that they +# are no-ops +do_test cipher-options-before-keys { + sqlite_orig db test.db + + execsql { + PRAGMA kdf_iter = 1000; + PRAGMA cipher_page_size = 8192; + PRAGMA cipher_use_hmac = OFF; + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + INSERT INTO t1 VALUES(1,2); + } + db close + + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + } + +} {ok 1} +db close +file delete -force test.db + +# verify memory security behavior +# initially should report OFF +# then enable, check that it is ON +# try to turn if off, but verify that it +# can't be unset. +do_test verify-memory-security { + sqlite_orig db test.db + execsql { + PRAGMA cipher_memory_security; + PRAGMA cipher_memory_security = ON; + PRAGMA cipher_memory_security; + PRAGMA cipher_memory_security = OFF; + PRAGMA cipher_memory_security; + } +} {0 1 1} +db close +file delete -force test.db + +# create two new database files, write to each +# and verify that they have different (i.e. random) +# salt values +do_test test-random-salt { + sqlite_orig db test.db + sqlite_orig db2 test2.db + execsql { + PRAGMA key = 'test'; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + execsql { + PRAGMA key = 'test'; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } db2 + db close + db2 close + string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16] +} {0} +file delete -force test.db +file delete -force test2.db + +# test scenario where multiple handles are opened +# to a file that does not exist, where both handles +# use the same key +do_test multiple-handles-same-key-and-salt { + sqlite_orig db test.db + sqlite_orig dba test.db + + execsql { + PRAGMA key = 'testkey'; + } + execsql { + PRAGMA key = 'testkey'; + } dba + + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,2); + } + + execsql { + SELECT count(*) FROM t1; + } + execsql { + SELECT count(*) FROM t1; + } dba + +} {1} +db close +dba close +file delete -force test.db + +do_test test_flags_fail_encrypt { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_test; + PRAGMA cipher_test_on = fail_encrypt; + PRAGMA cipher_test; + PRAGMA cipher_test_off = fail_encrypt; + PRAGMA cipher_test; + } +} {0 1 0} +db close + +do_test test_flags_fail_decrypt { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_test; + PRAGMA cipher_test_on = fail_decrypt; + PRAGMA cipher_test; + PRAGMA cipher_test_off = fail_decrypt; + PRAGMA cipher_test; + } +} {0 2 0} +db close + +do_test test_flags_fail_migrate { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_test; + PRAGMA cipher_test_on = fail_migrate; + PRAGMA cipher_test; + PRAGMA cipher_test_off = fail_migrate; + PRAGMA cipher_test; + } +} {0 4 0} +db close + +do_test test_flags_combo { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_test; + PRAGMA cipher_test_on = fail_encrypt; + PRAGMA cipher_test_on = fail_migrate; + PRAGMA cipher_test; + PRAGMA cipher_test_off = fail_encrypt; + PRAGMA cipher_test_off = fail_migrate; + PRAGMA cipher_test; + } +} {0 5 0} +db close + +# test empty key +# it should raise an error +do_test empty-key { + sqlite_orig db test.db + + catchsql { + PRAGMA key = ''; + } + +} {1 {An error occurred with PRAGMA key or rekey. PRAGMA key requires a key of one or more characters. PRAGMA rekey can only be run on an existing encrypted database. Use sqlcipher_export() and ATTACH to convert encrypted/plaintext databases.}} +db close +file delete -force test.db + +# configure URI filename support +# create a new encrypted database with the key via parameter +# close database +# open normally providing key via pragma verify +# correct key works +sqlite3_shutdown +sqlite3_config_uri 1 +do_test uri-key { + sqlite_orig db file:test.db?a=a&key=testkey&c=c + + execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1 VALUES(1,2); + } + + db close + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + } + + db close + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + } + +} {ok 1} +db close + +# verify wrong key fails +do_test uri-key-2 { + sqlite_orig db test.db + catchsql { + PRAGMA key = 'test'; + SELECT count(*) FROM t1; + } +} {1 {file is not a database}} +db close +file delete -force test.db +sqlite3_shutdown +sqlite3_config_uri 0 + +finish_test + diff --git a/test/sqlcipher-integrity.test b/test/sqlcipher-integrity.test new file mode 100644 index 0000000000..fa0949892c --- /dev/null +++ b/test/sqlcipher-integrity.test @@ -0,0 +1,382 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +# 1. create a database and insert a bunch of data, close the database +# 2. seek to the middle of the first database page and write some junk +# 3. Open the database and verify that the database is no longer readable +do_test hmac-tamper-resistence-first-page { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + + # write some junk into the hmac segment, leaving + # the page data valid but with an invalid signature + hexio_write test.db 1000 000000 + + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + } + +} {1 {file is not a database}} +db close +file delete -force test.db + +# 1. create a database and insert a bunch of data, close the database +# 2. seek to the middle of a database page and write some junk +# 3. Open the database and verify that the database is still readable +do_test nohmac-not-tamper-resistent { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_use_hmac = OFF; + PRAGMA cipher_page_size = 1024; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + + # write some junk into the middle of the page + hexio_write test.db 2560 000000 + + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_use_hmac = OFF; + PRAGMA cipher_page_size = 1024; + SELECT count(*) FROM t1; + } + +} {ok 1000} +db close +file delete -force test.db + +# 1. create a database and insert a bunch of data, close the database +# 2. seek to the middle of a database page (not the first page) and write bad data +# 3. Open the database and verify that the database is no longer readable +do_test hmac-tamper-resistence { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + } + + db close + + # write some junk into the hmac segment, leaving + # the page data valid but with an invalid signature + hexio_write test.db 16500 000000 + + sqlite_orig db test.db + + catchsql { + PRAGMA key = 'testkey'; + SELECT count(*) FROM t1; + } + +} {1 {database disk image is malformed}} +db close +file delete -force test.db + +# test that integrity checks work on a pristine +# newly created database +do_test integrity-check-clean-database { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=10000} {incr i} { + execsql "INSERT INTO t1 VALUES($i,'value $i');" + } + + execsql { + COMMIT; + } + + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } + +} {ok ok 10000} +db close +file delete -force test.db + +# try cipher_integrity_check on an in-memory database +# which should fail because the file doesn't exist +do_test memory-integrity-check-should-fail { + sqlite_orig db :memory: + execsql { + PRAGMA key = 'testkey'; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) values (1,2); + PRAGMA cipher_integrity_check; + } +} {ok {database file is undefined}} +db close + +# try cipher_integrity_check on a valid 1.1.8 database +# should fail because version 1.0 doesn't use HMAC +do_test version-1-integrity-check-fail-no-hmac { + file copy -force $sampleDir/sqlcipher-1.1.8-testkey.db test.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 1; + PRAGMA cipher_integrity_check; + } +} {ok {HMAC is not enabled, unable to integrity check}} +db close +file delete -force test.db + +# try cipher_integrity_check on a valid 2 database +do_test version-2-integrity-check-valid { + file copy -force $sampleDir/sqlcipher-2.0-le-testkey.db test.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 2; + PRAGMA cipher_integrity_check; + } +} {ok} +db close +file delete -force test.db + +# try cipher_integrity_check on a corrupted version 2 database +do_test version-2-integrity-check-invalid { + file copy -force $sampleDir/sqlcipher-2.0-le-testkey.db test.db + hexio_write test.db 8202 000000 + hexio_write test.db 10250 000000 + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 2; + PRAGMA cipher_integrity_check; + } +} {ok {HMAC verification failed for page 9} {HMAC verification failed for page 11}} +db close +file delete -force test.db + +# try cipher_integrity_check on a valid version 3 database +do_test version-3-integrity-check-valid { + file copy -force $sampleDir/sqlcipher-3.0-testkey.db test.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 3; + PRAGMA cipher_integrity_check; + } +} {ok} +db close +file delete -force test.db + +# try cipher_integrity_check on a corrupted version 3 database +do_test version-3-integrity-check-invalid { + file copy -force $sampleDir/sqlcipher-3.0-testkey.db test.db + hexio_write test.db 8202 000000 + hexio_write test.db 10250 000000 + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_compatibility = 3; + PRAGMA cipher_integrity_check; + } +} {ok {HMAC verification failed for page 9} {HMAC verification failed for page 11}} +db close +file delete -force test.db + +# try cipher_integrity_check on a valid version 4 database +do_test version-4-integrity-check-valid { + file copy -force $sampleDir/sqlcipher-4.0-testkey.db test.db + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + } +} {ok} +db close +file delete -force test.db + +# try cipher_integrity_check on a corrupted version 4 database +do_test version-4-integrity-check-invalid { + file copy -force $sampleDir/sqlcipher-4.0-testkey.db test.db + # corrupt page data + hexio_write test.db 5120 000000 + # corrupt iv + hexio_write test.db 12208 000000 + # corrupt the mac segment + hexio_write test.db 16320 000000 + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + } +} {ok {HMAC verification failed for page 2} {HMAC verification failed for page 3} {HMAC verification failed for page 4}} +db close +file delete -force test.db + +# try cipher_integrity_check on a corrupted version 4 database +do_test version-4-integrity-check-invalid-last-page { + file copy -force $sampleDir/sqlcipher-4.0-testkey.db test.db + hexio_write test.db 978944 0000 + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA cipher_integrity_check; + } +} {ok {page 240 has an invalid size of 2 bytes (expected 4096 bytes)}} +db close +file delete -force test.db + +# verify cipher_integrity_check works on a plaintext header db +do_test integrity-check-plaintext-header { + sqlite_orig db test.db + set rc {} + + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_plaintext_header_size = 32; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + + lappend rc [execsql { + PRAGMA cipher_integrity_check; + }] + + lappend rc [string equal [hexio_read test.db 16 5] "1000010150"] + + hexio_write test.db 120 000000 + hexio_write test.db 5120 000000 + + lappend rc [execsql { + PRAGMA cipher_integrity_check; + }] +} {{} 1 {{HMAC verification failed for page 1} {HMAC verification failed for page 2}}} +file delete -force test.db + +# test that changing the key in the middle of database operations does +# not cause a corruption +do_test change-key-middle { + sqlite_orig db test.db + + set rc {} + + execsql { + PRAGMA key = 'testkey'; + CREATE table t1(a,b); + } + + for {set i 1} {$i<=1000} {incr i} { + execsql "INSERT INTO t1 VALUES($i,'value $i');" + } + + execsql { + PRAGMA key = 'diffkey'; + } + + for {set i 1} {$i<=1000} {incr i} { + execsql "INSERT INTO t1 VALUES($i,'value $i');" + } + + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + SELECT name FROM sqlite_schema; + PRAGMA cipher_integrity_check; + PRAGMA integrity_check; + SELECT count(*) FROM t1; + } +} {ok t1 ok 2000} +db close +file delete -force test.db + +finish_test diff --git a/test/sqlcipher-plaintext-header.test b/test/sqlcipher-plaintext-header.test new file mode 100644 index 0000000000..81e1a82312 --- /dev/null +++ b/test/sqlcipher-plaintext-header.test @@ -0,0 +1,471 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +set hexkeyspec "\"x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C483648101010101010101010101010101010101'\"" + +# verify default plaintext header size is 0 +do_test test-default-plaintext-header-size { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_default_plaintext_header_size; + } +} {0} + +# verify pragma cipher_salt returns the first 16 bytes +# of an existing database +do_test test-pragma-salt-get { + sqlite_orig db test.db + execsql { PRAGMA key = 'test'; } + set salt [execsql { + CREATE TABLE t1(a,b); + PRAGMA cipher_salt; + }] + set header [string tolower [hexio_read test.db 0 16]] + string equal $header $salt +} {1} +file delete -force test.db + +# explicitly set the salt of a new database +do_test test-pragma-salt-set { + set rc {} + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_salt = "x'01010101010101010101010101010101'"; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + db close + + lappend rc [hexio_read test.db 0 16] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = 'test'; + SELECT count(*) FROM t1; + PRAGMA cipher_salt; + "] + +} {01010101010101010101010101010101 {ok 1 01010101010101010101010101010101}} +file delete -force test.db + + +# verify that a raw key with a fixed salt will work +# the first 16 bytes of database should be equal to the specified salt +# which is the last 32 characters of the hex key spec. +# also verify return value of cipher_salt +do_test test-raw-key-with-salt-spec { + set rc {} + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + " + db close + + lappend rc [hexio_read test.db 0 16] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = $hexkeyspec; + SELECT count(*) FROM t1; + PRAGMA cipher_salt; + "] +} {01010101010101010101010101010101 {ok 1 01010101010101010101010101010101}} +db close +file delete -force test.db + +# verify that a raw key with an invalid salt will not work to +# open an existing database. +# should cause hmac failure due to invalid generated HMAC key +do_test test-raw-key-with-invalid-salt-spec { + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + " + db close + + sqlite_orig db test.db + catchsql { + PRAGMA key="x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C483648100000000000000000000000000000001'"; + SELECT count(*) FROM t1; + } +} {1 {file is not a database}} +db close +file delete -force test.db + +# verify that a raw key with a bad salt *will* work if page HMAC is disabled +# in this case the salt will not actually be used for anything +# because the encryption key is provided explicitly +do_test test-raw-key-with-invalid-salt-spec-no-hmac { + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_use_hmac = OFF; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + " + db close + + sqlite_orig db test.db + execsql { + PRAGMA key="x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C483648100000000000000000000000000000001'"; + PRAGMA cipher_use_hmac = OFF; + SELECT count(*) FROM t1; + } +} {ok 1} +db close +file delete -force test.db + +# verify that invalid cipher_plaintext_header_sizes don't work +# 1. less than zero +# 2. Larger than available page size +# 2. Not a multiple of block size +do_test test-invalid-plaintext-header-sizes { + set rc {} + sqlite_orig db test.db + lappend rc [catchsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = -1; + CREATE TABLE t1(a,b); + "] + db close + sqlite_orig db test.db + lappend rc [catchsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = 4096; + CREATE TABLE t1(a,b); + "] + db close + sqlite_orig db test.db + lappend rc [catchsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = 24; + CREATE TABLE t1(a,b); + "] +} {{1 {out of memory}} {1 {out of memory}} {1 {out of memory}}} +db close +file delete -force test.db + +# verify that a valid cipher_plaintext_header_size leaves the +# start of the database unencrypted, i.e. "SQLite format 3\0" +do_test test-valid-plaintext-header-size { + set rc {} + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = 16; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + " + db close + + lappend rc [hexio_read test.db 0 16] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = 16; + SELECT count(*) FROM t1; + PRAGMA cipher_plaintext_header_size; + "] +} {53514C69746520666F726D6174203300 {ok 1 16}} +db close +file delete -force test.db + +# when using a standard mode database and 32 byte +# plaintext header, ensure that bytes 16 - 19 +# corresponding to the page size and file versions, and reserve size +# are readable and equal to 1024, 1, 1, and 80 respectively +do_test test-plaintext-header-journal-delete-mode-readable { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_plaintext_header_size = 32; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + db close + string equal [hexio_read test.db 16 5] "1000010150" +} {1} +file delete -force test.db + + +# when using a WAL mode database and 32 byte +# plaintext header, ensure that bytes 16 - 19 +# corresponding to the page size and file versions, and reserve size +# are readable and equal to 1024, 2, 2 and 80 respectively +do_test test-plaintext-header-journal-wal-mode-readable { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_plaintext_header_size = 32; + PRAGMA journal_mode = WAL; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + db close + string equal [hexio_read test.db 16 5] "1000020250" +} {1} +file delete -force test.db + +# verify that a valid default_cipher_plaintext_header_size leaves the +# start of the database unencrypted right from the start +# , i.e. "SQLite format 3\0" +do_test test-valid-default-plaintext-header-size { + set rc {} + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_plaintext_header_size = 16; + PRAGMA key = 'test'; + } + + set salt [execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + PRAGMA cipher_salt; + }] + db close + + lappend rc [hexio_read test.db 0 16] + + sqlite_orig db test.db + execsql { PRAGMA key = 'test'; } + lappend rc [execsql " + PRAGMA cipher_salt = \"x'$salt'\"; + SELECT count(*) FROM t1; + PRAGMA cipher_plaintext_header_size; + "] + + # reset the default back to 0 or subsequent tests will fail + execsql "PRAGMA cipher_default_plaintext_header_size = 0;" + + lappend rc [string equal $salt "53514c69746520666f726d6174203300"] +} {53514C69746520666F726D6174203300 {1 16} 0} +db close +file delete -force test.db + +# verify that a valid default_cipher_plaintext_header_size +# operates properly on an attached database, and that the +# salt pragma operates on the attached database as well +do_test test-valid-default-plaintext-header-size-attach { + set rc {} + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_plaintext_header_size = 16; + PRAGMA key = 'test'; + } + set salt [execsql { + CREATE TABLE temp(a); + ATTACH DATABASE 'test2.db' as db2; + CREATE TABLE db2.t2(a,b); + INSERT INTO db2.t2(a,b) VALUES (1,2); + PRAGMA db2.cipher_salt; + DETACH DATABASE db2; + }] + db close + lappend rc [hexio_read test2.db 0 16] + + sqlite_orig db test2.db + execsql { PRAGMA key = 'test'; } + lappend rc [execsql " + PRAGMA cipher_salt = \"x'$salt'\"; + SELECT count(*) FROM t2; + PRAGMA cipher_plaintext_header_size; + "] + + # reset the default back to 0 or subsequent tests will fail + execsql "PRAGMA cipher_default_plaintext_header_size = 0;" + + lappend rc [string equal $salt "53514c69746520666f726d6174203300"] +} {53514C69746520666F726D6174203300 {1 16} 0} +db close +file delete -force test.db +file delete -force test2.db + + +# migrate a standard database in place to use a +# plaintext header offset by opening it, adjusting +# the pragma, and rewriting the first page +do_test test-plaintext-header-migrate-journal-delete { + set rc {} + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + " + db close + + lappend rc [hexio_read test.db 0 16] + + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + SELECT count(*) FROM t1; + PRAGMA cipher_plaintext_header_size = 32; + PRAGMA user_version = 1; + " + db close + lappend rc [hexio_read test.db 0 21] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = 32; + SELECT count(*) FROM t1; + "] + +} {01010101010101010101010101010101 53514C69746520666F726D61742033001000010150 {ok 1}} +db close +file delete -force test.db + +# migrate a wal mode database in place to use a +# plaintext header offset by opening it, adjusting +# the pragma, and rewriting the first page +do_test test-plaintext-header-migrate-journal-wal { + set rc {} + sqlite_orig db test.db + execsql " + PRAGMA key = $hexkeyspec; + PRAGMA journal_mode = WAL; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + " + db close + + lappend rc [hexio_read test.db 0 16] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = $hexkeyspec; + SELECT count(*) FROM t1; + PRAGMA journal_mode; + PRAGMA cipher_plaintext_header_size = 32; + PRAGMA user_version = 1; + PRAGMA wal_checkpoint(FULL); + "] + db close + lappend rc [hexio_read test.db 0 21] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = $hexkeyspec; + PRAGMA cipher_plaintext_header_size = 32; + SELECT count(*) FROM t1; + PRAGMA journal_mode; + "] + +} {01010101010101010101010101010101 {ok 1 wal 0 1 1} 53514C69746520666F726D61742033001000020250 {ok 1 wal}} +db close +file delete -force test.db + +# migrate a wal mode database in place to use a plaintext header +# but instead of using a raw key syntax, use a derived key +# but explicitly set the salt using cipher_salt +do_test test-plaintext-header-migrate-journal-wal-string-key-random-salt { + set rc {} + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA journal_mode = WAL; + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + } + db close + + set salt [hexio_read test.db 0 16] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = 'test'; + SELECT count(*) FROM t1; + PRAGMA journal_mode; + PRAGMA cipher_plaintext_header_size = 32; + PRAGMA user_version = 1; + PRAGMA wal_checkpoint(FULL); + "] + db close + + lappend rc [hexio_read test.db 0 21] + + sqlite_orig db test.db + lappend rc [execsql " + PRAGMA key = 'test'; + PRAGMA cipher_salt = \"x'$salt'\"; + PRAGMA cipher_plaintext_header_size = 32; + SELECT count(*) FROM t1; + PRAGMA journal_mode; + "] + + +} {{ok 1 wal 0 1 1} 53514C69746520666F726D61742033001000020250 {ok 1 wal}} +db close +file delete -force test.db + +# when cipher_salt is the first statement a new salt should be generated +# and it should match the salt after key derviation occurs. At no point +# should the salt be zero +do_test plaintext-header-size-salt-first-op { + set rc {} + sqlite_orig db test.db + execsql { PRAGMA key = 'test'; } + set salt1 [execsql { + PRAGMA cipher_plaintext_header_size = 16; + PRAGMA cipher_salt; + }] + + set salt2 [execsql { + CREATE TABLE t1(a,b); + INSERT INTO t1(a,b) VALUES (1,2); + PRAGMA cipher_salt; + }] + + lappend rc [string equal $salt1 "00000000000000000000000000000000"] + lappend rc [string equal $salt2 "00000000000000000000000000000000"] + lappend rc [string equal $salt1 $salt2] +} {0 0 1} +db close +file delete -force test.db + +finish_test diff --git a/test/sqlcipher-pragmas.test b/test/sqlcipher-pragmas.test new file mode 100644 index 0000000000..2b611001a3 --- /dev/null +++ b/test/sqlcipher-pragmas.test @@ -0,0 +1,499 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +# verify the pragma cipher_version +# returns the currently configured +# sqlcipher version +do_test verify-pragma-cipher-version { + sqlite_orig db test.db + execsql { + PRAGMA cipher_version; + } +} {{4.9.0 community}} +db close +file delete -force test.db + +# verify the pragma cipher_use_hmac +# is set to true be default +do_test verify-pragma-cipher-use-hmac-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_use_hmac; + } +} {ok 1} +db close +file delete -force test.db + +# verify the pragma cipher_use_hmac +# reports the flag turned off +do_test verify-pragma-cipher-use-hmac-off { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_use_hmac = off; + PRAGMA cipher_use_hmac; + } +} {ok 0} +db close +file delete -force test.db + +# verify the pragma default_cipher_use_hmac +# is set to true by default +do_test verify-pragma-cipher-default-use-hmac-default { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_use_hmac; + } +} {1} +db close +file delete -force test.db + +# verify the pragma default_cipher_use_hmac +# reports the flag turned off +do_test verify-pragma-cipher-default-use-hmac-off { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_use_hmac = off; + PRAGMA cipher_default_use_hmac; + -- Be sure to turn cipher_default_use_hmac + -- back on or it will break later tests + -- (it's a global flag) + PRAGMA cipher_default_use_hmac = ON; + } +} {0} +db close +file delete -force test.db + +# verify the pragma default_cipher_kdf_iter +# is set to 256000 by default +do_test verify-pragma-cipher-default-kdf-iter-default { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_kdf_iter; + } +} {256000} +db close +file delete -force test.db + + +# verify the pragma default_cipher_kdf_ter +# reports changes +do_test verify-pragma-cipher-default-use-hmac-off { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_kdf_iter = 1000; + PRAGMA cipher_default_kdf_iter; + PRAGMA cipher_default_kdf_iter = 256000; + } +} {1000} +db close +file delete -force test.db + +# verify the pragma kdf_iter +# reports the default value +do_test verify-pragma-kdf-iter-reports-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA kdf_iter; + } +} {ok 256000} +db close +file delete -force test.db + +# verify the pragma kdf_iter +# reports value changed +do_test verify-pragma-kdf-iter-reports-value-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA kdf_iter = 8000; + PRAGMA kdf_iter; + } + } {ok 8000} +db close +file delete -force test.db + +# verify the pragma fast_kdf_iter +# reports the default value +do_test verify-pragma-fast-kdf-iter-reports-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA fast_kdf_iter; + } +} {ok 2} +db close +file delete -force test.db + +# verify the pragma fast_kdf_iter +# reports value changed +do_test verify-pragma-kdf-iter-reports-value-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA fast_kdf_iter = 4000; + PRAGMA fast_kdf_iter; + } +} {ok {PRAGMA fast_kdf_iter is deprecated, please remove from use} 4000} +db close +file delete -force test.db + +# verify the pragma cipher_page_size +# reports default value +do_test verify-pragma-cipher-page-size-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_page_size; + } +} {ok 4096} +db close +file delete -force test.db + +# verify the pragma cipher_page_size +# reports change in value +do_test verify-pragma-cipher-page-size-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_page_size = 8192; + PRAGMA cipher_page_size; + } +} {ok 8192} +db close +file delete -force test.db + +# verify that a call to pragma page_size +# will report change via both page_size and cipher_page_size +# when there is an attached codec +do_test verify-pragma-page-size-encrypted { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA page_size = 8192; + PRAGMA page_size; + PRAGMA cipher_page_size; + } +} {ok 8192 8192} +db close +file delete -force test.db + +# verify that a call to pragma page_size +# will not report a change to cipher_page_size for an +# unencrypted database +do_test verify-pragma-page-size-plaintext { + sqlite_orig db test.db + execsql { + PRAGMA page_size = 8192; + PRAGMA page_size; + PRAGMA cipher_page_size; + } +} {8192} +db close +file delete -force test.db + +# verify setting cipher_store_pass before key +# does not cause segfault +do_test verify-cipher-store-pass-before-key-does-not-segfault { + sqlite_orig db test.db + execsql { + PRAGMA cipher_store_pass = 1; + PRAGMA key = 'test'; + } +} {ok} +db close +file delete -force test.db + +# verify setting cipher_store_pass results in deprecation warning +do_test verify-cipher-store-pass-deprecated { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_store_pass = 1; + } +} {ok {PRAGMA cipher_store_pass is deprecated, please remove from use}} +db close +file delete -force test.db + +# verify the pragma cipher +# reports the default value +if_built_with_openssl verify-pragma-cipher-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher; + } +} {ok AES-256-CBC} +db close +file delete -force test.db + +# verify the pragma cipher_hmac_salt_mask reports default +do_test verify-pragma-hmac-salt-mask-reports-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_hmac_salt_mask; + } +} {ok 3a} +db close +file delete -force test.db + +# verify the pragma cipher_hmac_salt_mask reports +# reports value changed +do_test verify-pragma-hmac-salt-mask-reports-value-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_hmac_salt_mask = "x'11'"; + PRAGMA cipher_hmac_salt_mask; + PRAGMA cipher_hmac_salt_mask = "x'3a'"; + } +} {ok {PRAGMA cipher_hmac_salt_mask is deprecated, please remove from use} 11 {PRAGMA cipher_hmac_salt_mask is deprecated, please remove from use}} +db close +file delete -force test.db + +# verify the pragma cipher_hmac_pgno reports default +do_test verify-pragma-hmac-pgno-reports-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_hmac_pgno; + } +} {ok le} +db close +file delete -force test.db + +# verify the pragma cipher_hmac_pgno +# reports value changed +do_test verify-pragma-hmac-pgno-reports-value-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_hmac_pgno = be; + PRAGMA cipher_hmac_pgno; + PRAGMA cipher_hmac_pgno = native; + PRAGMA cipher_hmac_pgno; + PRAGMA cipher_hmac_pgno = le; + PRAGMA cipher_hmac_pgno; + } +} {ok {PRAGMA cipher_hmac_pgno is deprecated, please remove from use} be {PRAGMA cipher_hmac_pgno is deprecated, please remove from use} native {PRAGMA cipher_hmac_pgno is deprecated, please remove from use} le} +db close +file delete -force test.db + +# verify the pragma cipher_hmac_algorithm works properly +do_test verify-pragma-cipher-hmac-algorithm-reports-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_hmac_algorithm; + } +} {ok HMAC_SHA512} +db close +file delete -force test.db + +do_test verify-pragma-cipher-hmac-algorithm-reports-value-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_hmac_algorithm; + } +} {ok HMAC_SHA1} +db close +file delete -force test.db + +do_test verify-pragma-cipher-default-hmac-algorithm { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_hmac_algorithm; + PRAGMA cipher_default_hmac_algorithm = HMAC_SHA1; + PRAGMA cipher_default_hmac_algorithm; + PRAGMA cipher_default_hmac_algorithm = HMAC_SHA512; + } +} {HMAC_SHA512 HMAC_SHA1} +db close +file delete -force test.db + +# verify the pragma cipher_kdf_algorithm works properly +do_test verify-pragma-cipher-kdf-algorithm-reports-default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_kdf_algorithm; + } +} {ok PBKDF2_HMAC_SHA512} +db close +file delete -force test.db + +do_test verify-pragma-cipher-kdf-algorithm-reports-value-changed { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1; + PRAGMA cipher_kdf_algorithm; + } +} {ok PBKDF2_HMAC_SHA1} +db close +file delete -force test.db + +do_test verify-pragma-cipher-default-kdf-algorithm { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_kdf_algorithm; + PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA1; + PRAGMA cipher_default_kdf_algorithm; + PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA512; + } +} {PBKDF2_HMAC_SHA512 PBKDF2_HMAC_SHA1} +db close +file delete -force test.db + +if_built_with_openssl verify-default-cipher { + sqlite_orig db test.db + execsql { + PRAGMA key='test'; + PRAGMA cipher; + } +} {ok AES-256-CBC} +db close +file delete -force test.db + +if_built_with_libtomcrypt verify-default-cipher { + sqlite_orig db test.db + execsql { + PRAGMA key='test'; + PRAGMA cipher; + } +} {ok aes-256-cbc} +db close +file delete -force test.db + +if_built_with_commoncrypto verify-default-cipher { + sqlite_orig db test.db + execsql { + PRAGMA key='test'; + PRAGMA cipher; + } +} {ok aes-256-cbc} +db close +file delete -force test.db + +if_built_with_nss verify-default-cipher { + sqlite_orig db test.db + execsql { + PRAGMA key='test'; + PRAGMA cipher; + } +} {ok aes-256-cbc} +db close +file delete -force test.db + +do_test verify-cipher_settings_default { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_settings; + } +} {ok {PRAGMA kdf_iter = 256000;} {PRAGMA cipher_page_size = 4096;} {PRAGMA cipher_use_hmac = 1;} {PRAGMA cipher_plaintext_header_size = 0;} {PRAGMA cipher_hmac_algorithm = HMAC_SHA512;} {PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA512;}} +db close +file delete -force test.db + +do_test verify-cipher_settings_v1 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test'; + PRAGMA cipher_compatibility = 1; + PRAGMA cipher_settings; + } +} {ok {PRAGMA kdf_iter = 4000;} {PRAGMA cipher_page_size = 1024;} {PRAGMA cipher_use_hmac = 0;} {PRAGMA cipher_plaintext_header_size = 0;} {PRAGMA cipher_hmac_algorithm = HMAC_SHA1;} {PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;}} +db close +file delete -force test.db + +do_test verify-cipher_default_settings_v1 { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_compatibility = 1; + PRAGMA cipher_default_settings; + PRAGMA cipher_default_compatibility = 4; + } +} {{PRAGMA cipher_default_kdf_iter = 4000;} {PRAGMA cipher_default_page_size = 1024;} {PRAGMA cipher_default_use_hmac = 0;} {PRAGMA cipher_default_plaintext_header_size = 0;} {PRAGMA cipher_default_hmac_algorithm = HMAC_SHA1;} {PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA1;}} +db close +file delete -force test.db + +do_test verify-cipher_default_settings_default { + sqlite_orig db test.db + execsql { + PRAGMA cipher_default_settings; + } +} {{PRAGMA cipher_default_kdf_iter = 256000;} {PRAGMA cipher_default_page_size = 4096;} {PRAGMA cipher_default_use_hmac = 1;} {PRAGMA cipher_default_plaintext_header_size = 0;} {PRAGMA cipher_default_hmac_algorithm = HMAC_SHA512;} {PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA512;}} +db close +file delete -force test.db + +do_test verify-cipher_log_source { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_log_source; -- default should be ANY + PRAGMA cipher_log_source = NONE; --reset to NONE + PRAGMA cipher_log_source = PROVIDER; -- add PROVIDER to log source + PRAGMA cipher_log_source = CORE; -- add CORE to log source + PRAGMA cipher_log_source = MEMORY; -- add MEMORY to log source + PRAGMA cipher_log_source = NOTASOURCE; -- stay the same + PRAGMA cipher_log_source = ANY; -- reset to ANY + } +} {ANY NONE PROVIDER {CORE PROVIDER} {CORE MEMORY PROVIDER} {CORE MEMORY PROVIDER} ANY} +db close + +do_test verify-cipher_log_level { + sqlite_orig db :memory: + execsql { + PRAGMA cipher_log_level; -- default should be WARN + PRAGMA cipher_log_level = TRACE; + PRAGMA cipher_log_level = DEBUG; + PRAGMA cipher_log_level = INFO; + PRAGMA cipher_log_level = WARN; + PRAGMA cipher_log_level = ERROR; + PRAGMA cipher_log_level = NOTALEVEL; -- an unknown level should set back to none + } +} {WARN TRACE DEBUG INFO WARN ERROR NONE} +db close + +finish_test diff --git a/test/sqlcipher-rekey.test b/test/sqlcipher-rekey.test new file mode 100644 index 0000000000..267b890777 --- /dev/null +++ b/test/sqlcipher-rekey.test @@ -0,0 +1,275 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl + +# Test rekey as first operation on an empty database +# it should raise an error +do_test rekey-as-first-op-on-empty { + sqlite_orig db test.db + + catchsql { + PRAGMA rekey = 'testkey'; + } + +} {1 {An error occurred with PRAGMA key or rekey. PRAGMA key requires a key of one or more characters. PRAGMA rekey can only be run on an existing encrypted database. Use sqlcipher_export() and ATTACH to convert encrypted/plaintext databases.}} +db close +file delete -force test.db + +# test a rekey operation as the first op on an existing database +# then test that now the new key opens the database +# now close database re-open with new key +setup test.db "'testkey'" +do_test rekey-as-first-operation { + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkey'; + PRAGMA rekey = 'testkeynew'; + } + db close + + sqlite_orig db test.db + execsql { + PRAGMA key = 'testkeynew'; + SELECT name FROM sqlite_schema WHERE type='table'; + } +} {ok t1} +db close +file delete -force test.db + +# create a new database, insert some data +# then rekey it with the same password +do_test rekey-same-passkey { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test123'; + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + SELECT count(*) FROM t1; + PRAGMA rekey = 'test123'; + SELECT count(*) FROM t1; + } +} {1000 ok 1000} +db close +file delete -force test.db + +# create a new database, insert some data +# then rekey it. Make sure it is immediately +# readable. Then close it and make sure it can be +# read back +do_test rekey-and-query-1 { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test123'; + CREATE TABLE t1(a,b); + BEGIN; + } + + for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*500000)}] + execsql "INSERT INTO t1 VALUES($i,'value $r');" + } + + execsql { + COMMIT; + SELECT count(*) FROM t1; + PRAGMA rekey = 'test321'; + SELECT count(*) FROM t1; + } +} {1000 ok 1000} + +db close + +do_test rekey-and-query-2 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test321'; + SELECT count(*) FROM t1; + } +} {ok 1000} +db close +file delete -force test.db + +# create a new database, insert some data +# delete about 50% of the data +# write some new data +# delete another 50% +# then rekey it. Make sure it is immediately +# readable. Then close it and make sure it can be +# read back. This test will ensure that Secure Delete +# is enabled and all pages are being written and are not +# being optimized out by sqlite3PagerDontWrite +do_test rekey-delete-and-query-1 { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test123'; + CREATE TABLE t1(a,b); + CREATE INDEX ta_a ON t1(a); + BEGIN; + } + + for {set i 1} {$i<1000} {incr i} { + set r1 [expr {int(rand()*32767)}] + execsql "INSERT INTO t1 VALUES($i,$r1);" + } + + execsql "DELETE FROM t1 WHERE a < 500;" + + set r1 [expr {int(rand()*32767)}] + execsql "UPDATE t1 SET b = $r1 WHERE a < 750;" + + execsql "DELETE FROM t1 WHERE a > 750;" + + execsql { + COMMIT; + SELECT (count(*) > 0) FROM t1; + } +} {1} +db close + +do_test rekey-delete-and-query-2 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test123'; + PRAGMA rekey = 'test321'; + SELECT count(*) > 1 FROM t1; + PRAGMA integrity_check; + } +} {ok ok 1 ok} +db close + +do_test rekey-delete-and-query-3 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test321'; + SELECT count(*) > 1 FROM t1; + } +} {ok 1} +db close +file delete -force test.db + + +# same as previous test, but use WAL +do_test rekey-delete-and-query-wal-1 { + sqlite_orig db test.db + + execsql { + PRAGMA key = 'test123'; + PRAGMA journal_mode = WAL; + CREATE TABLE t1(a,b); + CREATE INDEX ta_a ON t1(a); + BEGIN; + } + + for {set i 1} {$i<1000} {incr i} { + set r1 [expr {int(rand()*32767)}] + execsql "INSERT INTO t1 VALUES($i,$r1);" + } + + execsql "DELETE FROM t1 WHERE a < 500;" + + set r1 [expr {int(rand()*32767)}] + execsql "UPDATE t1 SET b = $r1 WHERE a < 750;" + + execsql "DELETE FROM t1 WHERE a > 750;" + + execsql { + COMMIT; + SELECT (count(*) > 0) FROM t1; + } +} {1} +db close + +do_test rekey-delete-and-query-wal-2 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test123'; + PRAGMA journal_mode = WAL; + PRAGMA rekey = 'test321'; + SELECT count(*) > 1 FROM t1; + PRAGMA integrity_check; + } +} {ok wal ok 1 ok} +db close + +do_test rekey-delete-and-query-wal-3 { + sqlite_orig db test.db + execsql { + PRAGMA key = 'test321'; + PRAGMA journal_mode = WAL; + SELECT count(*) > 1 FROM t1; + } +} {ok wal 1} +db close +file delete -force test.db + +do_test rekey-database-by-name { + sqlite_orig db test.db + execsql { + attach database 'new.db' as new; + pragma new.key = 'foo'; + create table new.t1(a,b); + insert into new.t1(a,b) values('foo', 'bar'); + pragma new.rekey = 'bar'; + detach database new; + } + db close + + sqlite_orig db new.db + execsql { + pragma key = 'bar'; + select * from t1; + } +} {ok foo bar} +db close +file delete -force test.db +file delete -force new.db + +finish_test diff --git a/test/sqlcipher-template.test b/test/sqlcipher-template.test new file mode 100644 index 0000000000..4372ee4910 --- /dev/null +++ b/test/sqlcipher-template.test @@ -0,0 +1,41 @@ +# SQLCipher +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + +#set testdir [file dirname $argv0] +#source $testdir/tester.tcl +#source $testdir/sqlcipher.tcl + +#finish_test diff --git a/test/sqlcipher.tcl b/test/sqlcipher.tcl new file mode 100644 index 0000000000..d058d366ba --- /dev/null +++ b/test/sqlcipher.tcl @@ -0,0 +1,113 @@ +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + + +file delete -force test.db +file delete -force test2.db +file delete -force test3.db +file delete -force test4.db + +set testdir [file dirname $argv0] +set sampleDir [file normalize [file dirname [file dirname $argv0]]]/sqlcipher-resources + +# If the library is not compiled with has_codec support then +# skip all tests in this file. +if {![sqlite_orig -has-codec]} { + finish_test + return +} + +proc setup {file key} { + sqlite_orig db $file + execsql "PRAGMA key=$key;" + execsql { + CREATE table t1(a,b); + INSERT INTO t1 VALUES ('test1', 'test2'); + } db + db close +} + +proc get_cipher_provider {} { + sqlite_orig db test.db + return [execsql { + PRAGMA key = 'test'; + PRAGMA cipher_provider; + }]; +} + +proc if_built_with_openssl {name cmd expected} { + if {[get_cipher_provider] == "openssl"} { + do_test $name $cmd $expected + } +} + +proc if_built_with_libtomcrypt {name cmd expected} { + if {[get_cipher_provider] == "libtomcrypt"} { + do_test $name $cmd $expected + } +} + +proc if_built_with_commoncrypto {name cmd expected} { + if {[get_cipher_provider] == "commoncrypto"} { + do_test $name $cmd $expected + } +} + +proc if_built_with_nss {name cmd expected} { + if {[get_cipher_provider] == "nss"} { + do_test $name $cmd $expected + } +} + +proc cmpFilesChunked {file1 file2 {chunksize 16384}} { + set f1 [open $file1]; fconfigure $f1 -translation binary + set f2 [open $file2]; fconfigure $f2 -translation binary + while {1} { + set d1 [read $f1 $chunksize] + set d2 [read $f2 $chunksize] + set diff [string compare $d1 $d2] + if {$diff != 0 || [eof $f1] || [eof $f2]} { + close $f1; close $f2 + return $diff + } + } + return 0 +} + +proc trace_proc sql { + global TRACE_OUT + lappend TRACE_OUT [string trim $sql] +} + diff --git a/test/sqlcipher.test b/test/sqlcipher.test new file mode 100644 index 0000000000..88ab1b5c4c --- /dev/null +++ b/test/sqlcipher.test @@ -0,0 +1,64 @@ +# codec.test developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2018, ZETETIC LLC +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# This file implements regression tests for SQLite library. The +# focus of this script is testing code cipher features. +# +# NOTE: tester.tcl has overridden the definition of sqlite3 to +# automatically pass in a key value. Thus tests in this file +# should explicitly close and open db with sqlite_orig in order +# to bypass default key assignment. + + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/sqlcipher.tcl +source $testdir/permutations.test + +set pretests "" + +sqlite_orig db :memory: +execsql { + PRAGMA cipher_log = 'sqlcipher-test.log'; +} +db close + +test_suite "sqlcipher" -prefix "" -description { + Runs SQLCipher tests +} -files [ + test_set $pretests \ + sqlcipher-core.test \ + sqlcipher-compatibility.test \ + sqlcipher-rekey.test \ + sqlcipher-plaintext-header.test \ + sqlcipher-pragmas.test \ + sqlcipher-integrity.test \ + sqlcipher-codecerror.test \ + sqlcipher-backup.test +] +run_test_suite sqlcipher +finish_test diff --git a/test/tester.tcl b/test/tester.tcl index b5f49ebde9..07cf67fa8a 100644 --- a/test/tester.tcl +++ b/test/tester.tcl @@ -1328,6 +1328,12 @@ proc finalize_testing {} { incr nErr } } + # BEGIN SQLCIPHER + # prior to calculating malloc stats, call sqlite3_shutdown to invoke + # sqlcipher_extra_shutdown() to release private heap memory if all + # private allocations have been freed + sqlite3_shutdown + # END SQLCIPHER if {[lindex [sqlite3_status SQLITE_STATUS_MALLOC_COUNT 0] 1]>0 || [sqlite3_memory_used]>0} { output2 "Unfreed memory: [sqlite3_memory_used] bytes in\ diff --git a/tool/crypto-speedtest.tcl b/tool/crypto-speedtest.tcl new file mode 100755 index 0000000000..fa923a0102 --- /dev/null +++ b/tool/crypto-speedtest.tcl @@ -0,0 +1,287 @@ +#!/usr/bin/tclsh +# +# SQLite Cipher +# codec-speedtest.tcl developed by Stephen Lombardo (Zetetic LLC) +# sjlombardo at zetetic dot net +# http://zetetic.net +# +# Copyright (c) 2008, ZETETIC LLC +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# * Neither the name of the ZETETIC LLC nor the +# names of its contributors may be used to endorse or promote products +# derived from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +# Run this script using TCLSH to do a speed comparison between +# a single version of sqlite running with and without the codec +# based heavily on tools/speetest.tcl in the standard SQLite package +# + +# Run a test +# +set cnt 1 +proc runtest {title} { + global cnt + set sqlfile test$cnt.sql + set logfile test$cnt.log + puts "

Test $cnt: $title

" + incr cnt + set fd [open $sqlfile r] + set sql [string trim [read $fd [file size $sqlfile]]] + close $fd + set sx [split $sql \n] + set n [llength $sx] + if {$n>8} { + set sql {} + for {set i 0} {$i<3} {incr i} {append sql [lindex $sx $i]
\n} + append sql "... [expr {$n-6}] lines omitted
\n" + for {set i [expr {$n-3}]} {$i<$n} {incr i} { + append sql [lindex $sx $i]
\n + } + } else { + regsub -all \n [string trim $sql]
sql + } + puts "
" + puts "$sql" + puts "
" + set format {} + set delay 10 + + exec sync; after $delay; + set t [time "exec cat perftest0.sql $sqlfile | ./sqlite3 perftest0.db 2>&1" 1] + set t [expr {[lindex $t 0]/1000000.0}] + puts [format $format {Config0:} $t] + exec sync; after $delay; + + set t0 $t; + + set t [time "exec cat perftest1.sql $sqlfile | ./sqlite3 perftest1.db 2>&1" 1] + set t [expr {[lindex $t 0]/1000000.0}] + puts [format $format {Config1:} $t] + exec sync; after $delay; + + set slowdown [expr {(($t - $t0)/$t0)*100.0}] + puts [format $format {Slowdown:} $slowdown] + + puts "
%s   %.3f
" +} + +# Initialize the environment +# + +file delete perftest0.db +file delete perftest1.db + +expr srand(1) +catch {exec /bin/sh -c {rm -f perftest*.db}} + +set fd [open perftest0.sql w] +puts $fd { +} +close $fd + +set fd [open perftest1.sql w] +puts $fd { +PRAGMA key='xyzzy'; +} +close $fd + +exec cat perftest0.sql | ./sqlite3 perftest0.db +exec cat perftest1.sql | ./sqlite3 perftest1.db + +set ones {zero one two three four five six seven eight nine + ten eleven twelve thirteen fourteen fifteen sixteen seventeen + eighteen nineteen} +set tens {{} ten twenty thirty forty fifty sixty seventy eighty ninety} +proc number_name {n} { + if {$n>=1000} { + set txt "[number_name [expr {$n/1000}]] thousand" + set n [expr {$n%1000}] + } else { + set txt {} + } + if {$n>=100} { + append txt " [lindex $::ones [expr {$n/100}]] hundred" + set n [expr {$n%100}] + } + if {$n>=20} { + append txt " [lindex $::tens [expr {$n/10}]]" + set n [expr {$n%10}] + } + if {$n>0} { + append txt " [lindex $::ones $n]" + } + set txt [string trim $txt] + if {$txt==""} {set txt zero} + return $txt +} + + + +set fd [open test$cnt.sql w] +puts $fd "CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));" +for {set i 1} {$i<=1000} {incr i} { + set r [expr {int(rand()*100000)}] + puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');" +} +close $fd +runtest {1000 INSERTs} + + + +set fd [open test$cnt.sql w] +puts $fd "BEGIN;" +puts $fd "CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100));" +for {set i 1} {$i<=25000} {incr i} { + set r [expr {int(rand()*500000)}] + puts $fd "INSERT INTO t2 VALUES($i,$r,'[number_name $r]');" +} +puts $fd "COMMIT;" +close $fd +runtest {25000 INSERTs in a transaction} + + + +set fd [open test$cnt.sql w] +for {set i 0} {$i<100} {incr i} { + set lwr [expr {$i*100}] + set upr [expr {($i+10)*100}] + puts $fd "SELECT count(*), avg(b) FROM t2 WHERE b>=$lwr AND b<$upr;" +} +close $fd +runtest {100 SELECTs without an index} + + + +set fd [open test$cnt.sql w] +for {set i 1} {$i<=100} {incr i} { + puts $fd "SELECT count(*), avg(b) FROM t2 WHERE c LIKE '%[number_name $i]%';" +} +close $fd +runtest {100 SELECTs on a string comparison} + + + +set fd [open test$cnt.sql w] +puts $fd {CREATE INDEX i2a ON t2(a);} +puts $fd {CREATE INDEX i2b ON t2(b);} +close $fd +runtest {Creating an index} + + + +set fd [open test$cnt.sql w] +for {set i 0} {$i<5000} {incr i} { + set lwr [expr {$i*100}] + set upr [expr {($i+1)*100}] + puts $fd "SELECT count(*), avg(b) FROM t2 WHERE b>=$lwr AND b<$upr;" +} +close $fd +runtest {5000 SELECTs with an index} + + + +set fd [open test$cnt.sql w] +puts $fd "BEGIN;" +for {set i 0} {$i<1000} {incr i} { + set lwr [expr {$i*10}] + set upr [expr {($i+1)*10}] + puts $fd "UPDATE t1 SET b=b*2 WHERE a>=$lwr AND a<$upr;" +} +puts $fd "COMMIT;" +close $fd +runtest {1000 UPDATEs without an index} + + + +set fd [open test$cnt.sql w] +puts $fd "BEGIN;" +for {set i 1} {$i<=25000} {incr i} { + set r [expr {int(rand()*500000)}] + puts $fd "UPDATE t2 SET b=$r WHERE a=$i;" +} +puts $fd "COMMIT;" +close $fd +runtest {25000 UPDATEs with an index} + + +set fd [open test$cnt.sql w] +puts $fd "BEGIN;" +for {set i 1} {$i<=25000} {incr i} { + set r [expr {int(rand()*500000)}] + puts $fd "UPDATE t2 SET c='[number_name $r]' WHERE a=$i;" +} +puts $fd "COMMIT;" +close $fd +runtest {25000 text UPDATEs with an index} + + + +set fd [open test$cnt.sql w] +puts $fd "BEGIN;" +puts $fd "INSERT INTO t1 SELECT * FROM t2;" +puts $fd "INSERT INTO t2 SELECT * FROM t1;" +puts $fd "COMMIT;" +close $fd +runtest {INSERTs from a SELECT} + + + +set fd [open test$cnt.sql w] +puts $fd {DELETE FROM t2 WHERE c LIKE '%fifty%';} +close $fd +runtest {DELETE without an index} + + + +set fd [open test$cnt.sql w] +puts $fd {DELETE FROM t2 WHERE a>10 AND a<20000;} +close $fd +runtest {DELETE with an index} + + + +set fd [open test$cnt.sql w] +puts $fd {INSERT INTO t2 SELECT * FROM t1;} +close $fd +runtest {A big INSERT after a big DELETE} + + + +set fd [open test$cnt.sql w] +puts $fd {BEGIN;} +puts $fd {DELETE FROM t1;} +for {set i 1} {$i<=3000} {incr i} { + set r [expr {int(rand()*100000)}] + puts $fd "INSERT INTO t1 VALUES($i,$r,'[number_name $r]');" +} +puts $fd {COMMIT;} +close $fd +runtest {A big DELETE followed by many small INSERTs} + + + +set fd [open test$cnt.sql w] +puts $fd {DROP TABLE t1;} +puts $fd {DROP TABLE t2;} +close $fd +runtest {DROP TABLE} + diff --git a/tool/mkctimec.tcl b/tool/mkctimec.tcl index 69d25c678e..276b833824 100755 --- a/tool/mkctimec.tcl +++ b/tool/mkctimec.tcl @@ -283,6 +283,7 @@ set boolean_defnil_options { SQLITE_VDBE_COVERAGE SQLITE_WIN32_MALLOC SQLITE_ZERO_MALLOC + SQLITE_HAS_CODEC } # All compile time options for which the assigned value is other than boolean diff --git a/tool/mkpragmatab.tcl b/tool/mkpragmatab.tcl index 8a0b5c6fa6..32fa442e58 100644 --- a/tool/mkpragmatab.tcl +++ b/tool/mkpragmatab.tcl @@ -384,6 +384,36 @@ set pragma_def { COLS: database status IF: defined(SQLITE_DEBUG) || defined(SQLITE_TEST) + NAME: key + TYPE: KEY + ARG: 0 + IF: defined(SQLITE_HAS_CODEC) + + NAME: rekey + TYPE: KEY + ARG: 1 + IF: defined(SQLITE_HAS_CODEC) + + NAME: hexkey + TYPE: KEY + ARG: 2 + IF: defined(SQLITE_HAS_CODEC) + + NAME: hexrekey + TYPE: KEY + ARG: 3 + IF: defined(SQLITE_HAS_CODEC) + + NAME: textkey + TYPE: KEY + ARG: 4 + IF: defined(SQLITE_HAS_CODEC) + + NAME: textrekey + TYPE: KEY + ARG: 5 + IF: defined(SQLITE_HAS_CODEC) + NAME: activate_extensions IF: defined(SQLITE_ENABLE_CEROD) diff --git a/tool/mksqlite3c.tcl b/tool/mksqlite3c.tcl index 1d0f892363..5ba67c3cf4 100644 --- a/tool/mksqlite3c.tcl +++ b/tool/mksqlite3c.tcl @@ -165,6 +165,7 @@ close $in # text of the file in-line. The file only needs to be included once. # foreach hdr { + sqlcipher.h btree.h btreeInt.h fts3.h @@ -434,6 +435,12 @@ set flist { vdbevtab.c memjournal.c + sqlcipher.c + crypto_libtomcrypt.c + crypto_nss.c + crypto_openssl.c + crypto_cc.c + walker.c resolve.c expr.c @@ -487,6 +494,7 @@ set flist { json.c rtree.c icu.c + fts3_icu.c sqlite3rbu.c dbstat.c