SwCrypt icon indicating copy to clipboard operation
SwCrypt copied to clipboard

Problem with CC.EC.computeSharedSecret

Open NikSrg opened this issue 6 years ago • 0 comments

Hi @soyersoyer, first of all: great work, I really appreciate! So happy to find you repo, I was wasting endless hours till I found you :)

My problem is getting the shared secret for a elliptic curve - diffie-hellman - key agreement I have the public key from a server

let serverPublicKeyString = """
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEzgg42Iyyx+DZs4vql5tb/zmrF0zFnnfXOsBvmr+Q7MjXViCAiwgaxrNpGn3pN5f67qY3r7p+qUO6sVakjT82cg==
-----END PUBLIC KEY-----
"""

I created my own key pair in the app using: let clientKeys = try! CC.EC.generateKeyPair(256)

I created a data object from the server public key string: let serverKeyData = try! SwKeyConvert.PublicKey.pemToPKCS8DER(serverPublicKeyString)

But I can't figure why I don't get a shared secret through let shared = try? CC.EC.computeSharedSecret(clientKeys.0, publicKey: serverKeyData) Error: [generateKeyPair(_:)] SwCrypt.CC.CCError: paramError (-4300)

What I know about the server public key:

  • Created in Java ECGenParameterSpec("secp256r1")
  • Exported as base64. I added -----BEGIN PUBLIC KEY----- and -----END PUBLIC KEY----- myself in the iOS client

Am I missing a step? Looking forward to hear from you and once again: I really appreciate your great work!

Cheers Nick

NikSrg avatar Jan 31 '20 08:01 NikSrg