[line 120]
Crypt_DiffieHellman class
Example usage: Bob and Alice have started to communicate and wish to establish a shared common secret key with which to sign messages. Both establish two common pieces of information:
- a large prime number
- a generator number
Both also generate a private key (different for each) and a public key. They then transmit their public keys to each other, and agree on the prime and generator also. Both then perform identical sets of Diffie Hellman calculations and calculate a key which only each could calculate.
This is secure for a very simple reason - no other party can reverse engineer the public keys to get hold of the private keys which are essential pieces of calculating the Diffie-Hellman shared secret. The algorithm ensures this by using Modular Exponentiation which expresses a one-way-function behaviour (it's computationally infeasible to reverse it).
Using the data below, both will agree a shared secret key of 117.
Alice: prime = 563 generator = 5 private key = 9 Bob: prime = 563 generator = 5 private key = 14
$alice = new Crypt_DiffieHellman(563, 5, 9); $alice_pubKey = alice->generateKeys()->getPublicKey(); $bob = new Crypt_DiffieHellman(563, 5, 14); $bob_pubKey = $bob->generateKeys()->getPublicKey();
// the public keys are then exchanged (with agreed prime and generator)
$alice_computeKey = $alice->computeSecretKey($bob_pubKey)->getSharedSecretKey(); $bob_computeKey = $bob->computeSecretKey($alice_pubKey)->getSharedSecretKey();
assert($alice_computeKey == $bob_computeKey);
Alice and Bob have now established the same shared secret key of 117. They may now sign exchanged messages which the other party may then authenticate upon receipt.
In order to facilitate the practice of transmitting large integers in their binary form, input and output methods may accept an additional parameter of Crypt_DiffieHellman::BINARY to tell this method when the input/output should be converted from, or to, binary form. An alternate parameter of Crypt_DiffieHellman::BTWOC is used only for output methods and returns the binary big-endian twos complement of the binary form to maintain consistent binary conversion across platforms.
Although the example above uses a simple prime number, it is important to always use a sufficiently large prime, preferably one of the primes deemed to have positive cryptographic qualities. The generator is always a number less than the prime number.