AES-DH Implementation
Loading...
Searching...
No Matches
exchange Namespace Reference

The namespace for Key-Exchange functions. More...

Functions

uint64_t compute_intermediary (const uint64_t &p, const uint64_t &g, const uint64_t &k)
 Compute the intermeidary value to send across the wire.
 
uint64_t exchange_keys (const bool &server)
 Exchange keys on an established connection.
 

Detailed Description

The namespace for Key-Exchange functions.

Remarks
This code has been created with reference to: https://datatracker.ietf.org/doc/html/rfc2631#section-2.1 Herein referred to as "The Reference"
The Diffie-Hellman Key Exchange Algorithm is a means for two peers to negotiate a secure, shared key in an untrusted network. Each peer will generate a private key a, and b. They will then used A shared, public key containing a large prime number p, and a primitive root of p: g. Each party will take g, and raise it to their private key, sending it to the other party. Then, they will take their other peer's intermediary key, and raise it by their own private key. This leads to the following values (g^a)^b, (g^b)^b, which when Simplied leads to g^(ab), g^(ba), which due to the associative property of multiplication will lead to the same, shared key. Every operation in this exchange is done with mod p. Due to the difficulty in computing discrete logarithms, while computing the intermediary and shared key is easy if a private key is known (Simply raising a value), it is infesible For an attacker to try and determine the private key used to create an intermediary given only the intermediary value and the public p,g. This makes Diffie-Hellman a one-way function.
In this implementation, The server will generate the public p and g, and will send them alongside the intermediary to the other party.

Function Documentation

◆ compute_intermediary()

uint64_t exchange::compute_intermediary ( const uint64_t & p,
const uint64_t & g,
const uint64_t & k )

Compute the intermeidary value to send across the wire.

Parameters
pThe prime.
gThe g.
kThe private key.
Returns
The g**k % p via a reduced g**r % p
Remarks
This function uses the reduced calculation by generating q,r. prime::raise is leagues fast enough to just do the computation directly, but if working with very large numbers, this would make a substantial difference on performance.

◆ exchange_keys()

uint64_t exchange::exchange_keys ( const bool & server)

Exchange keys on an established connection.

Parameters
serverWhether this is the server.
Returns
The shared key to be used for communication
Remarks
See 2.1.1 of the Reference.