Crypto Lab 4- Key Exchange

Posted on Dec 5, 2024

Diffie-Hellman key exchange is a method that allows two parties to establish a shared secret over an insecure channel. Here’s how you can perform a basic Diffie-Hellman key exchange using OpenSSL on Linux:

Step-by-Step Demo

  1. Generate Diffie-Hellman Parameters:
openssl dhparam -out dhparam.pem 2048
  1. Generate Private Keys for Both Parties:
  • For Party A:
openssl genpkey -paramfile dhparam.pem -out privateA.pem
  • For Party B:
openssl genpkey -paramfile dhparam.pem -out privateB.pem
  1. Extract Public Keys from the Private Keys:
  • For Party A:
openssl pkey -in privateA.pem -pubout -out publicA.pem
  • For Party B:
  openssl pkey -in privateB.pem -pubout -out publicB.pem
  1. Generate the Shared Secret:
    • For Party A (using Party B’s public key):
openssl pkeyutl -derive -inkey privateA.pem -peerkey publicB.pem -out shared_secret_A.bin
  • For Party B (using Party A’s public key):
     openssl pkeyutl -derive -inkey privateB.pem -peerkey publicA.pem -out shared_secret_B.bin
  1. Verify the Shared Secrets Match:
   diff shared_secret_A.bin shared_secret_B.bin && echo "Shared secrets match!" || echo "Shared secrets do not match!"

Explanation

  • Step 1: Generates Diffie-Hellman parameters (a shared foundation) which both parties use.
  • Step 2: Each party generates their private keys using the shared parameters.
  • Step 3: Extracts public keys from the private keys to be shared with the other party.
  • Step 4: Each party generates a shared secret using their private key and the other party’s public key.
  • Step 5: Verifies that both parties have derived the same shared secret.

This demonstration helps you understand the process of establishing a shared secret securely. If you have more questions or need further details, feel free to ask!