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.
Ta.here's the original string without any tampering :)
Decoding that I get this ASN.1:
Code Block % dumpasn1 -a public-key.asn1 0 305: SEQUENCE { 4 234: SEQUENCE { 7 7: OBJECT IDENTIFIER ecPublicKey (1 2 840 10045 2 1) 16 222: SEQUENCE { 19 1: INTEGER 1 22 43: SEQUENCE { 24 7: OBJECT IDENTIFIER prime-field (1 2 840 10045 1 1) 33 32: INTEGER : 7F FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF : FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ED : } 67 68: SEQUENCE { 69 32: OCTET STRING : 2A AA AA AA AA AA AA AA AA AA AA AA AA AA AA AA : AA AA AA AA AA AA AA AA AA AA AA 98 49 14 A1 44 103 32: OCTET STRING : 7B 42 5E D0 97 B4 25 ED 09 7B 42 5E D0 97 B4 25 : ED 09 7B 42 5E D0 97 B4 26 0B 5E 9C 77 10 C8 64 : } 137 65: OCTET STRING : 04 2A AA AA AA AA AA AA AA AA AA AA AA AA AA AA : AA AA AA AA AA AA AA AA AA AA AA AA AA AA AD 24 : 5A 20 AE 19 A1 B8 A0 86 B4 E0 1E DD 2C 77 48 D1 : 4C 92 3D 4D 7E 6D 7C 61 B2 29 E9 C5 A2 7E CE D3 : D9 204 32: INTEGER : 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 : 14 DE F9 DE A2 F7 9C D6 58 12 63 1A 5C F5 D3 ED 238 1: INTEGER 8 : } : } 241 66: BIT STRING : 04 5A 26 73 A7 F9 8F 7D 04 79 D5 93 4E A4 47 9F : C9 16 01 23 C1 6C 14 DD 58 FE 55 E1 C6 C9 01 F2 : 2C 6B 3C 0E A8 1E 55 FE 1A D8 67 DA CC 5F A3 F4 : 72 8C 5A D4 B7 0C 21 D0 58 A8 DA 46 1A E0 E6 A6 : 72 : } 0 warnings, 0 errors.
and this hex dump:
Code Block % xxd public-key.asn1 00000000: 3082 0131 3081 ea06 072a 8648 ce3d 0201 0..10....*.H.=.. 00000010: 3081 de02 0101 302b 0607 2a86 48ce 3d01 0.....0+..*.H.=. 00000020: 0102 207f ffff ffff ffff ffff ffff ffff .. ............. 00000030: ffff ffff ffff ffff ffff ffff ffff ffff ................ 00000040: ffff ed30 4404 202a aaaa aaaa aaaa aaaa ...0D. *........ 00000050: aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa ................ 00000060: aaaa 9849 14a1 4404 207b 425e d097 b425 ...I..D. {B^...% 00000070: ed09 7b42 5ed0 97b4 25ed 097b 425e d097 ..{B^...%..{B^.. 00000080: b426 0b5e 9c77 10c8 6404 4104 2aaa aaaa .&.^.w..d.A.*... 00000090: aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa ................ 000000a0: aaaa aaaa aaaa aaaa aaad 245a 20ae 19a1 ..........$Z ... 000000b0: b8a0 86b4 e01e dd2c 7748 d14c 923d 4d7e .......,wH.L.=M~ 000000c0: 6d7c 61b2 29e9 c5a2 7ece d3d9 0220 1000 m|a.)...~.... .. 000000d0: 0000 0000 0000 0000 0000 0000 0000 14de ................ 000000e0: f9de a2f7 9cd6 5812 631a 5cf5 d3ed 0201 ......X.c.\..... 000000f0: 0803 4200 045a 2673 a7f9 8f7d 0479 d593 ..B..Z&s...}.y.. 00000100: 4ea4 479f c916 0123 c16c 14dd 58fe 55e1 N.G....#.l..X.U. 00000110: c6c9 01f2 2c6b 3c0e a81e 55fe 1ad8 67da ....,k<...U...g. 00000120: cc5f a3f4 728c 5ad4 b70c 21d0 58a8 da46 ._..r.Z...!.X..F 00000130: 1ae0 e6a6 72 ....r
As you can see the key bytes are the last 65 bytes of the data. So, you have a couple of options here:
You can this properly by parsing the ASN.1 SubjectPublicKeyInfo structure yourself. For this you’ll need to either write or acquire an ASN.1 parser )-:
You can bodgy this by just extracting the last 65 bytes of the data (someData.suffix(65)). The problem with this is that it only works for this specific key type and size.
Please post your bug number, just for the record.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"