Skip to content

Commit a994fbf

Browse files
authored
crypto_box: select x25519-dalek backend automatically (#55)
Introspects the target pointer width to automatically select either the `u32_backend` or `u64_backend`. If the backend isn't 32/64-bit, generate a compile error with an informative message. Removes the explicit `u32_backend`/`u64_backend` crate features in `cargo_box` itself. Now `--all-features` works.
1 parent 49dca07 commit a994fbf

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

.github/workflows/crypto_box.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ jobs:
3535
toolchain: ${{ matrix.rust }}
3636
target: ${{ matrix.target }}
3737
override: true
38-
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features u32_backend
39-
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features u32_backend,heapless
38+
- run: cargo build --target ${{ matrix.target }} --release --no-default-features
39+
- run: cargo build --target ${{ matrix.target }} --release --no-default-features --features heapless
4040

4141
test:
4242
runs-on: ubuntu-latest
@@ -52,6 +52,6 @@ jobs:
5252
profile: minimal
5353
toolchain: ${{ matrix.rust }}
5454
override: true
55-
- run: cargo test --release --features std
56-
- run: cargo test --release --features std,heapless
57-
- run: cargo test --release --features std,serde
55+
- run: cargo test --release --no-default-features
56+
- run: cargo test --release
57+
- run: cargo test --release --all-features

crypto_box/Cargo.toml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ aead = { version = "0.5.1", default-features = false }
2222
chacha20 = "0.9"
2323
chacha20poly1305 = { version = "0.10.1", default-features = false, features = ["rand_core"] }
2424
salsa20 = "0.10"
25-
x25519-dalek = { version = "1", default-features = false }
2625
xsalsa20poly1305 = { version = "0.9", default-features = false, features = ["rand_core"] }
2726
zeroize = { version = "1", default-features = false }
2827

28+
[target.'cfg(target_pointer_width = "32")'.dependencies]
29+
x25519-dalek = { version = "1", default-features = false, features = ["u32_backend"] }
30+
31+
[target.'cfg(target_pointer_width = "64")'.dependencies]
32+
x25519-dalek = { version = "1", default-features = false, features = ["u64_backend"] }
33+
2934
# optional dependencies
3035
serdect = { version = "0.1", optional = true, default-features = false }
3136

@@ -35,16 +40,14 @@ rand = "0.8"
3540
rmp-serde = "1"
3641

3742
[features]
38-
default = ["alloc", "getrandom", "u64_backend"]
43+
default = ["alloc", "getrandom"]
3944
std = ["aead/std"]
40-
serde = ["serdect"]
4145
alloc = ["aead/alloc"]
4246
getrandom = ["aead/getrandom", "rand_core"]
4347
heapless = ["aead/heapless"]
4448
rand_core = ["aead/rand_core"]
45-
u32_backend = ["x25519-dalek/u32_backend"]
46-
u64_backend = ["x25519-dalek/u64_backend"]
49+
serde = ["serdect"]
4750

4851
[package.metadata.docs.rs]
49-
features = ["serde"]
52+
all-features = true
5053
rustdoc-args = ["--cfg", "docsrs"]

crypto_box/src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,15 @@
190190
//! [`heapless::Vec`]: https://docs.rs/heapless/latest/heapless/struct.Vec.html
191191
192192
#![no_std]
193+
#![cfg_attr(docsrs, feature(doc_cfg))]
193194
#![doc(
194195
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
195-
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
196-
html_root_url = "https://docs.rs/crypto_box/0.7.1"
196+
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
197197
)]
198198
#![warn(missing_docs, rust_2018_idioms)]
199-
#![cfg_attr(docsrs, feature(doc_cfg))]
199+
200+
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
201+
compile_error!("`crypto-box` requires either a 32-bit or 64-bit target");
200202

201203
pub use aead::{self, rand_core};
202204
pub use xsalsa20poly1305::Nonce;

0 commit comments

Comments
 (0)