diff --git a/Sources/curve_secp256k1/curve_secp256k1.xcframework/curve_secp256k1.xcframework/Info.plist b/Sources/curve_secp256k1/curve_secp256k1.xcframework/curve_secp256k1.xcframework/Info.plist new file mode 100644 index 0000000..33b9715 --- /dev/null +++ b/Sources/curve_secp256k1/curve_secp256k1.xcframework/curve_secp256k1.xcframework/Info.plist @@ -0,0 +1,59 @@ + + + + + AvailableLibraries + + + BinaryPath + libsecp256k1_rs.a + LibraryIdentifier + ios-arm64 + LibraryPath + libsecp256k1_rs.a + SupportedArchitectures + + arm64 + + SupportedPlatform + ios + + + BinaryPath + libsecp256k1_rs.a + LibraryIdentifier + ios-arm64_x86_64-simulator + LibraryPath + libsecp256k1_rs.a + SupportedArchitectures + + arm64 + x86_64 + + SupportedPlatform + ios + SupportedPlatformVariant + simulator + + + BinaryPath + libsecp256k1_rs.a + LibraryIdentifier + macos-arm64_x86_64 + LibraryPath + libsecp256k1_rs.a + SupportedArchitectures + + arm64 + x86_64 + + SupportedPlatform + macos + + + CFBundlePackageType + XFWK + XCFrameworkFormatVersion + 1.0 + + diff --git a/Sources/curve_secp256k1/curve_secp256k1.xcframework/curve_secp256k1.xcframework/ios-arm64/libsecp256k1_rs.a b/Sources/curve_secp256k1/curve_secp256k1.xcframework/curve_secp256k1.xcframework/ios-arm64/libsecp256k1_rs.a new file mode 100644 index 0000000..cb1fc64 Binary files /dev/null and b/Sources/curve_secp256k1/curve_secp256k1.xcframework/curve_secp256k1.xcframework/ios-arm64/libsecp256k1_rs.a differ diff --git a/Sources/curve_secp256k1/curve_secp256k1.xcframework/curve_secp256k1.xcframework/ios-arm64_x86_64-simulator/libsecp256k1_rs.a b/Sources/curve_secp256k1/curve_secp256k1.xcframework/curve_secp256k1.xcframework/ios-arm64_x86_64-simulator/libsecp256k1_rs.a new file mode 100644 index 0000000..4e42042 Binary files /dev/null and b/Sources/curve_secp256k1/curve_secp256k1.xcframework/curve_secp256k1.xcframework/ios-arm64_x86_64-simulator/libsecp256k1_rs.a differ diff --git a/Sources/curve_secp256k1/curve_secp256k1.xcframework/curve_secp256k1.xcframework/macos-arm64_x86_64/libsecp256k1_rs.a b/Sources/curve_secp256k1/curve_secp256k1.xcframework/curve_secp256k1.xcframework/macos-arm64_x86_64/libsecp256k1_rs.a new file mode 100644 index 0000000..7e586a2 Binary files /dev/null and b/Sources/curve_secp256k1/curve_secp256k1.xcframework/curve_secp256k1.xcframework/macos-arm64_x86_64/libsecp256k1_rs.a differ diff --git a/Sources/curvelib/encryption/Encryption.swift b/Sources/curvelib/encryption/Encryption.swift index 6081c84..a94ecdf 100644 --- a/Sources/curvelib/encryption/Encryption.swift +++ b/Sources/curvelib/encryption/Encryption.swift @@ -8,10 +8,37 @@ import Foundation import curveSecp256k1 #endif +extension Data { + var hexString: String { + return map { String(format: "%02x", $0) }.joined() + } + + init?(hexString: String) { + // Ensure the string has an even number of characters + guard hexString.count % 2 == 0 else { return nil } + + var data = Data(capacity: hexString.count / 2) + + // Convert each pair of characters to a byte and append to data + var index = hexString.startIndex + while index < hexString.endIndex { + let nextIndex = hexString.index(index, offsetBy: 2) + if let byte = UInt8(hexString[index.. EncryptedMessage { + public static func encrypt(pk: PublicKey, data: Data) throws -> EncryptedMessage { var errorCode: Int32 = -1 - let stringPtr = UnsafeMutablePointer(mutating: (plainText as NSString).utf8String) + let stringPtr = UnsafeMutablePointer(mutating: (data.hexString as NSString).utf8String) let result = withUnsafeMutablePointer(to: &errorCode, { error in curve_secp256k1_aes_cbc_hmac_encrypt(pk.pointer, stringPtr, error) }) @@ -23,7 +50,7 @@ public final class Encryption { return EncryptedMessage(ptr: result!) } - public static func decrypt(sk: SecretKey, encrypted: EncryptedMessage, skipMacCheck:Bool = false) throws -> String { + public static func decrypt(sk: SecretKey, encrypted: EncryptedMessage, skipMacCheck:Bool = false) throws -> Data { var errorCode: Int32 = -1 let result = withUnsafeMutablePointer(to: &errorCode, { error in curve_secp256k1_aes_cbc_hmac_decrypt(sk.pointer, encrypted.pointer, skipMacCheck, error) @@ -35,6 +62,10 @@ public final class Encryption { let value = String(cString: result!) curve_secp256k1_string_free(result) - return value + + guard let result = Data(hexString: value) else { + throw CurveError(code: 9) + } + return result } } diff --git a/Tests/curvelibTests/curvelibTests.swift b/Tests/curvelibTests/curvelibTests.swift index ef98284..6bb3e08 100644 --- a/Tests/curvelibTests/curvelibTests.swift +++ b/Tests/curvelibTests/curvelibTests.swift @@ -57,21 +57,21 @@ final class curvelibTests: XCTestCase { let sk = SecretKey() let pk = try sk.toPublic() let plainText = "this is testing data"; - let encrypted = try Encryption.encrypt(pk: pk, plainText: plainText) + let encrypted = try Encryption.encrypt(pk: pk, data: plainText.data(using: .utf8)! ) let cipherText = try encrypted.chipherText() let ephemeralPk = try encrypted.ephemeralPublicKey() let iv = try encrypted.iv() let mac = try encrypted.mac() let components = try EncryptedMessage(cipherText: cipherText, ephemeralPublicKey: ephemeralPk, iv: iv, mac: mac) let decrypted = try Encryption.decrypt(sk: sk, encrypted: components) - XCTAssertEqual(plainText, decrypted) + XCTAssertEqual(plainText.data(using: .utf8)!, decrypted) } func testEncryptionSkipMacCheck() throws { let sk = SecretKey() let pk = try sk.toPublic() let plainText = "this is testing data"; - let encrypted = try Encryption.encrypt(pk: pk, plainText: plainText) + let encrypted = try Encryption.encrypt(pk: pk, data: plainText.data(using: .utf8)!) let cipherText = try encrypted.chipherText() let ephemeralPk = try encrypted.ephemeralPublicKey() let iv = try encrypted.iv() @@ -79,6 +79,6 @@ final class curvelibTests: XCTestCase { let decrypted = try Encryption.decrypt(sk: sk, encrypted: components, skipMacCheck: true) XCTAssertThrowsError(try Encryption.decrypt(sk: sk, encrypted: components, skipMacCheck: false)) - XCTAssertEqual(plainText, decrypted) + XCTAssertEqual(plainText.data(using: .utf8)!, decrypted) } }