diff --git a/Package.swift b/Package.swift index d6b9be0..6e1d89c 100644 --- a/Package.swift +++ b/Package.swift @@ -11,7 +11,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/johnnzhou/lcl-k1.git", branch: "main"), - .package(url: "https://github.com/johnnzhou/swift-crypto.git", branch: "main"), + .package(url: "https://github.com/johnnzhou/swift-crypto.git", branch: "main") ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. diff --git a/Sources/LCLPingAuth/Auth/Authenticate.swift b/Sources/LCLPingAuth/Auth/Authenticate.swift index f514b4f..621e3e0 100644 --- a/Sources/LCLPingAuth/Auth/Authenticate.swift +++ b/Sources/LCLPingAuth/Auth/Authenticate.swift @@ -60,6 +60,6 @@ public func validate(credential: Data) throws -> ValidationResult { h_pkr = digest(data: outputData, algorithm: .SHA256) outputData.removeAll() - + return ValidationResult(R: r, skT: skT, hPKR: h_pkr) } diff --git a/Sources/LCLPingAuth/Cryptography/ECDSA.swift b/Sources/LCLPingAuth/Cryptography/ECDSA.swift index d1e30c9..6e41aba 100644 --- a/Sources/LCLPingAuth/Cryptography/ECDSA.swift +++ b/Sources/LCLPingAuth/Cryptography/ECDSA.swift @@ -92,7 +92,6 @@ public class ECDSA { } } - extension K1.ECDSA.Signature { /// The data representation of signature in bytes var toData: Data { @@ -104,4 +103,3 @@ extension K1.ECDSA.Signature { return Data(byteBuffer) } } - diff --git a/Sources/LCLPingAuth/Models.swift b/Sources/LCLPingAuth/Models.swift index ea0c049..92fd5ba 100644 --- a/Sources/LCLPingAuth/Models.swift +++ b/Sources/LCLPingAuth/Models.swift @@ -17,12 +17,6 @@ struct Keys: Decodable { var sigmaT: String var skT: String var pk_a: String - - init(sigmaT: String, skT: String, pk_a: String) { - self.sigmaT = sigmaT - self.skT = skT - self.pk_a = pk_a - } } extension Keys { diff --git a/Sources/LCLPingAuth/Utils/Data+LCLPingAuth.swift b/Sources/LCLPingAuth/Utils/Data+LCLPingAuth.swift index 7234319..6e3b3f3 100644 --- a/Sources/LCLPingAuth/Utils/Data+LCLPingAuth.swift +++ b/Sources/LCLPingAuth/Utils/Data+LCLPingAuth.swift @@ -31,7 +31,7 @@ private func htoi(_ value: UInt8) throws -> UInt8 { } extension Data { - + /// Initialize the `Data` from the hex string value init(hexString: String) throws { self.init() @@ -52,7 +52,7 @@ extension Data { } extension ByteArray { - + /// The `Data` representation of the byte array var toData: Data { return Data(self) @@ -60,7 +60,7 @@ extension ByteArray { } extension Data { - + /// The hex String representation of the given `Data` var hex: String { reduce("") { $0 + String(format: "%02hhx", $1) } diff --git a/Sources/LCLPingAuth/Utils/Security+LCLPingAuth.swift b/Sources/LCLPingAuth/Utils/Security+LCLPingAuth.swift index 28205c4..98234c3 100644 --- a/Sources/LCLPingAuth/Utils/Security+LCLPingAuth.swift +++ b/Sources/LCLPingAuth/Utils/Security+LCLPingAuth.swift @@ -44,21 +44,21 @@ public func generateSecureRandomBytes(count: Int) -> Result Data { } } +/** + Encrypt the plaintext using the given symmetric key + + - Parameters: + - plainText: the plaintext data to be encrypted + - key: the symmetric key that will be used for encryption + + - Returns: the encrypted data +*/ +public func encrypt(plainText: Data, key: SymmetricKey) throws -> Data { + let box = try AES.GCM.seal(plainText, using: key) + return box.combined! +} + +/** + Decrypt the cipher data using the given symmetric key + + - Parameters: + - cipher: the cipher text to be decrypted + - key: the symmetric key that will be used for decryption + - Returns: the decrypted data +*/ +public func decrypt(cipher: Data, key: SymmetricKey) throws -> Data { + let box = try AES.GCM.SealedBox(combined: cipher) + return try AES.GCM.open(box, using: key) +}