Skip to content
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
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,21 @@ pqclean_kyber1024 = ["use-pqcrypto-kyber1024"]
xchachapoly = ["use-xchacha20poly1305"]

# Enable std features on dependencies if possible.
std = ["getrandom?/std", "subtle/std", "ring?/std", "blake2?/std", "sha2?/std"]
std = [
"getrandom?/std",
"subtle/std",
"ring?/std",
"blake2?/std",
"blake3?/std",
"sha2?/std",
]

# Crypto primitives for default-resolver.
use-curve25519 = ["curve25519-dalek", "default-resolver"]
use-chacha20poly1305 = ["chacha20poly1305", "default-resolver"]
use-xchacha20poly1305 = ["chacha20poly1305", "default-resolver"]
use-blake2 = ["blake2", "default-resolver"]
use-blake3 = ["blake3", "default-resolver"]
use-sha2 = ["sha2", "default-resolver"]
use-aes-gcm = ["aes-gcm", "default-resolver"]
use-getrandom = ["getrandom", "default-resolver"]
Expand Down Expand Up @@ -77,6 +85,7 @@ aes-gcm = { version = "0.10", optional = true, default-features = false, feature
] }
chacha20poly1305 = { version = "0.10", optional = true, default-features = false }
blake2 = { version = "0.10", optional = true, default-features = false }
blake3 = { version = "1.8", optional = true, default-features = false }
sha2 = { version = "0.10", optional = true, default-features = false }
curve25519-dalek = { version = "4.1.3", optional = true, default-features = false }
p256 = { version = "0.13.2", features = ["ecdh"], optional = true }
Expand Down
52 changes: 28 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,24 @@ crypto implementations when available.

### Resolver primitives supported

| | default | ring |
| | default | ring |
| -------------------------------------: | :----------------: | :----------------: |
| CSPRNG | :heavy_check_mark: | :heavy_check_mark: |
| 25519 | :heavy_check_mark: | :heavy_check_mark: |
| 448 | | |
| P-256<sup>:checkered_flag:</sup> | :heavy_check_mark: | |
| AESGCM | :heavy_check_mark: | :heavy_check_mark: |
| ChaChaPoly | :heavy_check_mark: | :heavy_check_mark: |
| CSPRNG | :heavy_check_mark: | :heavy_check_mark: |
| 25519 | :heavy_check_mark: | :heavy_check_mark: |
| 448 | | |
| P-256<sup>:checkered_flag:</sup> | :heavy_check_mark: | |
| AESGCM | :heavy_check_mark: | :heavy_check_mark: |
| ChaChaPoly | :heavy_check_mark: | :heavy_check_mark: |
| XChaChaPoly<sup>:checkered_flag:</sup> | :heavy_check_mark: | |
| SHA256 | :heavy_check_mark: | :heavy_check_mark: |
| SHA512 | :heavy_check_mark: | :heavy_check_mark: |
| BLAKE2s | :heavy_check_mark: | |
| BLAKE2b | :heavy_check_mark: | |
| SHA256 | :heavy_check_mark: | :heavy_check_mark: |
| SHA512 | :heavy_check_mark: | :heavy_check_mark: |
| BLAKE2s | :heavy_check_mark: | |
| BLAKE2b | :heavy_check_mark: | |
| BLAKE3 | :heavy_check_mark: | |

> [!Note]
> :checkered_flag: P-256 and XChaChaPoly are not in the official specification of Noise, and thus need to be enabled
via the feature flags `use-p256` and `use-xchacha20poly1305`, respectively.
> via the feature flags `use-p256` and `use-xchacha20poly1305`, respectively.

## `no_std` support and feature selection

Expand All @@ -94,25 +95,27 @@ currently supports `no_std`.

To use a custom setup with `default-resolver`, enable your desired selection of cryptographic primitives:

| | Primitive | Feature flag |
| ----------: | :------------------------------------- | :--------------------- |
| **DHs** | Curve25519 | `use-curve25519` |
| | P-256<sup>:checkered_flag:</sup> | `use-p256` |
| **Ciphers** | AES-GCM | `use-aes-gcm` |
| | ChaChaPoly | `use-chacha20poly1305` |
| | XChaChaPoly<sup>:checkered_flag:</sup> | `use-xchacha20poly1305`|
| **Hashes** | SHA-256 | `use-sha2` |
| | SHA-512 | `use-sha2` |
| | BLAKE2s | `use-blake2` |
| | BLAKE2b | `use-blake2` |
| | Primitive | Feature flag |
| ----------: | :------------------------------------- | :---------------------- |
| **DHs** | Curve25519 | `use-curve25519` |
| | P-256<sup>:checkered_flag:</sup> | `use-p256` |
| **Ciphers** | AES-GCM | `use-aes-gcm` |
| | ChaChaPoly | `use-chacha20poly1305` |
| | XChaChaPoly<sup>:checkered_flag:</sup> | `use-xchacha20poly1305` |
| **Hashes** | SHA-256 | `use-sha2` |
| | SHA-512 | `use-sha2` |
| | BLAKE2s | `use-blake2` |
| | BLAKE2b | `use-blake2` |
| | BLAKE3<sup>:checkered_flag:</sup> | `use-blake3` |

> [!Note]
> :checkered_flag: XChaChaPoly and P-256 are not in the official specification of Noise, but they are supported
by Snow.
> by Snow.

### Example configurations

**Curve25519 + AES-GCM + SHA-2** with standard library features.

```toml
default-features = false
features = [
Expand All @@ -124,6 +127,7 @@ features = [
```

**Curve25519 + ChaChaPoly + BLAKE2** without standard library.

```toml
default-features = false
features = [
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,11 @@ extern crate alloc;
feature = "use-chacha20poly1305",
feature = "use-xchacha20poly1305"
)),
not(any(feature = "use-sha2", feature = "use-blake2"))
not(any(
feature = "use-sha2",
feature = "use-blake2",
feature = "use-blake3"
))
))]
compile_error!(
"Valid selection of crypto primitived must be enabled when using feature 'default-resolver'.
Expand Down
7 changes: 6 additions & 1 deletion src/params/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(clippy::match_on_vec_items)]
#![allow(clippy::enum_glob_use)]

//! All structures related to Noise parameter definitions (cryptographic primitive choices, protocol
Expand Down Expand Up @@ -105,6 +104,10 @@ pub enum HashChoice {
Blake2s,
/// The BLAKE2b hash function, designed to be more efficient on 64-bit architectures.
Blake2b,
#[cfg(feature = "use-blake3")]
/// The BLAKE3 hash function, designed to be more efficient than BLAKE2 through,
/// in part, parallelism and a reduced round count.
Blake3,
}

impl FromStr for HashChoice {
Expand All @@ -117,6 +120,8 @@ impl FromStr for HashChoice {
"SHA512" => Ok(SHA512),
"BLAKE2s" => Ok(Blake2s),
"BLAKE2b" => Ok(Blake2b),
#[cfg(feature = "use-blake3")]
"BLAKE3" => Ok(Blake3),
_ => Err(PatternProblem::UnsupportedHashType.into()),
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/resolvers/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ impl CryptoResolver for DefaultResolver {
HashChoice::Blake2s => Some(Box::<HashBLAKE2s>::default()),
#[cfg(feature = "use-blake2")]
HashChoice::Blake2b => Some(Box::<HashBLAKE2b>::default()),
#[cfg(feature = "use-blake3")]
HashChoice::Blake3 => Some(Box::<HashBlake3>::default()),
_ => None,
}
}
Expand Down Expand Up @@ -188,6 +190,13 @@ struct HashBLAKE2s {
hasher: Blake2s256,
}

/// Wraps `blake3`'s implementation
#[cfg(feature = "use-blake3")]
#[derive(Default)]
struct HashBlake3 {
hasher: blake3::Hasher,
}

/// Wraps `kyber1024`'s implementation
#[cfg(feature = "use-pqcrypto-kyber1024")]
struct Kyber1024 {
Expand Down Expand Up @@ -582,6 +591,34 @@ impl Hash for HashBLAKE2s {
}
}

#[cfg(feature = "use-blake3")]
impl Hash for HashBlake3 {
fn name(&self) -> &'static str {
"BLAKE3"
}

fn block_len(&self) -> usize {
blake3::BLOCK_LEN
}

fn hash_len(&self) -> usize {
blake3::OUT_LEN
}
Comment on lines +596 to +606
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to change these to trait constants once breaking changes are made in the library:

Suggested change
fn name(&self) -> &'static str {
"BLAKE3"
}
fn block_len(&self) -> usize {
blake3::BLOCK_LEN
}
fn hash_len(&self) -> usize {
blake3::OUT_LEN
}
const NAME: &'static str = "BLAKE3";
const BLOCK_LEN: usize = blake3::BLOCK_LEN;
const HASH_LEN: usize = blake3::OUT_LEN;


fn reset(&mut self) {
self.hasher = blake3::Hasher::new();
}

fn input(&mut self, data: &[u8]) {
self.hasher.update(data);
}

fn result(&mut self, out: &mut [u8]) {
let hash = self.hasher.finalize();
out[..blake3::OUT_LEN].copy_from_slice(hash.as_bytes());
}
}

#[cfg(feature = "use-pqcrypto-kyber1024")]
impl Default for Kyber1024 {
fn default() -> Self {
Expand Down
Loading