I am trying to create a derived key from my shared secret and shared key. I have done this before, in Java and Python, where it is a fairly straight forward process, however, it seems that in C this requires a few more steps.
Here is my working Java code:
HKDFBytesGenerator hkdf2 = new HKDFBytesGenerator(digest);
hkdf2.init(new HKDFParameters(sharedSecret, mobileDeviceEphemeralPublicKey, info));
byte[] sharedKey = new byte[48];
hkdf2.generateBytes(sharedKey, 0, sharedKey.length);
Where info is:
[-63, 20, 124, -15, -40, -14, -53, 23, 15, 24, -38, 39, 34, 28, -43, 47, 46, 37, 107, -15, 4, -92, -117, -45, 75, -7, -35, -80, -82, 78, 109, 95, -38, 40, -5, -123, 85, -79, 33, -96, 17, -86, 106, -121, -17, -52, -7, -111, -77, 11, 105, -117, 110, 12, -87, 19, 44, -74, 58, 95, 87, -98, -90, 19, 2, 13, 76, 41, 3, 44, 54, 4, 76, -11, 27, -82, 70, -107, -1, 36, -114, 41, -46, 122, -60, -111, -111, -14, -105, -85, -113, -74, 118, 7, -127, -100, 115, 37, -13, 71, -113, 48, 68, 2, 32, 83, -33, 54, -35, -77, 4, -95, -55, -128, -66, 126, -46, -23, -43, -63, -41, 66, 92, 104, 85, -19, 113, 85, -9, 95, -97, -4, -58, 89, 88, -76, -36, 2, 32, 119, -73, 86, -9, -45, 21, -66, -56, -101, -107, 118, -78, 51, -48, -118, 91, -14, 51, -40, -115, -7, -5, 58, -60, -75, 22, 29, 40, 13, -110, 94, 36]
mobileDeviceEphemeralPublicKey is:
[3, -13, 114, -79, -98, 38, 121, -52, -109, -35, 111, 100, 43, 78, -96, 42, -28, -32, -75, -87, 83, 22, -22, -105, 69, -109, 47, -23, 127, -62, -128, -109, -99]
And sharedSecret:
[-27, -24, -42, 64, 23, 20, 36, 24, 46, 45, 57, -60, -53, 5, 106, 82, -128, -126, 66, 62, 42, -60, -72, -75, -90, -38, 54, -97, 98, -67, 29, 59]
And as a result my sharedKey is
-85, 26, -57, -120, -55, -12, 58, 22, -36, -86, 40, -21, -69, -109, -86, 2, 2, 26, 87, 97, 51, -56, -16, -71, 95, -115, 9, -45, -98, 125, 35, -12, 100, 41, -68, -68, 9, -40, 43, 74, 108, 27, -101, -98, -67, 85, 119, -108
My broken C code is:
unsigned char *sharedKey = (unsigned char *)malloc(sharedKey_len);
if (sharedKey == NULL) {
set_error(error, "Failed to allocate memory for shared key");
return NULL;
}
if (EVP_PKEY_derive_init(pctx) <= 0) {
ERR_print_errors_fp(stderr);
set_error(error, "Failed to initialize HKDF");
EVP_PKEY_CTX_free(pctx);
free(sharedKey);
return NULL;
}
if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0) {
ERR_print_errors_fp(stderr);
set_error(error, "Failed to set HKDF hash function");
EVP_PKEY_CTX_free(pctx);
free(sharedKey);
return NULL;
}
if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, mobileDeviceEphemeralPublicKey, mobileDeviceEphemeralPublicKey_len) <= 0) {
ERR_print_errors_fp(stderr);
set_error(error, "Failed to set HKDF salt");
EVP_PKEY_CTX_free(pctx);
free(sharedKey);
return NULL;
}
if (EVP_PKEY_CTX_set1_hkdf_key(pctx, sharedSecret, sharedSecret_len) <= 0) {
ERR_print_errors_fp(stderr);
set_error(error, "Failed to set HKDF key");
EVP_PKEY_CTX_free(pctx);
free(sharedKey);
return NULL;
}
if (EVP_PKEY_CTX_add1_hkdf_info(pctx, info, info_len) <= 0) {
ERR_print_errors_fp(stderr);
set_error(error, "Failed to add HKDF info");
EVP_PKEY_CTX_free(pctx);
free(sharedKey);
return NULL;
}
printf("pctx: %pn", pctx);
if (EVP_PKEY_derive(pctx, sharedKey, &sharedKey_len) <= 0) {
ERR_print_errors_fp(stderr);
set_error(error, "HKDF operation failed");
EVP_PKEY_CTX_free(pctx);
free(sharedKey);
return NULL;
}
EVP_PKEY_CTX_free(pctx);
return sharedKey; // Return the derived shared key
}
However, I get the error “HKDF operation failed”. Any ideas would be greatly appreciated, I’ve spent a while trying to crack this ????
Tom Rowbotham is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.