What changed, and why it matters
This commit replaces an external cryptography adapter library with the project's own in-house version for the secure communication layer (Noise protocol) used in the BitBox02 hardware wallet. The stated reason is to unblock future dependency upgrades, not to fix a known bug or vulnerability. The new code closely mirrors the old implementation and adds test vectors to verify correct behavior, but any change to cryptographic code carries a risk of subtle mistakes that could weaken secure connections.
Treat as a routine but security-sensitive refactoring. Review the new adapters for equivalence with the original, confirm that zeroization behavior is preserved, run the new test vectors, and include this change in normal cryptographic regression testing before release. No immediate incident response is warranted based on the supplied materials.
Security signals we found
Cryptographic code changed: ChaCha20-Poly1305, SHA-256, and sensitive buffer adapters re-implemented locally
Dependency removed: vendored `noise-rust-crypto` crate deleted
Sensitive key storage wrapper (`Sensitive`) retained with `Zeroizing`/`Zeroize` semantics
New test coverage added, including a reference Noise_XX_25519_ChaChaPoly_SHA256 handshake vector
No explicit security fix, CVE, or vulnerability disclosure referenced in commit or supplied materials
Evidence from the diff
The change removes the vendored noise-rust-crypto 0.6.2 crate and re-implements its SHA-256, ChaCha20-Poly1305, and sensitive-buffer adapters inside bitbox-noise. It keeps the same underlying RustCrypto crate versions (chacha20poly1305 0.10.1, sha2 workspace version, x25519-dalek 2.0.0) and the same Sensitive zeroizing wrapper. The Noise XX handshake code is updated to use the local modules, and new unit tests plus a reference handshake vector test are added. No security bug is described in the commit message or diff.
Changed components
src/rust/bitbox-noiseexternal/vendor/noise-rust-cryptoNoise XX handshake implementationChaCha20-Poly1305 adapterSHA-256 adapterSensitive key storage wrapperInspect captured patch +359 / −520
### external/vendor/noise-rust-crypto/.cargo-checksum.json
@@ -1 +0,0 @@
-{"files":{".cargo_vcs_info.json":"e8af2e1bb305b7d662b2077f8ad114992ad12d9c8908dc6d536f22fb594f9f9a","Cargo.toml":"4ca0285804643735f3671497dd86bb1d048349331e6d8e4061b3f964b947442a","Cargo.toml.orig":"c28eda3335bea0e30575d8f1533bf723991eaac200b0cd51e4176f764c25984e","README.md":"345fc353f2c99f30f3bc664f764e183381a2e82a795fb8c24aefb4325732f758","src/lib.rs":"0c38ede5fc13904f3546648171c12eee30decbb9697fa0738297baa36acb2594","src/sensitive.rs":"7b51d734cd6ebf81777b7d8ce5a570ea4df31cf2bf26e59cf60b667ac906740e"},"package":"b4c6159f60beb3bbbcdc266bc789bfc6c37fdad7d7ca7152d3e049ef5af633f0"}
\ No newline at end of file
### external/vendor/noise-rust-crypto/.cargo_vcs_info.json
@@ -1,6 +0,0 @@
-{
- "git": {
- "sha1": "36bc85bd05db04b0d469843275ccafd848cddb99"
- },
- "path_in_vcs": "noise-rust-crypto"
-}
\ No newline at end of file
### external/vendor/noise-rust-crypto/Cargo.toml
@@ -1,79 +0,0 @@
-# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
-#
-# When uploading crates to the registry Cargo will automatically
-# "normalize" Cargo.toml files for maximal compatibility
-# with all versions of Cargo and also rewrite `path` dependencies
-# to registry (e.g., crates.io) dependencies.
-#
-# If you are reading this file be aware that the original Cargo.toml
-# will likely look very different (and much more reasonable).
-# See Cargo.toml.orig for the original contents.
-
-[package]
-edition = "2021"
-name = "noise-rust-crypto"
-version = "0.6.2"
-authors = ["Guanhao Yin <sopium@mysterious.site>"]
-description = "Wrappers of dalek and RustCrypto crates for noise-protocol"
-readme = "README.md"
-license = "Unlicense"
-repository = "https://github.com/sopium/noise-rust"
-
-[dependencies.aes-gcm]
-version = "0.10.3"
-features = ["aes"]
-optional = true
-default-features = false
-
-[dependencies.blake2]
-version = "0.10.6"
-optional = true
-
-[dependencies.chacha20poly1305]
-version = "0.10.1"
-optional = true
-default-features = false
-
-[dependencies.noise-protocol]
-version = "0.2.0"
-default-features = false
-
-[dependencies.sha2]
-version = "0.10.8"
-optional = true
-default-features = false
-
-[dependencies.x25519-dalek]
-version = "2.0.0"
-optional = true
-default-features = false
-
-[dependencies.zeroize]
-version = "1"
-default-features = false
-
-[dev-dependencies.hex]
-version = "0.4.3"
-
-[features]
-default = [
- "use-x25519",
- "use-chacha20poly1305",
- "use-aes-256-gcm",
- "use-blake2",
- "use-sha2",
-]
-use-aes-256-gcm = ["aes-gcm"]
-use-blake2 = ["blake2"]
-use-chacha20poly1305 = ["chacha20poly1305"]
-use-sha2 = ["sha2"]
-use-x25519 = [
- "x25519",
- "x25519-dalek/precomputed-tables",
- "x25519-dalek/zeroize",
- "x25519-dalek/getrandom",
-]
-x25519 = [
- "x25519-dalek",
- "x25519-dalek/static_secrets",
-]
### external/vendor/noise-rust-crypto/Cargo.toml.orig
@@ -1,34 +0,0 @@
-[package]
-edition = "2021"
-authors = ["Guanhao Yin <sopium@mysterious.site>"]
-license = "Unlicense"
-name = "noise-rust-crypto"
-readme = "README.md"
-repository = "https://github.com/sopium/noise-rust"
-version = "0.6.2"
-description = "Wrappers of dalek and RustCrypto crates for noise-protocol"
-
-[features]
-default = ["use-x25519", "use-chacha20poly1305", "use-aes-256-gcm", "use-blake2", "use-sha2"]
-x25519 = ["x25519-dalek", "x25519-dalek/static_secrets"]
-use-x25519 = ["x25519", "x25519-dalek/precomputed-tables", "x25519-dalek/zeroize", "x25519-dalek/getrandom"]
-use-chacha20poly1305 = ["chacha20poly1305"]
-use-aes-256-gcm = ["aes-gcm"]
-use-blake2 = ["blake2"]
-use-sha2 = ["sha2"]
-
-[dependencies]
-x25519-dalek = { version = "2.0.0", optional = true, default-features = false }
-aes-gcm = { version = "0.10.3", features = ["aes"], default-features = false, optional = true }
-chacha20poly1305 = { version = "0.10.1", default-features = false, optional = true }
-blake2 = { version = "0.10.6", optional = true }
-sha2 = { version = "0.10.8", optional = true, default-features = false }
-zeroize = { version = "1", default-features = false }
-
-[dependencies.noise-protocol]
-path = "../noise-protocol"
-version = "0.2.0"
-default-features = false
-
-[dev-dependencies]
-hex = "0.4.3"
### external/vendor/noise-rust-crypto/README.md
@@ -1,10 +0,0 @@
-This crate provides wrappers around pure rust implementations of the crypto
-primitives used in `noise-protocol`.
-
-The underlying implementations are:
-
-* [`x25519-dalek`](https://crates.io/crates/x25519-dalek)
-* [`chacha20poly1305`](https://crates.io/crates/chacha20poly1305)
-* [`aes-gcm`](https://crates.io/crates/aes-gcm)
-* [`sha2`](https://crates.io/crates/sha2)
-* [`blake2`](https://crates.io/crates/blake2)
### external/vendor/noise-rust-crypto/src/lib.rs
@@ -1,341 +0,0 @@
-//! This crate provides wrappers around pure rust implementations of the crypto
-//! primitives used in `noise-protocol`.
-//
-//! The underlying implementations are:
-//
-//! * [`x25519-dalek`](https://crates.io/crates/x25519-dalek)
-//! * [`chacha20poly1305`](https://crates.io/crates/chacha20poly1305)
-//! * [`aes-gcm`](https://crates.io/crates/aes-gcm)
-//! * [`sha2`](https://crates.io/crates/sha2)
-//! * [`blake2`](https://crates.io/crates/blake2)
-
-#![no_std]
-
-pub mod sensitive;
-
-use sensitive::Sensitive;
-
-use noise_protocol::*;
-#[cfg(feature = "x25519")]
-use x25519_dalek::{PublicKey, StaticSecret};
-
-#[cfg(feature = "x25519")]
-pub enum X25519 {}
-
-#[cfg(feature = "x25519")]
-impl DH for X25519 {
- type Key = Sensitive<[u8; 32]>;
- type Pubkey = [u8; 32];
- type Output = Sensitive<[u8; 32]>;
-
- fn name() -> &'static str {
- "25519"
- }
-
- fn genkey() -> Self::Key {
- Self::Key::from_slice(StaticSecret::random().as_bytes())
- }
-
- fn pubkey(k: &Self::Key) -> Self::Pubkey {
- let static_secret = StaticSecret::from(**k);
- *PublicKey::from(&static_secret).as_bytes()
- }
-
- fn dh(k: &Self::Key, pk: &Self::Pubkey) -> Result<Self::Output, ()> {
- let k = StaticSecret::from(**k);
- let pk = PublicKey::from(*pk);
- Ok(Self::Output::from_slice(k.diffie_hellman(&pk).as_bytes()))
- }
-}
-
-#[cfg(feature = "use-chacha20poly1305")]
-pub enum ChaCha20Poly1305 {}
-
-#[cfg(feature = "use-chacha20poly1305")]
-impl Cipher for ChaCha20Poly1305 {
- fn name() -> &'static str {
- "ChaChaPoly"
- }
-
- type Key = Sensitive<[u8; 32]>;
-
- fn encrypt(k: &Self::Key, nonce: u64, ad: &[u8], plaintext: &[u8], out: &mut [u8]) {
- assert!(plaintext.len().checked_add(16) == Some(out.len()));
-
- let mut full_nonce = [0u8; 12];
- full_nonce[4..].copy_from_slice(&nonce.to_le_bytes());
-
- let (in_out, tag_out) = out.split_at_mut(plaintext.len());
- in_out.copy_from_slice(plaintext);
-
- use chacha20poly1305::{AeadInPlace, KeyInit};
- let tag = chacha20poly1305::ChaCha20Poly1305::new(&(**k).into())
- .encrypt_in_place_detached(&full_nonce.into(), ad, in_out)
- .unwrap();
-
- tag_out.copy_from_slice(tag.as_ref())
- }
-
- fn encrypt_in_place(
- k: &Self::Key,
- nonce: u64,
- ad: &[u8],
- in_out: &mut [u8],
- plaintext_len: usize,
- ) -> usize {
- assert!(plaintext_len
- .checked_add(16)
- .map_or(false, |l| l <= in_out.len()));
-
- let mut full_nonce = [0u8; 12];
- full_nonce[4..].copy_from_slice(&nonce.to_le_bytes());
-
- let (in_out, tag_out) = in_out[..plaintext_len + 16].split_at_mut(plaintext_len);
-
- use chacha20poly1305::{AeadInPlace, KeyInit};
- let tag = chacha20poly1305::ChaCha20Poly1305::new(&(**k).into())
- .encrypt_in_place_detached(&full_nonce.into(), ad, in_out)
- .unwrap();
- tag_out.copy_from_slice(tag.as_ref());
-
- plaintext_len + 16
- }
-
- fn decrypt(
- k: &Self::Key,
- nonce: u64,
- ad: &[u8],
- ciphertext: &[u8],
- out: &mut [u8],
- ) -> Result<(), ()> {
- assert!(ciphertext.len().checked_sub(16) == Some(out.len()));
-
- let mut full_nonce = [0u8; 12];
- full_nonce[4..].copy_from_slice(&nonce.to_le_bytes());
-
- out.copy_from_slice(&ciphertext[..out.len()]);
- let tag = &ciphertext[out.len()..];
-
- use chacha20poly1305::{AeadInPlace, KeyInit};
- chacha20poly1305::ChaCha20Poly1305::new(&(**k).into())
- .decrypt_in_place_detached(&full_nonce.into(), ad, out, tag.into())
- .map_err(|_| ())
- }
-
- fn decrypt_in_place(
- k: &Self::Key,
- nonce: u64,
- ad: &[u8],
- in_out: &mut [u8],
- ciphertext_len: usize,
- ) -> Result<usize, ()> {
- assert!(ciphertext_len <= in_out.len());
- assert!(ciphertext_len >= 16);
-
- let mut full_nonce = [0u8; 12];
- full_nonce[4..].copy_from_slice(&nonce.to_le_bytes());
-
- let (in_out, tag) = in_out[..ciphertext_len].split_at_mut(ciphertext_len - 16);
-
- use chacha20poly1305::{AeadInPlace, KeyInit};
- chacha20poly1305::ChaCha20Poly1305::new(&(**k).into())
- .decrypt_in_place_detached(&full_nonce.into(), ad, in_out, tag.as_ref().into())
- .map_err(|_| ())?;
-
- Ok(in_out.len())
- }
-}
-
-#[cfg(feature = "use-aes-256-gcm")]
-pub enum Aes256Gcm {}
-
-#[cfg(feature = "use-aes-256-gcm")]
-impl Cipher for Aes256Gcm {
- fn name() -> &'static str {
- "AESGCM"
- }
-
- type Key = Sensitive<[u8; 32]>;
-
- fn encrypt(k: &Self::Key, nonce: u64, ad: &[u8], plaintext: &[u8], out: &mut [u8]) {
- assert!(plaintext.len().checked_add(16) == Some(out.len()));
-
- let mut full_nonce = [0u8; 12];
- full_nonce[4..].copy_from_slice(&nonce.to_be_bytes());
-
- let (in_out, tag_out) = out.split_at_mut(plaintext.len());
- in_out.copy_from_slice(plaintext);
-
- use aes_gcm::{AeadInPlace, KeyInit};
- let tag = aes_gcm::Aes256Gcm::new(&(**k).into())
- .encrypt_in_place_detached(&full_nonce.into(), ad, in_out)
- .unwrap();
-
- tag_out.copy_from_slice(tag.as_ref())
- }
-
- fn encrypt_in_place(
- k: &Self::Key,
- nonce: u64,
- ad: &[u8],
- in_out: &mut [u8],
- plaintext_len: usize,
- ) -> usize {
- assert!(plaintext_len
- .checked_add(16)
- .map_or(false, |l| l <= in_out.len()));
-
- let mut full_nonce = [0u8; 12];
- full_nonce[4..].copy_from_slice(&nonce.to_be_bytes());
-
- let (in_out, tag_out) = in_out[..plaintext_len + 16].split_at_mut(plaintext_len);
-
- use aes_gcm::{AeadInPlace, KeyInit};
- let tag = aes_gcm::Aes256Gcm::new(&(**k).into())
- .encrypt_in_place_detached(&full_nonce.into(), ad, in_out)
- .unwrap();
- tag_out.copy_from_slice(tag.as_ref());
-
- plaintext_len + 16
- }
-
- fn decrypt(
- k: &Self::Key,
- nonce: u64,
- ad: &[u8],
- ciphertext: &[u8],
- out: &mut [u8],
- ) -> Result<(), ()> {
- assert!(ciphertext.len().checked_sub(16) == Some(out.len()));
-
- let mut full_nonce = [0u8; 12];
- full_nonce[4..].copy_from_slice(&nonce.to_be_bytes());
-
- out.copy_from_slice(&ciphertext[..out.len()]);
- let tag = &ciphertext[out.len()..];
-
- use aes_gcm::{AeadInPlace, KeyInit};
- aes_gcm::Aes256Gcm::new(&(**k).into())
- .decrypt_in_place_detached(&full_nonce.into(), ad, out, tag.into())
- .map_err(|_| ())
- }
-
- fn decrypt_in_place(
- k: &Self::Key,
- nonce: u64,
- ad: &[u8],
- in_out: &mut [u8],
- ciphertext_len: usize,
- ) -> Result<usize, ()> {
- assert!(ciphertext_len <= in_out.len());
- assert!(ciphertext_len >= 16);
-
- let mut full_nonce = [0u8; 12];
- full_nonce[4..].copy_from_slice(&nonce.to_be_bytes());
-
- let (in_out, tag) = in_out[..ciphertext_len].split_at_mut(ciphertext_len - 16);
-
- use aes_gcm::{AeadInPlace, KeyInit};
- aes_gcm::Aes256Gcm::new(&(**k).into())
- .decrypt_in_place_detached(&full_nonce.into(), ad, in_out, tag.as_ref().into())
- .map_err(|_| ())?;
-
- Ok(in_out.len())
- }
-}
-
-#[cfg(feature = "use-sha2")]
-#[derive(Default, Clone)]
-pub struct Sha256(sha2::Sha256);
-
-#[cfg(feature = "use-sha2")]
-impl Hash for Sha256 {
- fn name() -> &'static str {
- "SHA256"
- }
-
- type Block = [u8; 64];
- type Output = Sensitive<[u8; 32]>;
-
- fn input(&mut self, data: &[u8]) {
- use sha2::Digest;
- self.0.update(data);
- }
-
- fn result(&mut self) -> Self::Output {
- use sha2::Digest;
- Self::Output::from_slice(self.0.finalize_reset().as_ref())
- }
-}
-
-#[cfg(feature = "use-sha2")]
-#[derive(Default, Clone)]
-pub struct Sha512(sha2::Sha512);
-
-#[cfg(feature = "use-sha2")]
-impl Hash for Sha512 {
- fn name() -> &'static str {
- "SHA512"
- }
-
- type Block = [u8; 128];
- type Output = Sensitive<[u8; 64]>;
-
- fn input(&mut self, data: &[u8]) {
- use sha2::Digest;
- self.0.update(data);
- }
-
- fn result(&mut self) -> Self::Output {
- use sha2::Digest;
- Self::Output::from_slice(self.0.finalize_reset().as_ref())
- }
-}
-
-#[cfg(feature = "use-blake2")]
-#[derive(Default, Clone)]
-pub struct Blake2s(blake2::Blake2s256);
-
-#[cfg(feature = "use-blake2")]
-impl Hash for Blake2s {
- fn name() -> &'static str {
- "BLAKE2s"
- }
-
- type Block = [u8; 64];
- type Output = Sensitive<[u8; 32]>;
-
- fn input(&mut self, data: &[u8]) {
- use blake2::Digest;
- self.0.update(data);
- }
-
- fn result(&mut self) -> Self::Output {
- use blake2::Digest;
- Self::Output::from_slice(self.0.finalize_reset().as_ref())
- }
-}
-
-#[cfg(feature = "use-blake2")]
-#[derive(Default, Clone)]
-pub struct Blake2b(blake2::Blake2b512);
-
-#[cfg(feature = "use-blake2")]
-impl Hash for Blake2b {
- fn name() -> &'static str {
- "BLAKE2b"
- }
-
- type Block = [u8; 128];
- type Output = Sensitive<[u8; 64]>;
-
- fn input(&mut self, data: &[u8]) {
- use blake2::Digest;
- self.0.update(data);
- }
-
- fn result(&mut self) -> Self::Output {
- use blake2::Digest;
- Self::Output::from_slice(self.0.finalize_reset().as_ref())
- }
-}
### src/rust/Cargo.lock
@@ -247,9 +247,12 @@ name = "bitbox-noise"
version = "0.1.0"
dependencies = [
"bitbox-hal",
+ "chacha20poly1305",
+ "hex_lit",
"noise-protocol",
- "noise-rust-crypto",
+ "sha2",
"x25519-dalek",
+ "zeroize",
]
[[package]]
@@ -1132,18 +1135,6 @@ dependencies = [
"arrayvec",
]
-[[package]]
-name = "noise-rust-crypto"
-version = "0.6.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b4c6159f60beb3bbbcdc266bc789bfc6c37fdad7d7ca7152d3e049ef5af633f0"
-dependencies = [
- "chacha20poly1305",
- "noise-protocol",
- "sha2",
- "zeroize",
-]
-
[[package]]
name = "num-bigint"
version = "0.4.6"
### src/rust/bitbox-noise/Cargo.toml
@@ -16,12 +16,20 @@ version = "0.2.0"
default-features = false
features = ["use_alloc"]
-[dependencies.noise-rust-crypto]
-version = "0.6.2"
+[dependencies.chacha20poly1305]
+version = "0.10.1"
default-features = false
-features = ["use-sha2", "use-chacha20poly1305"]
+
+[dependencies.sha2]
+workspace = true
+
+[dependencies.zeroize]
+workspace = true
[dependencies.x25519-dalek]
version = "2.0.0"
default-features = false
features = ["static_secrets", "zeroize"]
+
+[dev-dependencies.hex_lit]
+workspace = true
### src/rust/bitbox-noise/src/chacha20poly1305.rs
@@ -0,0 +1,202 @@
+// SPDX-License-Identifier: Apache-2.0
+
+// Adapter derived from noise-rust-crypto 0.6.2, originally licensed under the Unlicense.
+// https://github.com/sopium/noise-rust/blob/36bc85bd05db04b0d469843275ccafd848cddb99/noise-rust-crypto/src/lib.rs
+
+use chacha20poly1305::{AeadInPlace, KeyInit};
+use noise_protocol::Cipher;
+
+use crate::sensitive::Sensitive;
+
+pub enum ChaCha20Poly1305 {}
+
+impl Cipher for ChaCha20Poly1305 {
+ fn name() -> &'static str {
+ "ChaChaPoly"
+ }
+
+ type Key = Sensitive<[u8; 32]>;
+
+ fn encrypt(k: &Self::Key, nonce: u64, ad: &[u8], plaintext: &[u8], out: &mut [u8]) {
+ assert!(plaintext.len().checked_add(16) == Some(out.len()));
+
+ let mut full_nonce = [0u8; 12];
+ full_nonce[4..].copy_from_slice(&nonce.to_le_bytes());
+
+ let (in_out, tag_out) = out.split_at_mut(plaintext.len());
+ in_out.copy_from_slice(plaintext);
+
+ let tag = chacha20poly1305::ChaCha20Poly1305::new(&(**k).into())
+ .encrypt_in_place_detached(&full_nonce.into(), ad, in_out)
+ .unwrap();
+
+ tag_out.copy_from_slice(tag.as_ref());
+ }
+
+ fn encrypt_in_place(
+ k: &Self::Key,
+ nonce: u64,
+ ad: &[u8],
+ in_out: &mut [u8],
+ plaintext_len: usize,
+ ) -> usize {
+ assert!(
+ plaintext_len
+ .checked_add(16)
+ .is_some_and(|len| len <= in_out.len())
+ );
+
+ let mut full_nonce = [0u8; 12];
+ full_nonce[4..].copy_from_slice(&nonce.to_le_bytes());
+
+ let (in_out, tag_out) = in_out[..plaintext_len + 16].split_at_mut(plaintext_len);
+
+ let tag = chacha20poly1305::ChaCha20Poly1305::new(&(**k).into())
+ .encrypt_in_place_detached(&full_nonce.into(), ad, in_out)
+ .unwrap();
+ tag_out.copy_from_slice(tag.as_ref());
+
+ plaintext_len + 16
+ }
+
+ fn decrypt(
+ k: &Self::Key,
+ nonce: u64,
+ ad: &[u8],
+ ciphertext: &[u8],
+ out: &mut [u8],
+ ) -> Result<(), ()> {
+ assert!(ciphertext.len().checked_sub(16) == Some(out.len()));
+
+ let mut full_nonce = [0u8; 12];
+ full_nonce[4..].copy_from_slice(&nonce.to_le_bytes());
+
+ out.copy_from_slice(&ciphertext[..out.len()]);
+ let tag = &ciphertext[out.len()..];
+
+ chacha20poly1305::ChaCha20Poly1305::new(&(**k).into())
+ .decrypt_in_place_detached(&full_nonce.into(), ad, out, tag.into())
+ .map_err(|_| ())
+ }
+
+ fn decrypt_in_place(
+ k: &Self::Key,
+ nonce: u64,
+ ad: &[u8],
+ in_out: &mut [u8],
+ ciphertext_len: usize,
+ ) -> Result<usize, ()> {
+ assert!(ciphertext_len <= in_out.len());
+ assert!(ciphertext_len >= 16);
+
+ let mut full_nonce = [0u8; 12];
+ full_nonce[4..].copy_from_slice(&nonce.to_le_bytes());
+
+ let (in_out, tag) = in_out[..ciphertext_len].split_at_mut(ciphertext_len - 16);
+
+ chacha20poly1305::ChaCha20Poly1305::new(&(**k).into())
+ .decrypt_in_place_detached(&full_nonce.into(), ad, in_out, tag.as_ref().into())
+ .map_err(|_| ())?;
+
+ Ok(in_out.len())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use hex_lit::hex;
+ use noise_protocol::U8Array;
+
+ const KEY: [u8; 32] = hex!("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
+ const NONCE: u64 = 0x0807060504030201;
+ const AD: &[u8] = b"noise associated data";
+ const PLAINTEXT: &[u8] = b"noise transport payload";
+ // Generated independently with Python cryptography's ChaCha20Poly1305 using
+ // the 96-bit nonce 000000000102030405060708.
+ const CIPHERTEXT: [u8; 39] =
+ hex!("8189cc8bc0acdcee71d19ba6e59e537ee5ed1d3d732c4c77e7124b0242f252da7de1b8cc4a2b90");
+
+ #[test]
+ fn test_encrypt_decrypt() {
+ let key = Sensitive::from_slice(&KEY);
+ let cases: &[(&[u8], &[u8])] = &[
+ (b"", &hex!("a3bea751a894a825060dbce0756618d2")),
+ (PLAINTEXT, &CIPHERTEXT),
+ ];
+ for &(plaintext, ciphertext) in cases {
+ let mut encrypted = vec![0; ciphertext.len()];
+ ChaCha20Poly1305::encrypt(&key, NONCE, AD, plaintext, &mut encrypted);
+ assert_eq!(encrypted, ciphertext);
+
+ let mut decrypted = vec![0; plaintext.len()];
+ ChaCha20Poly1305::decrypt(&key, NONCE, AD, ciphertext, &mut decrypted).unwrap();
+ assert_eq!(decrypted, plaintext);
+
+ let mut in_out = vec![0xa5; ciphertext.len() + 8];
+ in_out[..plaintext.len()].copy_from_slice(plaintext);
+ assert_eq!(
+ ChaCha20Poly1305::encrypt_in_place(&key, NONCE, AD, &mut in_out, plaintext.len()),
+ ciphertext.len()
+ );
+ assert_eq!(&in_out[..ciphertext.len()], ciphertext);
+ assert_eq!(&in_out[ciphertext.len()..], &[0xa5; 8]);
+ assert_eq!(
+ ChaCha20Poly1305::decrypt_in_place(&key, NONCE, AD, &mut in_out, ciphertext.len())
+ .unwrap(),
+ plaintext.len()
+ );
+ assert_eq!(&in_out[..plaintext.len()], plaintext);
+ assert_eq!(&in_out[ciphertext.len()..], &[0xa5; 8]);
+ }
+ }
+
+ #[test]
+ fn test_decrypt_rejects_tampering() {
+ let key = Sensitive::from_slice(&KEY);
+ let mut decrypted = vec![0; PLAINTEXT.len()];
+ let mut changed_payload = CIPHERTEXT;
+ changed_payload[0] ^= 1;
+ let mut changed_tag = CIPHERTEXT;
+ changed_tag[CIPHERTEXT.len() - 1] ^= 1;
+ for (nonce, ad, ciphertext) in [
+ (NONCE, AD, changed_payload),
+ (NONCE, AD, changed_tag),
+ (NONCE + 1, AD, CIPHERTEXT),
+ (NONCE, b"changed".as_slice(), CIPHERTEXT),
+ ] {
+ assert!(
+ ChaCha20Poly1305::decrypt(&key, nonce, ad, &ciphertext, &mut decrypted).is_err()
+ );
+ let mut in_out = ciphertext;
+ assert!(
+ ChaCha20Poly1305::decrypt_in_place(&key, nonce, ad, &mut in_out, ciphertext.len())
+ .is_err()
+ );
+ }
+ }
+
+ #[test]
+ #[should_panic]
+ fn test_decrypt_in_place_short_ciphertext() {
+ let _ = ChaCha20Poly1305::decrypt_in_place(
+ &Sensitive::from_slice(&KEY),
+ NONCE,
+ AD,
+ &mut [0; 16],
+ 15,
+ );
+ }
+
+ #[test]
+ #[should_panic]
+ fn test_encrypt_in_place_length_overflow() {
+ ChaCha20Poly1305::encrypt_in_place(
+ &Sensitive::from_slice(&KEY),
+ NONCE,
+ AD,
+ &mut [0; 16],
+ usize::MAX,
+ );
+ }
+}
### src/rust/bitbox-noise/src/lib.rs
@@ -11,11 +11,14 @@
#[macro_use]
extern crate std;
+mod chacha20poly1305;
mod noise_xx;
+mod sensitive;
+mod sha256;
pub mod testing;
mod x25519;
pub use noise_xx::{Error, HandshakeHash, HandshakeResult, State};
pub use x25519::{X25519, genkey};
-pub use noise_rust_crypto::sensitive::Sensitive;
+pub use sensitive::Sensitive;
### src/rust/bitbox-noise/src/noise_xx.rs
@@ -5,8 +5,10 @@ use alloc::vec::Vec;
use bitbox_hal::Random;
+use crate::chacha20poly1305::ChaCha20Poly1305;
+use crate::sensitive::Sensitive;
+use crate::sha256::Sha256;
use crate::x25519::{PrivateKey, PublicKey, X25519, genkey};
-use noise_rust_crypto::{ChaCha20Poly1305, Sha256, sensitive::Sensitive};
/// Specialization of noise_protocol::HandshakeState, picking the implementations for Diffie
/// Hellman, Cipher and Hash.
@@ -226,6 +228,8 @@ impl State {
mod tests {
use super::*;
use crate::testing::{MockRandom, make_mock_host};
+ use hex_lit::hex;
+ use noise_protocol::U8Array;
impl HandshakeResult {
fn response(self) -> Result<Vec<u8>, ()> {
@@ -279,4 +283,68 @@ mod tests {
let decrypted = host_recv.decrypt_vec(encrypted).unwrap();
assert_eq!(decrypted, b"message from bb02");
}
+
+ #[test]
+ fn test_handshake_reference_vector() {
+ // Noise_XX_25519_ChaChaPoly_SHA256 from:
+ // https://github.com/rweather/noise-c/blob/cfe25410979a87391bb9ac8d4d4bef64e9f268c6/tests/vector/noise-c-basic.txt
+ let mut host = HandshakeState::new(
+ noise_protocol::patterns::noise_xx(),
+ true,
+ b"Prologue123",
+ Some(Sensitive::from_slice(&hex!(
+ "e61ef9919cde45dd5f82166404bd08e38bceb5dfdfded0a34c8df7ed542214d1"
+ ))),
+ Some(Sensitive::from_slice(&hex!(
+ "893e28b9dc6ca8d611ab664754b8ceb7bac5117349a4439a6b0569da977c464a"
+ ))),
+ None,
+ None,
+ );
+ assert_eq!(
+ host.write_message_vec(b"Ludwig von Mises").unwrap(),
+ hex!(
+ "ca35def5ae56cec33dc2036731ab14896bc4c75dbb07a61f879f8e3afa4c79444c756477696720766f6e204d69736573"
+ )
+ );
+ assert_eq!(
+ host.read_message_vec(&hex!(
+ "95ebc60d2b1fa672c1f46a8aa265ef51bfe38e7ccb39ec5be34069f14480884381cbad1f276e038c48378ffce2b65285e08d6b68aaa3629a5a8639392490e5b94a6d8798832d5372f220f161d9c2df035528f8982ffe09be9b5c412f8a0db5d21351f20af1370d0bf8ef1a8c59a30e"
+ ))
+ .unwrap(),
+ b"Murray Rothbard"
+ );
+ assert_eq!(
+ host.write_message_vec(b"F. A. Hayek").unwrap(),
+ hex!(
+ "c7195ffacac1307ff99046f219750fc47693e23c3cb08b89c2af808b444850a8589981dfbd651e6ff4724a781cc2aa6158c9fea0d4ec82a286427484c5b8c8123a7a6002b1de9f9775fc97"
+ )
+ );
+ assert!(host.completed());
+ assert_eq!(
+ host.get_hash(),
+ hex!("852a28d2146785c54bd8334f4e460c80d7fe4fd0cc5bc0abef2a24a3c4d44d5f")
+ );
+ let (mut send, mut receive) = host.get_ciphers();
+ assert_eq!(
+ receive
+ .decrypt_vec(&hex!(
+ "96763ed773f8e47bb3712f0e29b3060ffc956ffc146cee53d5e1df"
+ ))
+ .unwrap(),
+ b"Carl Menger"
+ );
+ assert_eq!(
+ send.encrypt_vec(b"Jean-Baptiste Say"),
+ hex!("3e40f15f6f3a46ae446b253bf8b1d9ffb6ed9b174d272328ff91a7e2e5c79c07f5")
+ );
+ assert_eq!(
+ receive
+ .decrypt_vec(&hex!(
+ "eb3f3515110702e047a6c9da4478b6ead94873c11c0f2d710ddb3f09fce024b3a58502ae3f"
+ ))
+ .unwrap(),
+ b"Eugen B\xf6hm von Bawerk"
+ );
+ }
}
### src/rust/bitbox-noise/src/sensitive.rs
@@ -1,3 +1,8 @@
+// SPDX-License-Identifier: Apache-2.0
+
+// Derived from noise-rust-crypto 0.6.2, originally licensed under the Unlicense.
+// https://github.com/sopium/noise-rust/blob/36bc85bd05db04b0d469843275ccafd848cddb99/noise-rust-crypto/src/sensitive.rs
+
use noise_protocol::U8Array;
use zeroize::{Zeroize, Zeroizing};
@@ -12,6 +17,7 @@ impl<A: U8Array + Zeroize> Sensitive<A> {
impl<A: U8Array + Zeroize> core::ops::Deref for Sensitive<A> {
type Target = A;
+
fn deref(&self) -> &Self::Target {
&self.0
}
@@ -23,10 +29,7 @@ impl<A: U8Array + Zeroize> core::ops::DerefMut for Sensitive<A> {
}
}
-impl<A> U8Array for Sensitive<A>
-where
- A: Zeroize + U8Array,
-{
+impl<A: U8Array + Zeroize> U8Array for Sensitive<A> {
fn new() -> Self {
Sensitive::from(Zeroizing::new(A::new()))
}
### src/rust/bitbox-noise/src/sha256.rs
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: Apache-2.0
+
+// Adapter derived from noise-rust-crypto 0.6.2, originally licensed under the Unlicense.
+// https://github.com/sopium/noise-rust/blob/36bc85bd05db04b0d469843275ccafd848cddb99/noise-rust-crypto/src/lib.rs
+
+use noise_protocol::{Hash, U8Array};
+use sha2::Digest;
+
+use crate::sensitive::Sensitive;
+
+#[derive(Default, Clone)]
+pub struct Sha256(sha2::Sha256);
+
+impl Hash for Sha256 {
+ fn name() -> &'static str {
+ "SHA256"
+ }
+
+ type Block = [u8; 64];
+ type Output = Sensitive<[u8; 32]>;
+
+ fn input(&mut self, data: &[u8]) {
+ self.0.update(data);
+ }
+
+ fn result(&mut self) -> Self::Output {
+ Self::Output::from_slice(self.0.finalize_reset().as_ref())
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use hex_lit::hex;
+
+ #[test]
+ fn test_result_resets() {
+ let mut hash = Sha256::default();
+ hash.input(b"a");
+ hash.input(b"bc");
+ let mut cloned = hash.clone();
+ let expected = hex!("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
+ assert_eq!(hash.result().as_slice(), expected);
+ assert_eq!(cloned.result().as_slice(), expected);
+ assert_eq!(
+ hash.result().as_slice(),
+ hex!("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
+ );
+ hash.input(b"discarded");
+ hash.reset();
+ hash.input(b"abc");
+ assert_eq!(hash.result().as_slice(), expected);
+ }
+}
### src/rust/bitbox-noise/src/x25519.rs
@@ -8,7 +8,8 @@
use bitbox_hal::Random;
use core::ops::Deref;
use noise_protocol::U8Array;
-use noise_rust_crypto::sensitive::Sensitive;
+
+use crate::sensitive::Sensitive;
pub struct X25519;
### test/simulator-graphical-bb03/Cargo.lock
@@ -408,9 +408,11 @@ name = "bitbox-noise"
version = "0.1.0"
dependencies = [
"bitbox-hal",
+ "chacha20poly1305",
"noise-protocol",
- "noise-rust-crypto",
+ "sha2",
"x25519-dalek",
+ "zeroize",
]
[[package]]
@@ -1981,18 +1983,6 @@ dependencies = [
"arrayvec",
]
-[[package]]
-name = "noise-rust-crypto"
-version = "0.6.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b4c6159f60beb3bbbcdc266bc789bfc6c37fdad7d7ca7152d3e049ef5af633f0"
-dependencies = [
- "chacha20poly1305",
- "noise-protocol",
- "sha2",
- "zeroize",
-]
-
[[package]]
name = "nom"
version = "8.0.0"
### test/simulator-graphical/Cargo.lock
@@ -353,9 +353,11 @@ name = "bitbox-noise"
version = "0.1.0"
dependencies = [
"bitbox-hal",
+ "chacha20poly1305",
"noise-protocol",
- "noise-rust-crypto",
+ "sha2",
"x25519-dalek",
+ "zeroize",
]
[[package]]
@@ -1931,18 +1933,6 @@ dependencies = [
"arrayvec",
]
-[[package]]
-name = "noise-rust-crypto"
-version = "0.6.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "b4c6159f60beb3bbbcdc266bc789bfc6c37fdad7d7ca7152d3e049ef5af633f0"
-dependencies = [
- "chacha20poly1305",
- "noise-protocol",
- "sha2",
- "zeroize",
-]
-
[[package]]
name = "nom"
version = "7.1.3"Why this scored 32/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.