[英]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.