Skip to content

Commit d742fc5

Browse files
authored
crypto_box: documentation improvements (#56)
1 parent a994fbf commit d742fc5

File tree

3 files changed

+51
-58
lines changed

3 files changed

+51
-58
lines changed

crypto_box/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "crypto_box"
3-
version = "0.8.0-pre" # Also update html_root_url in lib.rs when bumping this
3+
version = "0.8.0-pre"
44
description = """
55
Pure Rust implementation of NaCl's crypto_box public-key authenticated
66
encryption primitive which combines the X25519 Elliptic Curve Diffie-Hellman

crypto_box/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ Elliptic Curve Integrated Encryption Scheme ([ECIES]).
1414

1515
[Documentation][docs-link]
1616

17+
## About
18+
19+
Imagine Alice wants something valuable shipped to her. Because it's
20+
valuable, she wants to make sure it arrives securely (i.e. hasn't been
21+
opened or tampered with) and that it's not a forgery (i.e. it's actually
22+
from the sender she's expecting it to be from and nobody's pulling the old
23+
switcheroo).
24+
25+
One way she can do this is by providing the sender (let's call him Bob)
26+
with a high-security box of her choosing. She provides Bob with this box,
27+
and something else: a padlock, but a padlock without a key. Alice is
28+
keeping that key all to herself. Bob can put items in the box then put the
29+
padlock onto it, but once the padlock snaps shut, the box cannot be opened
30+
by anyone who doesn't have Alice's private key.
31+
32+
Here's the twist though, Bob also puts a padlock onto the box. This padlock
33+
uses a key Bob has published to the world, such that if you have one of
34+
Bob's keys, you know a box came from him because Bob's keys will open Bob's
35+
padlocks (let's imagine a world where padlocks cannot be forged even if you
36+
know the key). Bob then sends the box to Alice.
37+
38+
In order for Alice to open the box, she needs two keys: her private key
39+
that opens her own padlock, and Bob's well-known key. If Bob's key doesn't
40+
open the second padlock then Alice knows that this is not the box she was
41+
expecting from Bob, it's a forgery.
42+
1743
## License
1844

1945
Licensed under either of:

crypto_box/src/lib.rs

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,16 @@
1-
//! Pure Rust implementation of the [`crypto_box`] public-key authenticated
2-
//! encryption scheme from [NaCl]-family libraries (e.g. libsodium, TweetNaCl)
3-
//! which combines the [X25519] Diffie-Hellman function and the
4-
//! [XSalsa20Poly1305] authenticated encryption cipher into an Elliptic Curve
5-
//! Integrated Encryption Scheme ([ECIES]).
6-
//!
7-
//! # Introduction
8-
//!
9-
//! Imagine Alice wants something valuable shipped to her. Because it's
10-
//! valuable, she wants to make sure it arrives securely (i.e. hasn't been
11-
//! opened or tampered with) and that it's not a forgery (i.e. it's actually
12-
//! from the sender she's expecting it to be from and nobody's pulling the old
13-
//! switcheroo).
14-
//!
15-
//! One way she can do this is by providing the sender (let's call him Bob)
16-
//! with a high-security box of her choosing. She provides Bob with this box,
17-
//! and something else: a padlock, but a padlock without a key. Alice is
18-
//! keeping that key all to herself. Bob can put items in the box then put the
19-
//! padlock onto it, but once the padlock snaps shut, the box cannot be opened
20-
//! by anyone who doesn't have Alice's private key.
21-
//!
22-
//! Here's the twist though, Bob also puts a padlock onto the box. This padlock
23-
//! uses a key Bob has published to the world, such that if you have one of
24-
//! Bob's keys, you know a box came from him because Bob's keys will open Bob's
25-
//! padlocks (let's imagine a world where padlocks cannot be forged even if you
26-
//! know the key). Bob then sends the box to Alice.
27-
//!
28-
//! In order for Alice to open the box, she needs two keys: her private key
29-
//! that opens her own padlock, and Bob's well-known key. If Bob's key doesn't
30-
//! open the second padlock then Alice knows that this is not the box she was
31-
//! expecting from Bob, it's a forgery.
32-
//!
33-
//! # Usage
34-
//!
35-
//! NOTE: The following examples assume use of the `std`
36-
//! [feature](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#choosing-features).
37-
//!
38-
//! ```rust
39-
//! # #[cfg(all(feature = "getrandom", feature = "std"))]
40-
//! # {
1+
#![no_std]
2+
#![cfg_attr(docsrs, feature(doc_cfg))]
3+
#![doc(
4+
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
5+
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
6+
)]
7+
#![warn(missing_docs, rust_2018_idioms)]
8+
9+
//! ## Usage
10+
//!
11+
#![cfg_attr(all(feature = "getrandom", feature = "std"), doc = "```")]
12+
#![cfg_attr(not(all(feature = "getrandom", feature = "std")), doc = "```ignore")]
13+
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
4114
//! use crypto_box::{
4215
//! aead::{Aead, AeadCore, OsRng},
4316
//! SalsaBox, PublicKey, SecretKey
@@ -73,7 +46,7 @@
7346
//! let plaintext = b"Top secret message we're encrypting";
7447
//!
7548
//! // Encrypt the message using the box
76-
//! let ciphertext = alice_box.encrypt(&nonce, &plaintext[..]).unwrap();
49+
//! let ciphertext = alice_box.encrypt(&nonce, &plaintext[..])?;
7750
//!
7851
//! //
7952
//! // Decryption
@@ -96,23 +69,24 @@
9669
//! let bob_box = SalsaBox::new(&alice_public_key, &bob_secret_key);
9770
//!
9871
//! // Decrypt the message, using the same randomly generated nonce
99-
//! let decrypted_plaintext = bob_box.decrypt(&nonce, &ciphertext[..]).unwrap();
72+
//! let decrypted_plaintext = bob_box.decrypt(&nonce, &ciphertext[..])?;
10073
//!
10174
//! assert_eq!(&plaintext[..], &decrypted_plaintext[..]);
75+
//! # Ok(())
10276
//! # }
10377
//! ```
10478
//!
105-
//! ## Choosing `ChaChaBox` vs `SalsaBox`
79+
//! ## Choosing [`ChaChaBox`] vs [`SalsaBox`]
10680
//!
107-
//! The `crypto_box` construction was originally specified using `SalsaBox`.
81+
//! The `crypto_box` construction was originally specified using [`SalsaBox`].
10882
//!
109-
//! However, the newer `ChaChaBox` construction is also available, which
83+
//! However, the newer [`ChaChaBox`] construction is also available, which
11084
//! provides marginally better security and additional features such as
11185
//! additional associated data:
11286
//!
113-
//! ```rust
114-
//! # #[cfg(all(feature = "getrandom", feature = "std"))]
115-
//! # {
87+
#![cfg_attr(all(feature = "getrandom", feature = "std"), doc = "```")]
88+
#![cfg_attr(not(all(feature = "getrandom", feature = "std")), doc = "```ignore")]
89+
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
11690
//! use crypto_box::{
11791
//! aead::{Aead, AeadCore, Payload, OsRng},
11892
//! ChaChaBox, PublicKey, SecretKey
@@ -159,7 +133,8 @@
159133
//! }).unwrap();
160134
//!
161135
//! assert_eq!(&plaintext[..], &decrypted_plaintext[..]);
162-
//! }
136+
//! # Ok(())
137+
//! # }
163138
//! ```
164139
//!
165140
//! ## In-place Usage (eliminates `alloc` requirement)
@@ -189,14 +164,6 @@
189164
//! [ECIES]: https://en.wikipedia.org/wiki/Integrated_Encryption_Scheme
190165
//! [`heapless::Vec`]: https://docs.rs/heapless/latest/heapless/struct.Vec.html
191166
192-
#![no_std]
193-
#![cfg_attr(docsrs, feature(doc_cfg))]
194-
#![doc(
195-
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
196-
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
197-
)]
198-
#![warn(missing_docs, rust_2018_idioms)]
199-
200167
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
201168
compile_error!("`crypto-box` requires either a 32-bit or 64-bit target");
202169

0 commit comments

Comments
 (0)