I'm using Curve25519 to generate the public-private key pair that's to be used for creating the shared Secret. The public key received from the server is but a very long message(base64 encoded string), which is about 309 bytes when converted to Data.
When I try to create the shared secret key using this data, CryptoKit throws the error "CryptoKit.CryptoKitError.incorrectKeySize" at this line:
Code Block swift let serverPublicKey = try! Curve25519.KeyAgreement.PublicKey(rawRepresentation: serverPublicKeyData)
I need the output of this line(serverPublicKey) for generating the shared secret.
Here's the rest of the code:
Code Block swift let clientPrivateKey = Curve25519.KeyAgreement.PrivateKey() let clientSharedSecret = try! clientPrivateKey.sharedSecretFromKeyAgreement(with: serverPublicKey) let clientSharedSecretyKey = clientSharedSecret.x963DerivedSymmetricKey(using: SHA256.self, sharedInfo: Data(), outputByteCount: 32)
and I'm encrypting using AES.GCM
Code Block swift let clientSealedBoxData = try! AES.GCM.seal(messageData, using: clientSharedSecretyKey).combined! let clientSealedBox = try! AES.GCM.SealedBox(combined: clientSealedBoxData)
where messageData is the data to be encrypted
Is there any way to compress the received public key to 32 bytes to pass it to the KeyAgreement line? (Also, is this the right method to generate the public key for encrypting?)
I've referred a lot of sites but couldn't arrive at a solution. Please help.