2

I'm developing an iOS app and i would like to implemente ECDH for security. I used this app (https://github.com/DigitalLeaves/AsymmetricCrypto) to generate a pair of key (private and public key), using :

private let kAsymmetricCryptoManagerKeyType = kSecAttrKeyTypeEC
private let kAsymmetricCryptoManagerKeySize = 256
private let kAsymmetricCryptoManagerCypheredBufferSize = 128
private let kAsymmetricCryptoManagerSecPadding: SecPadding = .PKCS1

So, i can get the private and public key as strings as follow :

private key :3b3aef4e27c61e376bb1440f6a3077199d6d5ec665f6cd3595483a05ed96c498
public key X :3a405b5877d2939aea9dfb2995a89f4d63fca3c0cbb2a9d066fe67a08c499163
public key Y : 69bdbfc0ddea97ee03e85eb335db589cfcbee54b71f7fd74f6bc7344b5539ed6

That works perfectly.

Now, what i want is to perform a ECDH to generate a shared secret between the private key that i have generated and a public key that i get (from another platform) like a string as :

X: b1a2166411655482ad39630a480768dde4ccce5af4c53edec82496f17f0ddbfd
Y: e6dd01508da28f4f2295d9fee86239f88e8c5cbc94dbbb1f814b27c85d0d971a

So the first question is how to transform these two last lines of string into a SecKey to produce a public key.

And the second question is how to perform a ECDH between the private key that i have generated (as a SecKey) and the new public key that i wanna get in my first question.

Here is a bit of code i used to generate my key pair :

 func createSecureKeyPair(_ completion: ((_ success: Bool, _ error: AsymmetricCryptoException?) -> Void)? = nil) {
        // private key parameters
        let privateKeyParams: [String: AnyObject] = [
            kSecAttrIsPermanent as String: true as AnyObject,
            kSecAttrApplicationTag as String: kAsymmetricCryptoManagerApplicationTag as AnyObject
        ]

        //public key parameters
        let publicKeyParams: [String: AnyObject] = [
            kSecAttrIsPermanent as String: true as AnyObject,
            kSecAttrApplicationTag as String: kAsymmetricCryptoManagerApplicationTag as AnyObject
        ]

        // global parameters for our key generation
        let parameters: [String: AnyObject] = [
            kSecAttrKeyType as String:          kAsymmetricCryptoManagerKeyType,
            kSecAttrKeySizeInBits as String:    kAsymmetricCryptoManagerKeySize as AnyObject,
            kSecPublicKeyAttrs as String:       publicKeyParams as AnyObject,
            kSecPrivateKeyAttrs as String:      privateKeyParams as AnyObject,
        ]

        // asynchronously generate the key pair and call the completion block
        DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async { () -> Void in
            var pubKey, privKey: SecKey?
            let status = SecKeyGeneratePair(parameters as CFDictionary, &pubKey, &privKey)
            print("pub :",pubKey)

I wanna have a shared secret like on this website : http://www-cs-students.stanford.edu/~tjw/jsbn/ecdh.html with secp256r1 parameteres

Thanks in advance.

David R.
  • 51
  • 1
  • 8

1 Answers1

0

I got the your SecKey from x and y specified via code:

<SecKeyRef curve type: kSecECCurveSecp384r1, algorithm id: 3, key type: ECPublicKey, version: 4, block size: 384 bits, y: 7BA75DD35E74F1D6B6F1FE1FDB6F7977D7DE7BCEB6DFD7FCF1EF1CE5C6DCF7875B6DBD5FF35E1BDBB73CE5DD1DF7BD5A, x: 6F56B6D7AEB8D75EB9E78F3669DDFDEB7D1AE3CD3BEBC75D7B871C71EE5A7F8739DDE75E73CDB8F7A7F5EDFD1D75B7DD, addr: 0x7fda4a60d510>

you can follow my article.

https://medium.com/@vaibhav.pmeshram/creating-and-dismantling-ec-key-in-swift-f5bde8cb633f

vaibby
  • 1,255
  • 1
  • 9
  • 23