Convert key rawRepresentation to x963Representation to create SecKey

Hello

Here is a problem with creating SecKey for ios 10 after key derivation (HMAC + sha512)

After derivation we get 32 bytes of private key and chainCode There is no problem to use this bytes to create P256 private key with function from iOS13

try? P256.Signing.PrivateKey(rawRepresentation: bytes)

and then to get x963Representation to create a SecKey

But on iOS10 - 12 we don't have an ability to use this function... So I have to convert 32 bites key to 97 somehow to represent it in ANSI x9.63 format

Example of key bytes

[110, 181, 159, 0, 54, 16, 25, 129, 87, 128, 85, 36, 192, 64, 195, 4, 20, 47, 243, 134, 160, 57, 30, 210, 89, 225, 223, 114, 11, 121, 57, 156]

x963Representation

[4, 37, 167, 241, 121, 238, 41, 22, 35, 158, 89, 144, 215, 243, 4, 91, 217, 243, 23, 42, 171, 228, 247, 89, 136, 123, 22, 71, 11, 205, 134, 29, 110, 83, 241, 239, 135, 37, 226, 40, 179, 11, 191, 193, 232, 124, 41, 160, 136, 53, 95, 33, 233, 207, 151, 83, 136, 234, 97, 4, 79, 115, 227, 69, 42, 252, 66, 68, 64, 32, 176, 11, 75, 206, 158, 228, 246, 9, 179, 36, 94, 186, 209, 125, 152, 192, 192, 141, 242, 200, 108, 181, 75, 103, 86, 171, 231]

I see that x963Representation contains:

header(04) + ? + ? + 32_Key_Bytes So the question is - how to count x and y to get the key form (04 || X || Y || K) to be able to create SecKey?

I have a pair of pairs that explain this stuff in gory detail. Start with On Cryptographic Key Formats | Apple Developer Forums.

how to count x and y

In a raw SECG key, X, Y and K are all the same length based on the key’s length. So, for P-256, each will be 256 / 8, or 32 bytes long. That means that a private key is 97 bytes long.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Convert key rawRepresentation to x963Representation to create SecKey
 
 
Q