gpt4 book ai didi

ios - SecKeyCopyKeyExchangeResult 在 iOS 13 中不能用作 nodejs ecdh

转载 作者:行者123 更新时间:2023-11-28 05:37:27 41 4
gpt4 key购买 nike

我有一个应用程序,我在 secp521r1 曲线上使用 ECDH 在 iOS/android/nodejs 之间生成共享 key 。为了确保它能正常工作,我编写了一个测试,该测试使用由 nodejs 生成的数据,并且在 iOS 13 之前一直有效。

测试的源代码是:

let publicKey = "BABBvZ56c4bj1Zo73LIt/bBVa3jvGTA1fceoOG/M9TeXHx5ffCggRteEVS+bwrgQWPOwJPHhevNenaVn32ZnhztS0QFBqKGZTF1pKNSvuj+PDKQ625TauNroq+LQdeS+Pn6GVHL0iW5pp84NZ06L97VZ9HYm+g2lMnlUFV8hco2CmwBqHQ=="
let privateKey = "AXn994UN59QCEqmCmXmmNZ3hVZPlMwzTIeBupJGG4CqDWfWLuCTui7qiBfQtCFcQ1ks4NNB/tHEZUJ+bB97+pkJ3"
let otherBase64 = "BAAzWyzdh2e+ZNUCFt4oDADURb8+m9WA7gbWtTo57ZP3U23VuvMnRHf+12GpTSV8A5pt+vZfaR2cT02P+LPRc/kGzgAT2IYIgDz/cKbzMi520ZLa0GYk1xzCuNqFhdBZmrB5w0ymsPLdJzIG1QZ3xu7OufEipm5D41abphLLnbH+OyTX6w=="
let expectedShared = "AQkTOOHPcvlXufR2dm1FHaIJRlTgmxTJMI+h0kJ+nMVNopIP+opSqUNmflsgnJzT8JTodd/eehaaq5vvYdDVciIQ"

// iOS secKey is reconstructed by concatenating public and private key
let otherDataKey = Data.init(base64Encoded: otherBase64)!
var concatenatedKey = Data.init(base64Encoded: publicKey)!
concatenatedKey.append(Data.init(base64Encoded: privateKey)!)

// generate private key
var attributes: [String:Any] =
[
kSecAttrKeyClass as String: kSecAttrKeyClassPrivate,
kSecAttrKeySizeInBits as String: 521,
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
]
var error: Unmanaged<CFError>?
guard let secKey = SecKeyCreateWithData(concatenatedKey as CFData, attributes as CFDictionary, &error) else {
XCTAssertTrue(false)
return
}

// generate other public key
attributes[kSecAttrKeyClass as String] = kSecAttrKeyClassPublic
guard let otherKey = SecKeyCreateWithData(otherDataKey as CFData, attributes as CFDictionary, nil) else {
XCTAssertTrue(false)
return
}

// generate shared secret
let exchangeOptions: [String: Any] = [:]
guard let shared = SecKeyCopyKeyExchangeResult(secKey, SecKeyAlgorithm.ecdhKeyExchangeStandardX963SHA256, otherKey, exchangeOptions as CFDictionary, &error) else {
XCTAssertTrue(false)
return
}

// generate shared secret
XCTAssertEqual((shared as Data).base64EncodedString(), expectedShared);

在 iOS 13 中,我被迫修改我的 exchangeOptions 字典的内容,如此处讨论 (SecKeyCopyKeyExchangeResult() function return an error, "kSecKeyKeyExchangeParameterRequestedSize is missing")

let exchangeOptions: [String: Any] = [SecKeyKeyExchangeParameter.requestedSize.rawValue as String: 66]

问题是使用此选项时,SecKeyCopyKeyExchangeResult 的结果不再与 nodejs 匹配(在 iOS 12 上也是如此)

最佳答案

我终于找到了解决方案...在 iOS <= 12 中,尝试使用 ecdhKeyExchangeStandardX963SHA256 算法时将交换参数留空会退回到使用 SecKeyAlgorithm.ecdhKeyExchangeCofactor。

因此,重现先前行为的修复方法是使用

修改 SecKeyCopyKeyExchangeResult
// generate shared secret
let exchangeOptions: [String: Any] = [:]
guard let shared = SecKeyCopyKeyExchangeResult(secKey, SecKeyAlgorithm.ecdhKeyExchangeCofactor, otherKey, exchangeOptions as CFDictionary, &error) else {
XCTAssertTrue(false)
return
}

这至少适用于 iOS 10 到 13

关于ios - SecKeyCopyKeyExchangeResult 在 iOS 13 中不能用作 nodejs ecdh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58199716/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com