Skip to content

Commit

Permalink
Fixed linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
tobihagemann committed Aug 23, 2024
1 parent c9b4e74 commit d4e8f6d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Sources/CryptomatorCryptoLib/AesSiv.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class AesSiv {

var d = try cmac(macKey: macKey, data: zero)
for s in ad {
d = xor(dbl(d), try cmac(macKey: macKey, data: s))
d = try xor(dbl(d), cmac(macKey: macKey, data: s))
}

let t: [UInt8]
Expand Down
6 changes: 1 addition & 5 deletions Sources/CryptomatorCryptoLib/Cryptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,7 @@ public class Cryptor {

// decrypt:
let cleartext = try AesSiv.decrypt(aesKey: masterkey.aesMasterKey, macKey: masterkey.macMasterKey, ciphertext: [UInt8](ciphertextData), ad: [UInt8](dirId))
if let str = String(data: Data(cleartext), encoding: .utf8) {
return str
} else {
throw CryptoError.invalidParameter("Unable to decode cleartext using UTF-8.")
}
return String(decoding: Data(cleartext), as: UTF8.self)
}

// MARK: - File Header Encryption and Decryption
Expand Down
8 changes: 4 additions & 4 deletions Tests/CryptomatorCryptoLibTests/CryptorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ class CryptorTests: XCTestCase {
}

func testEncryptDirId() throws {
let rootDir = try cryptor.encryptDirId("".data(using: .utf8)!)
let rootDir = try cryptor.encryptDirId(Data("".utf8))
XCTAssertEqual("VLWEHT553J5DR7OZLRJAYDIWFCXZABOD", rootDir)

let testDir = try cryptor.encryptDirId("918acfbd-a467-3f77-93f1-f4a44f9cfe9c".data(using: .utf8)!)
let testDir = try cryptor.encryptDirId(Data("918acfbd-a467-3f77-93f1-f4a44f9cfe9c".utf8))
XCTAssertEqual("7C3USOO3VU7IVQRKFMRFV3QE4VEZJECV", testDir)
}

func testEncryptAndDecryptName() throws {
let dirId = "foo".data(using: .utf8)!
let dirId = Data("foo".utf8)
let originalName = "hello.txt"

let ciphertextName = try cryptor.encryptFileName(originalName, dirId: dirId)
Expand All @@ -52,7 +52,7 @@ class CryptorTests: XCTestCase {
}

func testDecryptInvalidName() throws {
let dirId = "foo".data(using: .utf8)!
let dirId = Data("foo".utf8)
XCTAssertThrowsError(try cryptor.decryptFileName("****", dirId: dirId), "invalid ciphertext name encoding") { error in
XCTAssertEqual(.invalidParameter("Can't base64url-decode ciphertext name: ****"), error as? CryptoError)
}
Expand Down
40 changes: 20 additions & 20 deletions Tests/CryptomatorCryptoLibTests/MasterkeyFileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import XCTest

class MasterkeyFileTests: XCTestCase {
func testCreateWithContentFromData() throws {
let data = """
let data = Data("""
{
"version": 7,
"scryptSalt": "AAAAAAAAAAA=",
Expand All @@ -22,7 +22,7 @@ class MasterkeyFileTests: XCTestCase {
"hmacMasterKey": "mM+qoQ+o0qvPTiDAZYt+flaC3WbpNAx1sTXaUzxwpy0M9Ctj6Tih/Q==",
"versionMac": "cn2sAK6l9p1/w9deJVUuW3h7br056mpv5srvALiYw+g="
}
""".data(using: .utf8)!
""".utf8)
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
XCTAssertEqual(7, masterkeyFile.content.version)
XCTAssertEqual("AAAAAAAAAAA=", masterkeyFile.content.scryptSalt)
Expand All @@ -35,7 +35,7 @@ class MasterkeyFileTests: XCTestCase {

func testUnlockWithPassphrase() throws {
let expectedKey = [UInt8](repeating: 0x00, count: 32)
let data = """
let data = Data("""
{
"version": 7,
"scryptSalt": "AAAAAAAAAAA=",
Expand All @@ -45,15 +45,15 @@ class MasterkeyFileTests: XCTestCase {
"hmacMasterKey": "mM+qoQ+o0qvPTiDAZYt+flaC3WbpNAx1sTXaUzxwpy0M9Ctj6Tih/Q==",
"versionMac": "cn2sAK6l9p1/w9deJVUuW3h7br056mpv5srvALiYw+g="
}
""".data(using: .utf8)!
""".utf8)
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
let masterkey = try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8]())
XCTAssertEqual(expectedKey, masterkey.aesMasterKey)
XCTAssertEqual(expectedKey, masterkey.macMasterKey)
}

func testUnlockWithWrongPassphrase() throws {
let data = """
let data = Data("""
{
"version": 7,
"scryptSalt": "AAAAAAAAAAA=",
Expand All @@ -63,15 +63,15 @@ class MasterkeyFileTests: XCTestCase {
"hmacMasterKey": "mM+qoQ+o0qvPTiDAZYt+flaC3WbpNAx1sTXaUzxwpy0M9Ctj6Tih/Q==",
"versionMac": "cn2sAK6l9p1/w9deJVUuW3h7br056mpv5srvALiYw+g="
}
""".data(using: .utf8)!
""".utf8)
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "qwe", pepper: [UInt8]()), "wrong passphrase") { error in
XCTAssertEqual(.invalidPassphrase, error as? MasterkeyFileError)
}
}

func testUnlockWithInvalidVersionMac() throws {
let data = """
let data = Data("""
{
"version": 7,
"scryptSalt": "AAAAAAAAAAA=",
Expand All @@ -81,15 +81,15 @@ class MasterkeyFileTests: XCTestCase {
"hmacMasterKey": "mM+qoQ+o0qvPTiDAZYt+flaC3WbpNAx1sTXaUzxwpy0M9Ctj6Tih/Q==",
"versionMac": "cn2sAK6l9p1/w9deJVUuW3h7br056mpv5srvALiYw+G="
}
""".data(using: .utf8)!
""".utf8)
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8]()), "invalid version mac") { error in
XCTAssertEqual(.malformedMasterkeyFile("incorrect version or versionMac"), error as? MasterkeyFileError)
}
}

func testUnlockWithMalformedJson1() throws {
let data = """
let data = Data("""
{
"version": 7,
"scryptSalt": "AAAAAAAAAAA=",
Expand All @@ -99,15 +99,15 @@ class MasterkeyFileTests: XCTestCase {
"hmacMasterKey": "mM+qoQ+o0qvPTiDAZYt+flaC3WbpNAx1sTXaUzxwpy0M9Ctj6Tih/Q==",
"versionMac": "cn2sAK6l9p1/w9deJVUuW3h7br056mpv5srvALiYw+g="
}
""".data(using: .utf8)!
""".utf8)
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8]()), "malformed json") { error in
XCTAssertEqual(.malformedMasterkeyFile("invalid base64 data in primaryMasterKey"), error as? MasterkeyFileError)
}
}

func testUnlockWithMalformedJson2() throws {
let data = """
let data = Data("""
{
"version": 7,
"scryptSalt": "AAAAAAAAAAA=",
Expand All @@ -117,15 +117,15 @@ class MasterkeyFileTests: XCTestCase {
"hmacMasterKey": "mM+qoQ+o0qvPTiDAZYt+flaC3WbpNAx1sTXaUzxwpy0M9Ctj6Tih/Q!!",
"versionMac": "cn2sAK6l9p1/w9deJVUuW3h7br056mpv5srvALiYw+g="
}
""".data(using: .utf8)!
""".utf8)
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8]()), "malformed json") { error in
XCTAssertEqual(.malformedMasterkeyFile("invalid base64 data in hmacMasterKey"), error as? MasterkeyFileError)
}
}

func testUnlockWithMalformedJson3() throws {
let data = """
let data = Data("""
{
"version": 7,
"scryptSalt": "AAAAAAAAAAA=",
Expand All @@ -135,7 +135,7 @@ class MasterkeyFileTests: XCTestCase {
"hmacMasterKey": "mM+qoQ+o0qvPTiDAZYt+flaC3WbpNAx1sTXaUzxwpy0M9Ctj6Tih/Q==",
"versionMac": "cn2sAK6l"
}
""".data(using: .utf8)!
""".utf8)
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
XCTAssertThrowsError(try masterkeyFile.unlock(passphrase: "asd", pepper: [UInt8]()), "malformed json") { error in
XCTAssertEqual(.malformedMasterkeyFile("invalid base64 data in versionMac"), error as? MasterkeyFileError)
Expand All @@ -149,7 +149,7 @@ class MasterkeyFileTests: XCTestCase {
0x62, 0x11, 0xD9, 0xAF, 0xE2, 0xF5, 0x5F, 0xDE,
0xDF, 0xCB, 0xB5, 0xE7, 0x5A, 0xEF, 0x34, 0xF9
]
let data = """
let data = Data("""
{
"version": 7,
"scryptSalt": "AAAAAAAAAAA=",
Expand All @@ -159,15 +159,15 @@ class MasterkeyFileTests: XCTestCase {
"hmacMasterKey": "mM+qoQ+o0qvPTiDAZYt+flaC3WbpNAx1sTXaUzxwpy0M9Ctj6Tih/Q==",
"versionMac": "cn2sAK6l9p1/w9deJVUuW3h7br056mpv5srvALiYw+g="
}
""".data(using: .utf8)!
""".utf8)
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
let kek = try masterkeyFile.deriveKey(passphrase: "asd", pepper: [UInt8]())
XCTAssertEqual(expectedKey, kek)
}

func testUnlockWithKEK() throws {
let expectedKey = [UInt8](repeating: 0x00, count: 32)
let data = """
let data = Data("""
{
"version": 7,
"scryptSalt": "AAAAAAAAAAA=",
Expand All @@ -177,7 +177,7 @@ class MasterkeyFileTests: XCTestCase {
"hmacMasterKey": "mM+qoQ+o0qvPTiDAZYt+flaC3WbpNAx1sTXaUzxwpy0M9Ctj6Tih/Q==",
"versionMac": "cn2sAK6l9p1/w9deJVUuW3h7br056mpv5srvALiYw+g="
}
""".data(using: .utf8)!
""".utf8)
let masterkeyFile = try MasterkeyFile.withContentFromData(data: data)
let kek: [UInt8] = [
0x8C, 0xF4, 0xA0, 0x4E, 0xC8, 0x45, 0xF4, 0x28,
Expand Down Expand Up @@ -211,7 +211,7 @@ class MasterkeyFileTests: XCTestCase {

func testChangePassphrase() throws {
let expectedKey = [UInt8](repeating: 0x00, count: 32)
let data = """
let data = Data("""
{
"version": 7,
"scryptSalt": "AAAAAAAAAAA=",
Expand All @@ -221,7 +221,7 @@ class MasterkeyFileTests: XCTestCase {
"hmacMasterKey": "mM+qoQ+o0qvPTiDAZYt+flaC3WbpNAx1sTXaUzxwpy0M9Ctj6Tih/Q==",
"versionMac": "cn2sAK6l9p1/w9deJVUuW3h7br056mpv5srvALiYw+g="
}
""".data(using: .utf8)!
""".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]())
Expand Down

0 comments on commit d4e8f6d

Please sign in to comment.