x25519-dalek

X25519 elliptic curve Diffie-Hellman key exchange in pure-Rust, using curve25519-dalek.

Github stars Tracking Chart

x25519-dalek

A pure-Rust implementation of x25519 elliptic curve Diffie-Hellman key exchange,
with curve operations provided by
curve25519-dalek.

This crate provides two levels of API: a bare byte-oriented x25519
function which matches the function specified in RFC7748, as
well as a higher-level Rust API for static and ephemeral Diffie-Hellman.

Examples

Alice and Bob are two adorable kittens who have lost their mittens, and they
wish to be able to send secret messages to each other to coordinate finding
them, otherwise—if their caretaker cat finds out—they will surely be called
naughty kittens and be given no pie!

But the two kittens are quite clever. Even though their paws are still too big
and the rest of them is 90% fuzziness, these clever kittens have been studying
up on modern public key cryptography and have learned a nifty trick called
elliptic curve Diffie-Hellman key exchange. With the right incantations, the
kittens will be able to secretly organise to find their mittens, and then spend
the rest of the afternoon nomming some yummy pie!

First, Alice uses EphemeralSecret::new() and then
PublicKey::from() to produce her secret and public keys:

extern crate rand_os;
extern crate x25519_dalek;

use rand_os::OsRng;

use x25519_dalek::EphemeralSecret;
use x25519_dalek::PublicKey;

let mut alice_csprng = OsRng::new().unwrap();
let     alice_secret = EphemeralSecret::new(&mut alice_csprng);
let     alice_public = PublicKey::from(&alice_secret);

Bob does the same:

let mut bob_csprng = OsRng::new().unwrap();
let     bob_secret = EphemeralSecret::new(&mut bob_csprng);
let     bob_public = PublicKey::from(&bob_secret);

Alice meows across the room, telling alice_public to Bob, and Bob
loudly meows bob_public back to Alice. Alice now computes her
shared secret with Bob by doing:

let alice_shared_secret = alice_secret.diffie_hellman(&bob_public);

Similarly, Bob computes a shared secret by doing:

let bob_shared_secret = bob_secret.diffie_hellman(&alice_public);

These secrets are the same:

assert_eq!(alice_shared_secret.as_bytes(), bob_shared_secret.as_bytes());

Voilá! Alice and Bob can now use their shared secret to encrypt their
meows, for example, by using it to generate a key and nonce for an
authenticated-encryption cipher.

This example used the ephemeral DH API, which ensures that secret keys
cannot be reused; Alice and Bob could instead use the static DH API
and load a long-term secret key.

Installation

To install, add the following to your project's Cargo.toml:

[dependencies.x25519-dalek]
version = "0.6"

Documentation

Documentation is available here.

Note

This code matches the RFC7748 test vectors.
The elliptic curve
operations are provided by curve25519-dalek, which makes a best-effort
attempt to prevent software side-channels.

"Secret Messages" cover image and zine
copyright © Amy Wibowo (@sailorhg)

Main metrics

Overview
Name With Ownerdalek-cryptography/x25519-dalek
Primary LanguageRust
Program languageRust (Language Count: 1)
Platform
License:BSD 3-Clause "New" or "Revised" License
所有者活动
Created At2017-09-12 22:14:24
Pushed At2023-09-01 19:33:25
Last Commit At
Release Count22
Last Release Name2.0.0-rc.3 (Posted on )
First Release Name0.1.0 (Posted on 2017-10-24 06:11:58)
用户参与
Stargazers Count338
Watchers Count18
Fork Count136
Commits Count254
Has Issues Enabled
Issues Count50
Issue Open Count9
Pull Requests Count53
Pull Requests Open Count0
Pull Requests Close Count33
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private