Skip to content

Commit

Permalink
fix: encryption via data type
Browse files Browse the repository at this point in the history
  • Loading branch information
ieow committed Apr 5, 2024
1 parent 807bdf5 commit 6c7fb78
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AvailableLibraries</key>
<array>
<dict>
<key>BinaryPath</key>
<string>libsecp256k1_rs.a</string>
<key>LibraryIdentifier</key>
<string>ios-arm64</string>
<key>LibraryPath</key>
<string>libsecp256k1_rs.a</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
</dict>
<dict>
<key>BinaryPath</key>
<string>libsecp256k1_rs.a</string>
<key>LibraryIdentifier</key>
<string>ios-arm64_x86_64-simulator</string>
<key>LibraryPath</key>
<string>libsecp256k1_rs.a</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>ios</string>
<key>SupportedPlatformVariant</key>
<string>simulator</string>
</dict>
<dict>
<key>BinaryPath</key>
<string>libsecp256k1_rs.a</string>
<key>LibraryIdentifier</key>
<string>macos-arm64_x86_64</string>
<key>LibraryPath</key>
<string>libsecp256k1_rs.a</string>
<key>SupportedArchitectures</key>
<array>
<string>arm64</string>
<string>x86_64</string>
</array>
<key>SupportedPlatform</key>
<string>macos</string>
</dict>
</array>
<key>CFBundlePackageType</key>
<string>XFWK</string>
<key>XCFrameworkFormatVersion</key>
<string>1.0</string>
</dict>
</plist>
Binary file not shown.
Binary file not shown.
Binary file not shown.
39 changes: 35 additions & 4 deletions Sources/curvelib/encryption/Encryption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<nextIndex], radix: 16) {
data.append(byte)
} else {
return nil // Invalid hexadecimal character
}
index = nextIndex
}

self = data
}
}

public final class Encryption {
public static func encrypt(pk: PublicKey, plainText: String) throws -> EncryptedMessage {
public static func encrypt(pk: PublicKey, data: Data) throws -> EncryptedMessage {
var errorCode: Int32 = -1
let stringPtr = UnsafeMutablePointer<Int8>(mutating: (plainText as NSString).utf8String)
let stringPtr = UnsafeMutablePointer<Int8>(mutating: (data.hexString as NSString).utf8String)
let result = withUnsafeMutablePointer(to: &errorCode, { error in
curve_secp256k1_aes_cbc_hmac_encrypt(pk.pointer, stringPtr, error)
})
Expand All @@ -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)
Expand All @@ -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
}
}
8 changes: 4 additions & 4 deletions Tests/curvelibTests/curvelibTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,28 @@ 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()
let components = try EncryptedMessage(cipherText: cipherText, ephemeralPublicKey: ephemeralPk, iv: iv, mac: "")
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)
}
}

0 comments on commit 6c7fb78

Please sign in to comment.