From 64ded9c58d531da53a48823c0ec608f8cc1f66fe Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Thu, 4 Oct 2018 18:44:23 -0700 Subject: [PATCH 01/20] installing pkg-config --- .cirrus.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.cirrus.yml b/.cirrus.yml index 31d596d..dc83b5d 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -2,4 +2,5 @@ task: name: macOS osx_instance: image: high-sierra-xcode-9.4.1 + install_pkg-config_script: brew install pkg-config test_script: make test From 3859f375ac45b076b9b66baff16320739aa6f0d9 Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Thu, 4 Oct 2018 21:55:09 -0700 Subject: [PATCH 02/20] downloading git submodules --- .cirrus.yml | 1 + sqlite-crypto-vfs.c | 4 ++-- sqlite-crypto-vfs.h | 2 +- test.cpp | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index dc83b5d..44f22bf 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -2,5 +2,6 @@ task: name: macOS osx_instance: image: high-sierra-xcode-9.4.1 + git_submodule_update_init_script: git submodule update --init --recursive install_pkg-config_script: brew install pkg-config test_script: make test diff --git a/sqlite-crypto-vfs.c b/sqlite-crypto-vfs.c index f25ed9e..2e18dfe 100644 --- a/sqlite-crypto-vfs.c +++ b/sqlite-crypto-vfs.c @@ -109,7 +109,7 @@ static sqlite3_io_methods sqlite_crypto_io_methods = { sqlite_crypto_io_unfetch, /* xUnfetch */ }; -int sqlite_crypto_vfs_register(uint8_t key[32], uint8_t initialization_vector[16], const char* vfs_name, int make_default) +int sqlite_crypto_vfs_register(const uint8_t key[32], const uint8_t initialization_vector[16], const char* vfs_name, const int make_default) { sqlite3_vfs* root_vfs = sqlite3_vfs_find(NULL); if (!root_vfs) { @@ -119,7 +119,7 @@ int sqlite_crypto_vfs_register(uint8_t key[32], uint8_t initialization_vector[16 crypto_vfs.pVfs = root_vfs; crypto_vfs.base.szOsFile = sizeof(Crypto_File) + root_vfs->szOsFile; aes_key_setup(key, crypto_vfs.key_schedule, 256); - memcpy(initialization_vector, crypto_vfs.initialization_vector, sizeof(uint8_t) * 16); + memcpy(crypto_vfs.initialization_vector, initialization_vector, sizeof(uint8_t) * 16); int result = sqlite3_vfs_register(&crypto_vfs.base, make_default); return result; } diff --git a/sqlite-crypto-vfs.h b/sqlite-crypto-vfs.h index e82e413..693750e 100644 --- a/sqlite-crypto-vfs.h +++ b/sqlite-crypto-vfs.h @@ -2,6 +2,6 @@ #include -int sqlite_crypto_vfs_register(uint8_t key[32], uint8_t initialization_vector[16], const char* vfs_name, int make_default); +int sqlite_crypto_vfs_register(const uint8_t key[32], const uint8_t initialization_vector[16], const char* vfs_name, const int make_default); #endif //_SQLITE_CRYPTO_VFS_H_ diff --git a/test.cpp b/test.cpp index 2062100..9da7079 100644 --- a/test.cpp +++ b/test.cpp @@ -18,10 +18,10 @@ int main (int argc, char *argv[]) char *zErrMsg = NULL; int rc; - uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, + const uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 }; - uint8_t initialization_vector[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; + const uint8_t initialization_vector[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; const char* vfs_name = "sqlite-crypto"; rc = sqlite_crypto_vfs_register(key, initialization_vector, vfs_name, 0); From 755fc50383dc7f29cfb1cbe10b12a20fdaacaefa Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Thu, 4 Oct 2018 23:39:17 -0700 Subject: [PATCH 03/20] adding support for Swift --- .cirrus.yml | 1 + sqlite-crypto-vfs.c | 13 +- sqlite-crypto-vfs.h | 3 +- swift/.gitignore | 4 + swift/Package.swift | 41 +++++ swift/README.md | 3 + swift/Sources/CSQLiteCryptoVFS/aes.c | 1 + swift/Sources/CSQLiteCryptoVFS/aes.h | 1 + .../include/sqlite-crypto-vfs.h | 1 + .../CSQLiteCryptoVFS/sqlite-crypto-vfs.c | 1 + .../SQLiteCryptoVFS/SQLiteCryptoVFS.swift | 102 +++++++++++ swift/Tests/LinuxMain.swift | 7 + .../SQLiteCryptoVFSTests.swift | 167 ++++++++++++++++++ .../XCTestManifests.swift | 9 + test.cpp | 4 +- 15 files changed, 351 insertions(+), 7 deletions(-) create mode 100644 swift/.gitignore create mode 100644 swift/Package.swift create mode 100644 swift/README.md create mode 120000 swift/Sources/CSQLiteCryptoVFS/aes.c create mode 120000 swift/Sources/CSQLiteCryptoVFS/aes.h create mode 120000 swift/Sources/CSQLiteCryptoVFS/include/sqlite-crypto-vfs.h create mode 120000 swift/Sources/CSQLiteCryptoVFS/sqlite-crypto-vfs.c create mode 100644 swift/Sources/SQLiteCryptoVFS/SQLiteCryptoVFS.swift create mode 100644 swift/Tests/LinuxMain.swift create mode 100644 swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift create mode 100644 swift/Tests/SQLiteCryptoVFSTests/XCTestManifests.swift diff --git a/.cirrus.yml b/.cirrus.yml index 44f22bf..78df3fb 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -5,3 +5,4 @@ task: git_submodule_update_init_script: git submodule update --init --recursive install_pkg-config_script: brew install pkg-config test_script: make test + swift_test_script: cd swift; swift build && swift test diff --git a/sqlite-crypto-vfs.c b/sqlite-crypto-vfs.c index 2e18dfe..f038419 100644 --- a/sqlite-crypto-vfs.c +++ b/sqlite-crypto-vfs.c @@ -62,13 +62,19 @@ static int sqlite_crypto_io_unfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p) void sqlite_crypto_debug(const void* buffer, int count); +#define SQLITE_CRYPTO_VFS_NAME ("sqlite-crypto") + +const char* sqlite_crypto_vfs_name() { + return SQLITE_CRYPTO_VFS_NAME; +} + static Crypto_VFS crypto_vfs = { { 1, /* iVersion */ 0, /* szOsFile (set by register_vlog()) */ 1024, /* mxPathname */ 0, /* pNext */ - "sqlite-crypto", /* zName */ + SQLITE_CRYPTO_VFS_NAME, /* zName */ 0, /* pAppData */ crypto_vfs_open, /* xOpen */ crypto_vfs_delete, /* xDelete */ @@ -109,13 +115,12 @@ static sqlite3_io_methods sqlite_crypto_io_methods = { sqlite_crypto_io_unfetch, /* xUnfetch */ }; -int sqlite_crypto_vfs_register(const uint8_t key[32], const uint8_t initialization_vector[16], const char* vfs_name, const int make_default) +int sqlite_crypto_vfs_register(const uint8_t key[32], const uint8_t initialization_vector[16], const int make_default) { sqlite3_vfs* root_vfs = sqlite3_vfs_find(NULL); if (!root_vfs) { return SQLITE_NOTFOUND; } - crypto_vfs.base.zName = vfs_name; crypto_vfs.pVfs = root_vfs; crypto_vfs.base.szOsFile = sizeof(Crypto_File) + root_vfs->szOsFile; aes_key_setup(key, crypto_vfs.key_schedule, 256); @@ -134,7 +139,7 @@ static int crypto_vfs_open( Crypto_VFS *pCrypto_VFS = (Crypto_VFS*) pVfs; Crypto_File *pCrypto_File = (Crypto_File*) pFile; pCrypto_File->pFile = (sqlite3_file*) &pCrypto_File[1]; - memcpy(&(pCrypto_File->key_schedule), &(pCrypto_VFS->key_schedule), sizeof(WORD) * 60); + memcpy(&(pCrypto_File->key_schedule), &(pCrypto_VFS->key_schedule), sizeof(uint32_t) * 60); memcpy(&(pCrypto_File->initialization_vector), &(pCrypto_VFS->initialization_vector), sizeof(uint8_t) * 16); int rc = REALVFS(pVfs)->xOpen(REALVFS(pVfs), zName, pCrypto_File->pFile, flags, pOutFlags); if (rc == SQLITE_OK) { diff --git a/sqlite-crypto-vfs.h b/sqlite-crypto-vfs.h index 693750e..6492ab0 100644 --- a/sqlite-crypto-vfs.h +++ b/sqlite-crypto-vfs.h @@ -2,6 +2,7 @@ #include -int sqlite_crypto_vfs_register(const uint8_t key[32], const uint8_t initialization_vector[16], const char* vfs_name, const int make_default); +const char* sqlite_crypto_vfs_name(); +int sqlite_crypto_vfs_register(const uint8_t key[32], const uint8_t initialization_vector[16], const int make_default); #endif //_SQLITE_CRYPTO_VFS_H_ diff --git a/swift/.gitignore b/swift/.gitignore new file mode 100644 index 0000000..02c0875 --- /dev/null +++ b/swift/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj diff --git a/swift/Package.swift b/swift/Package.swift new file mode 100644 index 0000000..5955efb --- /dev/null +++ b/swift/Package.swift @@ -0,0 +1,41 @@ +// swift-tools-version:4.1 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "SQLiteCryptoVFS", + products: [ + // Products define the executables and libraries produced by a package, and make them visible to other packages. + .library( + name: "SQLiteCryptoVFS", + targets: ["SQLiteCryptoVFS"] + ), + .library( + name: "CSQLiteCryptoVFS", + targets: ["CSQLiteCryptoVFS"] + ), + ], + dependencies: [ + // Dependencies declare other packages that this package depends on. + // .package(url: /* package url */, from: "1.0.0"), + ], + targets: [ + // Targets are the basic building blocks of a package. A target can define a module or a test suite. + // Targets can depend on other targets in this package, and on products in packages which this package depends on. + .target( + name: "SQLiteCryptoVFS", + dependencies: [ + "CSQLiteCryptoVFS" + ] + ), + .target( + name: "CSQLiteCryptoVFS", + dependencies: [] + ), + .testTarget( + name: "SQLiteCryptoVFSTests", + dependencies: ["SQLiteCryptoVFS"] + ), + ] +) diff --git a/swift/README.md b/swift/README.md new file mode 100644 index 0000000..c88b3b8 --- /dev/null +++ b/swift/README.md @@ -0,0 +1,3 @@ +# swift + +A description of this package. diff --git a/swift/Sources/CSQLiteCryptoVFS/aes.c b/swift/Sources/CSQLiteCryptoVFS/aes.c new file mode 120000 index 0000000..77dd1d9 --- /dev/null +++ b/swift/Sources/CSQLiteCryptoVFS/aes.c @@ -0,0 +1 @@ +../../../aes.c \ No newline at end of file diff --git a/swift/Sources/CSQLiteCryptoVFS/aes.h b/swift/Sources/CSQLiteCryptoVFS/aes.h new file mode 120000 index 0000000..332c6ef --- /dev/null +++ b/swift/Sources/CSQLiteCryptoVFS/aes.h @@ -0,0 +1 @@ +../../../aes.h \ No newline at end of file diff --git a/swift/Sources/CSQLiteCryptoVFS/include/sqlite-crypto-vfs.h b/swift/Sources/CSQLiteCryptoVFS/include/sqlite-crypto-vfs.h new file mode 120000 index 0000000..20537b5 --- /dev/null +++ b/swift/Sources/CSQLiteCryptoVFS/include/sqlite-crypto-vfs.h @@ -0,0 +1 @@ +../../../../sqlite-crypto-vfs.h \ No newline at end of file diff --git a/swift/Sources/CSQLiteCryptoVFS/sqlite-crypto-vfs.c b/swift/Sources/CSQLiteCryptoVFS/sqlite-crypto-vfs.c new file mode 120000 index 0000000..1a97ec6 --- /dev/null +++ b/swift/Sources/CSQLiteCryptoVFS/sqlite-crypto-vfs.c @@ -0,0 +1 @@ +../../../sqlite-crypto-vfs.c \ No newline at end of file diff --git a/swift/Sources/SQLiteCryptoVFS/SQLiteCryptoVFS.swift b/swift/Sources/SQLiteCryptoVFS/SQLiteCryptoVFS.swift new file mode 100644 index 0000000..cf240d7 --- /dev/null +++ b/swift/Sources/SQLiteCryptoVFS/SQLiteCryptoVFS.swift @@ -0,0 +1,102 @@ +import Foundation +import CSQLiteCryptoVFS + +public var vfsName: String { + return String(cString: sqlite_crypto_vfs_name()) +} + +public func registerVFS( + key: Data, + initializationVector: Data, + makeDefault: Bool = false +) throws -> Int32 { + return try registerVFS(key: [UInt8](key), initializationVector: [UInt8](initializationVector), makeDefault: makeDefault) +} + +public func registerVFS( + key: [UInt8], + initializationVector: [UInt8], + makeDefault: Bool = false +) throws -> Int32 { + guard key.count == 32 else { + throw SQLiteCryptoVFSError.invalidKey(key: key) + } + guard initializationVector.count == 16 else { + throw SQLiteCryptoVFSError.invalidInitializationVector(initializationVector: initializationVector) + } + let result = sqlite_crypto_vfs_register(key, initializationVector, makeDefault ? 1 : 0) + return result +} + +public enum SQLiteCryptoVFSError: Error { + + case invalidKey(key: [UInt8]) + case invalidInitializationVector(initializationVector: [UInt8]) + + public var localizedDescription: String { + return "\(failureReason!). \(recoverySuggestion!)." + } + +} + +extension SQLiteCryptoVFSError: LocalizedError { + + public var errorDescription: String? { + return localizedDescription + } + + public var failureReason: String? { + switch self { + case .invalidKey(let key): + return "Key \(key.hexDebugDescription) is not valid" + case .invalidInitializationVector(let initializationVector): + return "Initialization Vector \(initializationVector.hexDebugDescription) is not valid" + } + } + + public var recoverySuggestion: String? { + switch self { + case .invalidKey: + return "Key must have 32 bytes" + case .invalidInitializationVector: + return "Initialization Vector must have 16 bytes" + } + } + + public var helpAnchor: String? { + return localizedDescription + } + +} + +extension SQLiteCryptoVFSError: CustomStringConvertible { + + public var description: String { + return localizedDescription + } + +} + +extension SQLiteCryptoVFSError: CustomDebugStringConvertible { + + public var debugDescription: String { + return localizedDescription + } + +} + +extension Array where Element == UInt8 { + + var hexDescription: String { + let str = self.map { (byte) -> String in + let str = String(byte, radix: 16) + return str.count < 2 ? "0\(str)" : str + } + return "[\(str.joined(separator: " "))]" + } + + var hexDebugDescription: String { + return "\(hexDescription)(\(self.count) bytes)" + } + +} diff --git a/swift/Tests/LinuxMain.swift b/swift/Tests/LinuxMain.swift new file mode 100644 index 0000000..b838ca2 --- /dev/null +++ b/swift/Tests/LinuxMain.swift @@ -0,0 +1,7 @@ +import XCTest + +import swiftTests + +var tests = [XCTestCaseEntry]() +tests += swiftTests.allTests() +XCTMain(tests) \ No newline at end of file diff --git a/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift b/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift new file mode 100644 index 0000000..0142277 --- /dev/null +++ b/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift @@ -0,0 +1,167 @@ +import XCTest +import SQLiteCryptoVFS +import SQLite3 + +final class SQLiteCryptoVFSTests: XCTestCase { + + func testKeySize() { + let key = Data([ + UInt8("60", radix: 16)!, UInt8("3d", radix: 16)!, UInt8("eb", radix: 16)!, + ]) + let initializationVector = Data() + do { + _ = try SQLiteCryptoVFS.registerVFS( + key: key, + initializationVector: initializationVector + ) + XCTFail("Key should be invalid") + } catch { + XCTAssertEqual(error.localizedDescription, "Key [60 3d eb](3 bytes) is not valid. Key must have 32 bytes.") + } + } + + func testInitializationVectorSize() { + let key = Data([ + UInt8("60", radix: 16)!, UInt8("3d", radix: 16)!, UInt8("eb", radix: 16)!, UInt8("10", radix: 16)!, UInt8("15", radix: 16)!, UInt8("ca", radix: 16)!, UInt8("71", radix: 16)!, UInt8("be", radix: 16)!, + UInt8("2b", radix: 16)!, UInt8("73", radix: 16)!, UInt8("ae", radix: 16)!, UInt8("f0", radix: 16)!, UInt8("85", radix: 16)!, UInt8("7d", radix: 16)!, UInt8("77", radix: 16)!, UInt8("81", radix: 16)!, + UInt8("1f", radix: 16)!, UInt8("35", radix: 16)!, UInt8("2c", radix: 16)!, UInt8("07", radix: 16)!, UInt8("3b", radix: 16)!, UInt8("61", radix: 16)!, UInt8("08", radix: 16)!, UInt8("d7", radix: 16)!, + UInt8("2d", radix: 16)!, UInt8("98", radix: 16)!, UInt8("10", radix: 16)!, UInt8("a3", radix: 16)!, UInt8("09", radix: 16)!, UInt8("14", radix: 16)!, UInt8("df", radix: 16)!, UInt8("f4", radix: 16)!, + ]) + let initializationVector = Data([ + UInt8("00", radix: 16)!, UInt8("01", radix: 16)!, UInt8("02", radix: 16)!, + ]) + do { + _ = try SQLiteCryptoVFS.registerVFS( + key: key, + initializationVector: initializationVector + ) + XCTFail("Key should be invalid") + } catch { + XCTAssertEqual(error.localizedDescription, "Initialization Vector [00 01 02](3 bytes) is not valid. Initialization Vector must have 16 bytes.") + } + } + + func testEncryption() { + let key = Data([ + UInt8("60", radix: 16)!, UInt8("3d", radix: 16)!, UInt8("eb", radix: 16)!, UInt8("10", radix: 16)!, UInt8("15", radix: 16)!, UInt8("ca", radix: 16)!, UInt8("71", radix: 16)!, UInt8("be", radix: 16)!, + UInt8("2b", radix: 16)!, UInt8("73", radix: 16)!, UInt8("ae", radix: 16)!, UInt8("f0", radix: 16)!, UInt8("85", radix: 16)!, UInt8("7d", radix: 16)!, UInt8("77", radix: 16)!, UInt8("81", radix: 16)!, + UInt8("1f", radix: 16)!, UInt8("35", radix: 16)!, UInt8("2c", radix: 16)!, UInt8("07", radix: 16)!, UInt8("3b", radix: 16)!, UInt8("61", radix: 16)!, UInt8("08", radix: 16)!, UInt8("d7", radix: 16)!, + UInt8("2d", radix: 16)!, UInt8("98", radix: 16)!, UInt8("10", radix: 16)!, UInt8("a3", radix: 16)!, UInt8("09", radix: 16)!, UInt8("14", radix: 16)!, UInt8("df", radix: 16)!, UInt8("f4", radix: 16)!, + ]) + let initializationVector = Data([ + UInt8("00", radix: 16)!, UInt8("01", radix: 16)!, UInt8("02", radix: 16)!, UInt8("03", radix: 16)!, UInt8("04", radix: 16)!, UInt8("05", radix: 16)!, UInt8("06", radix: 16)!, UInt8("07", radix: 16)!, + UInt8("08", radix: 16)!, UInt8("09", radix: 16)!, UInt8("0a", radix: 16)!, UInt8("0b", radix: 16)!, UInt8("0c", radix: 16)!, UInt8("0d", radix: 16)!, UInt8("0e", radix: 16)!, UInt8("0f", radix: 16)!, + ]) + do { + let vfsName = SQLiteCryptoVFS.vfsName + var resultCode = try SQLiteCryptoVFS.registerVFS( + key: key, + initializationVector: initializationVector + ) + + guard resultCode == SQLITE_OK else { + XCTFail("VFS not registered. Result code: \(resultCode)") + return + } + + let filename = "test-encrypted.db" + + let fileManager = FileManager.default + print("Path: \(fileManager.currentDirectoryPath)") + if fileManager.fileExists(atPath: filename) { + try fileManager.removeItem(atPath: filename) + } + + var db: OpaquePointer? + resultCode = sqlite3_open_v2(filename, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, vfsName) +// resultCode = sqlite3_open_v2(filename, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, nil) + defer { + sqlite3_close_v2(db) + } + guard resultCode == SQLITE_OK else { + XCTFail("Can't open SQLite. Result code: \(resultCode) / \(sqlite3_extended_errcode(db))") + return + } + + var zErrMsg: UnsafeMutablePointer? = nil + resultCode = sqlite3_exec(db, "CREATE TABLE USER (_ID TEXT PRIMARY KEY)", nil, nil, &zErrMsg) + guard resultCode == SQLITE_OK else { + XCTFail("SQL Error: \(String(cString: zErrMsg!))") + sqlite3_free(zErrMsg) + return + } + + let plaintext = "Can you keep a secret?" + + do { + var stmt: OpaquePointer? + resultCode = sqlite3_prepare_v2(db, "INSERT INTO USER (_ID) VALUES (?)", -1, &stmt, nil) + defer { + sqlite3_finalize(stmt) + } + guard resultCode == SQLITE_OK else { + XCTFail("SQL Error: \(String(cString: sqlite3_errmsg(db)))") + sqlite3_free(zErrMsg) + return + } + + resultCode = sqlite3_bind_text(stmt, 1, plaintext, -1, nil) + guard resultCode == SQLITE_OK else { + XCTFail("SQL Error: \(String(cString: sqlite3_errmsg(db)))") + sqlite3_free(zErrMsg) + return + } + + resultCode = sqlite3_step(stmt) + guard resultCode == SQLITE_DONE else { + XCTFail("SQL Error: \(String(cString: sqlite3_errmsg(db)))") + sqlite3_free(zErrMsg) + return + } + } + + do { + var stmt: OpaquePointer? + resultCode = sqlite3_prepare_v2(db, "SELECT * FROM USER", -1, &stmt, nil) + defer { + sqlite3_finalize(stmt) + } + guard resultCode == SQLITE_OK else { + XCTFail("SQL Error: \(String(cString: sqlite3_errmsg(db)))") + sqlite3_free(zErrMsg) + return + } + + resultCode = sqlite3_step(stmt); + guard resultCode == SQLITE_ROW else { + XCTFail("SQL Error: \(String(cString: sqlite3_errmsg(db)))") + sqlite3_free(zErrMsg) + return + } + + XCTAssertEqual(String(cString: sqlite3_column_text(stmt, 0)), plaintext) + } + + let _fileHandle = FileHandle(forReadingAtPath: filename) + XCTAssertNotNil(_fileHandle) + guard let fileHandle = _fileHandle else { + return + } + defer { + fileHandle.closeFile() + } + let data = fileHandle.readData(ofLength: 16) + let header = String(data: data, encoding: .ascii) + XCTAssertNotEqual(header, "SQLite format 3\0") + } catch { + XCTFail(error.localizedDescription) + } + } + + static var allTests = [ + ("testKeySize", testKeySize), + ("testInitializationVectorSize", testInitializationVectorSize), + ("testEncryption", testEncryption), + ] + +} diff --git a/swift/Tests/SQLiteCryptoVFSTests/XCTestManifests.swift b/swift/Tests/SQLiteCryptoVFSTests/XCTestManifests.swift new file mode 100644 index 0000000..85d6c23 --- /dev/null +++ b/swift/Tests/SQLiteCryptoVFSTests/XCTestManifests.swift @@ -0,0 +1,9 @@ +import XCTest + +#if !os(macOS) +public func allTests() -> [XCTestCaseEntry] { + return [ + testCase(SQLiteCryptoVFSTests.allTests), + ] +} +#endif diff --git a/test.cpp b/test.cpp index 9da7079..b6f0b5d 100644 --- a/test.cpp +++ b/test.cpp @@ -23,8 +23,8 @@ int main (int argc, char *argv[]) const uint8_t initialization_vector[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; - const char* vfs_name = "sqlite-crypto"; - rc = sqlite_crypto_vfs_register(key, initialization_vector, vfs_name, 0); + const char* vfs_name = sqlite_crypto_vfs_name(); + rc = sqlite_crypto_vfs_register(key, initialization_vector, 0); if (rc != SQLITE_OK) { return EXIT_FAILURE; } From 315dd515578d54133e75579aeeb8887368dfafd1 Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 00:16:09 -0700 Subject: [PATCH 04/20] fix for Swift unit test --- .../SQLiteCryptoVFSTests.swift | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift b/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift index 0142277..395f9ee 100644 --- a/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift +++ b/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift @@ -97,25 +97,25 @@ final class SQLiteCryptoVFSTests: XCTestCase { var stmt: OpaquePointer? resultCode = sqlite3_prepare_v2(db, "INSERT INTO USER (_ID) VALUES (?)", -1, &stmt, nil) defer { - sqlite3_finalize(stmt) + resultCode = sqlite3_finalize(stmt) + if resultCode != SQLITE_OK { + XCTFail("SQL Error: \(String(cString: sqlite3_errmsg(db)))") + } } guard resultCode == SQLITE_OK else { XCTFail("SQL Error: \(String(cString: sqlite3_errmsg(db)))") - sqlite3_free(zErrMsg) return } resultCode = sqlite3_bind_text(stmt, 1, plaintext, -1, nil) guard resultCode == SQLITE_OK else { XCTFail("SQL Error: \(String(cString: sqlite3_errmsg(db)))") - sqlite3_free(zErrMsg) return } resultCode = sqlite3_step(stmt) guard resultCode == SQLITE_DONE else { XCTFail("SQL Error: \(String(cString: sqlite3_errmsg(db)))") - sqlite3_free(zErrMsg) return } } @@ -124,18 +124,19 @@ final class SQLiteCryptoVFSTests: XCTestCase { var stmt: OpaquePointer? resultCode = sqlite3_prepare_v2(db, "SELECT * FROM USER", -1, &stmt, nil) defer { - sqlite3_finalize(stmt) + resultCode = sqlite3_finalize(stmt) + if resultCode != SQLITE_OK { + XCTFail("SQL Error: \(String(cString: sqlite3_errmsg(db)))") + } } guard resultCode == SQLITE_OK else { XCTFail("SQL Error: \(String(cString: sqlite3_errmsg(db)))") - sqlite3_free(zErrMsg) return } resultCode = sqlite3_step(stmt); guard resultCode == SQLITE_ROW else { XCTFail("SQL Error: \(String(cString: sqlite3_errmsg(db)))") - sqlite3_free(zErrMsg) return } From 17d4a26fdc4006f216f5ad5801cc0acb4673d963 Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 00:31:55 -0700 Subject: [PATCH 05/20] removing warning --- sqlite-crypto-vfs.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sqlite-crypto-vfs.h b/sqlite-crypto-vfs.h index 6492ab0..a80b6af 100644 --- a/sqlite-crypto-vfs.h +++ b/sqlite-crypto-vfs.h @@ -1,8 +1,9 @@ #ifndef _SQLITE_CRYPTO_VFS_H_ #include +#include -const char* sqlite_crypto_vfs_name(); +const char* sqlite_crypto_vfs_name(void); int sqlite_crypto_vfs_register(const uint8_t key[32], const uint8_t initialization_vector[16], const int make_default); #endif //_SQLITE_CRYPTO_VFS_H_ From b9d3b30e3a5d75e8eb1c5dd77e9608936c3714dd Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 01:39:24 -0700 Subject: [PATCH 06/20] adding support for Carthage --- .cirrus.yml | 4 +++- .gitignore | 3 +++ swift/.gitignore | 2 ++ swift/Cartfile | 0 swift/Cartfile.resolved | 0 swift/Package.swift | 19 ++++++++++++++++--- swift/SQLiteCryptoVFS.xcconfig | 1 + 7 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 swift/Cartfile create mode 100644 swift/Cartfile.resolved create mode 100644 swift/SQLiteCryptoVFS.xcconfig diff --git a/.cirrus.yml b/.cirrus.yml index 78df3fb..a87fec5 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -4,5 +4,7 @@ task: image: high-sierra-xcode-9.4.1 git_submodule_update_init_script: git submodule update --init --recursive install_pkg-config_script: brew install pkg-config + install_sqlite_script: brew install sqlite3 test_script: make test - swift_test_script: cd swift; swift build && swift test + swift_pm_test_script: cd swift; swift build && test + swift_test_script: cd swift; swift package generate-xcodeproj --xcconfig-overrides SQLiteCryptoVFS.xcconfig && xcodebuild -scheme SQLiteCryptoVFS-Package test diff --git a/.gitignore b/.gitignore index c6127b3..165dafe 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,6 @@ modules.order Module.symvers Mkfile.old dkms.conf + +# SQLite +**/test-encrypted.db diff --git a/swift/.gitignore b/swift/.gitignore index 02c0875..c707841 100644 --- a/swift/.gitignore +++ b/swift/.gitignore @@ -2,3 +2,5 @@ /.build /Packages /*.xcodeproj +Carthage +build diff --git a/swift/Cartfile b/swift/Cartfile new file mode 100644 index 0000000..e69de29 diff --git a/swift/Cartfile.resolved b/swift/Cartfile.resolved new file mode 100644 index 0000000..e69de29 diff --git a/swift/Package.swift b/swift/Package.swift index 5955efb..7fbf0d6 100644 --- a/swift/Package.swift +++ b/swift/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:4.1 +// swift-tools-version:4.0 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -11,6 +11,16 @@ let package = Package( name: "SQLiteCryptoVFS", targets: ["SQLiteCryptoVFS"] ), + .library( + name: "SQLiteCryptoVFSStatic", + type: .static, + targets: ["SQLiteCryptoVFS"] + ), + .library( + name: "SQLiteCryptoVFSDynamic", + type: .dynamic, + targets: ["SQLiteCryptoVFS"] + ), .library( name: "CSQLiteCryptoVFS", targets: ["CSQLiteCryptoVFS"] @@ -26,7 +36,7 @@ let package = Package( .target( name: "SQLiteCryptoVFS", dependencies: [ - "CSQLiteCryptoVFS" + "CSQLiteCryptoVFS", ] ), .target( @@ -35,7 +45,10 @@ let package = Package( ), .testTarget( name: "SQLiteCryptoVFSTests", - dependencies: ["SQLiteCryptoVFS"] + dependencies: [ + "SQLiteCryptoVFS", + ] ), ] ) +package.targets[1].type diff --git a/swift/SQLiteCryptoVFS.xcconfig b/swift/SQLiteCryptoVFS.xcconfig new file mode 100644 index 0000000..2fabf3d --- /dev/null +++ b/swift/SQLiteCryptoVFS.xcconfig @@ -0,0 +1 @@ +OTHER_LDFLAGS = $(inherited) -lsqlite3 From 97d49712c3874062e3db7f430ce1b7bef28dd77b Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 01:44:28 -0700 Subject: [PATCH 07/20] bugfix for Package.swift --- swift/Package.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/swift/Package.swift b/swift/Package.swift index 7fbf0d6..bcb3856 100644 --- a/swift/Package.swift +++ b/swift/Package.swift @@ -51,4 +51,3 @@ let package = Package( ), ] ) -package.targets[1].type From 530dcbfd9433f7d65b0d81bb3365ed283568ba90 Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 09:33:45 -0700 Subject: [PATCH 08/20] trying compiling using xcodebuild instead of swift package manager --- .cirrus.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.cirrus.yml b/.cirrus.yml index a87fec5..436c4a6 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -6,5 +6,5 @@ task: install_pkg-config_script: brew install pkg-config install_sqlite_script: brew install sqlite3 test_script: make test - swift_pm_test_script: cd swift; swift build && test swift_test_script: cd swift; swift package generate-xcodeproj --xcconfig-overrides SQLiteCryptoVFS.xcconfig && xcodebuild -scheme SQLiteCryptoVFS-Package test + swift_pm_test_script: cd swift; swift build && test From 8fd109be8f3dfc802b52c6fcb0438713f400c72b Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 11:22:12 -0700 Subject: [PATCH 09/20] adding Swift Linux for Cirrus CI --- .cirrus.yml | 10 ++++++++++ swift/Package.swift | 5 +++++ swift/Sources/CSQLite/empty.c | 0 swift/Tests/LinuxMain.swift | 4 ++-- .../SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift | 6 +++++- 5 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 swift/Sources/CSQLite/empty.c diff --git a/.cirrus.yml b/.cirrus.yml index 436c4a6..51c68cd 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -6,5 +6,15 @@ task: install_pkg-config_script: brew install pkg-config install_sqlite_script: brew install sqlite3 test_script: make test + swift_pm_test_script: cd swift; swift build && test swift_test_script: cd swift; swift package generate-xcodeproj --xcconfig-overrides SQLiteCryptoVFS.xcconfig && xcodebuild -scheme SQLiteCryptoVFS-Package test + +task: + name: Linux + container: + image: swift + git_submodule_update_init_script: git submodule update --init --recursive + apt_update_script: apt update + install_pkg-config_script: apt install pkg-config + install_sqlite_script: apt install libsqlite3-dev swift_pm_test_script: cd swift; swift build && test diff --git a/swift/Package.swift b/swift/Package.swift index bcb3856..2bd1618 100644 --- a/swift/Package.swift +++ b/swift/Package.swift @@ -43,10 +43,15 @@ let package = Package( name: "CSQLiteCryptoVFS", dependencies: [] ), + .target( + name: "CSQLite", + dependencies: [] + ), .testTarget( name: "SQLiteCryptoVFSTests", dependencies: [ "SQLiteCryptoVFS", + "CSQLite", ] ), ] diff --git a/swift/Sources/CSQLite/empty.c b/swift/Sources/CSQLite/empty.c new file mode 100644 index 0000000..e69de29 diff --git a/swift/Tests/LinuxMain.swift b/swift/Tests/LinuxMain.swift index b838ca2..e9c6fea 100644 --- a/swift/Tests/LinuxMain.swift +++ b/swift/Tests/LinuxMain.swift @@ -1,7 +1,7 @@ import XCTest -import swiftTests +import SQLiteCryptoVFSTests var tests = [XCTestCaseEntry]() -tests += swiftTests.allTests() +tests += SQLiteCryptoVFSTests.allTests() XCTMain(tests) \ No newline at end of file diff --git a/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift b/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift index 395f9ee..3e00624 100644 --- a/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift +++ b/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift @@ -1,6 +1,10 @@ import XCTest import SQLiteCryptoVFS -import SQLite3 +#if os(Linux) +import CSQLiteLinux +#else +import CSQLiteDarwin +#endif final class SQLiteCryptoVFSTests: XCTestCase { From 83265614060526dcee0f4eb76fbe89b07a603eaf Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 11:33:12 -0700 Subject: [PATCH 10/20] adding modulemaps --- swift/.gitignore | 1 + swift/Sources/CSQLite/include/module.modulemap | 11 +++++++++++ .../Sources/CSQLiteCryptoVFS/include/module.modulemap | 5 +++++ 3 files changed, 17 insertions(+) create mode 100644 swift/Sources/CSQLite/include/module.modulemap create mode 100644 swift/Sources/CSQLiteCryptoVFS/include/module.modulemap diff --git a/swift/.gitignore b/swift/.gitignore index c707841..71b585d 100644 --- a/swift/.gitignore +++ b/swift/.gitignore @@ -4,3 +4,4 @@ /*.xcodeproj Carthage build +!**/module.modulemap diff --git a/swift/Sources/CSQLite/include/module.modulemap b/swift/Sources/CSQLite/include/module.modulemap new file mode 100644 index 0000000..8fc0281 --- /dev/null +++ b/swift/Sources/CSQLite/include/module.modulemap @@ -0,0 +1,11 @@ +module CSQLiteLinux [system] { + header "/usr/include/sqlite3.h" + link "sqlite3" + export * +} + +module CSQLiteDarwin [system] { + header "/usr/local/opt/sqlite/include/sqlite3.h" + link "sqlite3" + export * +} diff --git a/swift/Sources/CSQLiteCryptoVFS/include/module.modulemap b/swift/Sources/CSQLiteCryptoVFS/include/module.modulemap new file mode 100644 index 0000000..538da29 --- /dev/null +++ b/swift/Sources/CSQLiteCryptoVFS/include/module.modulemap @@ -0,0 +1,5 @@ +module CSQLiteCryptoVFS { + header "sqlite-crypto-vfs.h" + link "sqlite3" + export * +} From 6e6db9800a224b84ad8d54e2dc2426020887965e Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 11:38:02 -0700 Subject: [PATCH 11/20] updating Cirrus CI --- .cirrus.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index 51c68cd..b406512 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -6,7 +6,7 @@ task: install_pkg-config_script: brew install pkg-config install_sqlite_script: brew install sqlite3 test_script: make test - swift_pm_test_script: cd swift; swift build && test + swift_pm_test_script: cd swift; swift build && swift test swift_test_script: cd swift; swift package generate-xcodeproj --xcconfig-overrides SQLiteCryptoVFS.xcconfig && xcodebuild -scheme SQLiteCryptoVFS-Package test task: @@ -15,6 +15,5 @@ task: image: swift git_submodule_update_init_script: git submodule update --init --recursive apt_update_script: apt update - install_pkg-config_script: apt install pkg-config install_sqlite_script: apt install libsqlite3-dev - swift_pm_test_script: cd swift; swift build && test + swift_pm_test_script: cd swift; swift build && swift test From 619bc1e232715119103a923a34993f40874f4b6f Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 13:21:03 -0700 Subject: [PATCH 12/20] workaround which seems to be a SQLite bug --- swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift | 2 +- test.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift b/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift index 3e00624..298571d 100644 --- a/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift +++ b/swift/Tests/SQLiteCryptoVFSTests/SQLiteCryptoVFSTests.swift @@ -88,7 +88,7 @@ final class SQLiteCryptoVFSTests: XCTestCase { } var zErrMsg: UnsafeMutablePointer? = nil - resultCode = sqlite3_exec(db, "CREATE TABLE USER (_ID TEXT PRIMARY KEY)", nil, nil, &zErrMsg) + resultCode = sqlite3_exec(db, "CREATE TABLE USER (_ID TEXT)", nil, nil, &zErrMsg) guard resultCode == SQLITE_OK else { XCTFail("SQL Error: \(String(cString: zErrMsg!))") sqlite3_free(zErrMsg) diff --git a/test.cpp b/test.cpp index b6f0b5d..a8fb40d 100644 --- a/test.cpp +++ b/test.cpp @@ -50,7 +50,7 @@ int main (int argc, char *argv[]) return EXIT_FAILURE; } - rc = sqlite3_exec(db, "CREATE TABLE USER (_ID TEXT PRIMARY KEY)", NULL, NULL, &zErrMsg); + rc = sqlite3_exec(db, "CREATE TABLE USER (_ID TEXT)", NULL, NULL, &zErrMsg); if (rc != SQLITE_OK) { std::cerr << LOGGER_INFO << "SQL Error: " << zErrMsg << std::endl; sqlite3_free(zErrMsg); From 5a60f6cbaf63fc5e657b08275455831fdb16479a Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 20:36:24 -0700 Subject: [PATCH 13/20] adding .Net support --- .cirrus.yml | 11 +- Makefile | 24 +- dotnet/.gitignore | 330 ++++++++++++++++++ .../SQLiteCryptoVFS.Tests.csproj | 19 + dotnet/SQLiteCryptoVFS.Tests/UnitTests.cs | 186 ++++++++++ dotnet/SQLiteCryptoVFS.sln | 48 +++ dotnet/SQLiteCryptoVFS/SQLiteCryptoVFS.cs | 32 ++ dotnet/SQLiteCryptoVFS/SQLiteCryptoVFS.csproj | 7 + .../SQLiteCryptoVFS/SQLiteCryptoVFS.swift | 6 +- 9 files changed, 657 insertions(+), 6 deletions(-) create mode 100644 dotnet/.gitignore create mode 100644 dotnet/SQLiteCryptoVFS.Tests/SQLiteCryptoVFS.Tests.csproj create mode 100644 dotnet/SQLiteCryptoVFS.Tests/UnitTests.cs create mode 100644 dotnet/SQLiteCryptoVFS.sln create mode 100644 dotnet/SQLiteCryptoVFS/SQLiteCryptoVFS.cs create mode 100644 dotnet/SQLiteCryptoVFS/SQLiteCryptoVFS.csproj diff --git a/.cirrus.yml b/.cirrus.yml index b406512..11b118b 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -8,12 +8,21 @@ task: test_script: make test swift_pm_test_script: cd swift; swift build && swift test swift_test_script: cd swift; swift package generate-xcodeproj --xcconfig-overrides SQLiteCryptoVFS.xcconfig && xcodebuild -scheme SQLiteCryptoVFS-Package test + install_dotnet_script: brew install dotnet-sdk + dotnet_test_script: cd dotnet/SQLiteCryptoVFS.Tests; dotnet test task: - name: Linux + name: Linux Swift container: image: swift git_submodule_update_init_script: git submodule update --init --recursive apt_update_script: apt update install_sqlite_script: apt install libsqlite3-dev swift_pm_test_script: cd swift; swift build && swift test + +task: + name: Linux .Net + container: + image: microsoft/dotnet + build_script: make + dotnet_test_script: cd dotnet/SQLiteCryptoVFS.Tests; dotnet test diff --git a/Makefile b/Makefile index ac9b75c..8e8aca4 100644 --- a/Makefile +++ b/Makefile @@ -2,17 +2,33 @@ CC = gcc CXX = g++ LD = gcc CFLAGS = $(shell pkg-config --libs sqlite3) $(shell pkg-config --cflags sqlite3) -CXXFLAGS = -g $(CFLAGS) -lsqlite-crypto-vfs -L. +ARCH_64 = $(shell gcc -dumpmachine -m64) +ARCH_32 = $(shell gcc -dumpmachine -m32) +ARCH = $(shell gcc -dumpmachine) +CXXFLAGS = -g $(CFLAGS) -lsqlite-crypto-vfs -Llib/$(ARCH) +SQLITE_CRYPTO_VFS_SOURCE = aes.c sqlite-crypto-vfs.c default: build +clean: + rm -Rf lib + rm -f test + build: sqlite-crypto-vfs -sqlite-crypto-vfs: - $(CC) $(CFLAGS) -shared -o libsqlite-crypto-vfs.dylib aes.c sqlite-crypto-vfs.c +sqlite-crypto-vfs-arch64: + mkdir -p lib/$(ARCH_64) + $(CC) $(CFLAGS) -shared -m64 -o lib/$(ARCH_64)/libsqlite-crypto-vfs.dylib $(SQLITE_CRYPTO_VFS_SOURCE) + +sqlite-crypto-vfs-arch32: + mkdir -p lib/$(ARCH_32) + $(CC) $(CFLAGS) -shared -m32 -o lib/$(ARCH_32)/libsqlite-crypto-vfs.dylib $(SQLITE_CRYPTO_VFS_SOURCE) + +sqlite-crypto-vfs: sqlite-crypto-vfs-arch32 sqlite-crypto-vfs-arch64 + test-build: sqlite-crypto-vfs $(CXX) $(CXXFLAGS) -o test test.cpp test: test-build - DYLD_LIBRARY_PATH=tiny-AES-c ./test + ./test diff --git a/dotnet/.gitignore b/dotnet/.gitignore new file mode 100644 index 0000000..7d1145f --- /dev/null +++ b/dotnet/.gitignore @@ -0,0 +1,330 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ +coverage.opencover.xml diff --git a/dotnet/SQLiteCryptoVFS.Tests/SQLiteCryptoVFS.Tests.csproj b/dotnet/SQLiteCryptoVFS.Tests/SQLiteCryptoVFS.Tests.csproj new file mode 100644 index 0000000..9383506 --- /dev/null +++ b/dotnet/SQLiteCryptoVFS.Tests/SQLiteCryptoVFS.Tests.csproj @@ -0,0 +1,19 @@ + + + + netcoreapp2.1 + + false + + + + + + + + + + + + + diff --git a/dotnet/SQLiteCryptoVFS.Tests/UnitTests.cs b/dotnet/SQLiteCryptoVFS.Tests/UnitTests.cs new file mode 100644 index 0000000..597c44b --- /dev/null +++ b/dotnet/SQLiteCryptoVFS.Tests/UnitTests.cs @@ -0,0 +1,186 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using SQLitePCL; +using System.Text; +using System.Diagnostics; + +namespace SQLiteCryptoVFS.Tests +{ + [TestClass] + public class SQLiteCryptoVFSUnitTests + { + [TestMethod] + public void TestKeySize() + { + var exception = Assert.ThrowsException(() => + { + SQLiteCryptoVFS.RegisterVFS(new byte[] { 10, 20, 30 }, new byte[] { }); + }); + Assert.AreEqual(exception.ParamName, "key"); + Assert.AreEqual(exception.Message, "Key must have 32 bytes\nParameter name: key"); + } + + [TestMethod] + public void TestInitializationVectorSize() + { + var exception = Assert.ThrowsException(() => + { + SQLiteCryptoVFS.RegisterVFS( + new byte[] { + 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, + 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 + }, + new byte[] { 10, 20, 30 } + ); + }); + Assert.AreEqual(exception.ParamName, "initializationVector"); + Assert.AreEqual(exception.Message, "Initialization Vector must have 16 bytes\nParameter name: initializationVector"); + } + + private Func FilterLibraryFiles(string libraryName) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return (f) => { + return Path.GetExtension(f).Equals(".dll"); + }; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return (f) => { + return f.StartsWith("lib", StringComparison.Ordinal) && + Path.GetExtension(f).Equals(".so"); + }; + } + else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) + { + return (f) => { + return f.StartsWith("lib", StringComparison.Ordinal) && + Path.GetExtension(f).Equals(".dylib"); + }; + } + return null; + } + + private void CopyLibrary() + { + var arch = Environment.Is64BitProcess ? "-m64" : "-m32"; + var processInfo = new ProcessStartInfo + { + FileName = "gcc", + Arguments = $"-dumpmachine {arch}", + RedirectStandardOutput = true + }; + string gccOutput; + using (var process = Process.Start(processInfo)) + { + gccOutput = process.StandardOutput.ReadLine(); + process.WaitForExit(); + process.Close(); + } + + var currentDirectory = Directory.GetCurrentDirectory(); + var relativePath = $"../../../../../lib/{gccOutput}"; + var path = Path.Combine(currentDirectory, relativePath); + var files = Directory.EnumerateFiles(path) + .Select(Path.GetFileName) + .Where(FilterLibraryFiles("sqlite-crypto-vfs")).ToList(); + foreach (var file in files) + { + File.Copy(Path.Combine(currentDirectory, relativePath, file), file, true); + Console.WriteLine($"{file} {new FileInfo(file).Length} bytes"); + } + } + + [TestMethod] + public void TestEncryption() + { + CopyLibrary(); + + var vfsName = SQLiteCryptoVFS.VFSName; + + var resultCode = SQLiteCryptoVFS.RegisterVFS( + new byte[] { + 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, + 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 + }, + new byte[] { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + } + ); + Assert.AreEqual(resultCode, raw.SQLITE_OK); + + const string filename = "test-encrypted.db"; + + raw.SetProvider(new SQLite3Provider_sqlite3()); + Console.WriteLine($"{raw.sqlite3_libversion()}"); + Console.WriteLine($"{raw.sqlite3_libversion_number()}"); + + if (File.Exists(filename)) + File.Delete(filename); + + resultCode = raw.sqlite3_open_v2(filename, out sqlite3 db, raw.SQLITE_OPEN_READWRITE | raw.SQLITE_OPEN_CREATE, vfsName); + //resultCode = raw.sqlite3_open_v2(filename, out sqlite3 db, raw.SQLITE_OPEN_READWRITE | raw.SQLITE_OPEN_CREATE, null); + Assert.AreEqual(resultCode, raw.SQLITE_OK); + try + { + resultCode = raw.sqlite3_exec(db, "CREATE TABLE USER (_ID TEXT)"); + Assert.AreEqual(resultCode, raw.SQLITE_OK); + + const string plaintext = "Can you keep a secret?"; + + { + resultCode = raw.sqlite3_prepare_v2(db, "INSERT INTO USER (_ID) VALUES (?)", out sqlite3_stmt stmt); + Assert.AreEqual(resultCode, raw.SQLITE_OK); + try + { + resultCode = raw.sqlite3_bind_text(stmt, 1, plaintext); + Assert.AreEqual(resultCode, raw.SQLITE_OK); + + resultCode = raw.sqlite3_step(stmt); + Assert.AreEqual(resultCode, raw.SQLITE_DONE); + } + finally + { + resultCode = raw.sqlite3_finalize(stmt); + Assert.AreEqual(resultCode, raw.SQLITE_OK); + } + } + + { + resultCode = raw.sqlite3_prepare_v2(db, "SELECT * FROM USER", out sqlite3_stmt stmt); + Assert.AreEqual(resultCode, raw.SQLITE_OK); + try + { + resultCode = raw.sqlite3_step(stmt); + Assert.AreEqual(resultCode, raw.SQLITE_ROW); + + Assert.AreEqual(raw.sqlite3_column_text(stmt, 0), plaintext); + } + finally + { + resultCode = raw.sqlite3_finalize(stmt); + Assert.AreEqual(resultCode, raw.SQLITE_OK); + } + } + + using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) + { + byte[] buffer = new byte[16]; + fs.Read(buffer, 0, buffer.Length); + fs.Close(); + var header = Encoding.ASCII.GetString(buffer); + Assert.AreNotEqual(header, "SQLite format 3\0"); + } + } + finally + { + resultCode = raw.sqlite3_close_v2(db); + Assert.AreEqual(resultCode, raw.SQLITE_OK); + } + } + } +} diff --git a/dotnet/SQLiteCryptoVFS.sln b/dotnet/SQLiteCryptoVFS.sln new file mode 100644 index 0000000..0601ca3 --- /dev/null +++ b/dotnet/SQLiteCryptoVFS.sln @@ -0,0 +1,48 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26124.0 +MinimumVisualStudioVersion = 15.0.26124.0 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLiteCryptoVFS", "SQLiteCryptoVFS\SQLiteCryptoVFS.csproj", "{60D54712-3399-40FD-8758-1D439A47E03F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQLiteCryptoVFS.Tests", "SQLiteCryptoVFS.Tests\SQLiteCryptoVFS.Tests.csproj", "{24294549-D9B8-48B7-8C67-4E5B2C77B4AB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {60D54712-3399-40FD-8758-1D439A47E03F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {60D54712-3399-40FD-8758-1D439A47E03F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {60D54712-3399-40FD-8758-1D439A47E03F}.Debug|x64.ActiveCfg = Debug|Any CPU + {60D54712-3399-40FD-8758-1D439A47E03F}.Debug|x64.Build.0 = Debug|Any CPU + {60D54712-3399-40FD-8758-1D439A47E03F}.Debug|x86.ActiveCfg = Debug|Any CPU + {60D54712-3399-40FD-8758-1D439A47E03F}.Debug|x86.Build.0 = Debug|Any CPU + {60D54712-3399-40FD-8758-1D439A47E03F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {60D54712-3399-40FD-8758-1D439A47E03F}.Release|Any CPU.Build.0 = Release|Any CPU + {60D54712-3399-40FD-8758-1D439A47E03F}.Release|x64.ActiveCfg = Release|Any CPU + {60D54712-3399-40FD-8758-1D439A47E03F}.Release|x64.Build.0 = Release|Any CPU + {60D54712-3399-40FD-8758-1D439A47E03F}.Release|x86.ActiveCfg = Release|Any CPU + {60D54712-3399-40FD-8758-1D439A47E03F}.Release|x86.Build.0 = Release|Any CPU + {24294549-D9B8-48B7-8C67-4E5B2C77B4AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {24294549-D9B8-48B7-8C67-4E5B2C77B4AB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {24294549-D9B8-48B7-8C67-4E5B2C77B4AB}.Debug|x64.ActiveCfg = Debug|Any CPU + {24294549-D9B8-48B7-8C67-4E5B2C77B4AB}.Debug|x64.Build.0 = Debug|Any CPU + {24294549-D9B8-48B7-8C67-4E5B2C77B4AB}.Debug|x86.ActiveCfg = Debug|Any CPU + {24294549-D9B8-48B7-8C67-4E5B2C77B4AB}.Debug|x86.Build.0 = Debug|Any CPU + {24294549-D9B8-48B7-8C67-4E5B2C77B4AB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {24294549-D9B8-48B7-8C67-4E5B2C77B4AB}.Release|Any CPU.Build.0 = Release|Any CPU + {24294549-D9B8-48B7-8C67-4E5B2C77B4AB}.Release|x64.ActiveCfg = Release|Any CPU + {24294549-D9B8-48B7-8C67-4E5B2C77B4AB}.Release|x64.Build.0 = Release|Any CPU + {24294549-D9B8-48B7-8C67-4E5B2C77B4AB}.Release|x86.ActiveCfg = Release|Any CPU + {24294549-D9B8-48B7-8C67-4E5B2C77B4AB}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/dotnet/SQLiteCryptoVFS/SQLiteCryptoVFS.cs b/dotnet/SQLiteCryptoVFS/SQLiteCryptoVFS.cs new file mode 100644 index 0000000..af3bf22 --- /dev/null +++ b/dotnet/SQLiteCryptoVFS/SQLiteCryptoVFS.cs @@ -0,0 +1,32 @@ +using System; +using System.Runtime.InteropServices; + +namespace SQLiteCryptoVFS +{ + public abstract class SQLiteCryptoVFS + { + private const string LibraryName = "sqlite-crypto-vfs"; + + [DllImport(LibraryName, EntryPoint = "sqlite_crypto_vfs_name")] + private static extern IntPtr SQLiteCryptoVFSName(); + + [DllImport(LibraryName, EntryPoint = "sqlite_crypto_vfs_register")] + private static extern int SQLiteCryptoVFSRegister(byte[] key, byte[] initializationVector, bool makeDefault); + + public static string VFSName + { + get + { + return Marshal.PtrToStringAnsi(SQLiteCryptoVFSName()); + } + } + + public static int RegisterVFS(byte[] key, byte[] initializationVector, bool makeDefault = false) + { + if (key.Length != 32) throw new ArgumentException("Key must have 32 bytes", nameof(key)); + if (initializationVector.Length != 16) throw new ArgumentException("Initialization Vector must have 16 bytes", nameof(initializationVector)); + return SQLiteCryptoVFSRegister(key, initializationVector, makeDefault); + } + + } +} diff --git a/dotnet/SQLiteCryptoVFS/SQLiteCryptoVFS.csproj b/dotnet/SQLiteCryptoVFS/SQLiteCryptoVFS.csproj new file mode 100644 index 0000000..72764a6 --- /dev/null +++ b/dotnet/SQLiteCryptoVFS/SQLiteCryptoVFS.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + + diff --git a/swift/Sources/SQLiteCryptoVFS/SQLiteCryptoVFS.swift b/swift/Sources/SQLiteCryptoVFS/SQLiteCryptoVFS.swift index cf240d7..caa696d 100644 --- a/swift/Sources/SQLiteCryptoVFS/SQLiteCryptoVFS.swift +++ b/swift/Sources/SQLiteCryptoVFS/SQLiteCryptoVFS.swift @@ -10,7 +10,11 @@ public func registerVFS( initializationVector: Data, makeDefault: Bool = false ) throws -> Int32 { - return try registerVFS(key: [UInt8](key), initializationVector: [UInt8](initializationVector), makeDefault: makeDefault) + return try registerVFS( + key: [UInt8](key), + initializationVector: [UInt8](initializationVector), + makeDefault: makeDefault + ) } public func registerVFS( From a046999651eb7ec5f179efbd172c0d84d28acc6f Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 20:39:20 -0700 Subject: [PATCH 14/20] fix for .net --- .cirrus.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index 11b118b..aab2f93 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -12,7 +12,7 @@ task: dotnet_test_script: cd dotnet/SQLiteCryptoVFS.Tests; dotnet test task: - name: Linux Swift + name: Swift Linux container: image: swift git_submodule_update_init_script: git submodule update --init --recursive @@ -21,8 +21,9 @@ task: swift_pm_test_script: cd swift; swift build && swift test task: - name: Linux .Net + name: .Net Linux container: image: microsoft/dotnet + git_submodule_update_init_script: git submodule update --init --recursive build_script: make dotnet_test_script: cd dotnet/SQLiteCryptoVFS.Tests; dotnet test From a9ddb8116c678a7a2d56028765e29d6cab6362b2 Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 20:41:47 -0700 Subject: [PATCH 15/20] installing make for .Net --- .cirrus.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.cirrus.yml b/.cirrus.yml index aab2f93..8166455 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -25,5 +25,8 @@ task: container: image: microsoft/dotnet git_submodule_update_init_script: git submodule update --init --recursive + apt_update_script: apt update + install_make_script: apt install make + install_sqlite_script: apt install libsqlite3-dev build_script: make dotnet_test_script: cd dotnet/SQLiteCryptoVFS.Tests; dotnet test From ef6d41494ecbff9b88a1949bc89ea157f70e5a79 Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 20:44:02 -0700 Subject: [PATCH 16/20] installing make for .Net --- .cirrus.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.cirrus.yml b/.cirrus.yml index 8166455..051631d 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -27,6 +27,6 @@ task: git_submodule_update_init_script: git submodule update --init --recursive apt_update_script: apt update install_make_script: apt install make - install_sqlite_script: apt install libsqlite3-dev + install_sqlite_script: apt install libsqlite3-dev -y build_script: make dotnet_test_script: cd dotnet/SQLiteCryptoVFS.Tests; dotnet test From 38c6e21f3a7fd24340034911cfc8a02046ef4adf Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 21:03:20 -0700 Subject: [PATCH 17/20] updating .cirrus.yml --- .cirrus.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.cirrus.yml b/.cirrus.yml index 051631d..7efb7ba 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -3,6 +3,7 @@ task: osx_instance: image: high-sierra-xcode-9.4.1 git_submodule_update_init_script: git submodule update --init --recursive + brew_update_script: brew update install_pkg-config_script: brew install pkg-config install_sqlite_script: brew install sqlite3 test_script: make test @@ -28,5 +29,6 @@ task: apt_update_script: apt update install_make_script: apt install make install_sqlite_script: apt install libsqlite3-dev -y + install_gcc_script: apt install gcc -y build_script: make dotnet_test_script: cd dotnet/SQLiteCryptoVFS.Tests; dotnet test From 0d5fb002fa8974e00f5932c7843bad4722140a74 Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Fri, 5 Oct 2018 21:34:57 -0700 Subject: [PATCH 18/20] updating .cirrus.yml --- .cirrus.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.cirrus.yml b/.cirrus.yml index 7efb7ba..97fcff5 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -30,5 +30,6 @@ task: install_make_script: apt install make install_sqlite_script: apt install libsqlite3-dev -y install_gcc_script: apt install gcc -y + install_pkg-config_script: apt install pkg-config -y build_script: make dotnet_test_script: cd dotnet/SQLiteCryptoVFS.Tests; dotnet test From 22a30ee27d0285d25ca8f75588630fcd079a4aff Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Sat, 6 Oct 2018 10:27:15 -0700 Subject: [PATCH 19/20] updating .cirrus.yml --- .cirrus.yml | 3 +-- Makefile | 34 +++++++++++++++++++++------------- test.cpp | 1 + 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index 97fcff5..5d9966a 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -27,9 +27,8 @@ task: image: microsoft/dotnet git_submodule_update_init_script: git submodule update --init --recursive apt_update_script: apt update - install_make_script: apt install make + install_build-essential_script: apt install build-essential -y install_sqlite_script: apt install libsqlite3-dev -y - install_gcc_script: apt install gcc -y install_pkg-config_script: apt install pkg-config -y build_script: make dotnet_test_script: cd dotnet/SQLiteCryptoVFS.Tests; dotnet test diff --git a/Makefile b/Makefile index 8e8aca4..a415774 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,21 @@ CC = gcc CXX = g++ -LD = gcc -CFLAGS = $(shell pkg-config --libs sqlite3) $(shell pkg-config --cflags sqlite3) -ARCH_64 = $(shell gcc -dumpmachine -m64) -ARCH_32 = $(shell gcc -dumpmachine -m32) -ARCH = $(shell gcc -dumpmachine) -CXXFLAGS = -g $(CFLAGS) -lsqlite-crypto-vfs -Llib/$(ARCH) +UNAME = $(shell uname) +CFLAGS = $(shell pkg-config --libs sqlite3) $(shell pkg-config --cflags sqlite3) -fPIC +ARCH = $(shell getconf LONG_BIT) +DUMPMACHINE = $(shell $(CC) -dumpmachine -m$(ARCH)) +CXXFLAGS = -g $(CFLAGS) -lsqlite-crypto-vfs -Llib/$(DUMPMACHINE) SQLITE_CRYPTO_VFS_SOURCE = aes.c sqlite-crypto-vfs.c +ifeq ($(UNAME), Linux) +LIB_PREFIX = lib +LIB_SUFFIX = .so +endif +ifeq ($(UNAME), Darwin) +LIB_PREFIX = lib +LIB_SUFFIX = .dylib +endif + default: build clean: @@ -17,18 +25,18 @@ clean: build: sqlite-crypto-vfs sqlite-crypto-vfs-arch64: - mkdir -p lib/$(ARCH_64) - $(CC) $(CFLAGS) -shared -m64 -o lib/$(ARCH_64)/libsqlite-crypto-vfs.dylib $(SQLITE_CRYPTO_VFS_SOURCE) + mkdir -p lib/$(DUMPMACHINE) + $(CC) $(CFLAGS) -shared -m64 -o lib/$(DUMPMACHINE)/$(LIB_PREFIX)sqlite-crypto-vfs$(LIB_SUFFIX) $(SQLITE_CRYPTO_VFS_SOURCE) sqlite-crypto-vfs-arch32: - mkdir -p lib/$(ARCH_32) - $(CC) $(CFLAGS) -shared -m32 -o lib/$(ARCH_32)/libsqlite-crypto-vfs.dylib $(SQLITE_CRYPTO_VFS_SOURCE) + mkdir -p lib/$(DUMPMACHINE) + $(CC) $(CFLAGS) -shared -m32 -o lib/$(DUMPMACHINE)/$(LIB_PREFIX)sqlite-crypto-vfs$(LIB_SUFFIX) $(SQLITE_CRYPTO_VFS_SOURCE) -sqlite-crypto-vfs: sqlite-crypto-vfs-arch32 sqlite-crypto-vfs-arch64 - +sqlite-crypto-vfs: sqlite-crypto-vfs-arch$(ARCH) + @echo "*** BUILD SUCCESSFUL ***" test-build: sqlite-crypto-vfs $(CXX) $(CXXFLAGS) -o test test.cpp test: test-build - ./test + LD_LIBRARY_PATH=./lib/$(DUMPMACHINE) ./test diff --git a/test.cpp b/test.cpp index a8fb40d..7cdafb6 100644 --- a/test.cpp +++ b/test.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "sqlite-crypto-vfs.hpp" #define LOGGER_INFO __FILE__ << ":" << __LINE__ << " " From 0744d438c390137a8ad9f457f397dd932290938c Mon Sep 17 00:00:00 2001 From: Victor Hugo Barros Date: Sat, 6 Oct 2018 13:29:08 -0700 Subject: [PATCH 20/20] installing dotnet-sdk for macOS --- .cirrus.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index 5d9966a..9866286 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -2,6 +2,8 @@ task: name: macOS osx_instance: image: high-sierra-xcode-9.4.1 + env: + PATH: $PATH:/usr/local/share/dotnet git_submodule_update_init_script: git submodule update --init --recursive brew_update_script: brew update install_pkg-config_script: brew install pkg-config @@ -9,7 +11,7 @@ task: test_script: make test swift_pm_test_script: cd swift; swift build && swift test swift_test_script: cd swift; swift package generate-xcodeproj --xcconfig-overrides SQLiteCryptoVFS.xcconfig && xcodebuild -scheme SQLiteCryptoVFS-Package test - install_dotnet_script: brew install dotnet-sdk + install_dotnet_script: brew cask install dotnet-sdk dotnet_test_script: cd dotnet/SQLiteCryptoVFS.Tests; dotnet test task: @@ -30,5 +32,5 @@ task: install_build-essential_script: apt install build-essential -y install_sqlite_script: apt install libsqlite3-dev -y install_pkg-config_script: apt install pkg-config -y - build_script: make + test_script: make test dotnet_test_script: cd dotnet/SQLiteCryptoVFS.Tests; dotnet test