|
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>> { |
41 | 14 | //! use crypto_box::{
|
42 | 15 | //! aead::{Aead, AeadCore, OsRng},
|
43 | 16 | //! SalsaBox, PublicKey, SecretKey
|
|
73 | 46 | //! let plaintext = b"Top secret message we're encrypting";
|
74 | 47 | //!
|
75 | 48 | //! // Encrypt the message using the box
|
76 |
| -//! let ciphertext = alice_box.encrypt(&nonce, &plaintext[..]).unwrap(); |
| 49 | +//! let ciphertext = alice_box.encrypt(&nonce, &plaintext[..])?; |
77 | 50 | //!
|
78 | 51 | //! //
|
79 | 52 | //! // Decryption
|
|
96 | 69 | //! let bob_box = SalsaBox::new(&alice_public_key, &bob_secret_key);
|
97 | 70 | //!
|
98 | 71 | //! // 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[..])?; |
100 | 73 | //!
|
101 | 74 | //! assert_eq!(&plaintext[..], &decrypted_plaintext[..]);
|
| 75 | +//! # Ok(()) |
102 | 76 | //! # }
|
103 | 77 | //! ```
|
104 | 78 | //!
|
105 |
| -//! ## Choosing `ChaChaBox` vs `SalsaBox` |
| 79 | +//! ## Choosing [`ChaChaBox`] vs [`SalsaBox`] |
106 | 80 | //!
|
107 |
| -//! The `crypto_box` construction was originally specified using `SalsaBox`. |
| 81 | +//! The `crypto_box` construction was originally specified using [`SalsaBox`]. |
108 | 82 | //!
|
109 |
| -//! However, the newer `ChaChaBox` construction is also available, which |
| 83 | +//! However, the newer [`ChaChaBox`] construction is also available, which |
110 | 84 | //! provides marginally better security and additional features such as
|
111 | 85 | //! additional associated data:
|
112 | 86 | //!
|
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>> { |
116 | 90 | //! use crypto_box::{
|
117 | 91 | //! aead::{Aead, AeadCore, Payload, OsRng},
|
118 | 92 | //! ChaChaBox, PublicKey, SecretKey
|
|
159 | 133 | //! }).unwrap();
|
160 | 134 | //!
|
161 | 135 | //! assert_eq!(&plaintext[..], &decrypted_plaintext[..]);
|
162 |
| -//! } |
| 136 | +//! # Ok(()) |
| 137 | +//! # } |
163 | 138 | //! ```
|
164 | 139 | //!
|
165 | 140 | //! ## In-place Usage (eliminates `alloc` requirement)
|
|
189 | 164 | //! [ECIES]: https://en.wikipedia.org/wiki/Integrated_Encryption_Scheme
|
190 | 165 | //! [`heapless::Vec`]: https://docs.rs/heapless/latest/heapless/struct.Vec.html
|
191 | 166 |
|
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 |
| - |
200 | 167 | #[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
|
201 | 168 | compile_error!("`crypto-box` requires either a 32-bit or 64-bit target");
|
202 | 169 |
|
|
0 commit comments