Skip to content

Commit

Permalink
Removed expectedVaultVersion param during unlock
Browse files Browse the repository at this point in the history
  • Loading branch information
tobihagemann committed Feb 7, 2021
1 parent d246f3f commit d324ab1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
9 changes: 3 additions & 6 deletions Sources/CryptomatorCryptoLib/MasterkeyFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ public class MasterkeyFile {

- Parameter passphrase: The passphrase used during key derivation.
- Parameter pepper: An optional application-specific pepper added to the scrypt's salt. Defaults to empty byte array.
- Parameter expectedVaultVersion: An optional expected vault version.
- Returns: A masterkey with the unwrapped keys.
*/
public func unlock(passphrase: String, pepper: [UInt8] = [UInt8](), expectedVaultVersion: Int? = nil) throws -> Masterkey {
public func unlock(passphrase: String, pepper: [UInt8] = [UInt8]()) throws -> Masterkey {
// derive keys:
let pw = [UInt8](passphrase.precomposedStringWithCanonicalMapping.utf8)
let salt = [UInt8](Data(base64Encoded: content.scryptSalt)!)
Expand All @@ -98,15 +97,13 @@ public class MasterkeyFile {
let macKey = try MasterkeyFile.unwrapKey([UInt8](wrappedHmacKey), kek: kek)

// check MAC:
if let expectedVaultVersion = expectedVaultVersion {
try checkVaultVersion(expectedVaultVersion: expectedVaultVersion, macKey: macKey)
}
try checkVaultVersion(macKey: macKey)

// construct key:
return Masterkey.createFromRaw(aesMasterKey: aesKey, macMasterKey: macKey)
}

private func checkVaultVersion(expectedVaultVersion: Int, macKey: [UInt8]) throws {
private func checkVaultVersion(macKey: [UInt8]) throws {
guard let storedVersionMac = Data(base64Encoded: content.versionMac), storedVersionMac.count == CC_SHA256_DIGEST_LENGTH else {
throw MasterkeyFileError.malformedMasterkeyFile("invalid base64 data in versionMac")
}
Expand Down
16 changes: 8 additions & 8 deletions Tests/CryptomatorCryptoLibTests/MasterkeyFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MasterkeyFileTests: XCTestCase {
}
""".data(using: .utf8)!
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
let masterkey = try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8](), expectedVaultVersion: 7)
let masterkey = try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8]())
XCTAssertEqual(expectedKey, masterkey.aesMasterKey)
XCTAssertEqual(expectedKey, masterkey.macMasterKey)
}
Expand All @@ -65,7 +65,7 @@ class MasterkeyFileTests: XCTestCase {
}
""".data(using: .utf8)!
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "qwe", pepper: [UInt8](), expectedVaultVersion: 7), "wrong passphrase") { error in
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "qwe", pepper: [UInt8]()), "wrong passphrase") { error in
XCTAssertEqual(.invalidPassphrase, error as? MasterkeyFileError)
}
}
Expand All @@ -83,7 +83,7 @@ class MasterkeyFileTests: XCTestCase {
}
""".data(using: .utf8)!
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8](), expectedVaultVersion: 7), "invalid version mac") { error in
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8]()), "invalid version mac") { error in
XCTAssertEqual(.malformedMasterkeyFile("incorrect version or versionMac"), error as? MasterkeyFileError)
}
}
Expand All @@ -101,7 +101,7 @@ class MasterkeyFileTests: XCTestCase {
}
""".data(using: .utf8)!
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8](), expectedVaultVersion: 7), "malformed json") { error in
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8]()), "malformed json") { error in
XCTAssertEqual(.malformedMasterkeyFile("invalid base64 data in primaryMasterKey"), error as? MasterkeyFileError)
}
}
Expand All @@ -119,7 +119,7 @@ class MasterkeyFileTests: XCTestCase {
}
""".data(using: .utf8)!
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8](), expectedVaultVersion: 7), "malformed json") { error in
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8]()), "malformed json") { error in
XCTAssertEqual(.malformedMasterkeyFile("invalid base64 data in hmacMasterKey"), error as? MasterkeyFileError)
}
}
Expand All @@ -137,7 +137,7 @@ class MasterkeyFileTests: XCTestCase {
}
""".data(using: .utf8)!
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8](), expectedVaultVersion: 7), "malformed json") { error in
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8]()), "malformed json") { error in
XCTAssertEqual(.malformedMasterkeyFile("invalid base64 data in versionMac"), error as? MasterkeyFileError)
}
}
Expand Down Expand Up @@ -176,10 +176,10 @@ class MasterkeyFileTests: XCTestCase {
""".data(using: .utf8)!
let content = try MasterkeyFile.changePassphrase(masterkeyFileData: data, oldPassphrase: "asd", newPassphrase: "qwe", pepper: [UInt8](), scryptCostParam: 2, cryptoSupport: CryptoSupportMock())
let masterkeyFile = MasterkeyFile(content: content)
let masterkey = try masterkeyFile.unlock(passphrase: "qwe", pepper: [UInt8](), expectedVaultVersion: 7)
let masterkey = try masterkeyFile.unlock(passphrase: "qwe", pepper: [UInt8]())
XCTAssertEqual(expectedKey, masterkey.aesMasterKey)
XCTAssertEqual(expectedKey, masterkey.macMasterKey)
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8](), expectedVaultVersion: 7), "wrong passphrase") { error in
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8]()), "wrong passphrase") { error in
XCTAssertEqual(.invalidPassphrase, error as? MasterkeyFileError)
}
}
Expand Down

0 comments on commit d324ab1

Please sign in to comment.