iOS Swift Diffie-Hellman key exchange to encrypt and decrypt messages? using Secure Enclave

為{幸葍}努か 提交于 2020-04-30 07:11:17

问题


How can i use the Diffie-Hellman key exchange to encrypt and decrypt messages?

I'am able to generate the shared keys (for both bob and alice) but SecKeyCopyKeyExchangeResult returns me a Data...how can i get SecKey to use with SecKeyCreateDecryptedData and SecKeyCreateEncryptedData ?

So i think i should extract the SecKey somehow from the shared data so i can make symettrical encryption/decryption.

The code so far is:


let bob_shared_secret: NSData = generateSharedKey_ecdh(publicKey: alicePublicKey, privateKey: bobPrivateKey)!
let alice_shared_secret: NSData = generateSharedKey_ecdh(publicKey: bobPublicKey, privateKey: alicePrivateKey)!

print("equals? \(bob_shared_secret == alice_shared_secret)!") //true


let clearText = "Hello From Alice"
let algorithm: SecKeyAlgorithm = .eciesEncryptionCofactorVariableIVX963SHA256AESGCM

let cipherTextData: Data? = SecKeyCreateEncryptedData(alicePublicKey, algorithm,
                                                              clearTextData as CFData,
                                                              &error) as Data?

let clearTextData = SecKeyCreateDecryptedData(???? as SecKey, //what to put here??
                                                          algorithm,
                                                          cipherTextData as CFData,
                                                          &error) as Data?

private func generateSharedKey_ecdh(publicKey: SecKey, privateKey: SecKey) -> NSData?
    {
        var error: Unmanaged<CFError>?

        let keyPairAttr:[String : Any] = [
            kSecAttrKeySizeInBits as String: 256, //retro compatibility
            kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom, //Elliptic curve algorithm.
            kSecPrivateKeyAttrs as String: [kSecAttrIsPermanent as String: false],
            kSecPublicKeyAttrs as String:[kSecAttrIsPermanent as String: false],
            SecKeyKeyExchangeParameter.requestedSize.rawValue as String: 256
        ]
        let algorithm:SecKeyAlgorithm = SecKeyAlgorithm.ecdhKeyExchangeStandardX963SHA256

        let shared:CFData? = SecKeyCopyKeyExchangeResult(privateKey, algorithm, publicKey, keyPairAttr as CFDictionary, &error)

        return shared
    }

Key Pair generation... keypair it's a class i created to contain keys


class KeyPair {
    var publicKey: SecKey
    var privateKey: SecKey
    init(publicKey: SecKey, privateKey: SecKey) {
        self.publicKey = publicKey
        self.privateKey = privateKey
    }
}

private func generateKeyPair() -> KeyPair? {
        let attributes: [String: Any] = [kSecAttrKeySizeInBits as String: 256,
                                         kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
                                         kSecPrivateKeyAttrs as String: [kSecAttrIsPermanent as String: false],
                                         kSecPublicKeyAttrs as String:[kSecAttrIsPermanent as String: false]]

        var error: Unmanaged<CFError>?

        if let privateKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error),
            let publicKey = SecKeyCopyPublicKey(privateKey){
            return KeyPair(publicKey: publicKey, privateKey: privateKey)
        }

        return nil
    }

回答1:


SecKeyCreateEncryptedData and SecKeyCreateDecryptedData is for asymetric encryption (using ECIES or RSA)

To perform a symetric encryption / decryption operation you should use common crypto or an encapsulation of it like Crypto swift (https://github.com/krzyzanowskim/CryptoSwift) and AES algorithm



来源:https://stackoverflow.com/questions/61091401/ios-swift-diffie-hellman-key-exchange-to-encrypt-and-decrypt-messages-using-sec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!