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.