An experiment with Elliptic Curve Diffie-Hellman (ECDH) Key Exchange as a simple way for implementing end-to-end encryption.
- Initialize the server:
node main init-server
- Create users that will communicate with each other:
node main init-user alice
node main init-user bob
- Send a message from alice to bob:
node main send alice bob "Hello"
- View the mailbox for bob:
node main send alice bob "Hello"
The init-server command will create server.json file. This file contains the public keys and the mailbox for
all the users.
This file represents the database that will be used if this was a real server.
The init-user command will create a separate <user>.json file per user that will contains the
private key and public key for that user.
This file represents the data stored in the user device that doesn't need/mustn't be shared with the server.
The send command will take the sender, the receiver, and the message and will do the following:
-
read the private key of the sender from
<sender>.jsonasSenderprivate.Note: the
sendcommand can read<sender>.jsonbut not<receiver>.jsonbecause in reality, the encryption process happen on the sender device. -
Get the public key of the receiver from
server.jsonasReceiverpublic. -
Compute the shared secret:
SS = ECDH(Senderprivate, Receiverpublic) -
Derive a key from the shared secret using HKDF (HMAC Key Derivation Function):
K = HKDF(SS) -
Generate random IV using a secure random byte generator
IV. -
Encrypt the message using
AES-256-GCMwith the resulting key:C = ENCRYPT(K, IV, M) -
Mail the encrypted message
C,IV, andreceiverto the server. -
The server will store the received message in the mailbox for the receiver.
The mailbox command will do the following:
-
Get all the encrypted messages for the user from the mailbox.
-
Read the private key for the user from the file
<user>.jsonasReceiverprivate. -
For each message in the mailbox do the following steps:
-
Get the public key for the sender from the server as
Senderpublic -
Compute the shared secret:
SS = ECDH(Receiverprivate, Senderpublic) -
Derive a key from the shared secret using HKDF (HMAC Key Derivation Function):
K = HKDF(SS) -
Decrypt the message using the resulting key:
M = DECRYPT(K, IV, C)
-
- Secret Key Exchange (Diffie-Hellman) - Computerphile
- Diffie Hellman -the Mathematics bit- Computerphile
- Key Exchange Problems - Computerphile
- Elliptic Curves - Computerphile
Usage:
node main init-server
Initialize the server.
node main init-user <user-name>
Create a new user.
node main send <from> <to> <message>
Send an encrypted message.
node main mailbox <for-user>
Get and decrypt all messages for specified user.
node main clear-mailbox <for-user>
Delete all messages for specified user.