[英]ECC Curve25519 KeyChain
我正在尝试使用 iOS 上的 KeyChain 创建 Curve25519 密钥。 我知道 CryptoKit 的存在,不幸的是,它不适用于 iOS 12。有没有办法在 CryptoKit 之前创建一个 Curve25519 密钥,也许我在 KeyChain 中生成它时缺少一个参数? 下面的代码只会生成 P-256 密钥。
let attributes: [String: Any] = [
String(kSecClass): kSecClassKey,
String(kSecAttrKeyType): kSecAttrKeyTypeECSECPrimeRandom,
String(kSecAttrKeySizeInBits): 256
]
var error: Unmanaged<CFError>?
let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error)
print(privateKey ?? error!.takeUnretainedValue())
Apple 的旧核心加密库CommonCrypto
不支持像curve25519
这样的现代曲线,坦率地说,这完全是一团糟,散布着不安全的密码,他们甚至不清楚正在使用的实际曲线方程。
另外,虽然CryptoKit
支持curve25519
进行密钥交换,但它仍然是有限的,例如,你不能使用“Secure Enclave”来生成curve25519
密钥,只有P-256
,这很可能是后门的(看看曲线curve25519
),尽管所有金融机构似乎都喜欢它。
最终, curve25519
私钥只是一个大 ( 2^256
) 数字(尽管它在使用前被“限制”了),因此如果您只需要生成密钥,您可以使用SecRandomCopyBytes
来做到这SecRandomCopyBytes
。
不过,如果因为我怀疑你想要做一些X25519
KEX或EdDSA
在签名25519
,那么就使用libsodium
。 它是NaCl
的 goto 库,在Swift
有一个由libsodium
原作者编写的非常棒的界面,称为swift-sodium
,我用过它,它很棒。 它还支持 iOS 12+。
在libsodium
为curve25519
生成密钥非常简单:
import Sodium
let sodium = Sodium()
let curve25519KeyPair = sodium.box.keyPair()
let privateKey = curve25519KeyPair!.secretKey
let publicKey = curve25519KeyPair!.publicKey
然后您可以手动存储在 KeyChain 中。
如果您需要进一步的帮助,请25519
,使用25519
不错的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.