I cannot understand why my keys are not equal when executing this diffier hellman exchange example. I am using the openssl library in C (openssl / dh.h).
This seems pretty simple, but for some reason the keys do not match. What am I missing?
Any ideas? Thanks!
void hexprint(unsigned char *printBuf, int len) { int i; for(i = 0; i < len; i++) { printf("%x ", printBuf[i]); } printf("\n"); } int main(int argc, char *argv[]) { srand(time(NULL)); DH *dh1; DH *dh2; unsigned char *dh_secret1; unsigned char *dh_secret2; dh1 = DH_generate_parameters(256, 2, NULL, NULL); dh2 = DH_generate_parameters(256, 2, NULL, NULL); DH_generate_key(dh1); DH_generate_key(dh2); dh_secret1 = malloc(DH_size(dh1)); memset(dh_secret1, 0, DH_size(dh1)); dh_secret2 = malloc(DH_size(dh2)); memset(dh_secret2, 0, DH_size(dh2)); DH_compute_key(dh_secret1, dh2->pub_key, dh1); DH_compute_key(dh_secret2, dh1->pub_key, dh2); printf("Secret Key 1: \n"); hexprint(dh_secret1, 32); printf("Secret Key 2: \n"); hexprint(dh_secret2, 32); free(dh_secret1); free(dh_secret2); DH_free(dh1); DH_free(dh2); }
source share