diff --git a/Sources/CryptomatorCryptoLib/Masterkey.swift b/Sources/CryptomatorCryptoLib/Masterkey.swift index 230a405..656ec4d 100644 --- a/Sources/CryptomatorCryptoLib/Masterkey.swift +++ b/Sources/CryptomatorCryptoLib/Masterkey.swift @@ -44,6 +44,19 @@ public class Masterkey { return createFromRaw(aesMasterKey: aesMasterKey, macMasterKey: macMasterKey) } + /** + Creates masterkey from raw bytes. + + - Parameter rawKey: Key combination of `aesMasterKey` (used for encryption of file specific keys) and `macMasterKey` (used for file authentication). + - Returns: New masterkey instance using the keys from the supplied raw bytes. + */ + public static func createFromRaw(rawKey: [UInt8]) -> Masterkey { + assert(rawKey.count == 2 * kCCKeySizeAES256) + let aesMasterkey = Array(rawKey[0 ..< kCCKeySizeAES256]) + let macMasterKey = Array(rawKey[kCCKeySizeAES256...]) + return createFromRaw(aesMasterKey: aesMasterkey, macMasterKey: macMasterKey) + } + /** Creates masterkey from raw bytes. @@ -51,7 +64,7 @@ public class Masterkey { - Parameter macMasterKey: Key used for file authentication. - Returns: New masterkey instance using the keys from the supplied raw bytes. */ - public static func createFromRaw(aesMasterKey: [UInt8], macMasterKey: [UInt8]) -> Masterkey { + static func createFromRaw(aesMasterKey: [UInt8], macMasterKey: [UInt8]) -> Masterkey { assert(aesMasterKey.count == kCCKeySizeAES256) assert(macMasterKey.count == kCCKeySizeAES256) return Masterkey(aesMasterKey: aesMasterKey, macMasterKey: macMasterKey) diff --git a/Tests/CryptomatorCryptoLibTests/MasterkeyTests.swift b/Tests/CryptomatorCryptoLibTests/MasterkeyTests.swift index 485f7db..6adcbe0 100644 --- a/Tests/CryptomatorCryptoLibTests/MasterkeyTests.swift +++ b/Tests/CryptomatorCryptoLibTests/MasterkeyTests.swift @@ -13,7 +13,7 @@ class MasterkeyTests: XCTestCase { func testCreateFromRaw() throws { let aesMasterKey = [UInt8](repeating: 0x77, count: 32) let macMasterKey = [UInt8](repeating: 0x55, count: 32) - let masterkey = Masterkey.createFromRaw(aesMasterKey: aesMasterKey, macMasterKey: macMasterKey) + let masterkey = Masterkey.createFromRaw(rawKey: aesMasterKey + macMasterKey) XCTAssertEqual(aesMasterKey, masterkey.aesMasterKey) XCTAssertEqual(macMasterKey, masterkey.macMasterKey) }