Hello fellow developers,
I'm currently working on an SDK involving the SECP256R1 standard and facing an interesting issue. My goal is to ensure the Swift implementation of SECP256R1 signatures matches that of Rust's FastCrypto implementation.
The Issue:
When running tests to compare signatures generated by Swift and Rust implementations, the signatures do not match.
Despite this mismatch, verification tests still succeed.
I've tried using both the P256 class from CryptoKit and SecKey from the Security SDK.
The Swift code is being written in Xcode 15 Beta 8, Swift 5.9.
Code Snippet:
struct SECP256R1PrivateKey {
/// Commented is P256, uncommented is SecKey
// public init(key: Data) throws {
// if let privateKey = try? P256.Signing.PrivateKey(rawRepresentation: key) {
// self.key = privateKey
// } else {
// throw AccountError.invalidData
// }
// }
public init(key: Data) throws {
if let privateKeyP256 = try? P256.Signing.PrivateKey(rawRepresentation: key) {
let attributes: [String: Any] = [
kSecAttrKeyClass as String: kSecAttrKeyClassPrivate,
kSecAttrKeyType as String: kSecAttrKeyTypeECDSA,
kSecAttrTokenID as String: kSecAttrTokenIDSecureEnclave,
kSecAttrKeySizeInBits as String: 256
]
var error: Unmanaged<CFError>?
guard let privateKey = SecKeyCreateWithData(privateKeyP256.rawRepresentation as CFData, attributes as CFDictionary, &error) else {
throw error?.takeRetainedValue() as Error? ?? NSError(domain: NSOSStatusErrorDomain, code: Int(errSecParam), userInfo: nil)
}
self.key = privateKey
} else {
throw AccountError.invalidData
}
}
// public func sign(data: Data) throws -> Signature {
// let signature = try self.key.signature(for: data)
// return Signature(
// signature: signature.rawRepresentation,
// publickey: try self.publicKey().key.compressedRepresentation,
// signatureScheme: .SECP256R1
// )
// }
public func sign(data: Data) throws -> Signature {
let dataHash = Data(data.sha256)
var error: Unmanaged<CFError>?
guard let signature = SecKeyCreateSignature(self.key, .ecdsaSignatureMessageX962SHA256, dataHash as NSData, &error) as Data? else {
throw error!.takeRetainedValue() as Error
}
guard let publicKey = SecKeyCopyExternalRepresentation(try self.publicKey().key, &error) as Data? else {
throw AccountError.invalidData
}
return Signature(
signature: signature,
publickey: publicKey,
signatureScheme: .SECP256R1
)
}
}
func testThatTheRustImplementationForSignaturesIsTheSame() throws {
let account = try Account(privateKey: Data(self.validSecp256r1SecretKey), accountType: .secp256r1)
guard let signData = "Hello, world!".data(using: .utf8) else { XCTFail("Unable to encode message"); return; }
let signature = try account.sign(signData)
XCTAssertEqual(
try signature.hex(),
"26d84720652d8bc4ddd1986434a10b3b7b69f0e35a17c6a5987e6d1cba69652f4384a342487642df5e44592d304bea0ceb0fae2e347fa3cec5ce1a8144cfbbb2"
)
}
The Core Question:
How do I implement the R1 signature in Swift so that it matches the signature generated by Rust's FastCrypto?
Any insights, suggestions, or sample code snippets that could guide me in the right direction would be immensely appreciated!
Thank you in advance!
Post
Replies
Boosts
Views
Activity
Hello,
On the recent iOS / iPadOS 16, SwiftUI 4, and Xcode Version 14.0 beta (14A5228q), my app contains a NavigationStack within the content view; and the stack contains a ForEach that iterates over a list of fruits / items. The form loop outputs a navigation link, the label being a view, and the destination being another NavigationStack. And whenever I go to click on that link, the app shoots me back to the root view instantly. This seems to happen whenever I have any kind of content in the StackNavigation that's within the NavigationLink. I will post the code snippets below, along with a GIF showing the bug in action.
ContentView.Swift:
struct ContentView: View {
@State private var isShowingSettings: Bool = false
var fruits: [Fruit] = fruitsData
var body: some View {
NavigationStack {
List(fruits.shuffled()) { fruit in
NavigationLink(destination: FruitDetailView(fruit: fruit)) {
FruitRowView(fruit: fruit)
.padding(.vertical, 4)
}
}
.navigationBarTitle("Fruits")
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
isShowingSettings = true
}) {
Image(systemName: "slider.horizontal.3")
}
.sheet(isPresented: $isShowingSettings) {
SettingsView()
}
}
}
}
}
}
FruitDetailView.Swift:
// Code is shortened, but has the same bug as the actual code
struct FruitDetailView: View {
var fruit: Fruit
var body: some View {
NavigationStack {
ScrollView(.vertical, showsIndicators: false) {
Text(fruit.name)
}
.edgesIgnoringSafeArea(.top)
}
}
Preview:
Hello there,
I am currently trying to run an AR App using RealityKit and SwiftUI for an iPhone running iOS 15. However, after making the project, I go to build it to test it out; and I encounter the error that the Reality Kit module failed to build, along with a couple other errors. I have attached all of the errors I got while running the template app. I will also provide any sort of details needed for fixing this issue. As well the Mac I’m using is a MacBook Pro M1.