Skip to main content

Diffie-Hellman Key Exchange Samples

Purpose

There are many different ways to exchange information to pair the two devices using the MTE. One way to exchange information when in a zero-knowledge environment is by using the Diffie-Hellman key exchange as a secure way to generate MTE entropy. This sample demonstrates how to use a Diffie-Hellman Algorithm to exchange the public key values between two different devices, then each side uses this value and their own private keys to generate a value that will be the same for these two devices that then can be used for entropy.

The Diffie-Hellman Key exchange does not depend on the communication type used, it is a secure algorithm where public keys can be exchanged between devices to then compute a "secret key" that will be unique and secure as long as the private data on each device is not shared or available to outside sources. For more information about the Diffie-Hellman Key exchange please click here.

Below is a visual representation of the Diffie-Hellman functionality:

Public Key Exchange using Diffie-Hellman

Here is an example of how to use the Diffie-Hellman key exchange to exchange the public keys when pairing devices with the MTE.

Encoder Device Side

using Eclypses.EcdhP256;

public byte[] GetEntropyForMTEEncoder() {
// Create Eclypses DH.
EcdhP256 ecdh = new EcdhP256();

// Create key pair and retrieve the public key.
byte[] publicKey = new byte[EcdhP256.SzPublicKey];
int res = ecdh.CreateKeyPair(publicKey);
if (res < 0) {
throw new Exception("Unable to create key pair: " + res);
}

// Send public key to Decoder side and get back
// Decoder side public key.
//---------------------------------------------------------
// This method is not included in this sample!!!
// This is here only to demonstrate that the user MUST
// send the public key to other side and receive back the
// public key from the other side.
//---------------------------------------------------------
byte[] partnerPublicKey = SendPublicKeyGetPartnerPublicKey(publicKey);

// Create the shared secret with the public key received from the Decoder.
byte[] sharedSecret = new byte[EcdhP256.SzSecretData];
res = ecdh.GetSharedSecret(partnerPublicKey, sharedSecret);
if (res < 0) {
throw new Exception("Unable to create shared secret: " + res);
}

// Return the shared secret so it can be used as the entropy value
// when creating the MTE Encoder within the calling program.
return sharedSecret;
}

Decoder Device Side

using Eclypses.EcdhP256;

public byte[] GetEntropyForMTEEncoder() {
// Create Eclypses DH.
EcdhP256 ecdh = new EcdhP256();

// Create key pair and retrieve the public key.
byte[] publicKey = new byte[EcdhP256.SzPublicKey];
int res = ecdh.CreateKeyPair(publicKey);
if (res < 0) {
throw new Exception("Unable to create key pair: " + res);
}

// Send public key to Decoder side and get back
// Decoder side public key.
//---------------------------------------------------------
// This method is not included in this sample!!!
// This is here only to demonstrate that the user MUST
// send the public key to other side and receive back the
// public key from the other side.
//---------------------------------------------------------
byte[] partnerPublicKey = SendPublicKeyGetPartnerPublicKey(publicKey);

// Create the shared secret with the public key received from the Decoder.
byte[] sharedSecret = new byte[EcdhP256.SzSecretData];
res = ecdh.GetSharedSecret(partnerPublicKey, sharedSecret);
if (res < 0) {
throw new Exception("Unable to create shared secret: " + res);
}

// Return the shared secret so it can be used as the entropy value
// when creating the MTE Encoder within the calling program.
return sharedSecret;
}

Eclypses ECDH Package

Eclypses has developed language file and tests for many languages.

(https://github.com/Eclypses/package-mtesupport-ecdh)