Skip to content

Commit 95b5536

Browse files
authored
crypto_kx: select x25519-dalek backend automatically (#60)
Introspects the target pointer width to automatically select either the `u32_backend` or `u64_backend`, similar to #55. If the backend isn't 32/64-bit, generate a compile error with an informative message.
1 parent 63760d1 commit 95b5536

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

crypto_kx/Cargo.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ rust-version = "1.56"
1717
blake2 = { version = "0.10", default-features = false }
1818
rand_core = "0.6"
1919

20+
[target.'cfg(target_pointer_width = "32")'.dependencies]
21+
x25519-dalek = { version = "1", default-features = false, features = ["u32_backend"] }
22+
23+
[target.'cfg(target_pointer_width = "64")'.dependencies]
24+
x25519-dalek = { version = "1", default-features = false, features = ["u64_backend"] }
25+
2026
# optional dependencies
2127
serdect = { version = "0.1", optional = true, default-features = false }
2228

23-
[dependencies.x25519-dalek]
24-
version = "1"
25-
default-features = false
26-
features = ["u64_backend"] # to allow --no-default-features
27-
2829
[target.'cfg(target_family = "wasm")'.dependencies]
2930
getrandom = { version = "0.2", default-features = false, features = ["js"] }
3031

crypto_kx/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,25 @@ Pure Rust implementation of [libsodium]'s [`crypto_kx`] primitive.
1111

1212
[Documentation][docs-link]
1313

14+
## About
15+
16+
Imagine Alice wants to open a safe communication channel with Betty,
17+
using something like [`crypto_secretstream`]. They first need to agree on
18+
a shared secret.
19+
20+
To obtain this shared secret, Diffie-Hellman can be used, which works as follows:
21+
Suppose both Alice and Betty know the public key of each other.
22+
Then they use their private key and the other's public key to generate a
23+
secret. This secret is the same for both Alice and Betty, as described by
24+
the Diffie-Hellman algorithm.
25+
No eavesdropper can know what the secret is, as they only know the public keys, but
26+
not the private keys.
27+
28+
Using the same key for sending and receiving might pose cryptographic
29+
issues and/or reduce the overall throughput.
30+
So when computing the shared secret, you actually get two keys,
31+
one for each direction.
32+
1433
## License
1534

1635
Licensed under either of:

crypto_kx/src/lib.rs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
1-
//! Pure Rust implementation of the [`crypto_kx`] key exchange
2-
//! from [NaCl]-family libraries (e.g. libsodium, TweetNaCl)
3-
//! which uses [BLAKE2].
4-
//!
5-
//! # Introduction
6-
//!
7-
//! Imagine Alice wants to open a safe communication channel with Betty,
8-
//! using something like [`crypto_secretstream`]. They first need to agree on
9-
//! a shared secret.
10-
//!
11-
//! To obtain this shared secret, Diffie-Hellman can be used, which works as follows:
12-
//! Suppose both Alice and Betty know the public key of each other.
13-
//! Then they use their private key and the other's public key to generate a
14-
//! secret. This secret is the same for both Alice and Betty, as described by
15-
//! the Diffie-Hellman algorithm.
16-
//! No eavesdropper can know what the secret is, as they only know the public keys, but
17-
//! not the private keys.
18-
//!
19-
//! Using the same key for sending and receiving might pose cryptographic
20-
//! issues and/or reduce the overall throughput.
21-
//! So when computing the shared secret, you actually get two keys,
22-
//! one for each direction.
23-
//!
24-
//! # Usage
1+
#![no_std]
2+
#![doc(
3+
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
4+
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
5+
)]
6+
#![warn(missing_docs, rust_2018_idioms)]
7+
8+
//! ## Usage
259
//!
2610
//! ```rust
2711
//! use crypto_kx::*;
@@ -46,12 +30,8 @@
4630
//! [`crypto_secretstream`]: https://github.com/RustCrypto/nacl-compat/tree/master/crypto_secretstream
4731
//! [BLAKE2]: https://github.com/RustCrypto/hashes/tree/master/blake2
4832
49-
#![no_std]
50-
#![doc(
51-
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
52-
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
53-
)]
54-
#![warn(missing_docs, rust_2018_idioms)]
33+
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
34+
compile_error!("`crypto-box` requires either a 32-bit or 64-bit target");
5535

5636
mod keypair;
5737
mod keys;

0 commit comments

Comments
 (0)