Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch PRNG to ChaCha20 #21

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ categories = ["command-line-utilities"]

[dependencies]
rand = "0.8.5"
rand_chacha = "0.3.1"
clap = { version = "4.5.4", features = ["derive"] }
unicode-normalization = "0.1.23"
include-lines = "1.1.2"
Expand Down
4 changes: 2 additions & 2 deletions readme.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,9 @@ gave-model-coil-lent-deep-lam-chin-tall

## Source of randomness

Phraze uses the [rand crate](https://github.com/rust-random/rand)'s [ThreadRng](https://rust-random.github.io/rand/rand/rngs/struct.ThreadRng.html) as its cryptographically secure pseudo-random number generator (CSPRNG) for generating passphrases.
Phraze uses [ChaCha20](https://docs.rs/rand_chacha/latest/rand_chacha/struct.ChaCha20Rng.html) for its pseudo-random number generator. As the documentation notes:

According to [the rand crate documentation at the time of this writing, "ThreadRng uses the same CSPRNG as StdRng, ChaCha12"](https://rust-random.github.io/rand/rand/rngs/struct.ThreadRng.html), meaning it uses 12 rounds of the ChaCha stream cipher. That `StdRng` uses ChaCha12 is [relatively clear in the source code](https://docs.rs/rand/latest/src/rand/rngs/std.rs.html#9-15). (See [this issue](https://github.com/rust-random/rand/issues/932) for arguments in favor of using 12 rounds rather than 20.)
> With the ChaCha algorithm it is possible to choose the number of rounds the core algorithm should run. The number of rounds is a tradeoff between performance and security, where 8 rounds is the minimum potentially secure configuration, and 20 rounds is widely used as a conservative choice.

## Why another random passphrase generator?
There are already a few good passphrase generators, including [passphraseme](https://github.com/micahflee/passphraseme) and [Pgen](https://github.com/ctsrc/Pgen).
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ pub mod unicode_normalization_check;

use crate::separators::make_separator;
use include_lines::include_lines;
use rand::{seq::SliceRandom, thread_rng, Rng};
use rand::SeedableRng;
use rand::{seq::SliceRandom, Rng};
use rand_chacha::ChaCha20Rng;

/// This enum, `ListChoice`, represents all of the "built-in" word lists that Phraze can use.
#[derive(Clone, Debug, Copy)]
Expand Down Expand Up @@ -88,7 +90,7 @@ pub fn generate_a_passphrase<T: AsRef<str> + std::fmt::Display>(
title_case: bool,
list: &[T], // We accept either type by using `T`!
) -> String {
let mut rng = thread_rng();
let mut rng = ChaCha20Rng::from_entropy();
// Create a blank String to put words into to create our passphrase
let mut passphrase = String::new();
for i in 0..number_of_words_to_put_in_passphrase {
Expand Down